BAEL-797 adding rating http calls

This commit is contained in:
tschiman 2017-04-24 23:05:08 -06:00
parent f2a02cc278
commit 82f4657a7b
3 changed files with 57 additions and 32 deletions

View File

@ -2,6 +2,7 @@ import {Injectable} from "@angular/core";
import {Observable} from "rxjs";
import {Response, Http, Headers, RequestOptions} from "@angular/http";
import {Book} from "./book";
import {Rating} from "./rating";
@Injectable()
export class HttpService {
@ -9,18 +10,12 @@ export class HttpService {
constructor(private http: Http) { }
login(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});
let options = this.makeAuthOptions(user);
return this.http.get("/me", options)
}
logout(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});
let options = this.makeAuthOptions(user);
return this.http.post("/logout", '', options)
}
@ -31,26 +26,39 @@ export class HttpService {
}
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});
let options = this.makeAuthOptions(user);
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});
deleteBook(book: Book, user: any): Observable<Response> {
let options = this.makeAuthOptions(user);
return this.http.delete("/book-service/books/" + book.id, options)
}
createBook(newBook: Book, user: any) {
createBook(newBook: Book, user: any): Observable<Response> {
let options = this.makeAuthOptions(user);
return this.http.post("/book-service/books", newBook, options)
}
getRatings(bookId: number, user: any): Observable<Response> {
let options = this.makeAuthOptions(user);
return this.http.get("/rating-service/ratings?bookId=" + bookId, options)
}
createRating(rating: Rating, user: any): Observable<Response> {
let options = this.makeAuthOptions(user);
return this.http.post("/rating-service/ratings", rating, options)
}
deleteRating(ratingId: number, user: any) {
let options = this.makeAuthOptions(user);
return this.http.delete("/rating-service/ratings/" + ratingId, options)
}
private makeAuthOptions(user: any): RequestOptions {
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)
return new RequestOptions({headers: headers});;
}
}

View File

@ -12,7 +12,7 @@ Ratings:
</div>
</div>
<form (ngSubmit)="onSubmit(f)" #f="ngForm">
<form (ngSubmit)="onSaveRating(f)" #f="ngForm">
<div class="form-check form-check-inline" *ngFor="let star of stars; let i = index;">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="star" [(ngModel)]="newRating.stars" [value]="star">{{star}}

View File

@ -1,6 +1,8 @@
import {Component, OnInit, Input, OnChanges} from "@angular/core";
import {Rating} from "../rating";
import {Principal} from "../principal";
import {HttpService} from "../http.service";
import {Response} from "@angular/http";
@Component({
selector: 'app-rating',
@ -15,7 +17,7 @@ export class RatingComponent implements OnInit, OnChanges {
stars: number[] = [1,2,3,4,5];
newRating: Rating = null;
constructor() { }
constructor(private httpService: HttpService) { }
ngOnInit() {}
@ -31,15 +33,25 @@ export class RatingComponent implements OnInit, OnChanges {
}
private loadRatings() {
let rating: Rating = new Rating(1, this.bookId, 1);
let rating1: Rating = new Rating(1, this.bookId, 1);
this.ratings.push(rating, rating1);
this.httpService.getRatings(this.bookId, 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);
});
}
onSubmit() {
onSaveRating() {
console.log(this.newRating);
let ratingCopy: Rating = Object.assign({}, this.newRating, {id: Math.floor(Math.random() * 1000)});
this.ratings.push(ratingCopy);
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);
});
}
selectRating(rating: Rating) {
@ -53,10 +65,15 @@ export class RatingComponent implements OnInit, OnChanges {
}
deleteRating(index: number) {
if (this.ratings[index] === this.newRating) {
this.newRating = new Rating(null, this.bookId, 1);
}
this.ratings.splice(index, 1);
let rating = this.ratings[index];
this.httpService.deleteRating(rating.id, this.principal.credentials)
.subscribe(() => {
if (this.ratings[index] === this.newRating) {
this.newRating = new Rating(null, this.bookId, 1);
}
this.ratings.splice(index, 1);
});
}
}