Implement code for new JHipster article
This commit is contained in:
parent
57ae9db854
commit
a6e573b363
@ -101,6 +101,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||||
.and()
|
.and()
|
||||||
.authorizeRequests()
|
.authorizeRequests()
|
||||||
|
.antMatchers("/api/books/purchase/**").authenticated()
|
||||||
.antMatchers("/api/register").permitAll()
|
.antMatchers("/api/register").permitAll()
|
||||||
.antMatchers("/api/activate").permitAll()
|
.antMatchers("/api/activate").permitAll()
|
||||||
.antMatchers("/api/authenticate").permitAll()
|
.antMatchers("/api/authenticate").permitAll()
|
||||||
|
@ -40,4 +40,11 @@ public interface BookService {
|
|||||||
* @param id the id of the entity
|
* @param id the id of the entity
|
||||||
*/
|
*/
|
||||||
void delete(Long id);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import com.baeldung.jhipster5.domain.Book;
|
|||||||
import com.baeldung.jhipster5.repository.BookRepository;
|
import com.baeldung.jhipster5.repository.BookRepository;
|
||||||
import com.baeldung.jhipster5.service.dto.BookDTO;
|
import com.baeldung.jhipster5.service.dto.BookDTO;
|
||||||
import com.baeldung.jhipster5.service.mapper.BookMapper;
|
import com.baeldung.jhipster5.service.mapper.BookMapper;
|
||||||
|
import com.baeldung.jhipster5.web.rest.errors.BadRequestAlertException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -87,4 +88,22 @@ public class BookServiceImpl implements BookService {
|
|||||||
log.debug("Request to delete Book : {}", id);
|
log.debug("Request to delete Book : {}", id);
|
||||||
bookRepository.deleteById(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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,4 +109,10 @@ public class BookResource {
|
|||||||
bookService.delete(id);
|
bookService.delete(id);
|
||||||
return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,12 @@
|
|||||||
class="btn btn-primary">
|
class="btn btn-primary">
|
||||||
<fa-icon [icon]="'pencil-alt'"></fa-icon> <span> Edit</span>
|
<fa-icon [icon]="'pencil-alt'"></fa-icon> <span> Edit</span>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button type="button"
|
||||||
|
class="btn btn-primary"
|
||||||
|
(click)="purchase(book.id)">
|
||||||
|
<span>Purchase</span>
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -2,6 +2,8 @@ import { Component, OnInit } from '@angular/core';
|
|||||||
import { ActivatedRoute } from '@angular/router';
|
import { ActivatedRoute } from '@angular/router';
|
||||||
|
|
||||||
import { IBook } from 'app/shared/model/book.model';
|
import { IBook } from 'app/shared/model/book.model';
|
||||||
|
import { BookService } from 'app/entities/book/book.service';
|
||||||
|
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'jhi-book-detail',
|
selector: 'jhi-book-detail',
|
||||||
@ -10,7 +12,7 @@ import { IBook } from 'app/shared/model/book.model';
|
|||||||
export class BookDetailComponent implements OnInit {
|
export class BookDetailComponent implements OnInit {
|
||||||
book: IBook;
|
book: IBook;
|
||||||
|
|
||||||
constructor(protected activatedRoute: ActivatedRoute) {}
|
constructor(protected activatedRoute: ActivatedRoute, protected bookService: BookService) {}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.activatedRoute.data.subscribe(({ book }) => {
|
this.activatedRoute.data.subscribe(({ book }) => {
|
||||||
@ -21,4 +23,14 @@ export class BookDetailComponent implements OnInit {
|
|||||||
previousState() {
|
previousState() {
|
||||||
window.history.back();
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,13 @@ export class BookService {
|
|||||||
.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
|
.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> {
|
query(req?: any): Observable<EntityArrayResponseType> {
|
||||||
const options = createRequestOption(req);
|
const options = createRequestOption(req);
|
||||||
return this.http
|
return this.http
|
||||||
|
Loading…
x
Reference in New Issue
Block a user