BAEL-797 adding book related http calls
This commit is contained in:
parent
136bb775f3
commit
f2a02cc278
|
@ -1,7 +1,8 @@
|
||||||
import {Component, OnInit, Input, Output, EventEmitter} from "@angular/core";
|
import {Component, OnInit, Input, Output, EventEmitter} from "@angular/core";
|
||||||
import {Principal} from "../../principal";
|
import {Principal} from "../../principal";
|
||||||
import {Book} from "../../book";
|
import {Book} from "../../book";
|
||||||
import {Http} from "@angular/http";
|
import {Response} from "@angular/http";
|
||||||
|
import {HttpService} from "../../http.service";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-book-list',
|
selector: 'app-book-list',
|
||||||
|
@ -20,18 +21,21 @@ export class BookListComponent implements OnInit {
|
||||||
isAddNewBook: boolean = false;
|
isAddNewBook: boolean = false;
|
||||||
selectedBook: Book = null;
|
selectedBook: Book = null;
|
||||||
|
|
||||||
constructor(private http: Http) { }
|
constructor(private httpService: HttpService) { }
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.loadBooks();
|
this.loadBooks();
|
||||||
}
|
}
|
||||||
|
|
||||||
loadBooks() {
|
loadBooks() {
|
||||||
let book: Book = new Book(1, 'Tom Sawyer', 'Huckleberry Finn');
|
this.httpService.getBooks()
|
||||||
let book1: Book = new Book(2, 'Michael Crichton', 'Jurassic Park');
|
.map((response: Response) => response.json())
|
||||||
let book2: Book = new Book(3, 'McLaughlin, Pollice, and West', 'Object Oriented Analysis And Design');
|
.map((data: any) => new Book(data.id, data.author, data.title))
|
||||||
this.books.push(book, book1, book2);
|
.subscribe((book: Book) => {
|
||||||
this.books.forEach(book => this.newBooks.push(new Book(book.id, book.author, book.title)))
|
console.log(book);
|
||||||
|
this.books.push(book);
|
||||||
|
this.newBooks.push(new Book(book.id, book.author, book.title))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
cancelEditBook(bookIndex: number) {
|
cancelEditBook(bookIndex: number) {
|
||||||
|
@ -49,21 +53,31 @@ export class BookListComponent implements OnInit {
|
||||||
|
|
||||||
saveBook(bookIndex: number, newBook: Book) {
|
saveBook(bookIndex: number, newBook: Book) {
|
||||||
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)
|
||||||
|
.map((response: Response) => response.json())
|
||||||
|
.map((data: any) => new Book(data.id, data.author, data.title))
|
||||||
|
.subscribe((book: Book) => {
|
||||||
|
console.log(book);
|
||||||
//update the current array of books
|
//update the current array of books
|
||||||
let book: Book = this.books.find(b => b.id === newBook.id);
|
let bookArr: Book = this.books.find(b => b.id === book.id);
|
||||||
book.title = newBook.title;
|
bookArr.title = book.title;
|
||||||
book.author = newBook.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
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(bookIndex: number) {
|
delete(bookIndex: number) {
|
||||||
|
let book: Book = this.books[bookIndex];
|
||||||
|
this.httpService.deleteBook(book, this.principal.credentials)
|
||||||
|
.subscribe(() => {
|
||||||
if (this.selectedBook !== null && this.books[bookIndex].id === this.selectedBook.id) {this.selectedBook = null;}
|
if (this.selectedBook !== null && this.books[bookIndex].id === this.selectedBook.id) {this.selectedBook = null;}
|
||||||
|
|
||||||
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
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
activateAddNewBook() {
|
activateAddNewBook() {
|
||||||
|
@ -73,11 +87,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
|
||||||
//on success:
|
this.httpService.createBook(newBook, this.principal.credentials)
|
||||||
this.books.push(newBook);
|
.map((response: Response) => response.json())
|
||||||
this.newBooks.push(newBook);
|
.map((data: any) => new Book(data.id, data.author, data.title))
|
||||||
this.newBook = new Book(null, '', '');
|
.subscribe((book: Book) => {
|
||||||
|
console.log(book);
|
||||||
|
this.books.push(book);
|
||||||
|
this.newBooks.push(book);
|
||||||
|
this.newBook = new Book(Math.floor(Math.random() * 1000), '', '');
|
||||||
element.focus();
|
element.focus();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
cancelAddBook() {
|
cancelAddBook() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import {Injectable} from "@angular/core";
|
import {Injectable} from "@angular/core";
|
||||||
import {Observable} from "rxjs";
|
import {Observable} from "rxjs";
|
||||||
import {Response, Http, Headers, RequestOptions} from "@angular/http";
|
import {Response, Http, Headers, RequestOptions} from "@angular/http";
|
||||||
|
import {Book} from "./book";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HttpService {
|
export class HttpService {
|
||||||
|
@ -22,4 +23,34 @@ export class HttpService {
|
||||||
let options = new RequestOptions({headers: headers});
|
let options = new RequestOptions({headers: headers});
|
||||||
return this.http.post("/logout", '', options)
|
return this.http.post("/logout", '', options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getBooks(): Observable<Response> {
|
||||||
|
let headers = new Headers({'Content-Type': 'application/json'});
|
||||||
|
let options = new RequestOptions({headers: headers});
|
||||||
|
return this.http.get("/book-service/books", options)
|
||||||
|
}
|
||||||
|
|
||||||
|
updateBook(newBook: Book, user: any): Observable<Response> {
|
||||||
|
let headers = new Headers({'Content-Type': 'application/json'});
|
||||||
|
headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password));
|
||||||
|
headers.append('X-Requested-With','XMLHttpRequest');
|
||||||
|
let options = new RequestOptions({headers: headers});
|
||||||
|
return this.http.put("/book-service/books/" + newBook.id, newBook, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteBook(book: Book, user: any) {
|
||||||
|
let headers = new Headers({'Content-Type': 'application/json'});
|
||||||
|
headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password));
|
||||||
|
headers.append('X-Requested-With','XMLHttpRequest');
|
||||||
|
let options = new RequestOptions({headers: headers});
|
||||||
|
return this.http.delete("/book-service/books/" + book.id, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
createBook(newBook: Book, user: any) {
|
||||||
|
let headers = new Headers({'Content-Type': 'application/json'});
|
||||||
|
headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password));
|
||||||
|
headers.append('X-Requested-With','XMLHttpRequest');
|
||||||
|
let options = new RequestOptions({headers: headers});
|
||||||
|
return this.http.post("/book-service/books", newBook, options)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue