Skip to content

Commit

Permalink
feat: updateNotice 추가, valid 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
skfotakf committed Jul 26, 2023
1 parent 068225a commit 8d8d5cd
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
runtimeOnly("com.mysql:mysql-connector-j")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.wafflestudio.csereal.core.notice.api

import com.wafflestudio.csereal.core.notice.dto.CreateNoticeRequest
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.web.bind.annotation.*

@RequestMapping("/notice")
Expand All @@ -19,8 +21,16 @@ class NoticeController(

@PostMapping
fun createNotice(
@RequestBody request: CreateNoticeRequest
@Valid @RequestBody request: CreateNoticeRequest
) : NoticeDto {
return noticeService.createNotice(request)
}

@PutMapping("/{noticeId}")
fun updateNotice(
@PathVariable noticeId: Long,
@Valid @RequestBody request: UpdateNoticeRequest,
) : NoticeDto {
return noticeService.updateNotice(noticeId, request)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ class NoticeEntity(
var title: String,

@Column(columnDefinition = "text")
var description: String
var description: String,

// var postType: String,
//
// var isPublic: Boolean,
//
// var isSlide: Boolean,
//
// var isPinned: Boolean,
): BaseTimeEntity() {

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.wafflestudio.csereal.core.notice.dto

import jakarta.validation.constraints.NotBlank

data class CreateNoticeRequest(
@NotBlank(message = "제목은 비어있을 수 없습니다")
val title: String,
val description: String

@NotBlank(message = "내용은 비어있을 수 없습니다")
val description: String,
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ data class NoticeDto(
id = this.id,
title = this.title,
description = this.description,
// postType = this.postType,
createdAt = this.createdAt,
modifiedAt = this.modifiedAt
modifiedAt = this.modifiedAt,
// isPublic = this.isPublic,
// isSlide = this.isSlide,
// isPinned = this.isPinned,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wafflestudio.csereal.core.notice.dto

data class UpdateNoticeRequest(
val title: String?,
val description: String?
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.wafflestudio.csereal.core.notice.database.NoticeEntity
import com.wafflestudio.csereal.core.notice.database.NoticeRepository
import com.wafflestudio.csereal.core.notice.dto.CreateNoticeRequest
import com.wafflestudio.csereal.core.notice.dto.NoticeDto
import com.wafflestudio.csereal.core.notice.dto.UpdateNoticeRequest
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

interface NoticeService {
fun readNotice(noticeId: Long): NoticeDto
fun createNotice(request: CreateNoticeRequest): NoticeDto
fun updateNotice(noticeId: Long, request: UpdateNoticeRequest) : NoticeDto
}

@Service
Expand All @@ -28,7 +30,6 @@ class NoticeServiceImpl(

@Transactional
override fun createNotice(request: CreateNoticeRequest): NoticeDto {
// TODO:"아직 날짜가 제대로 안 뜸"
val newNotice = NoticeEntity(
title = request.title,
description = request.description,
Expand All @@ -39,4 +40,17 @@ class NoticeServiceImpl(
return NoticeDto.of(newNotice)

}

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

notice.title = request.title ?: notice.title
notice.description = request.description ?: notice.description

noticeRepository.save(notice)

return NoticeDto.of(notice)
}
}

0 comments on commit 8d8d5cd

Please sign in to comment.