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() + } +}