BAEL-797 refactoring the http calls

This commit is contained in:
tschiman
2017-04-26 22:36:26 -06:00
parent 10d526ac3c
commit d08db87870
12 changed files with 3864 additions and 20490 deletions
@@ -1,7 +1,6 @@
import {Component} from "@angular/core"; import {Component} from "@angular/core";
import {Principal} from "./principal"; import {Principal} from "./principal";
import {Response} from "@angular/http"; import {Response} from "@angular/http";
import {Observable} from "rxjs";
import {NgForm} from "@angular/forms"; import {NgForm} from "@angular/forms";
import {Book} from "./book"; import {Book} from "./book";
import {HttpService} from "./http.service"; import {HttpService} from "./http.service";
@@ -38,27 +37,16 @@ export class AppComponent {
this.loginFailed = false; this.loginFailed = false;
this.credentials = {username: form.value.username, password: form.value.password}; this.credentials = {username: form.value.username, password: form.value.password};
this.httpService.login(this.credentials) this.httpService.login(this.credentials)
.map((response: Response) => response.json()) .subscribe((response: Response) => {
.catch((error: Response) => { let principalJson = response.json();
if (error.status === 401) { this.principal = new Principal(principalJson.authenticated, principalJson.authorities, this.credentials);
this.loginFailed = true; }, (error) => {
}
console.log(error); console.log(error);
return Observable.throw(error);
})
.map((data: any) => new Principal(data.authenticated, data.authorities, this.credentials))
.subscribe((principal: Principal) => {
console.log(principal);
this.principal = principal;
}); });
} }
onLogout() { onLogout() {
this.httpService.logout(this.principal.credentials) this.httpService.logout(this.principal.credentials)
.catch((error: Response) => {
console.log(error);
return Observable.throw(error);
})
.subscribe((response: Response) => { .subscribe((response: Response) => {
if (response.status === 204) { if (response.status === 204) {
this.loginFailed = false; this.loginFailed = false;
@@ -66,6 +54,8 @@ export class AppComponent {
this.credentials.password = ''; this.credentials.password = '';
this.principal = new Principal(false, [], null); this.principal = new Principal(false, [], null);
} }
}, (error) => {
console.log(error);
}); });
} }
@@ -3,7 +3,6 @@ import {Principal} from "../../principal";
import {Book} from "../../book"; import {Book} from "../../book";
import {Response} from "@angular/http"; import {Response} from "@angular/http";
import {HttpService} from "../../http.service"; import {HttpService} from "../../http.service";
import {Observable} from "rxjs";
@Component({ @Component({
selector: 'app-book-list', selector: 'app-book-list',
@@ -30,18 +29,14 @@ export class BookListComponent implements OnInit {
loadBooks() { loadBooks() {
this.httpService.getBooks() this.httpService.getBooks()
.map((response: Response) => response.json()) .subscribe((response: Response) => {
.map(books => { let booksJson: any[] = response.json()
return Observable.from(books) booksJson.forEach(book => {
}) this.books.push(new Book(book.id, book.author, book.title));
.flatMap(x => x) this.newBooks.push(new Book(book.id, book.author, book.title));
.map((data: any) => { })
return new Book(data.id, data.author, data.title) }, (error) => {
}) console.log(error);
.subscribe((book: Book) => {
console.log(book);
this.books.push(book);
this.newBooks.push(new Book(book.id, book.author, book.title))
}); });
} }
@@ -62,15 +57,16 @@ export class BookListComponent implements OnInit {
console.log(newBook); console.log(newBook);
//save the book to the database //save the book to the database
this.httpService.updateBook(newBook, this.principal.credentials) this.httpService.updateBook(newBook, this.principal.credentials)
.map((response: Response) => response.json()) .subscribe((response: Response) => {
.map((data: any) => new Book(data.id, data.author, data.title)) let bookJson = response.json();
.subscribe((book: Book) => { let book: Book = new Book(bookJson.id, bookJson.author, bookJson.title);
console.log(book);
//update the current array of books //update the current array of books
let bookArr: Book = this.books.find(b => b.id === book.id); let bookArr: Book = this.books.find(b => b.id === book.id);
bookArr.title = book.title; bookArr.title = book.title;
bookArr.author = book.author; bookArr.author = book.author;
this.booksToEdit.splice(this.booksToEdit.indexOf(bookIndex), 1); //remove the index of the book to edit this.booksToEdit.splice(this.booksToEdit.indexOf(bookIndex), 1); //remove the index of the book to edit
}, (error) => {
console.log(error);
}); });
@@ -84,6 +80,8 @@ export class BookListComponent implements OnInit {
this.books.splice(bookIndex, 1); //remove the book at this index; this.books.splice(bookIndex, 1); //remove the book at this index;
this.newBooks.splice(bookIndex, 1); //remove the editing book at this index this.newBooks.splice(bookIndex, 1); //remove the editing book at this index
}, (error) => {
console.log(error);
}); });
} }
@@ -95,14 +93,16 @@ export class BookListComponent implements OnInit {
addNewBook(newBook: Book, element: any) { addNewBook(newBook: Book, element: any) {
//write new book to db //write new book to db
this.httpService.createBook(newBook, this.principal.credentials) this.httpService.createBook(newBook, this.principal.credentials)
.map((response: Response) => response.json()) .subscribe((response: Response) => {
.map((data: any) => new Book(data.id, data.author, data.title)) let bookJson = response.json();
.subscribe((book: Book) => { let book: Book = new Book(bookJson.id, bookJson.author, bookJson.title);
console.log(book); console.log(book);
this.books.push(book); this.books.push(book);
this.newBooks.push(book); this.newBooks.push(book);
this.newBook = new Book(Math.floor(Math.random() * 1000), '', ''); this.newBook = new Book(Math.floor(Math.random() * 1000), '', '');
element.focus(); element.focus();
}, (error) => {
console.log(error);
}); });
} }
@@ -3,7 +3,6 @@ import {Rating} from "../rating";
import {Principal} from "../principal"; import {Principal} from "../principal";
import {HttpService} from "../http.service"; import {HttpService} from "../http.service";
import {Response} from "@angular/http"; import {Response} from "@angular/http";
import {Observable} from "rxjs";
@Component({ @Component({
selector: 'app-rating', selector: 'app-rating',
@@ -35,15 +34,11 @@ export class RatingComponent implements OnInit, OnChanges {
private loadRatings() { private loadRatings() {
this.httpService.getRatings(this.bookId, this.principal.credentials) this.httpService.getRatings(this.bookId, this.principal.credentials)
.map((response: Response) => response.json()) .subscribe((response: Response) => {
.map(ratings => { let responseJson: any[] = response.json();
return Observable.from(ratings) responseJson.forEach(rating => this.ratings.push(new Rating(rating.id, rating.bookId, rating.stars)))
}) }, (error) => {
.flatMap(x => x) console.log(error);
.map((data: any) => new Rating(data.id, data.bookId, data.stars))
.subscribe((rating: Rating) => {
console.log(rating);
this.ratings.push(rating);
}); });
} }
@@ -51,11 +46,11 @@ export class RatingComponent implements OnInit, OnChanges {
console.log(this.newRating); console.log(this.newRating);
let ratingCopy: Rating = Object.assign({}, this.newRating); let ratingCopy: Rating = Object.assign({}, this.newRating);
this.httpService.createRating(ratingCopy, this.principal.credentials) this.httpService.createRating(ratingCopy, this.principal.credentials)
.map((response: Response) => response.json()) .subscribe((response: Response) => {
.map((data: any) => new Rating(data.id, data.bookId, data.stars)) let ratingJson = response.json()
.subscribe((rating: Rating) => { this.ratings.push(new Rating(ratingJson.id, ratingJson.bookId, ratingJson.stars))
console.log(rating); }, (error) => {
this.ratings.push(rating); console.log(error);
}); });
} }
@@ -63,7 +58,9 @@ export class RatingComponent implements OnInit, OnChanges {
this.httpService.updateRating(this.newRating, this.principal.credentials) this.httpService.updateRating(this.newRating, this.principal.credentials)
.subscribe(() => { .subscribe(() => {
this.newRating = new Rating(null, this.bookId, 1); this.newRating = new Rating(null, this.bookId, 1);
}) }, (error) => {
console.log(error);
});
} }
selectRating(rating: Rating) { selectRating(rating: Rating) {
@@ -84,6 +81,8 @@ export class RatingComponent implements OnInit, OnChanges {
this.newRating = new Rating(null, this.bookId, 1); this.newRating = new Rating(null, this.bookId, 1);
} }
this.ratings.splice(index, 1); this.ratings.splice(index, 1);
}, (error) => {
console.log(error);
}); });
} }
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long