diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorController.java b/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorController.java new file mode 100644 index 0000000..7e9ea45 --- /dev/null +++ b/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorController.java @@ -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 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)); + } +} diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorDto.java b/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorDto.java new file mode 100644 index 0000000..4601838 --- /dev/null +++ b/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorDto.java @@ -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; + } +} diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/model/AuthorEntity.java b/SpringApp/library/src/main/java/com/ip/library/authors/model/AuthorEntity.java new file mode 100644 index 0000000..9f6df0a --- /dev/null +++ b/SpringApp/library/src/main/java/com/ip/library/authors/model/AuthorEntity.java @@ -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); + } + +} diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/repository/AuthorRepository.java b/SpringApp/library/src/main/java/com/ip/library/authors/repository/AuthorRepository.java new file mode 100644 index 0000000..f8c04fd --- /dev/null +++ b/SpringApp/library/src/main/java/com/ip/library/authors/repository/AuthorRepository.java @@ -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 { +} diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java b/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java new file mode 100644 index 0000000..cc616d3 --- /dev/null +++ b/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java @@ -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 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); + } +} diff --git a/SpringApp/library/src/main/java/com/ip/library/items/api/ItemController.java b/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java similarity index 66% rename from SpringApp/library/src/main/java/com/ip/library/items/api/ItemController.java rename to SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java index 5d459d4..56a20e3 100644 --- a/SpringApp/library/src/main/java/com/ip/library/items/api/ItemController.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java @@ -1,4 +1,4 @@ -package com.ip.library.items.api; +package com.ip.library.books.api; 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.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.items.model.ItemEntity; -import com.ip.library.items.service.ItemService; import com.ip.library.types.service.TypeService; import jakarta.validation.Valid; @RestController @RequestMapping(Constants.API_URL + "/item") -public class ItemController { - private final ItemService itemService; +public class BookController { + private final BookService itemService; private final TypeService typeService; private final ModelMapper modelMapper; - public ItemController(ItemService itemService, TypeService typeService, ModelMapper modelMapper) { + public BookController(BookService itemService, TypeService typeService, ModelMapper modelMapper) { this.itemService = itemService; this.typeService = typeService; this.modelMapper = modelMapper; } - private ItemDto toDto(ItemEntity entity) { - return modelMapper.map(entity, ItemDto.class); + private BookDto toDto(BookEntity entity) { + return modelMapper.map(entity, BookDto.class); } - private ItemEntity toEntity(ItemDto dto) { - final ItemEntity entity = modelMapper.map(dto, ItemEntity.class); + private BookEntity toEntity(BookDto dto) { + final BookEntity entity = modelMapper.map(dto, BookEntity.class); entity.setType(typeService.get(dto.getTypeId())); return entity; } @GetMapping - public List getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) { + public List getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) { return itemService.getAll(typeId).stream().map(this::toDto).toList(); } @GetMapping("/{id}") - public ItemDto get(@PathVariable(name = "id") Long id) { + public BookDto get(@PathVariable(name = "id") Long id) { return toDto(itemService.get(id)); } @PostMapping - public ItemDto create(@RequestBody @Valid ItemDto dto) { + public BookDto create(@RequestBody @Valid BookDto dto) { return toDto(itemService.create(toEntity(dto))); } @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))); } @DeleteMapping("/{id}") - public ItemDto delete(@PathVariable(name = "id") Long id) { + public BookDto delete(@PathVariable(name = "id") Long id) { return toDto(itemService.delete(id)); } } diff --git a/SpringApp/library/src/main/java/com/ip/library/items/api/ItemDto.java b/SpringApp/library/src/main/java/com/ip/library/books/api/BookDto.java similarity index 94% rename from SpringApp/library/src/main/java/com/ip/library/items/api/ItemDto.java rename to SpringApp/library/src/main/java/com/ip/library/books/api/BookDto.java index 2700a2b..d7ddcbc 100644 --- a/SpringApp/library/src/main/java/com/ip/library/items/api/ItemDto.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/api/BookDto.java @@ -1,11 +1,11 @@ -package com.ip.library.items.api; +package com.ip.library.books.api; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; -public class ItemDto { +public class BookDto { private Long id; @NotNull @Min(1) diff --git a/SpringApp/library/src/main/java/com/ip/library/items/model/ItemEntity.java b/SpringApp/library/src/main/java/com/ip/library/books/model/BookEntity.java similarity index 85% rename from SpringApp/library/src/main/java/com/ip/library/items/model/ItemEntity.java rename to SpringApp/library/src/main/java/com/ip/library/books/model/BookEntity.java index b8b2311..afdb489 100644 --- a/SpringApp/library/src/main/java/com/ip/library/items/model/ItemEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/model/BookEntity.java @@ -1,20 +1,20 @@ -package com.ip.library.items.model; +package com.ip.library.books.model; import java.util.Objects; import com.ip.library.core.model.BaseEntity; import com.ip.library.types.model.TypeEntity; -public class ItemEntity extends BaseEntity { +public class BookEntity extends BaseEntity { private TypeEntity type; private Double price; private Integer count; - public ItemEntity() { + public BookEntity() { super(); } - public ItemEntity(Long id, TypeEntity type, Double price, Integer count) { + public BookEntity(Long id, TypeEntity type, Double price, Integer count) { super(id); this.type = type; this.price = price; @@ -56,7 +56,7 @@ public class ItemEntity extends BaseEntity { return true; if (obj == null || getClass() != obj.getClass()) return false; - final ItemEntity other = (ItemEntity) obj; + final BookEntity other = (BookEntity) obj; return Objects.equals(other.getId(), id) && Objects.equals(other.getType(), type) && Objects.equals(other.getPrice(), price) diff --git a/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java b/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java new file mode 100644 index 0000000..55e82c7 --- /dev/null +++ b/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java @@ -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 { +} diff --git a/SpringApp/library/src/main/java/com/ip/library/items/service/ItemService.java b/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java similarity index 60% rename from SpringApp/library/src/main/java/com/ip/library/items/service/ItemService.java rename to SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java index 2eaf637..90c4775 100644 --- a/SpringApp/library/src/main/java/com/ip/library/items/service/ItemService.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java @@ -1,4 +1,4 @@ -package com.ip.library.items.service; +package com.ip.library.books.service; import java.util.List; import java.util.Objects; @@ -6,19 +6,19 @@ import java.util.Optional; 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.items.model.ItemEntity; -import com.ip.library.items.repository.ItemRepository; @Service -public class ItemService { - private final ItemRepository repository; +public class BookService { + private final BookRepository repository; - public ItemService(ItemRepository repository) { + public BookService(BookRepository repository) { this.repository = repository; } - public List getAll(Long typeId) { + public List getAll(Long typeId) { if (Objects.equals(typeId, 0L)) { return repository.getAll(); } @@ -27,25 +27,25 @@ public class ItemService { .toList(); } - public ItemEntity get(Long id) { + public BookEntity get(Long id) { return Optional.ofNullable(repository.get(id)) .orElseThrow(() -> new NotFoundException(id)); } - public ItemEntity create(ItemEntity entity) { + public BookEntity create(BookEntity entity) { return repository.create(entity); } - public ItemEntity update(Long id, ItemEntity entity) { - final ItemEntity existsEntity = get(id); + public BookEntity update(Long id, BookEntity entity) { + final BookEntity existsEntity = get(id); existsEntity.setType(entity.getType()); existsEntity.setPrice(entity.getPrice()); existsEntity.setCount(entity.getCount()); return repository.update(existsEntity); } - public ItemEntity delete(Long id) { - final ItemEntity existsEntity = get(id); + public BookEntity delete(Long id) { + final BookEntity existsEntity = get(id); return repository.delete(existsEntity); } } diff --git a/SpringApp/library/src/main/java/com/ip/library/items/repository/ItemRepository.java b/SpringApp/library/src/main/java/com/ip/library/items/repository/ItemRepository.java deleted file mode 100644 index 80030f8..0000000 --- a/SpringApp/library/src/main/java/com/ip/library/items/repository/ItemRepository.java +++ /dev/null @@ -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 { -}