Compare commits

...

4 Commits

Author SHA1 Message Date
revengel66
431abe9c99 lab2 5 2024-04-09 13:24:58 +04:00
revengel66
f087bc5e96 lab2 4 2024-04-09 12:17:32 +04:00
revengel66
e364145f5e lab2 3 2024-04-09 11:29:30 +04:00
revengel66
e79da32126 lab2 2 2024-04-09 11:28:29 +04:00
27 changed files with 570 additions and 554 deletions

View File

@ -1,5 +1,6 @@
package com.example.demo; package com.example.demo;
import java.text.SimpleDateFormat;
import java.util.Objects; import java.util.Objects;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -8,21 +9,30 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.demo.itemUsers.model.ItemEntity; import com.example.demo.itemCategories.model.CategoriesEntity;
import com.example.demo.itemUsers.service.UserService; import com.example.demo.itemCategories.service.CategoriesService;
import com.example.demo.types.model.TypeEntity; import com.example.demo.itemOrders.model.OrdersEntity;
import com.example.demo.types.service.TypeService; import com.example.demo.itemOrders.service.OrdersService;
import com.example.demo.itemProducts.model.ProductsEntity;
import com.example.demo.itemProducts.service.ProductsService;
import com.example.demo.itemUsers.model.UsersEntity;
import com.example.demo.itemUsers.service.UsersService;
@SpringBootApplication @SpringBootApplication
public class DemoApplication implements CommandLineRunner { public class DemoApplication implements CommandLineRunner {
private final Logger log = LoggerFactory.getLogger(DemoApplication.class); private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
private final TypeService typeService; private final UsersService userService;
private final UserService itemService; private final CategoriesService categoriesService;
private final ProductsService productsService;
private final OrdersService ordersService;
public DemoApplication(TypeService typeService, UserService itemService) { public DemoApplication(UsersService userService, CategoriesService categoriesService, ProductsService productsService, OrdersService ordersService) {
this.typeService = typeService;
this.itemService = itemService; this.userService = userService;
this.categoriesService = categoriesService;
this.productsService = productsService;
this.ordersService = ordersService;
} }
public static void main(String[] args) { public static void main(String[] args) {
@ -32,19 +42,25 @@ public class DemoApplication implements CommandLineRunner {
@Override @Override
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
if (args.length > 0 && Objects.equals("--populate", args[0])) { if (args.length > 0 && Objects.equals("--populate", args[0])) {
log.info("Create default types values");
final var type1 = typeService.create(new TypeEntity(null, "Ноутбук"));
final var type2 = typeService.create(new TypeEntity(null, "Телефон"));
final var type3 = typeService.create(new TypeEntity(null, "Игровая приставка"));
log.info("Create default items values"); log.info("Создание пользователей");
itemService.create(new OrderEntity(null, type1, 49999.00, 20)); userService.create(new UsersEntity(null, "Natalia", "1234"));
itemService.create(new OrderEntity(null, type1, 129999.00, 3)); userService.create(new UsersEntity(null, "Revengel", "4567"));
itemService.create(new OrderEntity(null, type2, 15450.50, 30));
itemService.create(new OrderEntity(null, type2, 69900.50, 10)); log.info("Создание категорий");
itemService.create(new OrderEntity(null, type2, 150000.00, 6)); final var category1 = categoriesService.create(new CategoriesEntity(null, "Ноутбуки"));
itemService.create(new OrderEntity(null, type3, 75000.00, 6)); final var category2 = categoriesService.create(new CategoriesEntity(null, "Телефоны"));
itemService.create(new OrderEntity(null, type3, 67800.00, 3));
log.info("Создание продуктов");
final var product1 = productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00));
final var product2 = productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00));
final var product3 = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00));
log.info("Создание заказов");
ordersService.create(new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
ordersService.create(new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
ordersService.create(new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
ordersService.create(new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
} }
} }
} }

View File

@ -0,0 +1,64 @@
package com.example.demo.itemCategories.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.example.demo.core.configuration.Constants;
import com.example.demo.itemCategories.model.CategoriesEntity;
import com.example.demo.itemCategories.service.CategoriesService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/Categories")
public class CategoriesController {
private final CategoriesService CategoriesService;
private final ModelMapper modelMapper;
public CategoriesController(CategoriesService CategoriesService, ModelMapper modelMapper) {
this.CategoriesService = CategoriesService;
this.modelMapper = modelMapper;
}
private CategoriesDto toDto(CategoriesEntity entity) {
return modelMapper.map(entity, CategoriesDto.class);
}
private CategoriesEntity toEntity(CategoriesDto dto) {
return modelMapper.map(dto, CategoriesEntity.class);
}
@GetMapping
public List<CategoriesDto> getAll() {
return CategoriesService.getAll().stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public CategoriesDto get(@PathVariable(name = "id") Long id) {
return toDto(CategoriesService.get(id));
}
@PostMapping
public CategoriesDto create(@RequestBody @Valid CategoriesDto dto) {
return toDto(CategoriesService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public CategoriesDto update(@PathVariable(name = "id") Long id, @RequestBody CategoriesDto dto) {
return toDto(CategoriesService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public CategoriesDto delete(@PathVariable(name = "id") Long id) {
return toDto(CategoriesService.delete(id));
}
}

View File

@ -1,10 +1,10 @@
package com.example.demo.types.api; package com.example.demo.itemCategories.api;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
public class TypeDto { public class CategoriesDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY) @JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id; private Long id;
@NotBlank @NotBlank

View File

@ -1,21 +1,18 @@
package com.example.demo.types.model; package com.example.demo.itemCategories.model;
import java.util.Objects; import java.util.Objects;
import com.example.demo.core.model.BaseEntity; import com.example.demo.core.model.BaseEntity;
public class TypeEntity extends BaseEntity { public class CategoriesEntity extends BaseEntity {
private String name; private String name;
public TypeEntity() { public CategoriesEntity() {
super(); super();
} }
public TypeEntity(Long id, String name) { public CategoriesEntity(Long id, String name) {
super(id); super(id);
this.name = name; this.name = name;
} }
public String getName() { public String getName() {
return name; return name;
} }
@ -35,9 +32,8 @@ public class TypeEntity extends BaseEntity {
return true; return true;
if (obj == null || getClass() != obj.getClass()) if (obj == null || getClass() != obj.getClass())
return false; return false;
final TypeEntity other = (TypeEntity) obj; final CategoriesEntity other = (CategoriesEntity) obj;
return Objects.equals(other.getId(), id) return Objects.equals(other.getId(), id)
&& Objects.equals(other.getName(), name); && Objects.equals(other.getName(), name);
} }
} }

View File

@ -1,56 +0,0 @@
package com.example.demo.itemCategories.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.types.model.TypeEntity;
public class ItemEntity extends BaseEntity {
private TypeEntity type;
// private Double price;
// private Integer count;
private String name;
public ItemEntity() {
super();
}
public ItemEntity(Long id, TypeEntity type, String name) {
super(id);
this.type = type;
this.name = name;
}
public TypeEntity getType() {
return type;
}
public void setType(TypeEntity type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
return Objects.hash(id, type, name);
}
@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.getName(), name);
}
}

View File

@ -0,0 +1,10 @@
package com.example.demo.itemCategories.repository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.itemCategories.model.CategoriesEntity;
@Repository
public class CategoriesRepository extends MapRepository<CategoriesEntity> {
}

View File

@ -0,0 +1,42 @@
package com.example.demo.itemCategories.service;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.itemCategories.model.CategoriesEntity;
import com.example.demo.itemCategories.repository.CategoriesRepository;
@Service
public class CategoriesService {
private final CategoriesRepository repository;
public CategoriesService(CategoriesRepository repository) {
this.repository = repository;
}
public List<CategoriesEntity> getAll() {
return repository.getAll();
}
public CategoriesEntity get(Long id) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public CategoriesEntity create(CategoriesEntity entity) {
return repository.create(entity);
}
public CategoriesEntity update(Long id, CategoriesEntity entity) {
final CategoriesEntity existsEntity = get(id);
existsEntity.setName(entity.getName());
return repository.update(existsEntity);
}
public CategoriesEntity delete(Long id) {
final CategoriesEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -1,66 +0,0 @@
package com.example.demo.itemOrders.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.itemProducts.model.ProductsEntity;
import com.example.demo.itemUsers.model.UserEntity;
import com.example.demo.types.model.TypeEntity;
public class OrderEntity extends BaseEntity {
private TypeEntity type;
private UserEntity user;
private List<ProductsEntity> products = new ArrayList<>();
public OrderEntity() {
super();
}
public OrderEntity(Long id, TypeEntity type, UserEntity user, List<ProductsEntity> products) {
super(id);
this.type = type;
this.user = user;
this.products = products;
}
public TypeEntity getType() {
return type;
}
public void setType(TypeEntity type) {
this.type = type;
}
public UserEntity getUser() {
return user;
}
public void setUser(UserEntity user) {
this.user = user;
}
// добавить методы добавить и удалить продукт, и получить продукт
public List<ProductsEntity> getProducts() {
return products;
}
@Override
public int hashCode() {
return Objects.hash(id, type, login, password);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final OrderEntity other = (OrderEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getLogin(), login)
&& Objects.equals(other.getPassword(), password);
}
}

View File

@ -0,0 +1,61 @@
package com.example.demo.itemOrders.model;
import java.text.SimpleDateFormat;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.itemProducts.model.ProductsEntity;
public class OrdersEntity extends BaseEntity {
private ProductsEntity product;
private Integer count;
private SimpleDateFormat date;
private Double sum;
public OrdersEntity() {
super();
}
public OrdersEntity(Long id, ProductsEntity product, Integer count, SimpleDateFormat date) {
super(id);
this.product = product;
this.count = count;
this.date = date;
}
public ProductsEntity getProduct() {
return product;
}
public void setProduct(ProductsEntity product) {
this.product = product;
}
public Double getSum() {
sum = count * product.getPrice();
return sum;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public SimpleDateFormat getDate() {
return date;
}
public void setDate(SimpleDateFormat date) {
this.date = date;
}
@Override
public int hashCode() {
return Objects.hash(id, product, count, date);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final OrdersEntity other = (OrdersEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getProduct(), product)
&& Objects.equals(other.getCount(), count)
&& Objects.equals(other.getDate(), date);
}
}

View File

@ -0,0 +1,10 @@
package com.example.demo.itemOrders.repository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.itemOrders.model.OrdersEntity;
@Repository
public class OrdersRepository extends MapRepository<OrdersEntity> {
}

View File

@ -0,0 +1,47 @@
package com.example.demo.itemOrders.service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.itemOrders.model.OrdersEntity;
import com.example.demo.itemOrders.repository.OrdersRepository;
@Service
public class OrdersService {
private final OrdersRepository repository;
public OrdersService(OrdersRepository repository) {
this.repository = repository;
}
public List<OrdersEntity> getAll(Long productId) {
if (Objects.equals(productId, 0L)) {
return repository.getAll();
}
return repository.getAll().stream().filter(item -> item.getProduct().getId().equals(productId)).toList();
}
public OrdersEntity get(Long id) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public OrdersEntity create(OrdersEntity entity) {
return repository.create(entity);
}
public OrdersEntity update(Long id, OrdersEntity entity) {
final OrdersEntity existsEntity = get(id);
existsEntity.setProduct(entity.getProduct());
existsEntity.setCount(entity.getCount());
existsEntity.setDate(entity.getDate());
return repository.update(existsEntity);
}
public OrdersEntity delete(Long id) {
final OrdersEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -3,51 +3,48 @@ package com.example.demo.itemProducts.model;
import java.util.Objects; import java.util.Objects;
import com.example.demo.core.model.BaseEntity; import com.example.demo.core.model.BaseEntity;
import com.example.demo.types.model.TypeEntity; import com.example.demo.itemCategories.model.CategoriesEntity;
public class ProductsEntity extends BaseEntity { public class ProductsEntity extends BaseEntity{
private TypeEntity type; private CategoriesEntity type;
private String name;
private Double price; private Double price;
private Integer count;
public ProductsEntity() { public ProductsEntity(Object object, String string) {
super(); super();
} }
public ProductsEntity(Long id, TypeEntity type, Double price, Integer count) { public ProductsEntity(Long id, CategoriesEntity type, String name, Double price) {
super(id); super(id);
this.type = type; this.type = type;
this.name = name;
this.price = price; this.price = price;
this.count = count;
} }
public TypeEntity getType() { public CategoriesEntity getType() {
return type; return type;
} }
public void setType(TypeEntity type) { public void setType(CategoriesEntity type) {
this.type = type; this.type = type;
} }
public Double getPrice() { public Double getPrice() {
return price; return price;
} }
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPrice(Double price) { public void setPrice(Double price) {
this.price = price; this.price = price;
} }
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, type, price, count); return Objects.hash(id, type, name, price);
} }
@Override @Override
@ -58,8 +55,8 @@ public class ProductsEntity extends BaseEntity {
return false; return false;
final ProductsEntity other = (ProductsEntity) obj; final ProductsEntity other = (ProductsEntity) 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.getName(), name)
&& Objects.equals(other.getCount(), count); && Objects.equals(other.getPrice(), price);
} }
} }

View File

@ -0,0 +1,11 @@
package com.example.demo.itemProducts.repository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.itemProducts.model.ProductsEntity;
@Repository
public class ProductsRepository extends MapRepository<ProductsEntity>{
}

View File

@ -0,0 +1,49 @@
package com.example.demo.itemProducts.service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.itemProducts.model.ProductsEntity;
import com.example.demo.itemProducts.repository.ProductsRepository;
@Service
public class ProductsService {
private final ProductsRepository repository;
public ProductsService(ProductsRepository repository) {
this.repository = repository;
}
public List<ProductsEntity> getAll(Long typeId) {
if (Objects.equals(typeId, 0L)) {
return repository.getAll();
}
return repository.getAll().stream().filter(item -> item.getType().getId().equals(typeId)).toList();
}
public ProductsEntity get(Long id) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public ProductsEntity create(ProductsEntity entity) {
return repository.create(entity);
}
public ProductsEntity update(Long id, ProductsEntity entity) {
final ProductsEntity existsEntity = get(id);
existsEntity.setType(entity.getType());
existsEntity.setName(entity.getName());
existsEntity.setPrice(entity.getPrice());
return repository.update(existsEntity);
}
public ProductsEntity delete(Long id) {
final ProductsEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -1,70 +0,0 @@
package com.example.demo.itemUsers.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.example.demo.core.configuration.Constants;
import com.example.demo.itemUsers.model.UserEntity;
import com.example.demo.itemUsers.service.UserService;
import com.example.demo.types.service.TypeService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/item")
public class UserController {
private final UserService itemService;
private final TypeService typeService;
private final ModelMapper modelMapper;
public UserController(UserService itemService, TypeService typeService, ModelMapper modelMapper) {
this.itemService = itemService;
this.typeService = typeService;
this.modelMapper = modelMapper;
}
private UserDto toDto(OrderEntity entity) {
return modelMapper.map(entity, UserDto.class);
}
private OrderEntity toEntity(UserDto dto) {
final OrderEntity entity = modelMapper.map(dto, OrderEntity.class);
entity.setType(typeService.get(dto.getTypeId()));
return entity;
}
@GetMapping
public List<UserDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) {
return itemService.getAll(typeId).stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public UserDto get(@PathVariable(name = "id") Long id) {
return toDto(itemService.get(id));
}
@PostMapping
public UserDto create(@RequestBody @Valid UserDto dto) {
return toDto(itemService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto) {
return toDto(itemService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public UserDto delete(@PathVariable(name = "id") Long id) {
return toDto(itemService.delete(id));
}
}

View File

@ -1,57 +0,0 @@
package com.example.demo.itemUsers.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class UserDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@NotNull
@Min(1)
private Long typeId;
@NotNull
@Min(1)
private Double price;
@NotNull
@Min(1)
private Integer count;
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;
}
}

View File

@ -0,0 +1,64 @@
package com.example.demo.itemUsers.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.example.demo.core.configuration.Constants;
import com.example.demo.itemUsers.model.UsersEntity;
import com.example.demo.itemUsers.service.UsersService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/Users")
public class UsersController {
private final UsersService UsersService;
private final ModelMapper modelMapper;
public UsersController(UsersService UsersService, ModelMapper modelMapper) {
this.UsersService = UsersService;
this.modelMapper = modelMapper;
}
private UsersDto toDto(UsersEntity entity) {
return modelMapper.map(entity, UsersDto.class);
}
private UsersEntity toEntity(UsersDto dto) {
return modelMapper.map(dto, UsersEntity.class);
}
@GetMapping
public List<UsersDto> getAll() {
return UsersService.getAll().stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public UsersDto get(@PathVariable(name = "id") Long id) {
return toDto(UsersService.get(id));
}
@PostMapping
public UsersDto create(@RequestBody @Valid UsersDto dto) {
return toDto(UsersService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public UsersDto update(@PathVariable(name = "id") Long id, @RequestBody UsersDto dto) {
return toDto(UsersService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public UsersDto delete(@PathVariable(name = "id") Long id) {
return toDto(UsersService.delete(id));
}
}

View File

@ -0,0 +1,37 @@
package com.example.demo.itemUsers.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
public class UsersDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@NotBlank
private String login;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@ -1,34 +1,21 @@
package com.example.demo.itemUsers.model; package com.example.demo.itemUsers.model;
import java.util.Objects; import java.util.Objects;
import com.example.demo.core.model.BaseEntity; import com.example.demo.core.model.BaseEntity;
import com.example.demo.types.model.TypeEntity;
public class UserEntity extends BaseEntity { public class UsersEntity extends BaseEntity {
private TypeEntity type;
private String login; private String login;
private String password; private String password;
public UserEntity() { public UsersEntity() {
super(); super();
} }
public UserEntity(Long id, TypeEntity type, String login, String password) { public UsersEntity(Long id, String login, String password) {
super(id); super(id);
this.type = type;
this.login = login; this.login = login;
this.password = password; this.password = password;
} }
public TypeEntity getType() {
return type;
}
public void setType(TypeEntity type) {
this.type = type;
}
public String getLogin() { public String getLogin() {
return login; return login;
} }
@ -47,7 +34,7 @@ public class UserEntity extends BaseEntity {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, type, login, password); return Objects.hash(id, login, password);
} }
@Override @Override
@ -56,9 +43,8 @@ public class UserEntity extends BaseEntity {
return true; return true;
if (obj == null || getClass() != obj.getClass()) if (obj == null || getClass() != obj.getClass())
return false; return false;
final UserEntity other = (UserEntity) obj; final UsersEntity other = (UsersEntity) obj;
return Objects.equals(other.getId(), id) return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getLogin(), login) && Objects.equals(other.getLogin(), login)
&& Objects.equals(other.getPassword(), password); && Objects.equals(other.getPassword(), password);
} }

View File

@ -3,8 +3,8 @@ package com.example.demo.itemUsers.repository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository; import com.example.demo.core.repository.MapRepository;
import com.example.demo.itemUsers.model.ItemEntity; import com.example.demo.itemUsers.model.UsersEntity;
@Repository @Repository
public class UserRepository extends MapRepository<OrderEntity> { public class UsersRepository extends MapRepository<UsersEntity> {
} }

View File

@ -1,51 +0,0 @@
package com.example.demo.itemUsers.service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.itemUsers.model.UserEntity;
import com.example.demo.itemUsers.repository.UserRepository;
@Service
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
public List<UserEntity> getAll(Long typeId) {
if (Objects.equals(typeId, 0L)) {
return repository.getAll();
}
return repository.getAll().stream()
.filter(item -> item.getType().getId().equals(typeId))
.toList();
}
public UserEntity get(Long id) {
return Optional.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
}
public UserEntity create(UserEntity entity) {
return repository.create(entity);
}
public UserEntity update(Long id, UserEntity entity) {
final UserEntity existsEntity = get(id);
existsEntity.setType(entity.getType());
existsEntity.setPrice(entity.getPrice());
existsEntity.setCount(entity.getCount());
return repository.update(existsEntity);
}
public UserEntity delete(Long id) {
final UserEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -0,0 +1,44 @@
package com.example.demo.itemUsers.service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.itemUsers.model.UsersEntity;
import com.example.demo.itemUsers.repository.UsersRepository;
@Service
public class UsersService {
private final UsersRepository repository;
public UsersService(UsersRepository repository) {
this.repository = repository;
}
public List<UsersEntity> getAll() {
return repository.getAll();
}
public UsersEntity get(Long id) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public UsersEntity create(UsersEntity entity) {
return repository.create(entity);
}
public UsersEntity update(Long id, UsersEntity entity) {
final UsersEntity existsEntity = get(id);
existsEntity.setLogin(entity.getLogin());
existsEntity.setPassword(entity.getPassword());
return repository.update(existsEntity);
}
public UsersEntity delete(Long id) {
final UsersEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -1,64 +0,0 @@
package com.example.demo.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.example.demo.core.configuration.Constants;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.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));
}
}

View File

@ -1,10 +0,0 @@
package com.example.demo.types.repository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.types.model.TypeEntity;
@Repository
public class TypeRepository extends MapRepository<TypeEntity> {
}

View File

@ -1,43 +0,0 @@
package com.example.demo.types.service;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.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);
}
}

View File

@ -0,0 +1,60 @@
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.itemCategories.service.CategoriesService;
import com.example.demo.itemCategories.model.CategoriesEntity;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class CategoriesServiceTests {
@Autowired
private CategoriesService CategoriesService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> CategoriesService.get(0L));
}
@Test
@Order(1)
void createTest() {
CategoriesService.create(new CategoriesEntity(null, "Ноутбуки"));
final CategoriesEntity last = CategoriesService.create(new CategoriesEntity(null, "Телефоны"));
Assertions.assertEquals(3, CategoriesService.getAll().size());
Assertions.assertEquals(last, CategoriesService.get(3L));
}
@Test
@Order(2)
void updateTest() {
final String test = "TEST";
final CategoriesEntity entity = CategoriesService.get(3L);
final String oldName = entity.getName();
final CategoriesEntity newEntity = CategoriesService.update(3L, new CategoriesEntity(1L, test));
Assertions.assertEquals(3, CategoriesService.getAll().size());
Assertions.assertEquals(newEntity, CategoriesService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
}
@Test
@Order(3)
void deleteTest() {
CategoriesService.delete(3L);
Assertions.assertEquals(2, CategoriesService.getAll().size());
final CategoriesEntity last = CategoriesService.get(2L);
Assertions.assertEquals(2L, last.getId());
final CategoriesEntity newEntity = CategoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
Assertions.assertEquals(3, CategoriesService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
}
}

View File

@ -1,61 +0,0 @@
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class TypeServiceTests {
@Autowired
private TypeService typeService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
}
@Test
@Order(1)
void createTest() {
typeService.create(new TypeEntity(null, "Ноутбук"));
typeService.create(new TypeEntity(null, "Телефон"));
final TypeEntity last = typeService.create(new TypeEntity(null, "Игровая приставка"));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(last, typeService.get(3L));
}
@Test
@Order(2)
void updateTest() {
final String test = "TEST";
final TypeEntity entity = typeService.get(3L);
final String oldName = entity.getName();
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(newEntity, typeService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
}
@Test
@Order(3)
void deleteTest() {
typeService.delete(3L);
Assertions.assertEquals(2, typeService.getAll().size());
final TypeEntity last = typeService.get(2L);
Assertions.assertEquals(2L, last.getId());
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игровая приставка"));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
}
}