/
human
/
Mini-bank
Обзор
Документация
Войти
/
human
/
Mini-bank
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
clients/src/main/java/org/example/controller/UserController.java
119 строк
7 KB
gbb
Fix code for demonstration
07 фев 2025, 08:30
07 фев 2025, 08:30
d08673a
Код
Авторство
О чём код?
package org.example.controller; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; import java.util.UUID; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.example.dto.userinfo.UserInfoCreateDTO; import org.example.dto.userinfo.UserInfoDTO; import org.example.dto.userinfo.UserInfoUpdateDTO; import org.example.dto.userinfo.UserLoginDTO; import org.example.service.UserService; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.web.ErrorResponse; 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; import org.springframework.web.bind.annotation.RestController; /** * Контроллер сервиса управления данными пользователей. */ @RequiredArgsConstructor @RestController @RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE) @Tag(name = "Пользовательский сервис.", description = "Сервис для взаимодействия с пользовательскими данными.") @Slf4j public class UserController { private final UserService userService; @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Регистрация нового пользователя", description = "Регистрация нового пользователя с использованием предоставленных пользовательских данных." ) @ApiResponses( value = { @ApiResponse(responseCode = "201", description = "Новый пользователь успешно зарегистрирован", content = {@Content(schema = @Schema(implementation = UserInfoDTO.class))}), @ApiResponse(responseCode = "400", description = "Указаны неверные параметры") }) public UserInfoDTO create(@Valid @RequestBody UserInfoCreateDTO dto, HttpServletResponse response) { log.info("Входящие данные для создания пользователя: {}", dto.toString()); response.setStatus(HttpStatus.CREATED.value()); log.info("Ответ (статус): {}", response.getStatus()); return userService.createUser(dto); } @GetMapping @Operation(summary = "Получить сведения о пользователе", description = "Вернуть информацию о зарегистрированном пользователе.", security = {@SecurityRequirement(name = "bearerAuth")} ) @ApiResponses( value = { @ApiResponse(responseCode = "200", description = "Найдена информация о пользователе", content = {@Content(schema = @Schema(implementation = UserInfoDTO.class))}), @ApiResponse(responseCode = "404", description = "Пользователь не найден", content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}) }) public UserInfoDTO getInfo(@RequestParam(name = "id") UUID userId) { return userService.getUserInfo(userId); } @GetMapping("/{username}") public UserLoginDTO getUser(@PathVariable(name = "username") String username) { return userService.getUserInfo(username); } @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Обновить зарегистрированного пользователя", description = "Обновляет информацию о зарегистрированном пользователе.", security = {@SecurityRequirement(name = "bearerAuth")} ) @ApiResponses( value = { @ApiResponse(responseCode = "200", description = "Пользователь успешно обновлен", content = {@Content(schema = @Schema(implementation = UserInfoDTO.class))}), @ApiResponse(responseCode = "400", description = "Указаны неправильные параметры", content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}), @ApiResponse(responseCode = "404", description = "Пользователь не найден", content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}) }) public UserInfoDTO update(@Valid @RequestParam(name = "id") UUID userId, @RequestBody UserInfoUpdateDTO userInfo) { return userService.updateUserInfo(userId, userInfo); } @DeleteMapping @Operation(summary = "Удаление зарегистрированного пользователя", description = "Удаление зарегистрированного пользователя и закрытие его счетов.", security = {@SecurityRequirement(name = "bearerAuth")} ) @ApiResponses( value = { @ApiResponse(responseCode = "200", description = "Пользователь удален", content = {@Content(schema = @Schema(implementation = UserInfoDTO.class))}), @ApiResponse(responseCode = "404", description = "Пользователь не найден", content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}) }) public UserInfoDTO delete(@RequestParam(name = "id") UUID userId, @AuthenticationPrincipal Jwt token) { return userService.deleteInfo(userId, token); } }