Skip to content

Commit

Permalink
Feat :: 룸 삭제 완성
Browse files Browse the repository at this point in the history
  • Loading branch information
gwon11225 committed Jun 13, 2024
1 parent 63165c9 commit 047fe28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/main/kotlin/com/teamapi/palette/controller/RoomController.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.teamapi.palette.controller

import com.azure.core.annotation.PathParam
import com.teamapi.palette.response.Response
import com.teamapi.palette.response.ResponseBody
import com.teamapi.palette.service.RoomService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Flux

@RestController
Expand All @@ -22,4 +19,8 @@ class RoomController (
@GetMapping("/list")
fun getRoomList() = roomService.getRoomList()
.map { ResponseBody.ok("룸 조회 성공", it) }

@DeleteMapping("/{roomId}")
fun deleteRoom(@PathVariable roomId: Long) = roomService.deleteRoom(roomId)
.thenReturn(Response.ok("룸 삭제 완료"))
}
4 changes: 3 additions & 1 deletion src/main/kotlin/com/teamapi/palette/response/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ enum class ErrorCode(
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "잘못된 파라미터"),
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "허용되지 않은 요청 메소드"),
MEDIA_TYPE_NOT_SUPPORTED(HttpStatus.BAD_REQUEST, "허용되지 않은 미디어 자료형"),
BAD_REQUEST(HttpStatus.BAD_REQUEST, "올바르지 않은 요청")
BAD_REQUEST(HttpStatus.BAD_REQUEST, "올바르지 않은 요청"),
ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 룸을 찾을 수 없음"),
FORBIDDEN(HttpStatus.FORBIDDEN, "권한 없음")
;

override fun getName() = name
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/com/teamapi/palette/service/RoomService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import com.teamapi.palette.dto.room.RoomResponse
import com.teamapi.palette.entity.Room
import com.teamapi.palette.repository.RoomRepository
import com.teamapi.palette.repository.UserRepository
import com.teamapi.palette.response.ErrorCode
import com.teamapi.palette.response.exception.CustomException
import com.teamapi.palette.util.findUser
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.switchIfEmpty

@Service
class RoomService (
Expand All @@ -33,4 +36,22 @@ class RoomService (
}
.collectList()
}

fun deleteRoom(roomId: Long): Mono<Void> {
return sessionHolder
.me()
.findUser(userRepository)
.flatMap { user ->
roomRepository.findById(roomId)
.switchIfEmpty {
Mono.error(CustomException(ErrorCode.ROOM_NOT_FOUND))
}
.flatMap { room ->
if (room.userId == user.id)
roomRepository.delete(room)
else
Mono.error(CustomException(ErrorCode.FORBIDDEN))
}
}
}
}

0 comments on commit 047fe28

Please sign in to comment.