This commit is contained in:
revengel66 2024-04-09 12:17:32 +04:00
parent e364145f5e
commit f087bc5e96
13 changed files with 229 additions and 81 deletions

View File

@ -8,8 +8,13 @@ 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.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.model.TypeEntity;
import com.example.demo.types.service.TypeService; 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 Logger log = LoggerFactory.getLogger(DemoApplication.class);
private final TypeService typeService; 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.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,6 +43,19 @@ 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("Создание пользователей");
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"); log.info("Create default types values");
final var type1 = typeService.create(new TypeEntity(null, "Ноутбук")); final var type1 = typeService.create(new TypeEntity(null, "Ноутбук"));
final var type2 = typeService.create(new TypeEntity(null, "Телефон")); final var type2 = typeService.create(new TypeEntity(null, "Телефон"));

View File

@ -1,32 +1,18 @@
package com.example.demo.itemCategories.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;
import com.example.demo.types.model.TypeEntity;
public class CategoriesEntity extends BaseEntity { public class CategoriesEntity extends BaseEntity {
private TypeEntity type;
// private Double price;
// private Integer count;
private String name; private String name;
public CategoriesEntity() { public CategoriesEntity() {
super(); super();
} }
public CategoriesEntity(Long id, TypeEntity type, String name) { public CategoriesEntity(Long id, String name) {
super(id); super(id);
this.type = type;
this.name = name; this.name = name;
} }
public TypeEntity getType() {
return type;
}
public void setType(TypeEntity type) {
this.type = type;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -37,7 +23,7 @@ public class CategoriesEntity extends BaseEntity {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, type, name); return Objects.hash(id, name);
} }
@Override @Override
@ -48,7 +34,6 @@ public class CategoriesEntity extends BaseEntity {
return false; return false;
final CategoriesEntity other = (CategoriesEntity) obj; final CategoriesEntity other = (CategoriesEntity) obj;
return Objects.equals(other.getId(), id) return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getName(), name); && 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,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);
}
}

View File

@ -2,11 +2,10 @@ package com.example.demo.itemOrders.model;
import java.sql.Date; import java.sql.Date;
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 OrdersEntity extends BaseEntity { public class OrdersEntity extends BaseEntity {
private TypeEntity type; private CategoriesEntity type;
private Integer categoriesId;
private Double price; private Double price;
private Integer count; private Integer count;
private Date date; private Date date;
@ -15,29 +14,21 @@ public class OrdersEntity extends BaseEntity {
super(); 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); super(id);
this.type = type; this.type = type;
this.categoriesId = categoriesId;
this.price = price; this.price = price;
this.count = count; this.count = count;
this.date = date; this.date = date;
} }
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 Integer getCategoriesId() {
return categoriesId;
}
public void setCategoriesId(Integer categoriesId) {
this.categoriesId = categoriesId;
}
public Double getPrice() { public Double getPrice() {
return price; return price;
} }
@ -62,7 +53,7 @@ public class OrdersEntity extends BaseEntity {
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, type, categoriesId, price, count); return Objects.hash(id, type, price, count);
} }
@Override @Override
@ -74,7 +65,6 @@ public class OrdersEntity extends BaseEntity {
final OrdersEntity other = (OrdersEntity) obj; final OrdersEntity other = (OrdersEntity) 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.getCategoriesId(), categoriesId)
&& Objects.equals(other.getPrice(), price) && Objects.equals(other.getPrice(), price)
&& Objects.equals(other.getCount(), count) && Objects.equals(other.getCount(), count)
&& Objects.equals(other.getDate(), date); && 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,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);
}
}

View File

@ -3,42 +3,33 @@ 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 Integer categoriesId;
private Double price; private Double price;
private Integer count; private Integer count;
private Double sum; private Double sum;
public ProductsEntity() { public ProductsEntity(Object object, String string) {
super(); 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); super(id);
this.type = type; this.type = type;
this.categoriesId = categoriesId;
this.price = price; this.price = price;
this.count = count; this.count = count;
this.sum = sum; this.sum = sum;
} }
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 Integer getCategoriesId() {
return categoriesId;
}
public void setCategoriesId(Integer categoriesId) {
this.categoriesId = categoriesId;
}
public Double getPrice() { public Double getPrice() {
return price; return price;
} }
@ -63,7 +54,7 @@ public class ProductsEntity extends BaseEntity{
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, type, categoriesId, price, count, sum); return Objects.hash(id, type, price, count, sum);
} }
@Override @Override
@ -75,7 +66,6 @@ public class ProductsEntity extends BaseEntity{
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.getCategoriesId(), categoriesId)
&& Objects.equals(other.getPrice(), price) && Objects.equals(other.getPrice(), price)
&& Objects.equals(other.getCount(), count) && Objects.equals(other.getCount(), count)
&& Objects.equals(other.getSum(), sum); && Objects.equals(other.getSum(), sum);

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

View File

@ -1,10 +1,8 @@
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 UsersEntity extends BaseEntity { public class UsersEntity extends BaseEntity {
private TypeEntity type;
private String login; private String login;
private String password; private String password;
@ -12,21 +10,12 @@ public class UsersEntity extends BaseEntity {
super(); super();
} }
public UsersEntity(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;
} }
@ -45,7 +34,7 @@ public class UsersEntity 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,7 +45,6 @@ public class UsersEntity extends BaseEntity {
return false; return false;
final UsersEntity other = (UsersEntity) 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 UsersRepository extends MapRepository<ProductsEntity> { public class UsersRepository extends MapRepository<UsersEntity> {
} }

View File

@ -11,10 +11,10 @@ import com.example.demo.itemUsers.model.UsersEntity;
import com.example.demo.itemUsers.repository.UsersRepository; import com.example.demo.itemUsers.repository.UsersRepository;
@Service @Service
public class UserService { public class UsersService {
private final UsersRepository repository; private final UsersRepository repository;
public UserService(UsersRepository repository) { public UsersService(UsersRepository repository) {
this.repository = repository; this.repository = repository;
} }
@ -22,14 +22,11 @@ public class UserService {
if (Objects.equals(typeId, 0L)) { if (Objects.equals(typeId, 0L)) {
return repository.getAll(); return repository.getAll();
} }
return repository.getAll().stream() return repository.getAll();
.filter(item -> item.getType().getId().equals(typeId))
.toList();
} }
public UsersEntity get(Long id) { public UsersEntity get(Long id) {
return Optional.ofNullable(repository.get(id)) return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
.orElseThrow(() -> new NotFoundException(id));
} }
public UsersEntity create(UsersEntity entity) { public UsersEntity create(UsersEntity entity) {
@ -38,9 +35,8 @@ public class UserService {
public UsersEntity update(Long id, UsersEntity entity) { public UsersEntity update(Long id, UsersEntity entity) {
final UsersEntity existsEntity = get(id); final UsersEntity existsEntity = get(id);
existsEntity.setType(entity.getType()); existsEntity.setLogin(entity.getLogin());
existsEntity.setPrice(entity.getPrice()); existsEntity.setPassword(entity.getPassword());
existsEntity.setCount(entity.getCount());
return repository.update(existsEntity); return repository.update(existsEntity);
} }