lab2 start
This commit is contained in:
parent
3c38ab7688
commit
cb869e0dd3
@ -18,6 +18,9 @@ repositories {
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
|
||||
implementation 'org.modelmapper:modelmapper:3.2.0'
|
||||
implementation 'org.springframework.boot:spring-boot-validation'
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
@ -1,53 +0,0 @@
|
||||
package com.ip.library;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/types")
|
||||
public class ApiController {
|
||||
private final Logger log = LoggerFactory.getLogger(ApiController.class);
|
||||
|
||||
private List<Type> types = new ArrayList<>();
|
||||
|
||||
@PostMapping
|
||||
public Type createType(@RequestBody Type type) {
|
||||
types.add(type);
|
||||
return type;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Type> getAllTypes() {
|
||||
return types;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Type getType(@PathVariable(name = "id") int id) {
|
||||
return types.get(id);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public Type updateType(@PathVariable(name = "id") int id, @RequestBody Type type) {
|
||||
types.remove(id);
|
||||
types.add(id, type);
|
||||
return type;
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Type postMethodName(@PathVariable(name = "id") int id) {
|
||||
Type type = types.get(id);
|
||||
types.remove(id);
|
||||
return type;
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.ip.library;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Type {
|
||||
private String name;
|
||||
|
||||
public Type() {
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public Type( @JsonProperty(value = "name") String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.ip.library.core.configuration;
|
||||
|
||||
public class Constants {
|
||||
public static final String API_URL = "/api/1.0";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.ip.library.core.configuration;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MapperConfiguration {
|
||||
@Bean
|
||||
ModelMapper modelMapper() {
|
||||
return new ModelMapper();
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.ip.library;
|
||||
package com.ip.library.core.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.lang.NonNull;
|
||||
@ -6,7 +6,7 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
public class WebConfiguration implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addCorsMappings(@NonNull CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
@ -0,0 +1,7 @@
|
||||
package com.ip.library.core.error;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
public NotFoundException(Long id) {
|
||||
super(String.format("Entity with id [%s] is not found or not exists", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.ip.library.core.model;
|
||||
|
||||
public abstract class BaseEntity {
|
||||
protected Long id;
|
||||
|
||||
protected BaseEntity() {
|
||||
}
|
||||
|
||||
protected BaseEntity(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.ip.library.core.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CommonRepository<E, T> {
|
||||
List<E> getAll();
|
||||
|
||||
E get(T id);
|
||||
|
||||
E create(E entity);
|
||||
|
||||
E update(E entity);
|
||||
|
||||
E delete(E entity);
|
||||
|
||||
void deleteAll();
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.ip.library.core.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
|
||||
public abstract class MapRepository<E extends BaseEntity> implements CommonRepository<E, Long> {
|
||||
private final Map<Long, E> entities = new TreeMap<>();
|
||||
private Long lastId = 0L;
|
||||
|
||||
protected MapRepository() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<E> getAll() {
|
||||
return entities.values().stream().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(Long id) {
|
||||
return entities.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E create(E entity) {
|
||||
lastId++;
|
||||
entity.setId(lastId);
|
||||
entities.put(lastId, entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E update(E entity) {
|
||||
if (get(entity.getId()) == null) {
|
||||
return null;
|
||||
}
|
||||
entities.put(entity.getId(), entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E delete(E entity) {
|
||||
if (get(entity.getId()) == null) {
|
||||
return null;
|
||||
}
|
||||
entities.remove(entity.getId());
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
lastId = 0L;
|
||||
entities.clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package com.ip.library.items.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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
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;
|
||||
private final TypeService typeService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public ItemController(ItemService 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 ItemEntity toEntity(ItemDto dto) {
|
||||
final ItemEntity entity = modelMapper.map(dto, ItemEntity.class);
|
||||
entity.setType(typeService.get(dto.getTypeId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<ItemDto> 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) {
|
||||
return toDto(itemService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ItemDto create(@RequestBody @Valid ItemDto dto) {
|
||||
return toDto(itemService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ItemDto update(@PathVariable(name = "id") Long id, @RequestBody ItemDto dto) {
|
||||
return toDto(itemService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ItemDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(itemService.delete(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.ip.library.items.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class ItemDto {
|
||||
private Long id;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long typeId;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Double price;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Integer count;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getTypeId() {
|
||||
return typeId;
|
||||
}
|
||||
|
||||
public void setTypeId(Long typeId) {
|
||||
this.typeId = typeId;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package com.ip.library.items.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
|
||||
public class ItemEntity extends BaseEntity {
|
||||
private TypeEntity type;
|
||||
private Double price;
|
||||
private Integer count;
|
||||
|
||||
public ItemEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ItemEntity(Long id, TypeEntity type, Double price, Integer count) {
|
||||
super(id);
|
||||
this.type = type;
|
||||
this.price = price;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public TypeEntity getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(TypeEntity type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, price, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final ItemEntity other = (ItemEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getPrice(), price)
|
||||
&& Objects.equals(other.getCount(), count);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
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> {
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.ip.library.items.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
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 ItemService(ItemRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<ItemEntity> getAll(Long typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
return repository.getAll().stream()
|
||||
.filter(item -> item.getType().getId().equals(typeId))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public ItemEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
|
||||
public ItemEntity create(ItemEntity entity) {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public ItemEntity update(Long id, ItemEntity entity) {
|
||||
final ItemEntity 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);
|
||||
return repository.delete(existsEntity);
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.ip.library.types.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.types.model.TypeEntity;
|
||||
import com.ip.library.types.service.TypeService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/type")
|
||||
public class TypeController {
|
||||
private final TypeService typeService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public TypeController(TypeService typeService, ModelMapper modelMapper) {
|
||||
this.typeService = typeService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private TypeDto toDto(TypeEntity entity) {
|
||||
return modelMapper.map(entity, TypeDto.class);
|
||||
}
|
||||
|
||||
private TypeEntity toEntity(TypeDto dto) {
|
||||
return modelMapper.map(dto, TypeEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<TypeDto> getAll() {
|
||||
return typeService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public TypeDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(typeService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public TypeDto create(@RequestBody @Valid TypeDto dto) {
|
||||
return toDto(typeService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody TypeDto dto) {
|
||||
return toDto(typeService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public TypeDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(typeService.delete(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.ip.library.types.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class TypeDto {
|
||||
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.types.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
|
||||
public class TypeEntity extends BaseEntity {
|
||||
private String name;
|
||||
|
||||
public TypeEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TypeEntity(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 TypeEntity other = (TypeEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.ip.library.types.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.ip.library.core.repository.MapRepository;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
|
||||
@Repository
|
||||
public class TypeRepository extends MapRepository<TypeEntity> {
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.ip.library.types.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.types.model.TypeEntity;
|
||||
import com.ip.library.types.repository.TypeRepository;
|
||||
|
||||
@Service
|
||||
public class TypeService {
|
||||
private final TypeRepository repository;
|
||||
|
||||
public TypeService(TypeRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<TypeEntity> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public TypeEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
|
||||
public TypeEntity create(TypeEntity entity) {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public TypeEntity update(Long id, TypeEntity entity) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
existsEntity.setName(entity.getName());
|
||||
return repository.update(existsEntity);
|
||||
}
|
||||
|
||||
public TypeEntity delete(Long id) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user