Готовая 2 лаба
This commit is contained in:
parent
4d5ff0399a
commit
3c6cc35264
@ -60,9 +60,11 @@ public class DemoApplication implements CommandLineRunner {
|
||||
games.add(game1);
|
||||
games.add(game2);
|
||||
|
||||
final var order = orderService.create(new OrderEntity(null,games));
|
||||
userService.create(new UserEntity(null, "login1", "email@mail.com", "qwerty123"));
|
||||
userService.create(new UserEntity(null, "login2", "email@gmail.com", "qwerty1234"));
|
||||
|
||||
userService.create(new UserEntity(null, order, "login1", "email@mail.com", "qwerty123"));
|
||||
orderService.create(new OrderEntity(null, 1L,games));
|
||||
orderService.create(new OrderEntity(null, 2L,games));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -5,7 +5,6 @@ import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
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;
|
||||
@ -18,6 +17,7 @@ import com.example.demo.games.model.GameEntity;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
import com.example.demo.orders.service.OrderService;
|
||||
import com.example.demo.users.service.UserService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@ -27,11 +27,13 @@ public class OrderController {
|
||||
private final GameService gameService;
|
||||
private final ModelMapper modelMapper;
|
||||
private final OrderService orderService;
|
||||
//private final UserService userService;
|
||||
|
||||
public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService){
|
||||
public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService, UserService userService){
|
||||
this.gameService = gameService;
|
||||
this.modelMapper = modelMapper;
|
||||
this.orderService = orderService;
|
||||
//this.userService = userService;
|
||||
}
|
||||
|
||||
private OrderDto toDto(OrderEntity entity){
|
||||
@ -41,6 +43,7 @@ public class OrderController {
|
||||
for(var game : entity.getGames()){
|
||||
sum += game.getPrice();
|
||||
}
|
||||
dto.setUserId(entity.getUserId());
|
||||
dto.setSum(sum);
|
||||
dto.setGames(entity.getGames().stream().map(GameEntity::getId).toList());
|
||||
return dto;
|
||||
@ -56,8 +59,8 @@ public class OrderController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OrderDto> getAll(@RequestParam(name = "games", defaultValue = "") List<Long> games){
|
||||
return orderService.getAll(games).stream().map(this::toDto).toList();
|
||||
public List<OrderDto> getAll(@RequestParam(name = "userId", defaultValue = "") Long userId){
|
||||
return orderService.getAll(userId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ -70,11 +73,6 @@ public class OrderController {
|
||||
return toDto(orderService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public OrderDto update(@PathVariable(name = "id") Long id, @RequestBody OrderDto dto){
|
||||
return toDto(orderService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public OrderDto delete(@PathVariable(name = "id") Long id){
|
||||
return toDto(orderService.delete(id));
|
||||
|
@ -14,6 +14,9 @@ public class OrderDto {
|
||||
@Min(1)
|
||||
private double sum;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long userId;
|
||||
@NotNull
|
||||
private final List<Long> games = new ArrayList<>();
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
@ -39,4 +42,12 @@ public class OrderDto {
|
||||
this.games.clear();
|
||||
this.games.addAll(games);
|
||||
}
|
||||
|
||||
public Long getUserId(){
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId){
|
||||
this.userId = userId;
|
||||
}
|
||||
}
|
||||
|
@ -11,14 +11,16 @@ import com.example.demo.games.model.GameEntity;
|
||||
|
||||
public class OrderEntity extends BaseEntity{
|
||||
private double sum;
|
||||
private Long userId;
|
||||
private final List<GameEntity> games = new ArrayList<>();
|
||||
|
||||
public OrderEntity(){
|
||||
super();
|
||||
}
|
||||
|
||||
public OrderEntity(Long id, List<GameEntity> games){
|
||||
public OrderEntity(Long id, Long userId, List<GameEntity> games){
|
||||
super(id);
|
||||
this.userId = userId;
|
||||
this.games.clear();
|
||||
this.games.addAll(games);
|
||||
}
|
||||
@ -32,10 +34,16 @@ public class OrderEntity extends BaseEntity{
|
||||
return sum;
|
||||
}
|
||||
|
||||
public Long getUserId(){
|
||||
return userId;
|
||||
}
|
||||
public void setUserId(Long userId){
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public List<GameEntity> getGames(){
|
||||
return games;
|
||||
}
|
||||
|
||||
public void setGames(GameEntity game){
|
||||
this.games.add(game);
|
||||
}
|
||||
|
@ -3,32 +3,24 @@ package com.example.demo.orders.service;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
import com.example.demo.orders.repository.OrderRepository;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
private final OrderRepository repository;
|
||||
private final GameService gameService;
|
||||
|
||||
public OrderService(OrderRepository repository, GameService gameService){
|
||||
public OrderService(OrderRepository repository){
|
||||
this.repository = repository;
|
||||
this.gameService = gameService;
|
||||
}
|
||||
|
||||
public List<OrderEntity> getAll(List<Long> games){
|
||||
var gamesEnt = new ArrayList<>();
|
||||
for(var gameId : games){
|
||||
gamesEnt.add(gameService.get(gameId));
|
||||
}
|
||||
if(!Objects.equals(gamesEnt.size(), 0)){
|
||||
return repository.getAll().stream().filter(game -> game.getGames().containsAll(gamesEnt)).toList();
|
||||
public List<OrderEntity> getAll(Long userId){
|
||||
if(!Objects.equals(userId, null)){
|
||||
return repository.getAll().stream().filter(user -> user.getId().equals(userId)).toList();
|
||||
}
|
||||
return repository.getAll();
|
||||
}
|
||||
@ -41,15 +33,6 @@ public class OrderService {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public OrderEntity update(Long id, OrderEntity entity){
|
||||
final OrderEntity existEntity = get(id);
|
||||
var games = entity.getGames();
|
||||
for(var game : games){
|
||||
existEntity.setGames(game);
|
||||
}
|
||||
return repository.update(existEntity);
|
||||
}
|
||||
|
||||
public OrderEntity delete(Long id){
|
||||
final OrderEntity exisEntity = get(id);
|
||||
return repository.delete(exisEntity);
|
||||
|
@ -22,13 +22,11 @@ import jakarta.validation.Valid;
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL+"/user")
|
||||
public class UserController {
|
||||
private final OrderService orderService;
|
||||
private final ModelMapper modelMapper;
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(OrderService orderService, ModelMapper modelMapper, UserService userService){
|
||||
this.modelMapper = modelMapper;
|
||||
this.orderService = orderService;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@ -38,13 +36,11 @@ public class UserController {
|
||||
dto.setLogin(entity.getLogin());
|
||||
dto.setEmail(entity.getEmail());
|
||||
dto.setPassword(entity.getPassword());
|
||||
dto.setOrderId(entity.getOrder().getId());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private UserEntity toEntity(UserDto dto){
|
||||
final UserEntity entity = modelMapper.map(dto, UserEntity.class);
|
||||
entity.setOrder(orderService.get(dto.getOrderId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
@ -2,9 +2,7 @@ package com.example.demo.users.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class UserDto {
|
||||
private Long id;
|
||||
@ -14,9 +12,6 @@ public class UserDto {
|
||||
private String email;
|
||||
@NotBlank
|
||||
private String password;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long orderId;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId(){
|
||||
@ -46,11 +41,4 @@ public class UserDto {
|
||||
public void setPassword(String password){
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Long getOrderId(){
|
||||
return orderId;
|
||||
}
|
||||
public void setOrderId(Long orderId){
|
||||
this.orderId = orderId;
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,21 @@
|
||||
package com.example.demo.users.model;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
|
||||
public class UserEntity extends BaseEntity{
|
||||
private String login;
|
||||
private String email;
|
||||
private String password;
|
||||
private OrderEntity order;
|
||||
|
||||
public UserEntity(){
|
||||
super();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, OrderEntity order, String login, String email, String password){
|
||||
public UserEntity(Long id, String login, String email, String password){
|
||||
super(id);
|
||||
this.login = login;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public String getLogin(){
|
||||
@ -41,11 +38,4 @@ public class UserEntity extends BaseEntity{
|
||||
public void setPassword(String password){
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public OrderEntity getOrder(){
|
||||
return order;
|
||||
}
|
||||
public void setOrder(OrderEntity order){
|
||||
this.order = order;
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,6 @@ public class UserService {
|
||||
existEntity.setLogin(entity.getLogin());
|
||||
existEntity.setEmail(entity.getEmail());
|
||||
existEntity.setPassword(entity.getPassword());
|
||||
existEntity.setOrder(entity.getOrder());
|
||||
return repository.update(existEntity);
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,8 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
@ -11,6 +14,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import com.example.demo.genres.model.GenreEntity;
|
||||
|
||||
|
||||
@SpringBootTest
|
||||
@ -18,36 +22,41 @@ import com.example.demo.games.service.GameService;
|
||||
class GameServiceTests {
|
||||
@Autowired
|
||||
private GameService gameService;
|
||||
List<GenreEntity> list = new ArrayList<GenreEntity>();
|
||||
List<Long> list2 = new ArrayList<Long>();
|
||||
|
||||
@Test
|
||||
void getTest(){
|
||||
Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L));
|
||||
}
|
||||
|
||||
// @Test
|
||||
// @Order(1)
|
||||
// void createTest(){
|
||||
// gameService.create(new GameEntity(null, null, "Game", 210.0, "cool game", null));
|
||||
// gameService.create(new GameEntity(null, null, "VideoGame", 2100.0, "very cool game", null));
|
||||
// Assertions.assertEquals(2, gameService.getAll(0L,0L).size());
|
||||
// }
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest(){
|
||||
|
||||
// @Test
|
||||
// @Order(2)
|
||||
// void updateTest(){
|
||||
// final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", null);
|
||||
// final GameEntity updGame = gameService.update(2L, newGame);
|
||||
// Assertions.assertEquals(2, gameService.getAll(0L, 0L).size());
|
||||
// Assertions.assertEquals(updGame, gameService.get(2L));
|
||||
// Assertions.assertEquals(newGame.getName(), updGame.getName());
|
||||
// Assertions.assertEquals(newGame.getDescription(), updGame.getDescription());
|
||||
// Assertions.assertEquals(newGame.getPrice(), updGame.getPrice());
|
||||
// }
|
||||
list.add(new GenreEntity(null, "приключения"));
|
||||
list.add(new GenreEntity(null, "Симулятор"));
|
||||
gameService.create(new GameEntity(null, null, "Game", 210.0, "cool game", list));
|
||||
gameService.create(new GameEntity(null, null, "VideoGame", 2100.0, "very cool game", list));
|
||||
Assertions.assertEquals(4, gameService.getAll(0L,list2).size());
|
||||
}
|
||||
|
||||
// @Test
|
||||
// @Order(3)
|
||||
// void deleteTest(){
|
||||
// gameService.delete(2L);
|
||||
// Assertions.assertEquals(1L, gameService.getAll(0L,0L).size());
|
||||
// }
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest(){
|
||||
final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", list);
|
||||
final GameEntity updGame = gameService.update(2L, newGame);
|
||||
Assertions.assertEquals(4, gameService.getAll(0L, list2).size());
|
||||
Assertions.assertEquals(updGame, gameService.get(2L));
|
||||
Assertions.assertEquals(newGame.getName(), updGame.getName());
|
||||
Assertions.assertEquals(newGame.getDescription(), updGame.getDescription());
|
||||
Assertions.assertEquals(newGame.getPrice(), updGame.getPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest(){
|
||||
gameService.delete(2L);
|
||||
Assertions.assertEquals(3L, gameService.getAll(0L,list2).size());
|
||||
}
|
||||
}
|
||||
|
52
src/test/java/com/example/demo/OrderServiceTest.java
Normal file
52
src/test/java/com/example/demo/OrderServiceTest.java
Normal file
@ -0,0 +1,52 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
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.games.model.GameEntity;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
import com.example.demo.orders.service.OrderService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class OrderServiceTest {
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
@Autowired
|
||||
private GameService gameService;
|
||||
|
||||
@Test
|
||||
void getTest(){
|
||||
Assertions.assertThrows(NotFoundException.class, () -> orderService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest(){
|
||||
final List<GameEntity> games = new ArrayList<GameEntity>();
|
||||
games.add(gameService.get(1L));
|
||||
orderService.create(new OrderEntity(null, 1L,games));
|
||||
final var last = orderService.create(new OrderEntity(null, 2L,games));
|
||||
Assertions.assertEquals(4, orderService.getAll(null).size());
|
||||
Assertions.assertEquals(last, orderService.get(4L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void deleteTest(){
|
||||
orderService.delete(2L);
|
||||
Assertions.assertEquals(3, orderService.getAll(null).size());
|
||||
final OrderEntity last = orderService.get(1L);
|
||||
Assertions.assertEquals(1L, last.getId());
|
||||
}
|
||||
}
|
@ -29,30 +29,30 @@ class TypeServiceTests {
|
||||
typeService.create(new TypeEntity(null, "Игра"));
|
||||
typeService.create(new TypeEntity(null, "Программа"));
|
||||
final TypeEntity last = typeService.create(new TypeEntity(null, "Игра2"));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(last, typeService.get(3L));
|
||||
Assertions.assertEquals(5, typeService.getAll().size());
|
||||
Assertions.assertEquals(last, typeService.get(5L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest(){
|
||||
final String test = "TEST";
|
||||
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, typeService.get(3L));
|
||||
final TypeEntity newEntity = typeService.update(5L, new TypeEntity(1L, test));
|
||||
Assertions.assertEquals(5, typeService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, typeService.get(5L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest(){
|
||||
typeService.delete(3L);
|
||||
Assertions.assertEquals(2, typeService.getAll().size());
|
||||
final TypeEntity last = typeService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
typeService.delete(5L);
|
||||
Assertions.assertEquals(4, typeService.getAll().size());
|
||||
final TypeEntity last = typeService.get(4L);
|
||||
Assertions.assertEquals(4L, last.getId());
|
||||
|
||||
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игра"));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(4L, newEntity.getId());
|
||||
Assertions.assertEquals(5, typeService.getAll().size());
|
||||
Assertions.assertEquals(6L, newEntity.getId());
|
||||
}
|
||||
}
|
||||
|
48
src/test/java/com/example/demo/UserServiceTest.java
Normal file
48
src/test/java/com/example/demo/UserServiceTest.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
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.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UserService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class UserServiceTest {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
@Test
|
||||
void getTest(){
|
||||
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest(){
|
||||
userService.create(new UserEntity(null, "login5", "gmail", "qwert123"));
|
||||
final UserEntity last = userService.create(new UserEntity(null, "login6", "mail", "qwer123"));
|
||||
Assertions.assertEquals(4, userService.getAll().size());
|
||||
Assertions.assertEquals(last, userService.get(4L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest(){
|
||||
userService.update(4L, new UserEntity(null,"login","email", "qwe123"));
|
||||
Assertions.assertEquals(4, userService.getAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest(){
|
||||
userService.delete(2L);
|
||||
Assertions.assertEquals(3, userService.getAll().size());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user