adding authors

This commit is contained in:
Zakharov_Rostislav 2024-03-06 20:35:00 +04:00
parent cb869e0dd3
commit afe847c255
11 changed files with 233 additions and 45 deletions

View File

@ -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));
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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> {
}

View File

@ -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);
}
}

View File

@ -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<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();
}
@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));
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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> {
}

View File

@ -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<ItemEntity> getAll(Long typeId) {
public List<BookEntity> 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);
}
}

View File

@ -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> {
}