complete?

This commit is contained in:
revengel66 2024-05-17 16:09:09 +03:00
parent 2313a080a7
commit 566bb17f71
12 changed files with 155 additions and 15 deletions

Binary file not shown.

View File

@ -1596,3 +1596,44 @@ Caused by: org.h2.mvstore.MVStoreException: The file is locked: C:/Users/New/Des
at org.h2.mvstore.SingleFileStore.open(SingleFileStore.java:81)
at org.h2.mvstore.MVStore.<init>(MVStore.java:286)
... 132 more
2024-05-17 14:55:42.726304+03:00 jdbc[13]: exception
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "[*]USER_ID SELECT * FROM USER_ID"; ожидалось "UPDATE"
Syntax error in SQL statement "[*]USER_ID SELECT * FROM USER_ID"; expected "UPDATE"; SQL statement:
USER_ID SELECT * FROM USER_ID [42001-224]
2024-05-17 14:55:48.732316+03:00 jdbc[13]: exception
org.h2.jdbc.JdbcSQLSyntaxErrorException: Таблица "USER_ID" не найдена
Table "USER_ID" not found; SQL statement:
SELECT * FROM USER_ID [42102-224]
2024-05-17 15:09:27.889512+03:00 jdbc[14]: exception
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "SELECT o.USER_ID, SUM(o.SUM) AS TOTAL_SUM\000d\000aFROM orders AS o\000d\000a[*]from TypeEntity t left join UsersEntity u on o.type = t and o.user.id = ?1\000d\000aGROUP BY USER_ID\000d\000aORDER BY TOTAL_SUM DESC\000d\000aLIMIT 5"
Syntax error in SQL statement "SELECT o.USER_ID, SUM(o.SUM) AS TOTAL_SUM\000d\000aFROM orders AS o\000d\000a[*]from TypeEntity t left join UsersEntity u on o.type = t and o.user.id = ?1\000d\000aGROUP BY USER_ID\000d\000aORDER BY TOTAL_SUM DESC\000d\000aLIMIT 5"; SQL statement:
SELECT o.USER_ID, SUM(o.SUM) AS TOTAL_SUM
FROM orders AS o
from TypeEntity t left join UsersEntity u on o.type = t and o.user.id = ?1
GROUP BY USER_ID
ORDER BY TOTAL_SUM DESC
LIMIT 5 [42000-224]
2024-05-17 15:52:29.562546+03:00 jdbc[25]: null
org.h2.message.DbException: Незакрытое приложением соединение уничтожено сборщиком мусора
The connection was not closed by the application and is garbage collected [90018-224]
at org.h2.message.DbException.get(DbException.java:223)
at org.h2.message.DbException.get(DbException.java:199)
at org.h2.message.DbException.get(DbException.java:188)
at org.h2.jdbc.JdbcConnection.closeOld(JdbcConnection.java:218)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:135)
at org.h2.Driver.connect(Driver.java:59)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:733)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:712)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Незакрытое приложением соединение уничтожено сборщиком мусора
The connection was not closed by the application and is garbage collected [90018-224]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:690)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:489)
... 16 more

View File

@ -61,12 +61,11 @@ public class DemoApplication implements CommandLineRunner {
log.info("Создание заказов");
final var orders = List.of(
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()));
new OrdersEntity(product1, 3, "03.04.2024"),
new OrdersEntity(product2, 2, "02.04.2024"),
new OrdersEntity(product3, 1, "05.04.2024"),
new OrdersEntity(product1, 4, "03.04.2024"));
orders.forEach(order -> ordersService.create(user1.getId(), order));
}
}
}

View File

@ -1,5 +1,8 @@
package com.example.demo.orders.api;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.modelmapper.ModelMapper;
@ -27,6 +30,7 @@ public class OrdersController {
private final OrdersService ordersService;
private final ProductsService productsService;
private final ModelMapper modelMapper;
private SimpleDateFormat formater = new SimpleDateFormat("dd.MM.yyyy");
public OrdersController(OrdersService ordersService, ProductsService productsService, ModelMapper modelMapper) {
this.ordersService = ordersService;
@ -34,7 +38,23 @@ public class OrdersController {
this.modelMapper = modelMapper;
}
public Date StringConvertDate(String date) {
try {
return formater.parse(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public String DateConvertString(Date date) {
return formater.format(date);
}
private OrdersDto toDto(OrdersEntity entity) {
// String dateEntity = DateConvertString(entity.getDate());
// entity.setDate(dateEntity);
return modelMapper.map(entity, OrdersDto.class);
}

View File

@ -4,6 +4,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.example.demo.core.model.BaseEntity;
@ -37,14 +38,24 @@ public class OrdersEntity extends BaseEntity {
@Temporal(TemporalType.DATE)
private Date date;
public Date StringConvertDate(String date) {
SimpleDateFormat formater = new SimpleDateFormat("dd.MM.yyyy");
try {
return formater.parse(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public OrdersEntity() {
}
public OrdersEntity(ProductsEntity product, Integer count, Date date) {
public OrdersEntity(ProductsEntity product, Integer count, String date) {
this.product = product;
this.count = count;
this.sum = getSum();
this.date = date;
this.date = StringConvertDate(date);
}
public ProductsEntity getProduct() {

View File

@ -46,7 +46,7 @@ public class OrdersService {
}
@Transactional
public OrdersEntity update(long userId, Long id, OrdersEntity entity, Date date) {
public OrdersEntity update(long userId, Long id, OrdersEntity entity) {
userService.get(userId);
final OrdersEntity existsEntity = get(userId, id);
existsEntity.setProduct(entity.getProduct());

View File

@ -1,5 +1,7 @@
package com.example.demo.users.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@ -15,6 +17,7 @@ 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.users.model.UsersEntity;
import com.example.demo.users.model.UsersGrouped;
import com.example.demo.users.service.UsersService;
import jakarta.validation.Valid;
@ -34,6 +37,10 @@ public class UsersController {
return modelMapper.map(entity, UsersDto.class);
}
private UsersGroupedDto toGroupedDto(UsersGrouped entity) {
return (UsersGroupedDto) this.modelMapper.map(entity, UsersGroupedDto.class);
}
private UsersEntity toEntity(UsersDto dto) {
return modelMapper.map(dto, UsersEntity.class);
}
@ -64,4 +71,9 @@ public class UsersController {
public UsersDto delete(@PathVariable(name = "id") Long id) {
return toDto(usersService.delete(id));
}
@GetMapping({ "/total" })
public List<UsersGroupedDto> getMethodName() {
return this.usersService.getFiveUsers().stream().map(this::toGroupedDto).toList();
}
}

View File

@ -0,0 +1,26 @@
package com.example.demo.users.api;
public class UsersGroupedDto {
private Long userId;
private Double totalSum;
public UsersGroupedDto() {
}
public Long getUserId() {
return this.userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Double getTotalSum() {
return this.totalSum;
}
public void setTotalSum(Double totalSum) {
this.totalSum = totalSum;
}
}

View File

@ -0,0 +1,7 @@
package com.example.demo.users.model;
public interface UsersGrouped {
UsersEntity getUser();
Double getTotalSum();
}

View File

@ -1,16 +1,26 @@
package com.example.demo.users.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.example.demo.users.model.UsersEntity;
import com.example.demo.users.model.UsersGrouped;
public interface UsersRepository
extends CrudRepository<UsersEntity, Long>, PagingAndSortingRepository<UsersEntity, Long> {
Optional<UsersEntity> findByLoginIgnoreCase(String login);
@Query("SELECT user AS u, SUM(o.sum) AS totalSum " +
"FROM OrdersEntity o " +
"GROUP BY user " +
"ORDER BY totalSum DESC")
List<UsersGrouped> getFiveUsersHaveMaxSummPurchase(Pageable limit);
}
// вывести 5 пользователей с наибольшей суммой всех покупок на jpql

View File

@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.users.model.UsersEntity;
import com.example.demo.users.model.UsersGrouped;
import com.example.demo.users.repository.UsersRepository;
@Service
@ -58,4 +59,9 @@ public class UsersService {
repository.delete(existsEntity);
return existsEntity;
}
@Transactional(readOnly = true)
public List<UsersGrouped> getFiveUsers() {
return this.repository.getFiveUsersHaveMaxSummPurchase(PageRequest.of(0, 5));
}
}

View File

@ -1,5 +1,7 @@
package com.example.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
@ -40,6 +42,11 @@ class UsersOrdersServiceTests {
private ProductsEntity product;
private CategoriesEntity category;
private OrdersEntity order;
private SimpleDateFormat formater = new SimpleDateFormat("dd.MM.yyyy");
public String DateConvertString(Date date) {
return formater.format(date);
}
@BeforeEach
void createData() {
@ -53,10 +60,10 @@ class UsersOrdersServiceTests {
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));
new OrdersEntity(product, 3, "03.04.2024"),
new OrdersEntity(product, 2, "02.04.2024"),
new OrdersEntity(product, 1, "05.04.2024"),
new OrdersEntity(product, 4, "03.04.2024"));
orders.forEach(order -> ordersService.create(user1.getId(), order));
// order = ordersService.get(user1.getId(), 1);
@ -86,8 +93,9 @@ class UsersOrdersServiceTests {
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));
final Date dateEntity = entity.getDate();
final OrdersEntity newEntity = ordersService.update(user1.getId(), order.getId(),
new OrdersEntity(product, newCount, DateConvertString(dateEntity)));
Assertions.assertEquals(4, ordersService.getAll(user1.getId()).size());
Assertions.assertEquals(newEntity,
ordersService.get(user1.getId(), newEntity.getId()));
@ -113,7 +121,7 @@ class UsersOrdersServiceTests {
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));
final OrdersEntity newEntity = ordersService.create(user1.getId(), new OrdersEntity(product, 3, "03.04.2024"));
Assertions.assertEquals(4, ordersService.getAll(user1.getId()).size());
Assertions.assertNotEquals(order.getId(), newEntity.getId());
}