Implement code for new JHipster article

This commit is contained in:
Michael Pratt 2019-03-19 21:31:28 -06:00
parent 57ae9db854
commit a6e573b363
7 changed files with 59 additions and 1 deletions

View File

@ -101,6 +101,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/books/purchase/**").authenticated()
.antMatchers("/api/register").permitAll()
.antMatchers("/api/activate").permitAll()
.antMatchers("/api/authenticate").permitAll()

View File

@ -40,4 +40,11 @@ public interface BookService {
* @param id the id of the entity
*/
void delete(Long id);
/**
* Simulates purchasing a book by reducing the stock of a book by 1.
* @param id the id of the book
* @return Updated BookDTO, empty if not found, or throws exception if an error occurs.
*/
Optional<BookDTO> purchase(Long id);
}

View File

@ -5,6 +5,7 @@ import com.baeldung.jhipster5.domain.Book;
import com.baeldung.jhipster5.repository.BookRepository;
import com.baeldung.jhipster5.service.dto.BookDTO;
import com.baeldung.jhipster5.service.mapper.BookMapper;
import com.baeldung.jhipster5.web.rest.errors.BadRequestAlertException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -87,4 +88,22 @@ public class BookServiceImpl implements BookService {
log.debug("Request to delete Book : {}", id);
bookRepository.deleteById(id);
}
@Override
public Optional<BookDTO> purchase(Long id) {
Optional<BookDTO> bookDTO = findOne(id);
if(bookDTO.isPresent()) {
int quantity = bookDTO.get().getQuantity();
if(quantity > 0) {
bookDTO.get().setQuantity(quantity - 1);
Book book = bookMapper.toEntity(bookDTO.get());
book = bookRepository.save(book);
return bookDTO;
}
else {
throw new BadRequestAlertException("Book is not in stock", "book", "notinstock");
}
}
return Optional.empty();
}
}

View File

@ -109,4 +109,10 @@ public class BookResource {
bookService.delete(id);
return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
}
@GetMapping("/books/purchase/{id}")
public ResponseEntity<BookDTO> purchase(@PathVariable Long id) {
Optional<BookDTO> bookDTO = bookService.purchase(id);
return ResponseUtil.wrapOrNotFound(bookDTO);
}
}

View File

@ -38,6 +38,12 @@
class="btn btn-primary">
<fa-icon [icon]="'pencil-alt'"></fa-icon>&nbsp;<span> Edit</span>
</button>
<button type="button"
class="btn btn-primary"
(click)="purchase(book.id)">
<span>Purchase</span>
</button>
</div>
</div>
</div>

View File

@ -2,6 +2,8 @@ import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { IBook } from 'app/shared/model/book.model';
import { BookService } from 'app/entities/book/book.service';
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
@Component({
selector: 'jhi-book-detail',
@ -10,7 +12,7 @@ import { IBook } from 'app/shared/model/book.model';
export class BookDetailComponent implements OnInit {
book: IBook;
constructor(protected activatedRoute: ActivatedRoute) {}
constructor(protected activatedRoute: ActivatedRoute, protected bookService: BookService) {}
ngOnInit() {
this.activatedRoute.data.subscribe(({ book }) => {
@ -21,4 +23,14 @@ export class BookDetailComponent implements OnInit {
previousState() {
window.history.back();
}
purchase(id: number) {
console.log('Purchasing book ' + id);
this.bookService.purchase(id).subscribe(
(res: HttpResponse<IBook>) => {
this.book = res.body;
},
(res: HttpErrorResponse) => console.log(res.message)
);
}
}

View File

@ -38,6 +38,13 @@ export class BookService {
.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
}
purchase(id: number): Observable<EntityResponseType> {
console.log('Calling /api/books/purchase/ ' + id);
return this.http
.get<IBook>(`${this.resourceUrl}/purchase/${id}`, { observe: 'response' })
.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
}
query(req?: any): Observable<EntityArrayResponseType> {
const options = createRequestOption(req);
return this.http