diff --git a/src/main/kotlin/hs/kr/equus/feed/domain/notice/domain/Notice.kt b/src/main/kotlin/hs/kr/equus/feed/domain/notice/domain/Notice.kt index 800b624..2c76e28 100644 --- a/src/main/kotlin/hs/kr/equus/feed/domain/notice/domain/Notice.kt +++ b/src/main/kotlin/hs/kr/equus/feed/domain/notice/domain/Notice.kt @@ -13,22 +13,38 @@ class Notice( id: UUID? = null, @Column(name = "title", length = 100, nullable = false) - val title: String, + var title: String, @Column(name = "content", length = 5000, nullable = false) - val content: String, + var content: String, - @Column(name = "file_name") - val fileName: String, + @Column(name = "file_name", nullable = true) + var fileName: String? = null, - @Column(name = "admin_name", nullable = false, columnDefinition = "BINARY(16)") - val adminId: UUID, + @Column(name = "admin_id", nullable = false, columnDefinition = "BINARY(16)") + var adminId: UUID, @Column(nullable = false) - val isPinned: Boolean, + var isPinned: Boolean, @Column(nullable = false) @Enumerated(value = EnumType.STRING) - val type: NoticeType - -) : BaseEntity(id) + var type: NoticeType + +) : BaseEntity(id) { + fun modifyNotice( + title: String, + content: String, + isPinned: Boolean, + adminId: UUID, + fileName: String, + type: NoticeType + ) { + this.title = title + this.content = content + this.type = type + this.isPinned = isPinned + this.adminId = adminId + this.fileName = fileName + } +} diff --git a/src/main/kotlin/hs/kr/equus/feed/domain/notice/exception/NoticeNotFoundException.kt b/src/main/kotlin/hs/kr/equus/feed/domain/notice/exception/NoticeNotFoundException.kt new file mode 100644 index 0000000..650c49c --- /dev/null +++ b/src/main/kotlin/hs/kr/equus/feed/domain/notice/exception/NoticeNotFoundException.kt @@ -0,0 +1,8 @@ +package hs.kr.equus.feed.domain.notice.exception + +import hs.kr.equus.feed.global.error.exception.EquusException +import hs.kr.equus.feed.global.error.exception.ErrorCode + +object NoticeNotFoundException : EquusException( + ErrorCode.NOTICE_NOT_FOUND +) diff --git a/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/NoticeController.kt b/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/NoticeController.kt index 0906656..cf761c5 100644 --- a/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/NoticeController.kt +++ b/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/NoticeController.kt @@ -1,11 +1,15 @@ package hs.kr.equus.feed.domain.notice.presentation import hs.kr.equus.feed.domain.notice.presentation.dto.request.CreateNoticeRequest +import hs.kr.equus.feed.domain.notice.presentation.dto.request.ModifyNoticeRequest import hs.kr.equus.feed.domain.notice.service.CreateNoticeService +import hs.kr.equus.feed.domain.notice.service.ModifyNoticeService import hs.kr.equus.feed.domain.notice.service.QueryNoticeTitleService import hs.kr.equus.feed.domain.notice.service.UploadNoticeImageService import org.springframework.http.HttpStatus import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PatchMapping +import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping @@ -13,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestPart import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.RestController import org.springframework.web.multipart.MultipartFile +import java.util.UUID import javax.validation.Valid @RestController @@ -20,6 +25,7 @@ import javax.validation.Valid class NoticeController( private val createNoticeService: CreateNoticeService, private val uploadNoticeImageService: UploadNoticeImageService, + private val modifyNoticeService: ModifyNoticeService, private val queryNoticeTitleService: QueryNoticeTitleService ) { @@ -32,6 +38,13 @@ class NoticeController( createNoticeService.execute(createNoticeRequest) } + @PatchMapping("/{notice-id}") + fun modifyNotice( + @PathVariable(name = "notice-id") id: UUID, + @RequestBody modifyNoticeRequest: ModifyNoticeRequest + ) = + modifyNoticeService.execute(id, modifyNoticeRequest) + @PostMapping("/image") fun uploadImage( @RequestPart(name = "photo") image: MultipartFile diff --git a/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/dto/request/CreateNoticeRequest.kt b/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/dto/request/CreateNoticeRequest.kt index a4f18b1..e9f0c89 100644 --- a/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/dto/request/CreateNoticeRequest.kt +++ b/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/dto/request/CreateNoticeRequest.kt @@ -15,11 +15,11 @@ data class CreateNoticeRequest( @Size(max = 5000, message = "content은 최대 5000자까지 가능합니다.") val content: String, - @field:NotNull(message = "Pinned은 null일수가 없습니다") + @field:NotNull(message = "Pinned는 null일수가 없습니다") val isPinned: Boolean, @field:NotNull(message = "type은 null일수가 없습니다") val type: NoticeType, - val fileName: String + val fileName: String? = null ) diff --git a/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/dto/request/ModifyNoticeRequest.kt b/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/dto/request/ModifyNoticeRequest.kt new file mode 100644 index 0000000..b13d6d0 --- /dev/null +++ b/src/main/kotlin/hs/kr/equus/feed/domain/notice/presentation/dto/request/ModifyNoticeRequest.kt @@ -0,0 +1,24 @@ +package hs.kr.equus.feed.domain.notice.presentation.dto.request + +import hs.kr.equus.feed.domain.notice.domain.type.NoticeType +import javax.validation.constraints.NotBlank +import javax.validation.constraints.NotNull +import javax.validation.constraints.Size + +data class ModifyNoticeRequest( + @field:NotBlank(message = "title은 null, 공백, 띄어쓰기를 허용하지 않습니다.") + @Size(max = 100, message = "title은 최대 100자까지 가능합니다.") + val title: String, + + @field:NotBlank(message = "content은 null, 공백, 띄어쓰기를 허용하지 않습니다.") + @Size(max = 5000, message = "content은 최대 5000자까지 가능합니다.") + val content: String, + + @field:NotNull(message = "Pinned은 null일수가 없습니다") + val isPinned: Boolean, + + @field:NotNull(message = "type은 null일수가 없습니다") + val type: NoticeType, + + val fileName: String? = null +) diff --git a/src/main/kotlin/hs/kr/equus/feed/domain/notice/service/CreateNoticeService.kt b/src/main/kotlin/hs/kr/equus/feed/domain/notice/service/CreateNoticeService.kt index f752e41..253982e 100644 --- a/src/main/kotlin/hs/kr/equus/feed/domain/notice/service/CreateNoticeService.kt +++ b/src/main/kotlin/hs/kr/equus/feed/domain/notice/service/CreateNoticeService.kt @@ -27,7 +27,6 @@ class CreateNoticeService( isPinned = request.isPinned, adminId = admin, fileName = request.fileName - ) ) } diff --git a/src/main/kotlin/hs/kr/equus/feed/domain/notice/service/ModifyNoticeService.kt b/src/main/kotlin/hs/kr/equus/feed/domain/notice/service/ModifyNoticeService.kt new file mode 100644 index 0000000..0251182 --- /dev/null +++ b/src/main/kotlin/hs/kr/equus/feed/domain/notice/service/ModifyNoticeService.kt @@ -0,0 +1,40 @@ +package hs.kr.equus.feed.domain.notice.service + +import hs.kr.equus.feed.domain.notice.domain.repository.NoticeRepository +import hs.kr.equus.feed.domain.notice.exception.NoticeNotFoundException +import hs.kr.equus.feed.domain.notice.presentation.dto.request.ModifyNoticeRequest +import hs.kr.equus.feed.global.utils.user.UserUtils +import hs.kr.equus.feed.infrastructure.s3.service.S3Service +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import java.util.UUID + +@Service +class ModifyNoticeService( + private val noticeRepository: NoticeRepository, + private val userUtils: UserUtils, + private val s3Service: S3Service +) { + companion object { + const val PATH = "notice/" + } + + @Transactional + fun execute(id: UUID, request: ModifyNoticeRequest): String { + val adminId = userUtils.getCurrentUser().id + val notice = noticeRepository.findByIdOrNull(id) ?: throw NoticeNotFoundException + + request.run { + notice.modifyNotice( + title = title, + content = content, + isPinned = isPinned, + type = type, + fileName = request.fileName, + adminId = adminId + ) + } + return s3Service.generateObjectUrl(request.fileName, PATH) + } +} diff --git a/src/main/kotlin/hs/kr/equus/feed/global/error/exception/ErrorCode.kt b/src/main/kotlin/hs/kr/equus/feed/global/error/exception/ErrorCode.kt index be4847d..64b47d2 100644 --- a/src/main/kotlin/hs/kr/equus/feed/global/error/exception/ErrorCode.kt +++ b/src/main/kotlin/hs/kr/equus/feed/global/error/exception/ErrorCode.kt @@ -26,6 +26,7 @@ enum class ErrorCode( QUESTION_NOT_FOUND(404, "Question Not Found"), REPLY_NOT_FOUND(404, "Reply Not Found"), FAQ_NOT_FOUND(404, "Faq Not Found"), + NOTICE_NOT_FOUND(404, "Notice Not Found"), // Bad Request FILE_IS_EMPTY(400, "File does not exist"),