From 01accbaea179912395b7ec3d2a3eee4b9e98340f Mon Sep 17 00:00:00 2001 From: sinkyoungdeok Date: Sun, 26 May 2024 07:02:56 +0900 Subject: [PATCH] =?UTF-8?q?[KAN-000]=20=EC=9C=A0=EC=A0=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../restaurant/be/user/domain/entity/User.kt | 5 +++ .../user/domain/service/DeleteUserService.kt | 19 +++++++++++ .../controller/DeleteUserController.kt | 32 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/main/kotlin/com/restaurant/be/user/domain/service/DeleteUserService.kt create mode 100644 src/main/kotlin/com/restaurant/be/user/presentation/controller/DeleteUserController.kt diff --git a/src/main/kotlin/com/restaurant/be/user/domain/entity/User.kt b/src/main/kotlin/com/restaurant/be/user/domain/entity/User.kt index 4e99528..aaaf0c3 100644 --- a/src/main/kotlin/com/restaurant/be/user/domain/entity/User.kt +++ b/src/main/kotlin/com/restaurant/be/user/domain/entity/User.kt @@ -45,4 +45,9 @@ class User( this.nickname = request.nickname this.profileImageUrl = request.profileImageUrl } + + fun delete() { + this.withdrawal = true + this.email = "" + } } diff --git a/src/main/kotlin/com/restaurant/be/user/domain/service/DeleteUserService.kt b/src/main/kotlin/com/restaurant/be/user/domain/service/DeleteUserService.kt new file mode 100644 index 0000000..7c6c02e --- /dev/null +++ b/src/main/kotlin/com/restaurant/be/user/domain/service/DeleteUserService.kt @@ -0,0 +1,19 @@ +package com.restaurant.be.user.domain.service + +import com.restaurant.be.common.exception.NotFoundUserException +import com.restaurant.be.user.repository.UserRepository +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class DeleteUserService( + private val userRepository: UserRepository +) { + @Transactional + fun deleteUser(email: String) { + val user = userRepository.findByEmail(email) ?: throw NotFoundUserException() + + user.delete() + userRepository.save(user) + } +} diff --git a/src/main/kotlin/com/restaurant/be/user/presentation/controller/DeleteUserController.kt b/src/main/kotlin/com/restaurant/be/user/presentation/controller/DeleteUserController.kt new file mode 100644 index 0000000..0ae255a --- /dev/null +++ b/src/main/kotlin/com/restaurant/be/user/presentation/controller/DeleteUserController.kt @@ -0,0 +1,32 @@ +package com.restaurant.be.user.presentation.controller + +import com.restaurant.be.common.response.CommonResponse +import com.restaurant.be.user.domain.service.DeleteUserService +import io.swagger.annotations.Api +import io.swagger.annotations.ApiOperation +import io.swagger.v3.oas.annotations.responses.ApiResponse +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import java.security.Principal + +@Api(tags = ["01. User Info"], description = "유저 서비스") +@RestController +@RequestMapping("/v1/users") +class DeleteUserController( + private val deleteUserService: DeleteUserService +) { + + @DeleteMapping + @PreAuthorize("hasRole('USER')") + @ApiOperation(value = "유저 삭제 API") + @ApiResponse( + responseCode = "200", + description = "성공" + ) + fun deleteUser(principal: Principal): CommonResponse { + deleteUserService.deleteUser(principal.name) + return CommonResponse.success() + } +}