commit
This commit is contained in:
parent
79bbcca648
commit
2313a080a7
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
1224
data.trace.db
1224
data.trace.db
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,6 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -9,14 +10,14 @@ import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
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.categories.model.CategoriesEntity;
|
||||
import com.example.demo.categories.service.CategoriesService;
|
||||
import com.example.demo.orders.model.OrdersEntity;
|
||||
import com.example.demo.orders.service.OrdersService;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
import com.example.demo.products.service.ProductsService;
|
||||
import com.example.demo.users.model.UsersEntity;
|
||||
import com.example.demo.users.service.UsersService;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication implements CommandLineRunner {
|
||||
@ -27,7 +28,8 @@ public class DemoApplication implements CommandLineRunner {
|
||||
private final ProductsService productsService;
|
||||
private final OrdersService ordersService;
|
||||
|
||||
public DemoApplication(CategoriesService categoriesService, ProductsService productsService, UsersService userService, OrdersService ordersService) {
|
||||
public DemoApplication(CategoriesService categoriesService, ProductsService productsService,
|
||||
UsersService userService, OrdersService ordersService) {
|
||||
|
||||
this.userService = userService;
|
||||
this.categoriesService = categoriesService;
|
||||
@ -57,14 +59,14 @@ public class DemoApplication implements CommandLineRunner {
|
||||
final var product3 = productsService.create(new ProductsEntity(category2, "Iphone 13", 150000.00));
|
||||
|
||||
log.info("Создание заказов");
|
||||
|
||||
|
||||
final var orders = List.of(
|
||||
new OrdersEntity(product1, 3),
|
||||
new OrdersEntity(product2, 2),
|
||||
new OrdersEntity(product3, 1),
|
||||
new OrdersEntity(product1, 4));
|
||||
orders.forEach(order -> ordersService.create(user1.getId(), order));
|
||||
|
||||
new OrdersEntity(product1, 3, Calendar.getInstance().getTime()),
|
||||
new OrdersEntity(product2, 2, Calendar.getInstance().getTime()),
|
||||
new OrdersEntity(product3, 1, Calendar.getInstance().getTime()),
|
||||
new OrdersEntity(product1, 4, Calendar.getInstance().getTime()));
|
||||
orders.forEach(order -> ordersService.create(user1.getId(), order));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.itemCategories.api;
|
||||
package com.example.demo.categories.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -13,8 +13,8 @@ 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 com.example.demo.categories.model.CategoriesEntity;
|
||||
import com.example.demo.categories.service.CategoriesService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.itemCategories.api;
|
||||
package com.example.demo.categories.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
@ -1,4 +1,5 @@
|
||||
package com.example.demo.itemCategories.model;
|
||||
package com.example.demo.categories.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
@ -6,7 +7,6 @@ import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
|
||||
@Entity
|
||||
@Table(name = "categories")
|
||||
public class CategoriesEntity extends BaseEntity {
|
||||
@ -20,6 +20,7 @@ public class CategoriesEntity extends BaseEntity {
|
||||
public CategoriesEntity(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package com.example.demo.itemCategories.repository;
|
||||
package com.example.demo.categories.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.categories.model.CategoriesEntity;
|
||||
|
||||
public interface CategoriesRepository extends CrudRepository<CategoriesEntity, Long> {
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.itemCategories.service;
|
||||
package com.example.demo.categories.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.StreamSupport;
|
||||
@ -7,9 +7,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.repository.CategoriesRepository;
|
||||
|
||||
import com.example.demo.categories.model.CategoriesEntity;
|
||||
import com.example.demo.categories.repository.CategoriesRepository;
|
||||
|
||||
@Service
|
||||
public class CategoriesService {
|
||||
@ -18,14 +17,17 @@ public class CategoriesService {
|
||||
public CategoriesService(CategoriesRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<CategoriesEntity> getAll() {
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public CategoriesEntity get(Long id) {
|
||||
return repository.findById(id).orElseThrow(() -> new NotFoundException(CategoriesEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CategoriesEntity create(CategoriesEntity entity) {
|
||||
if (entity == null) {
|
||||
@ -33,12 +35,14 @@ public class CategoriesService {
|
||||
}
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CategoriesEntity update(Long id, CategoriesEntity entity) {
|
||||
final CategoriesEntity existsEntity = get(id);
|
||||
existsEntity.setName(entity.getName());
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CategoriesEntity delete(Long id) {
|
||||
final CategoriesEntity existsEntity = get(id);
|
97
src/main/java/com/example/demo/core/api/PageDto.java
Normal file
97
src/main/java/com/example/demo/core/api/PageDto.java
Normal file
@ -0,0 +1,97 @@
|
||||
package com.example.demo.core.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PageDto<D> {
|
||||
private List<D> items = new ArrayList<>();
|
||||
private int itemsCount;
|
||||
private int currentPage;
|
||||
private int currentSize;
|
||||
private int totalPages;
|
||||
private long totalItems;
|
||||
private boolean isFirst;
|
||||
private boolean isLast;
|
||||
private boolean hasNext;
|
||||
private boolean hasPrevious;
|
||||
|
||||
public List<D> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<D> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public int getItemsCount() {
|
||||
return itemsCount;
|
||||
}
|
||||
|
||||
public void setItemsCount(int itemsCount) {
|
||||
this.itemsCount = itemsCount;
|
||||
}
|
||||
|
||||
public int getCurrentPage() {
|
||||
return currentPage;
|
||||
}
|
||||
|
||||
public void setCurrentPage(int currentPage) {
|
||||
this.currentPage = currentPage;
|
||||
}
|
||||
|
||||
public int getCurrentSize() {
|
||||
return currentSize;
|
||||
}
|
||||
|
||||
public void setCurrentSize(int currentSize) {
|
||||
this.currentSize = currentSize;
|
||||
}
|
||||
|
||||
public int getTotalPages() {
|
||||
return totalPages;
|
||||
}
|
||||
|
||||
public void setTotalPages(int totalPages) {
|
||||
this.totalPages = totalPages;
|
||||
}
|
||||
|
||||
public long getTotalItems() {
|
||||
return totalItems;
|
||||
}
|
||||
|
||||
public void setTotalItems(long totalItems) {
|
||||
this.totalItems = totalItems;
|
||||
}
|
||||
|
||||
public boolean isFirst() {
|
||||
return isFirst;
|
||||
}
|
||||
|
||||
public void setFirst(boolean isFirst) {
|
||||
this.isFirst = isFirst;
|
||||
}
|
||||
|
||||
public boolean isLast() {
|
||||
return isLast;
|
||||
}
|
||||
|
||||
public void setLast(boolean isLast) {
|
||||
this.isLast = isLast;
|
||||
}
|
||||
|
||||
public boolean isHasNext() {
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
public void setHasNext(boolean hasNext) {
|
||||
this.hasNext = hasNext;
|
||||
}
|
||||
|
||||
public boolean isHasPrevious() {
|
||||
return hasPrevious;
|
||||
}
|
||||
|
||||
public void setHasPrevious(boolean hasPrevious) {
|
||||
this.hasPrevious = hasPrevious;
|
||||
}
|
||||
}
|
25
src/main/java/com/example/demo/core/api/PageDtoMapper.java
Normal file
25
src/main/java/com/example/demo/core/api/PageDtoMapper.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.example.demo.core.api;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public class PageDtoMapper {
|
||||
private PageDtoMapper() {
|
||||
}
|
||||
|
||||
public static <D, E> PageDto<D> toDto(Page<E> page, Function<E, D> mapper) {
|
||||
final PageDto<D> dto = new PageDto<>();
|
||||
dto.setItems(page.getContent().stream().map(mapper::apply).toList());
|
||||
dto.setItemsCount(page.getNumberOfElements());
|
||||
dto.setCurrentPage(page.getNumber());
|
||||
dto.setCurrentSize(page.getSize());
|
||||
dto.setTotalPages(page.getTotalPages());
|
||||
dto.setTotalItems(page.getTotalElements());
|
||||
dto.setFirst(page.isFirst());
|
||||
dto.setLast(page.isLast());
|
||||
dto.setHasNext(page.hasNext());
|
||||
dto.setHasPrevious(page.hasPrevious());
|
||||
return dto;
|
||||
}
|
||||
}
|
@ -5,6 +5,8 @@ public class Constants {
|
||||
|
||||
public static final String API_URL = "/api/1.0";
|
||||
|
||||
public static final String DEFAULT_PAGE_SIZE = "5";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
package com.example.demo.itemUsers.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
|
||||
public interface UsersRepository extends CrudRepository<UsersEntity, Long> {
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.itemOrders.api;
|
||||
package com.example.demo.orders.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -14,10 +14,10 @@ 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.itemOrders.model.OrdersEntity;
|
||||
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.orders.model.OrdersEntity;
|
||||
import com.example.demo.orders.service.OrdersService;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
import com.example.demo.products.service.ProductsService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@ -37,6 +37,7 @@ public class OrdersController {
|
||||
private OrdersDto toDto(OrdersEntity entity) {
|
||||
return modelMapper.map(entity, OrdersDto.class);
|
||||
}
|
||||
|
||||
private OrdersEntity toEntity(OrdersDto dto) {
|
||||
final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class);
|
||||
entity.setProduct(productsService.get(dto.getProductId()));
|
||||
@ -44,9 +45,11 @@ public class OrdersController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, @RequestParam(name = "productId", defaultValue = "0") ProductsEntity product) {
|
||||
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId,
|
||||
@RequestParam(name = "productId", defaultValue = "0") ProductsEntity product) {
|
||||
return ordersService.getAll(userId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public OrdersDto get(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id) {
|
||||
return toDto(ordersService.get(userId, id));
|
||||
@ -58,7 +61,8 @@ public class OrdersController {
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public OrdersDto update(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id, @RequestBody OrdersDto dto) {
|
||||
public OrdersDto update(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id,
|
||||
@RequestBody OrdersDto dto) {
|
||||
return toDto(ordersService.update(userId, id, toEntity(dto)));
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package com.example.demo.itemOrders.api;
|
||||
package com.example.demo.orders.api;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
@ -17,7 +18,7 @@ public class OrdersDto {
|
||||
@Min(1)
|
||||
private Integer count;
|
||||
@NotNull
|
||||
private SimpleDateFormat date;
|
||||
private Date date;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@ -30,12 +31,15 @@ public class OrdersDto {
|
||||
public ProductsEntity getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
public void setProduct(ProductsEntity product) {
|
||||
this.product = product;
|
||||
}
|
||||
|
||||
public Long getProductId() {
|
||||
return product.getId();
|
||||
}
|
||||
|
||||
public void setProductId(Long productId) {
|
||||
productId = product.getId();
|
||||
}
|
||||
@ -43,15 +47,19 @@ public class OrdersDto {
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
public SimpleDateFormat getDate() {
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
public void setDate(SimpleDateFormat date) {
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Double getSum() {
|
||||
return count * product.getPrice();
|
@ -1,9 +1,17 @@
|
||||
package com.example.demo.itemOrders.model;
|
||||
import java.util.Objects;
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
package com.example.demo.orders.model;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
import com.example.demo.users.model.UsersEntity;
|
||||
|
||||
import jakarta.persistence.Temporal;
|
||||
import jakarta.persistence.TemporalType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
@ -23,23 +31,30 @@ public class OrdersEntity extends BaseEntity {
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer count;
|
||||
|
||||
|
||||
//@Column(nullable = false)
|
||||
//private Double sum = getSum();
|
||||
private Double sum;
|
||||
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date date;
|
||||
|
||||
public OrdersEntity() {
|
||||
}
|
||||
public OrdersEntity(ProductsEntity product, Integer count) {
|
||||
|
||||
public OrdersEntity(ProductsEntity product, Integer count, Date date) {
|
||||
this.product = product;
|
||||
this.count = count;
|
||||
this.sum = getSum();
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public ProductsEntity getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
public void setProduct(ProductsEntity product) {
|
||||
this.product = product;
|
||||
}
|
||||
|
||||
public UsersEntity getUser() {
|
||||
return user;
|
||||
}
|
||||
@ -50,17 +65,28 @@ public class OrdersEntity extends BaseEntity {
|
||||
user.getOrders().add(this);
|
||||
}
|
||||
}
|
||||
|
||||
// public Double getSum() {
|
||||
// sum = count * product.getPrice();
|
||||
// return sum;
|
||||
// }
|
||||
|
||||
public Double getSum() {
|
||||
sum = count * product.getPrice();
|
||||
return sum;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, product, count);
|
||||
@ -74,8 +100,9 @@ public class OrdersEntity extends BaseEntity {
|
||||
return false;
|
||||
final OrdersEntity other = (OrdersEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getProduct(), product)
|
||||
&& Objects.equals(other.getUser().getId(), user.getId())
|
||||
&& Objects.equals(other.getCount(), count);
|
||||
&& Objects.equals(other.getProduct(), product)
|
||||
&& Objects.equals(other.getUser().getId(), user.getId())
|
||||
&& Objects.equals(other.getCount(), count)
|
||||
&& Objects.equals(other.getDate(), date);
|
||||
}
|
||||
}
|
@ -1,16 +1,17 @@
|
||||
package com.example.demo.itemOrders.repository;
|
||||
package com.example.demo.orders.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
//import com.example.demo.itemOrders.model.OrdersGrouped;
|
||||
|
||||
import com.example.demo.orders.model.OrdersEntity;
|
||||
|
||||
public interface OrdersRepository extends CrudRepository<OrdersEntity, Long> {
|
||||
Optional<OrdersEntity> findOneByUserIdAndId(long userId, long id);
|
||||
|
||||
List<OrdersEntity> findByUserId(long userId);
|
||||
|
||||
|
||||
// select
|
||||
// tpe.name,
|
||||
// coalesce(sum(order.price), 0),
|
||||
@ -19,11 +20,12 @@ public interface OrdersRepository extends CrudRepository<OrdersEntity, Long> {
|
||||
// left join orders as order on tpe.id = order.type_id and order.user_id = ?
|
||||
// group by tpe.name order by tpe.id
|
||||
// @Query("select "
|
||||
// + "u as user, "
|
||||
// + "coalesce(sum(o.sum), 0) as totalSum, "
|
||||
// + "coalesce(sum(o.count), 0) as totalCount "
|
||||
// + "from UsersEntity u left join OrderEntity o on o.user = u and o.user.id = ?1 "
|
||||
// + "group by u order by u.id")
|
||||
// + "u as user, "
|
||||
// + "coalesce(sum(o.sum), 0) as totalSum, "
|
||||
// + "coalesce(sum(o.count), 0) as totalCount "
|
||||
// + "from UsersEntity u left join OrderEntity o on o.user = u and o.user.id =
|
||||
// ?1 "
|
||||
// + "group by u order by u.id")
|
||||
// List<OrdersGrouped> getOrdersTotalByUser(long userId);
|
||||
|
||||
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
package com.example.demo.itemOrders.service;
|
||||
package com.example.demo.orders.service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
//import com.example.demo.itemOrders.model.OrdersGrouped;
|
||||
import com.example.demo.itemOrders.repository.OrdersRepository;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
import com.example.demo.itemUsers.service.UsersService;
|
||||
import com.example.demo.orders.model.OrdersEntity;
|
||||
import com.example.demo.orders.repository.OrdersRepository;
|
||||
import com.example.demo.users.model.UsersEntity;
|
||||
import com.example.demo.users.service.UsersService;
|
||||
|
||||
@Service
|
||||
public class OrdersService {
|
||||
@ -27,28 +27,34 @@ public class OrdersService {
|
||||
userService.get(userId);
|
||||
return repository.findByUserId(userId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public OrdersEntity get(long userId, Long id) {
|
||||
userService.get(userId);
|
||||
return repository.findOneByUserIdAndId(userId, id).orElseThrow(() -> new NotFoundException(OrdersEntity.class, id));
|
||||
return repository.findOneByUserIdAndId(userId, id)
|
||||
.orElseThrow(() -> new NotFoundException(OrdersEntity.class, id));
|
||||
}
|
||||
@Transactional
|
||||
|
||||
@Transactional
|
||||
public OrdersEntity create(long userId, OrdersEntity entity) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
final UsersEntity existsUser = userService.get(userId);
|
||||
final UsersEntity existsUser = userService.get(userId);
|
||||
entity.setUser(existsUser);
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public OrdersEntity update(long userId, Long id, OrdersEntity entity) {
|
||||
public OrdersEntity update(long userId, Long id, OrdersEntity entity, Date date) {
|
||||
userService.get(userId);
|
||||
final OrdersEntity existsEntity = get(userId, id);
|
||||
existsEntity.setProduct(entity.getProduct());
|
||||
existsEntity.setCount(entity.getCount());
|
||||
existsEntity.setDate(entity.getDate());
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public OrdersEntity delete(long userId, Long id) {
|
||||
userService.get(userId);
|
||||
@ -56,11 +62,11 @@ public class OrdersService {
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
|
||||
// @Transactional(readOnly = true)
|
||||
// public List<OrdersGrouped> getTotal(long userId) {
|
||||
// //userService.get(userId);
|
||||
// return repository.getOrdersTotalByUser(userId);
|
||||
// //userService.get(userId);
|
||||
// return repository.getOrdersTotalByUser(userId);
|
||||
// }
|
||||
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.itemProducts.api;
|
||||
package com.example.demo.products.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -14,9 +14,9 @@ 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.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
import com.example.demo.categories.service.CategoriesService;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
import com.example.demo.products.service.ProductsService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@ -27,7 +27,8 @@ public class ProductsController {
|
||||
private final CategoriesService categoriesService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public ProductsController(ProductsService productsService, CategoriesService categoriesService, ModelMapper modelMapper) {
|
||||
public ProductsController(ProductsService productsService, CategoriesService categoriesService,
|
||||
ModelMapper modelMapper) {
|
||||
this.productsService = productsService;
|
||||
this.categoriesService = categoriesService;
|
||||
this.modelMapper = modelMapper;
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.itemProducts.api;
|
||||
package com.example.demo.products.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@ -45,6 +45,7 @@ public class ProductsDto {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package com.example.demo.itemProducts.model;
|
||||
package com.example.demo.products.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.categories.model.CategoriesEntity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@ -13,7 +13,7 @@ import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "products")
|
||||
public class ProductsEntity extends BaseEntity{
|
||||
public class ProductsEntity extends BaseEntity {
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "categoryId", nullable = false)
|
||||
private CategoriesEntity category;
|
||||
@ -38,15 +38,19 @@ public class ProductsEntity extends BaseEntity{
|
||||
public void setType(CategoriesEntity category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@ -64,8 +68,8 @@ public class ProductsEntity extends BaseEntity{
|
||||
return false;
|
||||
final ProductsEntity other = (ProductsEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), category)
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getPrice(), price);
|
||||
&& Objects.equals(other.getType(), category)
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getPrice(), price);
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package com.example.demo.itemProducts.repository;
|
||||
package com.example.demo.products.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
|
||||
public interface ProductsRepository extends CrudRepository<ProductsEntity, Long> {
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.itemProducts.service;
|
||||
package com.example.demo.products.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.StreamSupport;
|
||||
@ -7,8 +7,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.repository.ProductsRepository;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
import com.example.demo.products.repository.ProductsRepository;
|
||||
|
||||
@Service
|
||||
public class ProductsService {
|
||||
@ -17,14 +17,17 @@ public class ProductsService {
|
||||
public ProductsService(ProductsRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ProductsEntity> getAll() {
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ProductsEntity get(Long id) {
|
||||
return repository.findById(id).orElseThrow(() -> new NotFoundException(ProductsEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProductsEntity create(ProductsEntity entity) {
|
||||
if (entity == null) {
|
||||
@ -32,6 +35,7 @@ public class ProductsService {
|
||||
}
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProductsEntity update(Long id, ProductsEntity entity) {
|
||||
final ProductsEntity existsEntity = get(id);
|
||||
@ -40,6 +44,7 @@ public class ProductsService {
|
||||
existsEntity.setPrice(entity.getPrice());
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProductsEntity delete(Long id) {
|
||||
final ProductsEntity existsEntity = get(id);
|
@ -1,23 +0,0 @@
|
||||
package com.example.demo.speaker.api;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.speaker.service.SpeakerService;
|
||||
|
||||
@RestController
|
||||
public class SpeakerController {
|
||||
private final SpeakerService speakerService;
|
||||
|
||||
public SpeakerController(SpeakerService speakerService) {
|
||||
this.speakerService = speakerService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String hello(
|
||||
@RequestParam(value = "name", defaultValue = "Мир") String name,
|
||||
@RequestParam(value = "lang", defaultValue = "ru") String lang) {
|
||||
return speakerService.say(name, lang);
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.example.demo.speaker.configuration;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.example.demo.speaker.domain.Speaker;
|
||||
import com.example.demo.speaker.domain.SpeakerEng;
|
||||
import com.example.demo.speaker.domain.SpeakerRus;
|
||||
|
||||
@Configuration
|
||||
public class SpeakerConfiguration {
|
||||
private final Logger log = LoggerFactory.getLogger(SpeakerConfiguration.class);
|
||||
|
||||
@Bean(value = "ru", initMethod = "init", destroyMethod = "destroy")
|
||||
public SpeakerRus createRusSpeaker() {
|
||||
log.info("Call createRusSpeaker()");
|
||||
return new SpeakerRus();
|
||||
}
|
||||
|
||||
@Bean(value = "en")
|
||||
public Speaker createEngSpeaker() {
|
||||
log.info("Call createEngSpeaker()");
|
||||
return new SpeakerEng();
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
public interface Speaker {
|
||||
String say();
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
|
||||
@Component(value = "de")
|
||||
public class SpeakerDeu implements Speaker {
|
||||
private final Logger log = LoggerFactory.getLogger(SpeakerDeu.class);
|
||||
|
||||
@Override
|
||||
public String say() {
|
||||
return "Hallo";
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
log.info("SpeakerDeu.init()");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
log.info("SpeakerDeu.destroy()");
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
public class SpeakerEng implements Speaker, InitializingBean, DisposableBean {
|
||||
private final Logger log = LoggerFactory.getLogger(SpeakerEng.class);
|
||||
|
||||
@Override
|
||||
public String say() {
|
||||
return "Hello";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
log.info("SpeakerEng.afterPropertiesSet()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
log.info("SpeakerEng.destroy()");
|
||||
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SpeakerRus implements Speaker {
|
||||
private final Logger log = LoggerFactory.getLogger(SpeakerRus.class);
|
||||
|
||||
@Override
|
||||
public String say() {
|
||||
return "Привет";
|
||||
}
|
||||
|
||||
public void init() {
|
||||
log.info("SpeakerRus.init()");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
log.info("SpeakerRus.destroy()");
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package com.example.demo.speaker.service;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.example.demo.speaker.domain.Speaker;
|
||||
|
||||
@Service
|
||||
public class SpeakerService {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public SpeakerService(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public String say(String name, String lang) {
|
||||
final Speaker speaker = (Speaker) applicationContext.getBean(lang);
|
||||
return String.format("%s, %s!", speaker.say(), name);
|
||||
}
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
package com.example.demo.itemUsers.api;
|
||||
|
||||
import java.util.List;
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
@ -10,11 +8,14 @@ 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.api.PageDto;
|
||||
import com.example.demo.core.api.PageDtoMapper;
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
import com.example.demo.itemUsers.service.UsersService;
|
||||
import com.example.demo.users.model.UsersEntity;
|
||||
import com.example.demo.users.service.UsersService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@ -38,8 +39,10 @@ public class UsersController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<UsersDto> getAll() {
|
||||
return usersService.getAll().stream().map(this::toDto).toList();
|
||||
public PageDto<UsersDto> getAll(
|
||||
@RequestParam(name = "page", defaultValue = "0") int page,
|
||||
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
|
||||
return PageDtoMapper.toDto(usersService.getAll(page, size), this::toDto);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.itemUsers.api;
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.example.demo.itemUsers.model;
|
||||
package com.example.demo.users.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
//import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
import com.example.demo.orders.model.OrdersEntity;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
@ -33,6 +33,7 @@ public class UsersEntity extends BaseEntity {
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Set<OrdersEntity> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
@ -43,7 +44,7 @@ public class UsersEntity extends BaseEntity {
|
||||
}
|
||||
orders.add(order);
|
||||
}
|
||||
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.example.demo.users.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import com.example.demo.users.model.UsersEntity;
|
||||
|
||||
public interface UsersRepository
|
||||
extends CrudRepository<UsersEntity, Long>, PagingAndSortingRepository<UsersEntity, Long> {
|
||||
Optional<UsersEntity> findByLoginIgnoreCase(String login);
|
||||
|
||||
}
|
||||
|
||||
// вывести 5 пользователей с наибольшей суммой всех покупок на jpql
|
@ -1,14 +1,16 @@
|
||||
package com.example.demo.itemUsers.service;
|
||||
package com.example.demo.users.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
import com.example.demo.itemUsers.repository.UsersRepository;
|
||||
import com.example.demo.users.model.UsersEntity;
|
||||
import com.example.demo.users.repository.UsersRepository;
|
||||
|
||||
@Service
|
||||
public class UsersService {
|
||||
@ -17,14 +19,22 @@ public class UsersService {
|
||||
public UsersService(UsersRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<UsersEntity> getAll() {
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Page<UsersEntity> getAll(int page, int size) {
|
||||
return repository.findAll(PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public UsersEntity get(Long id) {
|
||||
return repository.findById(id).orElseThrow(() -> new NotFoundException(UsersEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UsersEntity create(UsersEntity entity) {
|
||||
if (entity == null) {
|
||||
@ -32,6 +42,7 @@ public class UsersService {
|
||||
}
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UsersEntity update(Long id, UsersEntity entity) {
|
||||
final UsersEntity existsEntity = get(id);
|
||||
@ -40,6 +51,7 @@ public class UsersService {
|
||||
repository.save(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UsersEntity delete(Long id) {
|
||||
final UsersEntity existsEntity = get(id);
|
@ -10,33 +10,28 @@ 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;
|
||||
import com.example.demo.categories.service.CategoriesService;
|
||||
import com.example.demo.categories.model.CategoriesEntity;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class CategoriesServiceTests {
|
||||
@Autowired
|
||||
private CategoriesService categoriesService;
|
||||
private CategoriesService categoriesService;
|
||||
|
||||
private CategoriesEntity category;
|
||||
private CategoriesEntity category;
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
category = categoriesService.create(new CategoriesEntity("Ноутбук"));
|
||||
categoriesService.create(new CategoriesEntity("Телефон"));
|
||||
categoriesService.create(new CategoriesEntity("Игровая приставка"));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
category = categoriesService.create(new CategoriesEntity("Ноутбук"));
|
||||
categoriesService.create(new CategoriesEntity("Телефон"));
|
||||
categoriesService.create(new CategoriesEntity("Игровая приставка"));
|
||||
}
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
categoriesService.getAll().forEach(item -> categoriesService.delete(item.getId()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> categoriesService.get(0L));
|
||||
void removeData() {
|
||||
categoriesService.getAll().forEach(item -> categoriesService.delete(item.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1,83 +0,0 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.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.itemOrders.model.OrdersEntity;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class OrdersServiceTests {
|
||||
@Autowired
|
||||
private OrdersService ordersService;
|
||||
@Autowired
|
||||
private UsersService usersService;
|
||||
@Autowired
|
||||
private CategoriesService categoriesService;
|
||||
@Autowired
|
||||
private ProductsService productsService;
|
||||
|
||||
private OrdersEntity order;
|
||||
private UsersEntity user;
|
||||
private CategoriesEntity category;
|
||||
private ProductsEntity product;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createData() {
|
||||
user = usersService.create(new UsersEntity("Natalia", "1234"));
|
||||
category = categoriesService.create(new CategoriesEntity("Телефон"));
|
||||
product = productsService.create(new ProductsEntity(category, "Iphone 13", 150000.00));
|
||||
|
||||
order = ordersService.create(user.getId(), new OrdersEntity(product, 3));
|
||||
ordersService.create(user.getId(), new OrdersEntity(product, 2));
|
||||
ordersService.create(user.getId(), new OrdersEntity(product, 1));
|
||||
ordersService.create(user.getId(), new OrdersEntity(product, 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void createTest() {
|
||||
Assertions.assertEquals(4, ordersService.getAll(user.getId()).size());
|
||||
Assertions.assertEquals(order, ordersService.get(user.getId(), order.getId()));
|
||||
}
|
||||
|
||||
// @Test
|
||||
// @Order(3)
|
||||
// void updateTest() {
|
||||
// final Integer newCount = 5;
|
||||
// final OrdersEntity entity = ordersService.get(user.getId(),order.getId());
|
||||
// final Integer oldCount = entity.getCount();
|
||||
// final OrdersEntity newEntity = ordersService.update(user.getId(),order.getId(), new OrdersEntity(product, newCount));
|
||||
// Assertions.assertEquals(4, ordersService.getAll(user.getId()).size());
|
||||
// Assertions.assertEquals(newEntity, ordersService.get(user.getId(),newEntity.getId()));
|
||||
// Assertions.assertEquals(newCount, newEntity.getCount());
|
||||
// Assertions.assertNotEquals(oldCount, newEntity.getCount());
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// @Order(4)
|
||||
// void deleteTest() {
|
||||
// ordersService.delete(user.getId(),order.getId());
|
||||
// Assertions.assertEquals(3, ordersService.getAll(user.getId()).size());
|
||||
|
||||
// final OrdersEntity newEntity = ordersService.create(user.getId(), new OrdersEntity(product, 3));
|
||||
// Assertions.assertEquals(3, ordersService.getAll(user.getId()).size());
|
||||
// Assertions.assertNotEquals(order.getId(), newEntity.getId());
|
||||
// }
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@ -9,38 +10,39 @@ 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.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.categories.model.CategoriesEntity;
|
||||
import com.example.demo.categories.service.CategoriesService;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
import com.example.demo.products.service.ProductsService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class ProductsServiceTests {
|
||||
@Autowired
|
||||
private ProductsService productsService;
|
||||
private ProductsService productsService;
|
||||
@Autowired
|
||||
private CategoriesService categoriesService;
|
||||
|
||||
private ProductsEntity product;
|
||||
private ProductsEntity product;
|
||||
|
||||
private CategoriesEntity category1;
|
||||
private CategoriesEntity category2;
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
category1 = categoriesService.create(new CategoriesEntity("Телефон"));
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
category1 = categoriesService.create(new CategoriesEntity("Телефон"));
|
||||
category2 = categoriesService.create(new CategoriesEntity("Игровая приставка"));
|
||||
product = productsService.create(new ProductsEntity(category1, "Lenovo IDEA PAD 13", 15232.00));
|
||||
productsService.create(new ProductsEntity(category1, "Acer", 20300.00));
|
||||
productsService.create(new ProductsEntity(category2, "Iphone 13", 150000.00));
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
productsService.getAll().forEach(item -> productsService.delete(item.getId()));
|
||||
void removeData() {
|
||||
productsService.getAll().forEach(item -> productsService.delete(item.getId()));
|
||||
categoriesService.getAll().forEach(item -> categoriesService.delete(item.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
@ -58,7 +60,8 @@ class ProductsServiceTests {
|
||||
final Double newPrice = 20000.00;
|
||||
final ProductsEntity entity = productsService.get(product.getId());
|
||||
final Double oldPrice = entity.getPrice();
|
||||
final ProductsEntity newEntity = productsService.update(product.getId(), new ProductsEntity(category1, "Lenovo IDEA PAD 13", newPrice));
|
||||
final ProductsEntity newEntity = productsService.update(product.getId(),
|
||||
new ProductsEntity(category1, "Lenovo IDEA PAD 13", newPrice));
|
||||
Assertions.assertEquals(3, productsService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, productsService.get(newEntity.getId()));
|
||||
Assertions.assertEquals(newPrice, newEntity.getPrice());
|
||||
@ -70,7 +73,8 @@ class ProductsServiceTests {
|
||||
productsService.delete(product.getId());
|
||||
Assertions.assertEquals(2, productsService.getAll().size());
|
||||
|
||||
final ProductsEntity newEntity = productsService.create(new ProductsEntity(category1, "Lenovo IDEA PAD 13", 15232.00));
|
||||
final ProductsEntity newEntity = productsService
|
||||
.create(new ProductsEntity(category1, "Lenovo IDEA PAD 13", 15232.00));
|
||||
Assertions.assertEquals(3, productsService.getAll().size());
|
||||
Assertions.assertNotEquals(product.getId(), newEntity.getId());
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.speaker.service.SpeakerService;
|
||||
|
||||
@SpringBootTest
|
||||
class SpeakerSrviceTests {
|
||||
@Autowired
|
||||
SpeakerService speakerService;
|
||||
|
||||
@Test
|
||||
void testSpeakerRus() {
|
||||
final String res = speakerService.say("Мир", "ru");
|
||||
Assertions.assertEquals("Привет, Мир!", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSpeakerEng() {
|
||||
final String res = speakerService.say("World", "en");
|
||||
Assertions.assertEquals("Hello, World!", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSpeakerDeu() {
|
||||
final String res = speakerService.say("Welt", "de");
|
||||
Assertions.assertEquals("Hallo, Welt!", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSpeakerErrorWired() {
|
||||
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> speakerService.say("Мир", "rus"));
|
||||
}
|
||||
}
|
132
src/test/java/com/example/demo/UsersOrdersServiceTests.java
Normal file
132
src/test/java/com/example/demo/UsersOrdersServiceTests.java
Normal file
@ -0,0 +1,132 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.categories.model.CategoriesEntity;
|
||||
import com.example.demo.categories.service.CategoriesService;
|
||||
import com.example.demo.orders.model.OrdersEntity;
|
||||
import com.example.demo.orders.service.OrdersService;
|
||||
import com.example.demo.products.model.ProductsEntity;
|
||||
import com.example.demo.products.service.ProductsService;
|
||||
import com.example.demo.users.model.UsersEntity;
|
||||
import com.example.demo.users.service.UsersService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class UsersOrdersServiceTests {
|
||||
@Autowired
|
||||
private UsersService usersService;
|
||||
@Autowired
|
||||
private OrdersService ordersService;
|
||||
@Autowired
|
||||
private ProductsService productsService;
|
||||
@Autowired
|
||||
private CategoriesService categoriesService;
|
||||
|
||||
private UsersEntity user1;
|
||||
private UsersEntity user2;
|
||||
|
||||
private ProductsEntity product;
|
||||
private CategoriesEntity category;
|
||||
private OrdersEntity order;
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
|
||||
category = categoriesService.create(new CategoriesEntity("Телефон"));
|
||||
|
||||
product = productsService.create(new ProductsEntity(category, "Iphone 13", 150000.00));
|
||||
|
||||
user1 = usersService.create(new UsersEntity("Natalia", "1234"));
|
||||
user2 = usersService.create(new UsersEntity("Revengel", "4567"));
|
||||
|
||||
final var orders = List.of(
|
||||
new OrdersEntity(product, 3),
|
||||
new OrdersEntity(product, 2),
|
||||
new OrdersEntity(product, 1),
|
||||
new OrdersEntity(product, 4));
|
||||
orders.forEach(order -> ordersService.create(user1.getId(), order));
|
||||
|
||||
// order = ordersService.get(user1.getId(), 1);
|
||||
order = orders.get(0);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
usersService.getAll().forEach(item -> usersService.delete(item.getId()));
|
||||
productsService.getAll().forEach(item -> productsService.delete(item.getId()));
|
||||
categoriesService.getAll().forEach(item -> categoriesService.delete(item.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOrderTest() {
|
||||
Assertions.assertEquals(4, ordersService.getAll(user1.getId()).size());
|
||||
Assertions.assertEquals(0, ordersService.getAll(user2.getId()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUserTest() {
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateOrderTest() {
|
||||
final Integer newCount = 5;
|
||||
final OrdersEntity entity = ordersService.get(user1.getId(), order.getId());
|
||||
final Integer oldCount = entity.getCount();
|
||||
final OrdersEntity newEntity = ordersService.update(user1.getId(), order.getId(), new OrdersEntity(product,
|
||||
newCount));
|
||||
Assertions.assertEquals(4, ordersService.getAll(user1.getId()).size());
|
||||
Assertions.assertEquals(newEntity,
|
||||
ordersService.get(user1.getId(), newEntity.getId()));
|
||||
Assertions.assertEquals(newCount, newEntity.getCount());
|
||||
Assertions.assertNotEquals(oldCount, newEntity.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateUserTest() {
|
||||
final String newPassword = "0000";
|
||||
final UsersEntity entity = usersService.get(user1.getId());
|
||||
final String login = entity.getLogin();
|
||||
final String oldPassword = entity.getPassword();
|
||||
final UsersEntity newEntity = usersService.update(user1.getId(), new UsersEntity(login, newPassword));
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, usersService.get(newEntity.getId()));
|
||||
Assertions.assertEquals(newPassword, newEntity.getPassword());
|
||||
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOrderTest() {
|
||||
ordersService.delete(user1.getId(), order.getId());
|
||||
Assertions.assertEquals(3, ordersService.getAll(user1.getId()).size());
|
||||
|
||||
final OrdersEntity newEntity = ordersService.create(user1.getId(), new OrdersEntity(product, 3));
|
||||
Assertions.assertEquals(4, ordersService.getAll(user1.getId()).size());
|
||||
Assertions.assertNotEquals(order.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteUserTest() {
|
||||
usersService.delete(user1.getId());
|
||||
Assertions.assertEquals(1, usersService.getAll().size());
|
||||
|
||||
final UsersEntity newEntity = usersService.create(new UsersEntity("Natalia",
|
||||
"1234"));
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertNotEquals(user1.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
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.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
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.service.UsersService;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class UsersServiceTests {
|
||||
@Autowired
|
||||
private UsersService usersService;
|
||||
@Autowired
|
||||
private OrdersService ordersService;
|
||||
@Autowired
|
||||
private ProductsService productsService;
|
||||
@Autowired
|
||||
private CategoriesService categoriesService;
|
||||
|
||||
private UsersEntity user;
|
||||
|
||||
private ProductsEntity product;
|
||||
private CategoriesEntity category;
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
|
||||
category = categoriesService.create(new CategoriesEntity("Телефон"));
|
||||
|
||||
product = productsService.create(new ProductsEntity(category, "Iphone 13", 150000.00));
|
||||
|
||||
user = usersService.create(new UsersEntity("Natalia", "1234"));
|
||||
usersService.create(new UsersEntity("Revengel", "4567"));
|
||||
|
||||
final var orders = List.of(
|
||||
new OrdersEntity(product, 3),
|
||||
new OrdersEntity(product, 2),
|
||||
new OrdersEntity(product, 1),
|
||||
new OrdersEntity(product, 4));
|
||||
orders.forEach(order -> ordersService.create(user.getId(), order));
|
||||
}
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
usersService.getAll().forEach(item -> usersService.delete(item.getId()));
|
||||
productsService.getAll().forEach(item -> productsService.delete(item.getId()));
|
||||
categoriesService.getAll().forEach(item -> categoriesService.delete(item.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> usersService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTest() {
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertEquals(user, usersService.get(user.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTest() {
|
||||
final String newPassword = "0000";
|
||||
final UsersEntity entity = usersService.get(user.getId());
|
||||
final String login = entity.getLogin();
|
||||
final String oldPassword = entity.getPassword();
|
||||
final UsersEntity newEntity = usersService.update(user.getId(), new UsersEntity(login, newPassword));
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, usersService.get(newEntity.getId()));
|
||||
Assertions.assertEquals(newPassword, newEntity.getPassword());
|
||||
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTest() {
|
||||
usersService.delete(user.getId());
|
||||
Assertions.assertEquals(1, usersService.getAll().size());
|
||||
|
||||
final UsersEntity newEntity = usersService.create(new UsersEntity("Natalia", "1234"));
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertNotEquals(user.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user