From b50a31d9f2fbe8cf46207c3c33d06a6b33e41315 Mon Sep 17 00:00:00 2001 From: ekallin Date: Mon, 18 Mar 2024 12:11:59 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D1=8E=D0=B7?= =?UTF-8?q?=D0=B5=D1=80=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/users/api/UserController.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 backend/src/main/java/com/example/backend/users/api/UserController.java diff --git a/backend/src/main/java/com/example/backend/users/api/UserController.java b/backend/src/main/java/com/example/backend/users/api/UserController.java new file mode 100644 index 0000000..3145f78 --- /dev/null +++ b/backend/src/main/java/com/example/backend/users/api/UserController.java @@ -0,0 +1,66 @@ +package com.example.backend.users.api; + +import java.util.List; + +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 com.example.backend.core.configurations.Constants; +import com.example.backend.users.model.UserEntity; +import com.example.backend.users.service.UserService; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/user") +public class UserController { + + private final UserService userService; + private final ModelMapper modelMapper; + + public UserController(UserService userService, ModelMapper modelMapper) { + this.modelMapper = modelMapper; + this.userService = userService; + } + + private UserEntity toEntity(UserDTO dto) { + return modelMapper.map(dto, UserEntity.class); + } + + private UserDTO toDto(UserEntity entity) { + return modelMapper.map(entity, UserDTO.class); + } + + @GetMapping() + public List getAll() { + return userService.getAll().stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public UserDTO get(@PathVariable(name = "id") Integer id) { + return toDto(userService.get(id)); + } + + @PostMapping + public UserDTO create(@RequestBody @Valid UserDTO userDTO) { + + return toDto(userService.create(toEntity(userDTO))); + } + + @PutMapping("/{id}") + public UserDTO update(@PathVariable(name = "id") Integer id, @RequestBody UserDTO userDTO) { + return toDto(userService.update(id, toEntity(userDTO))); + } + + @DeleteMapping("/{id}") + public UserDTO delete(@PathVariable(name = "id") Integer id) { + return toDto(userService.delete(id)); + } +}