Skip to content

Commit

Permalink
feat: deleteNotice 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
skfotakf committed Jul 26, 2023
1 parent 8d8d5cd commit 0382376
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.wafflestudio.csereal.core.notice.dto.NoticeDto
import com.wafflestudio.csereal.core.notice.dto.UpdateNoticeRequest
import com.wafflestudio.csereal.core.notice.service.NoticeService
import jakarta.validation.Valid
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RequestMapping("/notice")
Expand Down Expand Up @@ -33,4 +35,12 @@ class NoticeController(
) : NoticeDto {
return noticeService.updateNotice(noticeId, request)
}

@DeleteMapping("/{noticeId}")
fun deleteNotice(
@PathVariable noticeId: Long
) : ResponseEntity<String> {
noticeService.deleteNotice(noticeId)
return ResponseEntity<String>("삭제되었습니다. (noticeId: $noticeId)", HttpStatus.OK)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import jakarta.persistence.Entity

@Entity(name = "notice")
class NoticeEntity(

@Column
var isDeleted: Boolean = false,

@Column
var title: String,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.time.LocalDateTime

data class NoticeDto(
val id: Long,
val isDeleted: Boolean,
val title: String,
val description: String,
// val postType: String,
Expand All @@ -20,6 +21,7 @@ data class NoticeDto(
fun of(entity: NoticeEntity): NoticeDto = entity.run {
NoticeDto(
id = this.id,
isDeleted = false,
title = this.title,
description = this.description,
// postType = this.postType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface NoticeService {
fun readNotice(noticeId: Long): NoticeDto
fun createNotice(request: CreateNoticeRequest): NoticeDto
fun updateNotice(noticeId: Long, request: UpdateNoticeRequest) : NoticeDto
fun deleteNotice(noticeId: Long)
}

@Service
Expand All @@ -25,6 +26,7 @@ class NoticeServiceImpl(
override fun readNotice(noticeId: Long): NoticeDto {
val notice: NoticeEntity = noticeRepository.findByIdOrNull(noticeId)
?: throw CserealException.Csereal400("존재하지 않는 공지사항입니다.(noticeId: $noticeId)")
if(notice.isDeleted) throw CserealException.Csereal400("삭제된 공지사항입니다.(noticeId: $noticeId)")
return NoticeDto.of(notice)
}

Expand All @@ -45,6 +47,7 @@ class NoticeServiceImpl(
override fun updateNotice(noticeId: Long, request: UpdateNoticeRequest) : NoticeDto {
val notice: NoticeEntity = noticeRepository.findByIdOrNull(noticeId)
?: throw CserealException.Csereal400("존재하지 않는 공지사항입니다.(noticeId: $noticeId")
if(notice.isDeleted) throw CserealException.Csereal400("삭제된 공지사항입니다.(noticeId: $noticeId")

notice.title = request.title ?: notice.title
notice.description = request.description ?: notice.description
Expand All @@ -53,4 +56,13 @@ class NoticeServiceImpl(

return NoticeDto.of(notice)
}

@Transactional
override fun deleteNotice(noticeId: Long) {
val notice : NoticeEntity = noticeRepository.findByIdOrNull(noticeId)
?: throw CserealException.Csereal400("존재하지 않는 공지사항입니다.(noticeId: $noticeId)")

notice.isDeleted = true

}
}

0 comments on commit 0382376

Please sign in to comment.