BAEL-797 debugging the http calls

This commit is contained in:
tschiman 2017-04-25 00:25:49 -06:00
parent 82f4657a7b
commit 59f0e41483
10 changed files with 102 additions and 6 deletions

View File

@ -3,6 +3,7 @@ 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,7 +31,13 @@ export class BookListComponent implements OnInit {
loadBooks() {
this.httpService.getBooks()
.map((response: Response) => response.json())
.map((data: any) => new Book(data.id, data.author, data.title))
.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);

View File

@ -55,6 +55,11 @@ export class HttpService {
return this.http.delete("/rating-service/ratings/" + ratingId, options)
}
updateRating(rating: Rating, user: any) {
let options = this.makeAuthOptions(user);
return this.http.put("/rating-service/ratings/" + rating.id, rating, options)
}
private makeAuthOptions(user: any): RequestOptions {
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password));

View File

@ -19,6 +19,7 @@ Ratings:
</label>
</div>
<button *ngIf="newRating.id === null" type="submit" class="btn btn-secondary" [disabled]="!f.valid">Add Rating</button>
<button *ngIf="principal.isAdmin() && newRating.id !== null" type="button" class="btn btn-secondary" (click)="updateRating()">Save</button>
<button *ngIf="principal.isAdmin() && newRating.id !== null" type="button" class="btn btn-secondary" (click)="cancelSelection()">Cancel</button>
</form>

View File

@ -3,6 +3,7 @@ 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,6 +36,10 @@ 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);
@ -54,6 +59,13 @@ export class RatingComponent implements OnInit, OnChanges {
});
}
updateRating() {
this.httpService.updateRating(this.newRating, this.principal.credentials)
.subscribe(() => {
this.newRating = new Rating(null, this.bookId, 1);
})
}
selectRating(rating: Rating) {
if (this.principal.isAdmin()) {
this.newRating = rating;

View File

@ -0,0 +1,25 @@
package com.baeldung.spring.cloud.bootstrap.svcbook;
import com.baeldung.spring.cloud.bootstrap.svcbook.book.Book;
import com.baeldung.spring.cloud.bootstrap.svcbook.book.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class DataLoader implements ApplicationRunner {
private BookService bookService;
@Autowired
public DataLoader(BookService bookService) {
this.bookService = bookService;
}
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
this.bookService.createBook(new Book("Aldous Huxley", "Brave new world"));
this.bookService.createBook(new Book("George Orwell", "Animal Farm"));
}
}

View File

@ -19,6 +19,11 @@ public class Book {
public Book() {
}
public Book(String author, String title) {
this.author = author;
this.title = title;
}
public Long getId() {
return id;
}

View File

@ -0,0 +1,28 @@
package com.baeldung.spring.cloud.bootstrap.svcrating;
import com.baeldung.spring.cloud.bootstrap.svcrating.rating.Rating;
import com.baeldung.spring.cloud.bootstrap.svcrating.rating.RatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class DataLoader implements ApplicationRunner {
private RatingService ratingService;
@Autowired
public DataLoader(RatingService ratingService) {
this.ratingService = ratingService;
}
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
this.ratingService.createRating(new Rating(1L, 1));
this.ratingService.createRating(new Rating(1L, 2));
this.ratingService.createRating(new Rating(2L, 3));
this.ratingService.createRating(new Rating(2L, 4));
this.ratingService.createRating(new Rating(2L, 5));
}
}

View File

@ -26,6 +26,11 @@ public class Rating {
this.stars = stars;
}
public Rating(Long bookId, int stars) {
this.bookId = bookId;
this.stars = stars;
}
public Long getId() {
return id;
}

View File

@ -1,15 +1,14 @@
package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.base.Preconditions;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Service
@Transactional(readOnly = true)
@ -58,6 +57,7 @@ public class RatingService {
return ratingRepository.save(rating);
}
@Transactional(propagation = Propagation.REQUIRED)
public Rating updateRating(Rating rating, Long ratingId) {
Preconditions.checkNotNull(rating);
Preconditions.checkState(rating.getId() == ratingId);

View File

@ -0,0 +1,8 @@
insert into book (id, author, title) values
(1, 1, 1),
(2, 1, 2),
(3, 2, 3),
(4, 2, 4),
(5, 3, 5),
(6, 3, 1),
(7, 3, 2);