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 {Principal} from "./principal";
import {Response} from "@angular/http";
import {Observable} from "rxjs";
import {NgForm} from "@angular/forms";
import {Book} from "./book";
import {HttpService} from "./http.service";
@@ -38,27 +37,16 @@ export class AppComponent {
this.loginFailed = false;
this.credentials = {username: form.value.username, password: form.value.password};
this.httpService.login(this.credentials)
.map((response: Response) => response.json())
.catch((error: Response) => {
if (error.status === 401) {
this.loginFailed = true;
}
.subscribe((response: Response) => {
let principalJson = response.json();
this.principal = new Principal(principalJson.authenticated, principalJson.authorities, this.credentials);
}, (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() {
this.httpService.logout(this.principal.credentials)
.catch((error: Response) => {
console.log(error);
return Observable.throw(error);
})
.subscribe((response: Response) => {
if (response.status === 204) {
this.loginFailed = false;
@@ -66,6 +54,8 @@ export class AppComponent {
this.credentials.password = '';
this.principal = new Principal(false, [], null);
}
}, (error) => {
console.log(error);
});
}
@@ -3,7 +3,6 @@ import {Principal} from "../../principal";
import {Book} from "../../book";
import {Response} from "@angular/http";
import {HttpService} from "../../http.service";
import {Observable} from "rxjs";
@Component({
selector: 'app-book-list',
@@ -30,18 +29,14 @@ export class BookListComponent implements OnInit {
loadBooks() {
this.httpService.getBooks()
.map((response: Response) => response.json())
.map(books => {
return Observable.from(books)
})
.flatMap(x => x)
.map((data: any) => {
return new Book(data.id, data.author, data.title)
})
.subscribe((book: Book) => {
console.log(book);
this.books.push(book);
this.newBooks.push(new Book(book.id, book.author, book.title))
.subscribe((response: Response) => {
let booksJson: any[] = response.json()
booksJson.forEach(book => {
this.books.push(new Book(book.id, book.author, book.title));
this.newBooks.push(new Book(book.id, book.author, book.title));
})
}, (error) => {
console.log(error);
});
}
@@ -62,15 +57,16 @@ export class BookListComponent implements OnInit {
console.log(newBook);
//save the book to the database
this.httpService.updateBook(newBook, this.principal.credentials)
.map((response: Response) => response.json())
.map((data: any) => new Book(data.id, data.author, data.title))
.subscribe((book: Book) => {
console.log(book);
.subscribe((response: Response) => {
let bookJson = response.json();
let book: Book = new Book(bookJson.id, bookJson.author, bookJson.title);
//update the current array of books
let bookArr: Book = this.books.find(b => b.id === book.id);
bookArr.title = book.title;
bookArr.author = book.author;
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.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) {
//write new book to db
this.httpService.createBook(newBook, this.principal.credentials)
.map((response: Response) => response.json())
.map((data: any) => new Book(data.id, data.author, data.title))
.subscribe((book: Book) => {
.subscribe((response: Response) => {
let bookJson = response.json();
let book: Book = new Book(bookJson.id, bookJson.author, bookJson.title);
console.log(book);
this.books.push(book);
this.newBooks.push(book);
this.newBook = new Book(Math.floor(Math.random() * 1000), '', '');
element.focus();
}, (error) => {
console.log(error);
});
}
@@ -3,7 +3,6 @@ import {Rating} from "../rating";
import {Principal} from "../principal";
import {HttpService} from "../http.service";
import {Response} from "@angular/http";
import {Observable} from "rxjs";
@Component({
selector: 'app-rating',
@@ -35,15 +34,11 @@ export class RatingComponent implements OnInit, OnChanges {
private loadRatings() {
this.httpService.getRatings(this.bookId, this.principal.credentials)
.map((response: Response) => response.json())
.map(ratings => {
return Observable.from(ratings)
})
.flatMap(x => x)
.map((data: any) => new Rating(data.id, data.bookId, data.stars))
.subscribe((rating: Rating) => {
console.log(rating);
this.ratings.push(rating);
.subscribe((response: Response) => {
let responseJson: any[] = response.json();
responseJson.forEach(rating => this.ratings.push(new Rating(rating.id, rating.bookId, rating.stars)))
}, (error) => {
console.log(error);
});
}
@@ -51,11 +46,11 @@ export class RatingComponent implements OnInit, OnChanges {
console.log(this.newRating);
let ratingCopy: Rating = Object.assign({}, this.newRating);
this.httpService.createRating(ratingCopy, this.principal.credentials)
.map((response: Response) => response.json())
.map((data: any) => new Rating(data.id, data.bookId, data.stars))
.subscribe((rating: Rating) => {
console.log(rating);
this.ratings.push(rating);
.subscribe((response: Response) => {
let ratingJson = response.json()
this.ratings.push(new Rating(ratingJson.id, ratingJson.bookId, ratingJson.stars))
}, (error) => {
console.log(error);
});
}
@@ -63,7 +58,9 @@ export class RatingComponent implements OnInit, OnChanges {
this.httpService.updateRating(this.newRating, this.principal.credentials)
.subscribe(() => {
this.newRating = new Rating(null, this.bookId, 1);
})
}, (error) => {
console.log(error);
});
}
selectRating(rating: Rating) {
@@ -84,6 +81,8 @@ export class RatingComponent implements OnInit, OnChanges {
this.newRating = new Rating(null, this.bookId, 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