new
This commit is contained in:
parent
cd388cff5c
commit
33a16a76ae
@ -1,6 +1,6 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.2.4'
|
||||
id 'org.springframework.boot' version '3.2.5'
|
||||
id 'io.spring.dependency-management' version '1.1.4'
|
||||
}
|
||||
|
||||
|
BIN
data.mv.db
Normal file
BIN
data.mv.db
Normal file
Binary file not shown.
@ -44,8 +44,8 @@ public class DemoApplication implements CommandLineRunner {
|
||||
if (args.length > 0 && Objects.equals("--populate", args[0])) {
|
||||
|
||||
log.info("Создание пользователей");
|
||||
userService.create(new UsersEntity(null, "Natalia", "1234"));
|
||||
userService.create(new UsersEntity(null, "Revengel", "4567"));
|
||||
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, "Ноутбуки"));
|
||||
@ -57,10 +57,10 @@ public class DemoApplication implements CommandLineRunner {
|
||||
final var product3 = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00));
|
||||
|
||||
log.info("Создание заказов");
|
||||
ordersService.create(new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
|
||||
ordersService.create(new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
|
||||
ordersService.create(user1.getId(), new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(user1.getId(), new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(user2.getId(), new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
|
||||
ordersService.create(user2.getId(), new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
package com.example.demo.itemCategories.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
|
||||
public interface CategoriesRepository extends CrudRepository<CategoriesEntity, Long> {
|
||||
Optional<CategoriesEntity> findByNameIgnoreCase(String name);
|
||||
}
|
||||
|
@ -44,10 +44,9 @@ public class OrdersController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, @RequestParam(name = "productId", defaultValue = "0") Long productId) {
|
||||
return ordersService.getAll(userId, productId).stream().map(this::toDto).toList();
|
||||
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, @RequestParam(name = "productId", defaultValue = "0") ProductsEntity product) {
|
||||
return ordersService.getAll(userId, product.getId()).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));
|
||||
|
@ -0,0 +1,32 @@
|
||||
package com.example.demo.itemOrders.api;
|
||||
|
||||
public class OrdersGroupedDto {
|
||||
private Long userId;
|
||||
private Long totalSum;
|
||||
private Integer totalCount;
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Long getTotalSum() {
|
||||
return totalSum;
|
||||
}
|
||||
|
||||
public void setTotalSum(Long totalSum) {
|
||||
this.totalSum = totalSum;
|
||||
}
|
||||
|
||||
public Integer getTotalCount() {
|
||||
return totalCount;
|
||||
}
|
||||
|
||||
public void setTotalCount(Integer totalCount) {
|
||||
this.totalCount = totalCount;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.example.demo.itemOrders.model;
|
||||
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
|
||||
public interface OrdersGrouped {
|
||||
UsersEntity getUser();
|
||||
|
||||
double getTotalSum();
|
||||
|
||||
int getTotalCount();
|
||||
}
|
@ -2,8 +2,10 @@ package com.example.demo.itemOrders.repository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
import com.example.demo.itemOrders.model.OrdersGrouped;
|
||||
|
||||
public interface OrdersRepository extends CrudRepository<OrdersEntity, Long> {
|
||||
Optional<OrdersEntity> findOneByUserIdAndId(long userId, long id);
|
||||
@ -12,4 +14,18 @@ public interface OrdersRepository extends CrudRepository<OrdersEntity, Long> {
|
||||
|
||||
List<OrdersEntity> findByUserIdAndProductId(long userId, long productId);
|
||||
|
||||
// select
|
||||
// tpe.name,
|
||||
// coalesce(sum(order.price), 0),
|
||||
// coalesce(sum(order.count), 0)
|
||||
// from types as tpe
|
||||
// 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")
|
||||
List<OrdersGrouped> getOrdersTotalByUser(long userId);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ 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;
|
||||
@ -60,4 +61,10 @@ public class OrdersService {
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<OrdersGrouped> getTotal(long userId) {
|
||||
userService.get(userId);
|
||||
return repository.getOrdersTotalByUser(userId);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
package com.example.demo.itemProducts.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
|
||||
public interface ProductsRepository extends CrudRepository<ProductsEntity, Long> {
|
||||
Optional<ProductsEntity> findByNameIgnoreCase(String name);
|
||||
}
|
||||
|
@ -17,31 +17,33 @@ 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) {
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProductsEntity update(Long id, ProductsEntity entity) {
|
||||
final ProductsEntity existsEntity = get(id);
|
||||
existsEntity.setType(entity.getType());
|
||||
existsEntity.setName(entity.getName());
|
||||
existsEntity.setPrice(entity.getPrice());
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProductsEntity delete(Long id) {
|
||||
final ProductsEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
package com.example.demo.itemUsers.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
|
||||
public interface UsersRepository extends CrudRepository<UsersEntity, Long> {
|
||||
Optional<UsersEntity> findByNameIgnoreCase(String name);
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package com.example.demo.itemUsers.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
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;
|
||||
@ -16,28 +17,32 @@ public class UsersService {
|
||||
public UsersService(UsersRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<UsersEntity> getAll() {
|
||||
return repository.getAll();
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public UsersEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
|
||||
return repository.findById(id).orElseThrow(() -> new NotFoundException(UsersEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UsersEntity create(UsersEntity entity) {
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UsersEntity update(Long id, UsersEntity entity) {
|
||||
final UsersEntity existsEntity = get(id);
|
||||
existsEntity.setLogin(entity.getLogin());
|
||||
existsEntity.setPassword(entity.getPassword());
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UsersEntity delete(Long id) {
|
||||
final UsersEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ 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
|
||||
@ -24,6 +26,10 @@ class OrdersServiceTests {
|
||||
@Autowired
|
||||
private OrdersService ordersService;
|
||||
@Autowired
|
||||
private UsersService usersService;
|
||||
private UsersEntity user1;
|
||||
private UsersEntity user2;
|
||||
@Autowired
|
||||
private CategoriesService categoriesService;
|
||||
private CategoriesEntity category1;
|
||||
private CategoriesEntity category2;
|
||||
@ -35,13 +41,16 @@ class OrdersServiceTests {
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> ordersService.get(0L));
|
||||
Assertions.assertThrows(NotFoundException.class, () -> ordersService.get(1,0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
|
||||
user1 = usersService.create(new UsersEntity(null, "Natalia", "1234"));
|
||||
user2 = usersService.create(new UsersEntity(null, "Revengel", "4567"));
|
||||
|
||||
category1 = categoriesService.create(new CategoriesEntity(null, "Телефон"));
|
||||
category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
||||
|
||||
@ -49,26 +58,25 @@ class OrdersServiceTests {
|
||||
product2 = productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00));
|
||||
product3 = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00));
|
||||
|
||||
ordersService.create(new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
|
||||
final OrdersEntity last = ordersService
|
||||
.create(new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
|
||||
Assertions.assertEquals(4, ordersService.getAll(0L).size());
|
||||
Assertions.assertEquals(0, ordersService.getAll(-1L).size());
|
||||
Assertions.assertEquals(last, ordersService.get(last.getId()));
|
||||
ordersService.create(user1.getId(), new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(user1.getId(), new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(user2.getId(),new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
|
||||
final OrdersEntity last = ordersService.create(user2.getId(), new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
|
||||
Assertions.assertEquals(4, ordersService.getAll(1,0L).size());
|
||||
Assertions.assertEquals(0, ordersService.getAll(1,-1L).size());
|
||||
Assertions.assertEquals(last, ordersService.get(1,last.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final Integer newCount = 5;
|
||||
final OrdersEntity entity = ordersService.get(1L);
|
||||
final OrdersEntity entity = ordersService.get(1,1L);
|
||||
final Integer oldCount = entity.getCount();
|
||||
final OrdersEntity newEntity = ordersService.update(1L,
|
||||
final OrdersEntity newEntity = ordersService.update(1,1L,
|
||||
new OrdersEntity(null, product1, newCount, new SimpleDateFormat("09/04/2024")));
|
||||
Assertions.assertEquals(4, ordersService.getAll(0L).size());
|
||||
Assertions.assertEquals(newEntity, ordersService.get(1L));
|
||||
Assertions.assertEquals(4, ordersService.getAll(1,0L).size());
|
||||
Assertions.assertEquals(newEntity, ordersService.get(1,1L));
|
||||
Assertions.assertEquals(newCount, newEntity.getCount());
|
||||
Assertions.assertNotEquals(oldCount, newEntity.getCount());
|
||||
}
|
||||
@ -76,9 +84,9 @@ class OrdersServiceTests {
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
ordersService.delete(3L);
|
||||
Assertions.assertEquals(3, ordersService.getAll(0L).size());
|
||||
final OrdersEntity last = ordersService.get(2L);
|
||||
ordersService.delete(2,3L);
|
||||
Assertions.assertEquals(3, ordersService.getAll(1,0L).size());
|
||||
final OrdersEntity last = ordersService.get(1,2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
}
|
||||
}
|
||||
|
14
src/test/resources/application.properties
Normal file
14
src/test/resources/application.properties
Normal file
@ -0,0 +1,14 @@
|
||||
# Server
|
||||
spring.main.banner-mode=off
|
||||
|
||||
# Logger settings
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.com.example.demo=DEBUG
|
||||
|
||||
# JPA Settings
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.open-in-view=false
|
Loading…
Reference in New Issue
Block a user