lab2 4
This commit is contained in:
parent
e364145f5e
commit
f087bc5e96
@ -8,8 +8,13 @@ import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import com.example.demo.itemUsers.model.ItemEntity;
|
||||
import com.example.demo.itemUsers.service.UserService;
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
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;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
import com.example.demo.types.service.TypeService;
|
||||
|
||||
@ -18,11 +23,17 @@ public class DemoApplication implements CommandLineRunner {
|
||||
private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
|
||||
|
||||
private final TypeService typeService;
|
||||
private final UserService itemService;
|
||||
private final UsersService userService;
|
||||
private final CategoriesService categoriesService;
|
||||
private final ProductsService productsService;
|
||||
private final OrdersService ordersService;
|
||||
|
||||
public DemoApplication(TypeService typeService, UserService itemService) {
|
||||
public DemoApplication(TypeService typeService, 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) {
|
||||
@ -32,6 +43,19 @@ public class DemoApplication implements CommandLineRunner {
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
if (args.length > 0 && Objects.equals("--populate", args[0])) {
|
||||
|
||||
log.info("Создание пользователей");
|
||||
final var user1 = userService.create(new UsersEntity(null, "Natalia", "1234"));
|
||||
final var user2 = userService.create(new UsersEntity(null, "Revengel", "4567"));
|
||||
|
||||
log.info("Создание категорий");
|
||||
final var category1 = categoriesService.create(new CategoriesEntity(null, "Ноутбуки"));
|
||||
final var category2 = categoriesService.create(new CategoriesEntity(null, "Телефоны"));
|
||||
|
||||
log.info("Создание продуктов");
|
||||
final var products1 = productsService.create(new ProductsEntity(null, "Ноутбуки"));
|
||||
final var products2 = productsService.create(new ProductsEntity(null, "Телефоны"));
|
||||
|
||||
log.info("Create default types values");
|
||||
final var type1 = typeService.create(new TypeEntity(null, "Ноутбук"));
|
||||
final var type2 = typeService.create(new TypeEntity(null, "Телефон"));
|
||||
|
@ -1,32 +1,18 @@
|
||||
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 CategoriesEntity extends BaseEntity {
|
||||
private TypeEntity type;
|
||||
// private Double price;
|
||||
// private Integer count;
|
||||
private String name;
|
||||
|
||||
public CategoriesEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CategoriesEntity(Long id, TypeEntity type, String name) {
|
||||
public CategoriesEntity(Long id, 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;
|
||||
}
|
||||
@ -37,7 +23,7 @@ public class CategoriesEntity extends BaseEntity {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, name);
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -48,7 +34,6 @@ public class CategoriesEntity extends BaseEntity {
|
||||
return false;
|
||||
final CategoriesEntity other = (CategoriesEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getName(), name);
|
||||
}
|
||||
}
|
||||
|
@ -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> {
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.example.demo.itemCategories.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.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(Long typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.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);
|
||||
}
|
||||
}
|
@ -2,11 +2,10 @@ package com.example.demo.itemOrders.model;
|
||||
import java.sql.Date;
|
||||
import java.util.Objects;
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
|
||||
public class OrdersEntity extends BaseEntity {
|
||||
private TypeEntity type;
|
||||
private Integer categoriesId;
|
||||
private CategoriesEntity type;
|
||||
private Double price;
|
||||
private Integer count;
|
||||
private Date date;
|
||||
@ -15,29 +14,21 @@ public class OrdersEntity extends BaseEntity {
|
||||
super();
|
||||
}
|
||||
|
||||
public OrdersEntity(Long id, TypeEntity type, Integer categoriesId, Double price, Integer count, Date date) {
|
||||
public OrdersEntity(Long id, CategoriesEntity type, Double price, Integer count, Date date) {
|
||||
super(id);
|
||||
this.type = type;
|
||||
this.categoriesId = categoriesId;
|
||||
this.price = price;
|
||||
this.count = count;
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public TypeEntity getType() {
|
||||
public CategoriesEntity getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(TypeEntity type) {
|
||||
public void setType(CategoriesEntity type) {
|
||||
this.type = type;
|
||||
}
|
||||
public Integer getCategoriesId() {
|
||||
return categoriesId;
|
||||
}
|
||||
|
||||
public void setCategoriesId(Integer categoriesId) {
|
||||
this.categoriesId = categoriesId;
|
||||
}
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
@ -62,7 +53,7 @@ public class OrdersEntity extends BaseEntity {
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, categoriesId, price, count);
|
||||
return Objects.hash(id, type, price, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -74,7 +65,6 @@ public class OrdersEntity extends BaseEntity {
|
||||
final OrdersEntity other = (OrdersEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getCategoriesId(), categoriesId)
|
||||
&& Objects.equals(other.getPrice(), price)
|
||||
&& Objects.equals(other.getCount(), count)
|
||||
&& Objects.equals(other.getDate(), date);
|
||||
|
@ -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> {
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
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 typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
return repository.getAll().stream().filter(item -> item.getType().getId().equals(typeId)).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.setType(entity.getType());
|
||||
existsEntity.setPrice(entity.getPrice());
|
||||
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);
|
||||
}
|
||||
}
|
@ -3,42 +3,33 @@ package com.example.demo.itemProducts.model;
|
||||
import java.util.Objects;
|
||||
|
||||
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{
|
||||
private TypeEntity type;
|
||||
private Integer categoriesId;
|
||||
private CategoriesEntity type;
|
||||
private Double price;
|
||||
private Integer count;
|
||||
private Double sum;
|
||||
|
||||
public ProductsEntity() {
|
||||
public ProductsEntity(Object object, String string) {
|
||||
super();
|
||||
}
|
||||
|
||||
public ProductsEntity(Long id, TypeEntity type, Integer categoriesId, Double price, Integer count, Double sum) {
|
||||
public ProductsEntity(Long id, CategoriesEntity type, Double price, Integer count, Double sum) {
|
||||
super(id);
|
||||
this.type = type;
|
||||
this.categoriesId = categoriesId;
|
||||
this.price = price;
|
||||
this.count = count;
|
||||
this.sum = sum;
|
||||
}
|
||||
|
||||
public TypeEntity getType() {
|
||||
public CategoriesEntity getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(TypeEntity type) {
|
||||
public void setType(CategoriesEntity type) {
|
||||
this.type = type;
|
||||
}
|
||||
public Integer getCategoriesId() {
|
||||
return categoriesId;
|
||||
}
|
||||
|
||||
public void setCategoriesId(Integer categoriesId) {
|
||||
this.categoriesId = categoriesId;
|
||||
}
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
@ -63,7 +54,7 @@ public class ProductsEntity extends BaseEntity{
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, categoriesId, price, count, sum);
|
||||
return Objects.hash(id, type, price, count, sum);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -75,7 +66,6 @@ public class ProductsEntity extends BaseEntity{
|
||||
final ProductsEntity other = (ProductsEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getCategoriesId(), categoriesId)
|
||||
&& Objects.equals(other.getPrice(), price)
|
||||
&& Objects.equals(other.getCount(), count)
|
||||
&& Objects.equals(other.getSum(), sum);
|
||||
|
@ -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>{
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
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.setPrice(entity.getPrice());
|
||||
existsEntity.setCount(entity.getCount());
|
||||
existsEntity.setSum(entity.getSum());
|
||||
return repository.update(existsEntity);
|
||||
}
|
||||
|
||||
|
||||
public ProductsEntity delete(Long id) {
|
||||
final ProductsEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
}
|
||||
}
|
@ -1,10 +1,8 @@
|
||||
package com.example.demo.itemUsers.model;
|
||||
import java.util.Objects;
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
|
||||
public class UsersEntity extends BaseEntity {
|
||||
private TypeEntity type;
|
||||
private String login;
|
||||
private String password;
|
||||
|
||||
@ -12,21 +10,12 @@ public class UsersEntity extends BaseEntity {
|
||||
super();
|
||||
}
|
||||
|
||||
public UsersEntity(Long id, TypeEntity type, String login, String password) {
|
||||
public UsersEntity(Long id, String login, String password) {
|
||||
super(id);
|
||||
this.type = type;
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public TypeEntity getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(TypeEntity type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
@ -45,7 +34,7 @@ public class UsersEntity extends BaseEntity {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, login, password);
|
||||
return Objects.hash(id, login, password);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -56,7 +45,6 @@ public class UsersEntity extends BaseEntity {
|
||||
return false;
|
||||
final UsersEntity other = (UsersEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getLogin(), login)
|
||||
&& Objects.equals(other.getPassword(), password);
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ package com.example.demo.itemUsers.repository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.itemUsers.model.ItemEntity;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
|
||||
@Repository
|
||||
public class UsersRepository extends MapRepository<ProductsEntity> {
|
||||
public class UsersRepository extends MapRepository<UsersEntity> {
|
||||
}
|
||||
|
@ -11,10 +11,10 @@ import com.example.demo.itemUsers.model.UsersEntity;
|
||||
import com.example.demo.itemUsers.repository.UsersRepository;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
public class UsersService {
|
||||
private final UsersRepository repository;
|
||||
|
||||
public UserService(UsersRepository repository) {
|
||||
public UsersService(UsersRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@ -22,14 +22,11 @@ public class UserService {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
return repository.getAll().stream()
|
||||
.filter(item -> item.getType().getId().equals(typeId))
|
||||
.toList();
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public UsersEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
|
||||
public UsersEntity create(UsersEntity entity) {
|
||||
@ -38,9 +35,8 @@ public class UserService {
|
||||
|
||||
public UsersEntity update(Long id, UsersEntity entity) {
|
||||
final UsersEntity existsEntity = get(id);
|
||||
existsEntity.setType(entity.getType());
|
||||
existsEntity.setPrice(entity.getPrice());
|
||||
existsEntity.setCount(entity.getCount());
|
||||
existsEntity.setLogin(entity.getLogin());
|
||||
existsEntity.setPassword(entity.getPassword());
|
||||
return repository.update(existsEntity);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user