lab2 added books
This commit is contained in:
parent
1c197a1555
commit
ee6c906a48
@ -17,19 +17,23 @@ import com.ip.library.books.model.BookEntity;
|
||||
import com.ip.library.books.service.BookService;
|
||||
import com.ip.library.core.configuration.Constants;
|
||||
import com.ip.library.types.service.TypeService;
|
||||
import com.ip.library.authors.service.AuthorService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/item")
|
||||
@RequestMapping(Constants.API_URL + "/books")
|
||||
public class BookController {
|
||||
private final BookService itemService;
|
||||
private final TypeService typeService;
|
||||
private final AuthorService authorService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public BookController(BookService itemService, TypeService typeService, ModelMapper modelMapper) {
|
||||
public BookController(BookService itemService, TypeService typeService,
|
||||
AuthorService authorService, ModelMapper modelMapper) {
|
||||
this.itemService = itemService;
|
||||
this.typeService = typeService;
|
||||
this.authorService = authorService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
@ -40,12 +44,15 @@ public class BookController {
|
||||
private BookEntity toEntity(BookDto dto) {
|
||||
final BookEntity entity = modelMapper.map(dto, BookEntity.class);
|
||||
entity.setType(typeService.get(dto.getTypeId()));
|
||||
entity.setAuthor(authorService.get(dto.getAuthorId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<BookDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) {
|
||||
return itemService.getAll(typeId).stream().map(this::toDto).toList();
|
||||
public List<BookDto> getAll(
|
||||
@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
|
||||
@RequestParam(name = "authorId", defaultValue = "0") Long authorId) {
|
||||
return itemService.getAll(typeId, authorId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
@ -12,10 +12,7 @@ public class BookDto {
|
||||
private Long typeId;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Double price;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Integer count;
|
||||
private Long authorId;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
@ -34,24 +31,11 @@ public class BookDto {
|
||||
this.typeId = typeId;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
public Long getAuthorId() {
|
||||
return authorId;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Double getSum() {
|
||||
return price * count;
|
||||
public void setAuthorId(Long authorId) {
|
||||
this.authorId = authorId;
|
||||
}
|
||||
}
|
||||
|
@ -4,21 +4,20 @@ import java.util.Objects;
|
||||
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
import com.ip.library.authors.model.AuthorEntity;
|
||||
|
||||
public class BookEntity extends BaseEntity {
|
||||
private TypeEntity type;
|
||||
private Double price;
|
||||
private Integer count;
|
||||
private AuthorEntity author;
|
||||
|
||||
public BookEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BookEntity(Long id, TypeEntity type, Double price, Integer count) {
|
||||
public BookEntity(Long id, TypeEntity type, AuthorEntity author) {
|
||||
super(id);
|
||||
this.type = type;
|
||||
this.price = price;
|
||||
this.count = count;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public TypeEntity getType() {
|
||||
@ -29,25 +28,17 @@ public class BookEntity extends BaseEntity {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
public AuthorEntity getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
public void setAuthor(AuthorEntity author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, price, count);
|
||||
return Objects.hash(id, type, author);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -59,7 +50,6 @@ public class BookEntity extends BaseEntity {
|
||||
final BookEntity other = (BookEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getPrice(), price)
|
||||
&& Objects.equals(other.getCount(), count);
|
||||
&& Objects.equals(other.getAuthor(), author);
|
||||
}
|
||||
}
|
||||
|
@ -18,14 +18,20 @@ public class BookService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<BookEntity> getAll(Long typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
return repository.getAll().stream()
|
||||
public List<BookEntity> getAll(Long typeId, Long authorId) {
|
||||
List<BookEntity> result = repository.getAll();
|
||||
if (!Objects.equals(typeId, 0L)){
|
||||
result = result.stream()
|
||||
.filter(item -> item.getType().getId().equals(typeId))
|
||||
.toList();
|
||||
}
|
||||
if (!Objects.equals(authorId, 0L)){
|
||||
result = result.stream()
|
||||
.filter(item -> item.getAuthor().getId().equals(authorId))
|
||||
.toList();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public BookEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
@ -39,8 +45,7 @@ public class BookService {
|
||||
public BookEntity update(Long id, BookEntity entity) {
|
||||
final BookEntity existsEntity = get(id);
|
||||
existsEntity.setType(entity.getType());
|
||||
existsEntity.setPrice(entity.getPrice());
|
||||
existsEntity.setCount(entity.getCount());
|
||||
existsEntity.setAuthor(entity.getAuthor());
|
||||
return repository.update(existsEntity);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user