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 {Observable} from "rxjs";
import {Response, Http, Headers, RequestOptions} from "@angular/http"; import {Response, Http, Headers, RequestOptions} from "@angular/http";
import {Book} from "./book"; import {Book} from "./book";
import {Rating} from "./rating";
@Injectable() @Injectable()
export class HttpService { export class HttpService {
@ -9,18 +10,12 @@ export class HttpService {
constructor(private http: Http) { } constructor(private http: Http) { }
login(user: any): Observable<Response> { login(user: any): Observable<Response> {
let headers = new Headers({'Content-Type': 'application/json'}); let options = this.makeAuthOptions(user);
headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password));
headers.append('X-Requested-With','XMLHttpRequest');
let options = new RequestOptions({headers: headers});
return this.http.get("/me", options) return this.http.get("/me", options)
} }
logout(user: any): Observable<Response> { logout(user: any): Observable<Response> {
let headers = new Headers({'Content-Type': 'application/json'}); let options = this.makeAuthOptions(user);
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("/logout", '', options) return this.http.post("/logout", '', options)
} }
@ -31,26 +26,39 @@ export class HttpService {
} }
updateBook(newBook: Book, user: any): Observable<Response> { updateBook(newBook: Book, user: any): Observable<Response> {
let headers = new Headers({'Content-Type': 'application/json'}); let options = this.makeAuthOptions(user);
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) return this.http.put("/book-service/books/" + newBook.id, newBook, options)
} }
deleteBook(book: Book, user: any) { deleteBook(book: Book, user: any): Observable<Response> {
let headers = new Headers({'Content-Type': 'application/json'}); let options = this.makeAuthOptions(user);
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) 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'}); let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password)); headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password));
headers.append('X-Requested-With','XMLHttpRequest'); headers.append('X-Requested-With','XMLHttpRequest');
let options = new RequestOptions({headers: headers}); return new RequestOptions({headers: headers});;
return this.http.post("/book-service/books", newBook, options)
} }
} }

View File

@ -12,7 +12,7 @@ Ratings:
</div> </div>
</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;"> <div class="form-check form-check-inline" *ngFor="let star of stars; let i = index;">
<label class="form-check-label"> <label class="form-check-label">
<input class="form-check-input" type="radio" name="star" [(ngModel)]="newRating.stars" [value]="star">{{star}} <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 {Component, OnInit, Input, OnChanges} from "@angular/core";
import {Rating} from "../rating"; import {Rating} from "../rating";
import {Principal} from "../principal"; import {Principal} from "../principal";
import {HttpService} from "../http.service";
import {Response} from "@angular/http";
@Component({ @Component({
selector: 'app-rating', selector: 'app-rating',
@ -15,7 +17,7 @@ export class RatingComponent implements OnInit, OnChanges {
stars: number[] = [1,2,3,4,5]; stars: number[] = [1,2,3,4,5];
newRating: Rating = null; newRating: Rating = null;
constructor() { } constructor(private httpService: HttpService) { }
ngOnInit() {} ngOnInit() {}
@ -31,15 +33,25 @@ export class RatingComponent implements OnInit, OnChanges {
} }
private loadRatings() { private loadRatings() {
let rating: Rating = new Rating(1, this.bookId, 1); this.httpService.getRatings(this.bookId, this.principal.credentials)
let rating1: Rating = new Rating(1, this.bookId, 1); .map((response: Response) => response.json())
this.ratings.push(rating, rating1); .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); console.log(this.newRating);
let ratingCopy: Rating = Object.assign({}, this.newRating, {id: Math.floor(Math.random() * 1000)}); let ratingCopy: Rating = Object.assign({}, this.newRating);
this.ratings.push(ratingCopy); 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) { selectRating(rating: Rating) {
@ -53,10 +65,15 @@ export class RatingComponent implements OnInit, OnChanges {
} }
deleteRating(index: number) { deleteRating(index: number) {
if (this.ratings[index] === this.newRating) { let rating = this.ratings[index];
this.newRating = new Rating(null, this.bookId, 1); this.httpService.deleteRating(rating.id, this.principal.credentials)
} .subscribe(() => {
this.ratings.splice(index, 1); if (this.ratings[index] === this.newRating) {
this.newRating = new Rating(null, this.bookId, 1);
}
this.ratings.splice(index, 1);
});
} }
} }