diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index d7e74aa..85779ac 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -13,10 +13,12 @@ import com.example.demo.games.model.GameEntity; import com.example.demo.games.service.GameService; import com.example.demo.genres.model.GenreEntity; import com.example.demo.genres.service.GenreService; -import com.example.demo.order.model.OrderEntity; -import com.example.demo.order.service.OrderService; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; import com.example.demo.types.model.TypeEntity; import com.example.demo.types.service.TypeService; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; @SpringBootApplication @@ -27,12 +29,14 @@ public class DemoApplication implements CommandLineRunner { private final GenreService genreService; private final GameService gameService; private final OrderService orderService; + private final UserService userService; - public DemoApplication(TypeService typeService, GenreService genreService, GameService gameService, OrderService orderService){ + public DemoApplication(TypeService typeService, GenreService genreService, GameService gameService, OrderService orderService, UserService userService){ this.typeService = typeService; this.gameService = gameService; this.genreService = genreService; this.orderService = orderService; + this.userService = userService; } @Override @@ -56,7 +60,9 @@ public class DemoApplication implements CommandLineRunner { games.add(game1); games.add(game2); - orderService.create(new OrderEntity(null,games)); + final var order = orderService.create(new OrderEntity(null,games)); + + userService.create(new UserEntity(null, order, "login1", "email@mail.com", "qwerty123")); } public static void main(String[] args) { diff --git a/src/main/java/com/example/demo/order/api/OrderController.java b/src/main/java/com/example/demo/orders/api/OrderController.java similarity index 95% rename from src/main/java/com/example/demo/order/api/OrderController.java rename to src/main/java/com/example/demo/orders/api/OrderController.java index 430663a..a8f5834 100644 --- a/src/main/java/com/example/demo/order/api/OrderController.java +++ b/src/main/java/com/example/demo/orders/api/OrderController.java @@ -1,4 +1,4 @@ -package com.example.demo.order.api; +package com.example.demo.orders.api; import org.modelmapper.ModelMapper; import org.springframework.web.bind.annotation.DeleteMapping; @@ -16,8 +16,8 @@ import java.util.List; import com.example.demo.core.configuration.Constants; import com.example.demo.games.model.GameEntity; import com.example.demo.games.service.GameService; -import com.example.demo.order.model.OrderEntity; -import com.example.demo.order.service.OrderService; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; import jakarta.validation.Valid; diff --git a/src/main/java/com/example/demo/order/api/OrderDto.java b/src/main/java/com/example/demo/orders/api/OrderDto.java similarity index 95% rename from src/main/java/com/example/demo/order/api/OrderDto.java rename to src/main/java/com/example/demo/orders/api/OrderDto.java index 4996655..44f93a3 100644 --- a/src/main/java/com/example/demo/order/api/OrderDto.java +++ b/src/main/java/com/example/demo/orders/api/OrderDto.java @@ -1,4 +1,4 @@ -package com.example.demo.order.api; +package com.example.demo.orders.api; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/example/demo/order/model/OrderEntity.java b/src/main/java/com/example/demo/orders/model/OrderEntity.java similarity index 96% rename from src/main/java/com/example/demo/order/model/OrderEntity.java rename to src/main/java/com/example/demo/orders/model/OrderEntity.java index 5dab932..b0ae692 100644 --- a/src/main/java/com/example/demo/order/model/OrderEntity.java +++ b/src/main/java/com/example/demo/orders/model/OrderEntity.java @@ -1,4 +1,4 @@ -package com.example.demo.order.model; +package com.example.demo.orders.model; import java.util.List; diff --git a/src/main/java/com/example/demo/order/repository/OrderRepository.java b/src/main/java/com/example/demo/orders/repository/OrderRepository.java similarity index 66% rename from src/main/java/com/example/demo/order/repository/OrderRepository.java rename to src/main/java/com/example/demo/orders/repository/OrderRepository.java index b3eec62..18272ed 100644 --- a/src/main/java/com/example/demo/order/repository/OrderRepository.java +++ b/src/main/java/com/example/demo/orders/repository/OrderRepository.java @@ -1,8 +1,8 @@ -package com.example.demo.order.repository; +package com.example.demo.orders.repository; import org.springframework.stereotype.Repository; import com.example.demo.core.repository.MapRepository; -import com.example.demo.order.model.OrderEntity; +import com.example.demo.orders.model.OrderEntity; @Repository diff --git a/src/main/java/com/example/demo/order/service/OrderService.java b/src/main/java/com/example/demo/orders/service/OrderService.java similarity index 91% rename from src/main/java/com/example/demo/order/service/OrderService.java rename to src/main/java/com/example/demo/orders/service/OrderService.java index 25360d2..6cf17ab 100644 --- a/src/main/java/com/example/demo/order/service/OrderService.java +++ b/src/main/java/com/example/demo/orders/service/OrderService.java @@ -1,4 +1,4 @@ -package com.example.demo.order.service; +package com.example.demo.orders.service; import org.springframework.stereotype.Service; @@ -9,8 +9,8 @@ import java.util.Optional; import com.example.demo.core.error.NotFoundException; import com.example.demo.games.service.GameService; -import com.example.demo.order.repository.OrderRepository; -import com.example.demo.order.model.OrderEntity; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.repository.OrderRepository; @Service public class OrderService { diff --git a/src/main/java/com/example/demo/users/api/UserController.java b/src/main/java/com/example/demo/users/api/UserController.java new file mode 100644 index 0000000..e9e23ca --- /dev/null +++ b/src/main/java/com/example/demo/users/api/UserController.java @@ -0,0 +1,75 @@ +package com.example.demo.users.api; + +import org.modelmapper.ModelMapper; +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.RestController; + +import java.util.List; + +import com.example.demo.core.configuration.Constants; +import com.example.demo.orders.service.OrderService; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; + +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; + } + + private UserDto toDto(UserEntity entity){ + var dto = new UserDto(); + dto.setId(entity.getId()); + 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; + } + + @GetMapping + public List getAll(){ + return userService.getAll().stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public UserDto get(@PathVariable(name = "id") Long id){ + return toDto(userService.get(id)); + } + + @PostMapping + public UserDto create(@RequestBody @Valid UserDto dto){ + return toDto(userService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto){ + return toDto(userService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public UserDto delete(@PathVariable(name = "id")Long id){ + return toDto(userService.delete(id)); + } +} diff --git a/src/main/java/com/example/demo/users/api/UserDto.java b/src/main/java/com/example/demo/users/api/UserDto.java new file mode 100644 index 0000000..f79067b --- /dev/null +++ b/src/main/java/com/example/demo/users/api/UserDto.java @@ -0,0 +1,56 @@ +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; + @NotBlank + private String login; + @NotBlank + private String email; + @NotBlank + private String password; + @NotNull + @Min(1) + private Long orderId; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public Long getId(){ + return id; + } + public void setId(Long id){ + this.id = id; + } + + public String getLogin(){ + return login; + } + public void setLogin(String login){ + this.login = login; + } + + public String getEmail(){ + return email; + } + public void setEmail(String email){ + this.email = email; + } + + public String getPassword(){ + return password; + } + public void setPassword(String password){ + this.password = password; + } + + public Long getOrderId(){ + return orderId; + } + public void setOrderId(Long orderId){ + this.orderId = orderId; + } +} diff --git a/src/main/java/com/example/demo/users/model/UserEntity.java b/src/main/java/com/example/demo/users/model/UserEntity.java new file mode 100644 index 0000000..560b0d2 --- /dev/null +++ b/src/main/java/com/example/demo/users/model/UserEntity.java @@ -0,0 +1,51 @@ +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){ + super(id); + this.login = login; + this.email = email; + this.password = password; + this.order = order; + } + + public String getLogin(){ + return login; + } + public void setLogin(String login){ + this.login = login; + } + + public String getEmail(){ + return email; + } + public void setEmail(String email){ + this.email = email; + } + + public String getPassword(){ + return password; + } + public void setPassword(String password){ + this.password = password; + } + + public OrderEntity getOrder(){ + return order; + } + public void setOrder(OrderEntity order){ + this.order = order; + } +} diff --git a/src/main/java/com/example/demo/users/repository/UserRepository.java b/src/main/java/com/example/demo/users/repository/UserRepository.java new file mode 100644 index 0000000..dbc0e52 --- /dev/null +++ b/src/main/java/com/example/demo/users/repository/UserRepository.java @@ -0,0 +1,11 @@ +package com.example.demo.users.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.core.repository.MapRepository; +import com.example.demo.users.model.UserEntity; + +@Repository +public class UserRepository extends MapRepository { + +} diff --git a/src/main/java/com/example/demo/users/service/UserService.java b/src/main/java/com/example/demo/users/service/UserService.java new file mode 100644 index 0000000..f539565 --- /dev/null +++ b/src/main/java/com/example/demo/users/service/UserService.java @@ -0,0 +1,45 @@ +package com.example.demo.users.service; + +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.repository.UserRepository; + +@Service +public class UserService { + private final UserRepository repository; + + public UserService(UserRepository repository){ + this.repository = repository; + } + + public List getAll(){ + return repository.getAll().stream().toList(); + } + + public UserEntity get(Long id){ + return Optional.ofNullable(repository.get(id)).orElseThrow(()-> new NotFoundException(id)); + } + + public UserEntity create(UserEntity entity){ + return repository.create(entity); + } + + public UserEntity update(Long id, UserEntity entity){ + final UserEntity existEntity = get(id); + existEntity.setLogin(entity.getLogin()); + existEntity.setEmail(entity.getEmail()); + existEntity.setPassword(entity.getPassword()); + existEntity.setOrder(entity.getOrder()); + return repository.update(existEntity); + } + + public UserEntity delete(Long id){ + final UserEntity existEntity = get(id); + return repository.delete(existEntity); + } +}