adding authors
This commit is contained in:
parent
cb869e0dd3
commit
afe847c255
@ -0,0 +1,64 @@
|
|||||||
|
package com.ip.library.authors.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.ip.library.core.configuration.Constants;
|
||||||
|
import com.ip.library.authors.model.AuthorEntity;
|
||||||
|
import com.ip.library.authors.service.AuthorService;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(Constants.API_URL + "/author")
|
||||||
|
public class AuthorController {
|
||||||
|
private final AuthorService authorService;
|
||||||
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
|
public AuthorController(AuthorService authorService, ModelMapper modelMapper) {
|
||||||
|
this.authorService = authorService;
|
||||||
|
this.modelMapper = modelMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
private AuthorDto toDto(AuthorEntity entity) {
|
||||||
|
return modelMapper.map(entity, AuthorDto.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private AuthorEntity toEntity(AuthorDto dto) {
|
||||||
|
return modelMapper.map(dto, AuthorEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<AuthorDto> getAll() {
|
||||||
|
return authorService.getAll().stream().map(this::toDto).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public AuthorDto get(@PathVariable(name = "id") Long id) {
|
||||||
|
return toDto(authorService.get(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public AuthorDto create(@RequestBody @Valid AuthorDto dto) {
|
||||||
|
return toDto(authorService.create(toEntity(dto)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public AuthorDto update(@PathVariable(name = "id") Long id, @RequestBody AuthorDto dto) {
|
||||||
|
return toDto(authorService.update(id, toEntity(dto)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public AuthorDto delete(@PathVariable(name = "id") Long id) {
|
||||||
|
return toDto(authorService.delete(id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.ip.library.authors.api;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
public class AuthorDto {
|
||||||
|
private Long id;
|
||||||
|
@NotBlank
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.ip.library.authors.model;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import com.ip.library.core.model.BaseEntity;
|
||||||
|
|
||||||
|
public class AuthorEntity extends BaseEntity {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public AuthorEntity() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthorEntity(Long id, String name) {
|
||||||
|
super(id);
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final AuthorEntity other = (AuthorEntity) obj;
|
||||||
|
return Objects.equals(other.getId(), id)
|
||||||
|
&& Objects.equals(other.getName(), name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.ip.library.authors.repository;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import com.ip.library.authors.model.AuthorEntity;
|
||||||
|
import com.ip.library.core.repository.MapRepository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class AuthorRepository extends MapRepository<AuthorEntity> {
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.ip.library.authors.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.ip.library.core.error.NotFoundException;
|
||||||
|
import com.ip.library.authors.model.AuthorEntity;
|
||||||
|
import com.ip.library.authors.repository.AuthorRepository;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AuthorService {
|
||||||
|
private final AuthorRepository repository;
|
||||||
|
|
||||||
|
public AuthorService(AuthorRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<AuthorEntity> getAll() {
|
||||||
|
return repository.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthorEntity get(Long id) {
|
||||||
|
return Optional.ofNullable(repository.get(id))
|
||||||
|
.orElseThrow(() -> new NotFoundException(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthorEntity create(AuthorEntity entity) {
|
||||||
|
return repository.create(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthorEntity update(Long id, AuthorEntity entity) {
|
||||||
|
final AuthorEntity existsEntity = get(id);
|
||||||
|
existsEntity.setName(entity.getName());
|
||||||
|
return repository.update(existsEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthorEntity delete(Long id) {
|
||||||
|
final AuthorEntity existsEntity = get(id);
|
||||||
|
return repository.delete(existsEntity);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.ip.library.items.api;
|
package com.ip.library.books.api;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -13,58 +13,58 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
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.core.configuration.Constants;
|
||||||
import com.ip.library.items.model.ItemEntity;
|
|
||||||
import com.ip.library.items.service.ItemService;
|
|
||||||
import com.ip.library.types.service.TypeService;
|
import com.ip.library.types.service.TypeService;
|
||||||
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(Constants.API_URL + "/item")
|
@RequestMapping(Constants.API_URL + "/item")
|
||||||
public class ItemController {
|
public class BookController {
|
||||||
private final ItemService itemService;
|
private final BookService itemService;
|
||||||
private final TypeService typeService;
|
private final TypeService typeService;
|
||||||
private final ModelMapper modelMapper;
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
public ItemController(ItemService itemService, TypeService typeService, ModelMapper modelMapper) {
|
public BookController(BookService itemService, TypeService typeService, ModelMapper modelMapper) {
|
||||||
this.itemService = itemService;
|
this.itemService = itemService;
|
||||||
this.typeService = typeService;
|
this.typeService = typeService;
|
||||||
this.modelMapper = modelMapper;
|
this.modelMapper = modelMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ItemDto toDto(ItemEntity entity) {
|
private BookDto toDto(BookEntity entity) {
|
||||||
return modelMapper.map(entity, ItemDto.class);
|
return modelMapper.map(entity, BookDto.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ItemEntity toEntity(ItemDto dto) {
|
private BookEntity toEntity(BookDto dto) {
|
||||||
final ItemEntity entity = modelMapper.map(dto, ItemEntity.class);
|
final BookEntity entity = modelMapper.map(dto, BookEntity.class);
|
||||||
entity.setType(typeService.get(dto.getTypeId()));
|
entity.setType(typeService.get(dto.getTypeId()));
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<ItemDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) {
|
public List<BookDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) {
|
||||||
return itemService.getAll(typeId).stream().map(this::toDto).toList();
|
return itemService.getAll(typeId).stream().map(this::toDto).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public ItemDto get(@PathVariable(name = "id") Long id) {
|
public BookDto get(@PathVariable(name = "id") Long id) {
|
||||||
return toDto(itemService.get(id));
|
return toDto(itemService.get(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ItemDto create(@RequestBody @Valid ItemDto dto) {
|
public BookDto create(@RequestBody @Valid BookDto dto) {
|
||||||
return toDto(itemService.create(toEntity(dto)));
|
return toDto(itemService.create(toEntity(dto)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public ItemDto update(@PathVariable(name = "id") Long id, @RequestBody ItemDto dto) {
|
public BookDto update(@PathVariable(name = "id") Long id, @RequestBody BookDto dto) {
|
||||||
return toDto(itemService.update(id, toEntity(dto)));
|
return toDto(itemService.update(id, toEntity(dto)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public ItemDto delete(@PathVariable(name = "id") Long id) {
|
public BookDto delete(@PathVariable(name = "id") Long id) {
|
||||||
return toDto(itemService.delete(id));
|
return toDto(itemService.delete(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,11 +1,11 @@
|
|||||||
package com.ip.library.items.api;
|
package com.ip.library.books.api;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import jakarta.validation.constraints.Min;
|
import jakarta.validation.constraints.Min;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class ItemDto {
|
public class BookDto {
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotNull
|
@NotNull
|
||||||
@Min(1)
|
@Min(1)
|
@ -1,20 +1,20 @@
|
|||||||
package com.ip.library.items.model;
|
package com.ip.library.books.model;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import com.ip.library.core.model.BaseEntity;
|
import com.ip.library.core.model.BaseEntity;
|
||||||
import com.ip.library.types.model.TypeEntity;
|
import com.ip.library.types.model.TypeEntity;
|
||||||
|
|
||||||
public class ItemEntity extends BaseEntity {
|
public class BookEntity extends BaseEntity {
|
||||||
private TypeEntity type;
|
private TypeEntity type;
|
||||||
private Double price;
|
private Double price;
|
||||||
private Integer count;
|
private Integer count;
|
||||||
|
|
||||||
public ItemEntity() {
|
public BookEntity() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemEntity(Long id, TypeEntity type, Double price, Integer count) {
|
public BookEntity(Long id, TypeEntity type, Double price, Integer count) {
|
||||||
super(id);
|
super(id);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.price = price;
|
this.price = price;
|
||||||
@ -56,7 +56,7 @@ public class ItemEntity extends BaseEntity {
|
|||||||
return true;
|
return true;
|
||||||
if (obj == null || getClass() != obj.getClass())
|
if (obj == null || getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
final ItemEntity other = (ItemEntity) obj;
|
final BookEntity other = (BookEntity) obj;
|
||||||
return Objects.equals(other.getId(), id)
|
return Objects.equals(other.getId(), id)
|
||||||
&& Objects.equals(other.getType(), type)
|
&& Objects.equals(other.getType(), type)
|
||||||
&& Objects.equals(other.getPrice(), price)
|
&& Objects.equals(other.getPrice(), price)
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.ip.library.books.repository;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import com.ip.library.books.model.BookEntity;
|
||||||
|
import com.ip.library.core.repository.MapRepository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class BookRepository extends MapRepository<BookEntity> {
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.ip.library.items.service;
|
package com.ip.library.books.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -6,19 +6,19 @@ import java.util.Optional;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.ip.library.books.model.BookEntity;
|
||||||
|
import com.ip.library.books.repository.BookRepository;
|
||||||
import com.ip.library.core.error.NotFoundException;
|
import com.ip.library.core.error.NotFoundException;
|
||||||
import com.ip.library.items.model.ItemEntity;
|
|
||||||
import com.ip.library.items.repository.ItemRepository;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ItemService {
|
public class BookService {
|
||||||
private final ItemRepository repository;
|
private final BookRepository repository;
|
||||||
|
|
||||||
public ItemService(ItemRepository repository) {
|
public BookService(BookRepository repository) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ItemEntity> getAll(Long typeId) {
|
public List<BookEntity> getAll(Long typeId) {
|
||||||
if (Objects.equals(typeId, 0L)) {
|
if (Objects.equals(typeId, 0L)) {
|
||||||
return repository.getAll();
|
return repository.getAll();
|
||||||
}
|
}
|
||||||
@ -27,25 +27,25 @@ public class ItemService {
|
|||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemEntity get(Long id) {
|
public BookEntity get(Long id) {
|
||||||
return Optional.ofNullable(repository.get(id))
|
return Optional.ofNullable(repository.get(id))
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
.orElseThrow(() -> new NotFoundException(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemEntity create(ItemEntity entity) {
|
public BookEntity create(BookEntity entity) {
|
||||||
return repository.create(entity);
|
return repository.create(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemEntity update(Long id, ItemEntity entity) {
|
public BookEntity update(Long id, BookEntity entity) {
|
||||||
final ItemEntity existsEntity = get(id);
|
final BookEntity existsEntity = get(id);
|
||||||
existsEntity.setType(entity.getType());
|
existsEntity.setType(entity.getType());
|
||||||
existsEntity.setPrice(entity.getPrice());
|
existsEntity.setPrice(entity.getPrice());
|
||||||
existsEntity.setCount(entity.getCount());
|
existsEntity.setCount(entity.getCount());
|
||||||
return repository.update(existsEntity);
|
return repository.update(existsEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemEntity delete(Long id) {
|
public BookEntity delete(Long id) {
|
||||||
final ItemEntity existsEntity = get(id);
|
final BookEntity existsEntity = get(id);
|
||||||
return repository.delete(existsEntity);
|
return repository.delete(existsEntity);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,10 +0,0 @@
|
|||||||
package com.ip.library.items.repository;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import com.ip.library.core.repository.MapRepository;
|
|
||||||
import com.ip.library.items.model.ItemEntity;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class ItemRepository extends MapRepository<ItemEntity> {
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user