From 068225afc8b5efb52784d5e992bb8d4d5ba79f53 Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Wed, 26 Jul 2023 09:27:19 +0900 Subject: [PATCH 1/9] =?UTF-8?q?fix:=20ExceptionHandler=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/config/CserealExceptionHandler.kt | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/kotlin/com/wafflestudio/csereal/common/config/CserealExceptionHandler.kt diff --git a/src/main/kotlin/com/wafflestudio/csereal/common/config/CserealExceptionHandler.kt b/src/main/kotlin/com/wafflestudio/csereal/common/config/CserealExceptionHandler.kt new file mode 100644 index 00000000..6cf3d136 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/common/config/CserealExceptionHandler.kt @@ -0,0 +1,33 @@ +package com.wafflestudio.csereal.common.config + +import com.wafflestudio.csereal.common.CserealException +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.validation.BindingResult +import org.springframework.web.bind.MethodArgumentNotValidException +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.RestControllerAdvice +import java.sql.SQLIntegrityConstraintViolationException + +@RestControllerAdvice +class CserealExceptionHandler { + + // @Valid로 인해 오류 떴을 때 메시지 전송 + @ExceptionHandler(value = [MethodArgumentNotValidException::class]) + fun handle(e: MethodArgumentNotValidException): ResponseEntity { + val bindingResult: BindingResult = e.bindingResult + return ResponseEntity(bindingResult.fieldError?.defaultMessage, HttpStatus.BAD_REQUEST) + } + + // csereal 내부 규정 오류 + @ExceptionHandler(value = [CserealException::class]) + fun handle(e: CserealException): ResponseEntity { + return ResponseEntity(e.message, e.status) + } + + // db에서 중복된 값 있을 때 + @ExceptionHandler(value = [SQLIntegrityConstraintViolationException::class]) + fun handle(e: SQLIntegrityConstraintViolationException): ResponseEntity { + return ResponseEntity("중복된 값이 있습니다.", HttpStatus.CONFLICT) + } +} \ No newline at end of file From 8d8d5cde065bdfb688868be84eea4ff4fa21b5d0 Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Wed, 26 Jul 2023 10:03:51 +0900 Subject: [PATCH 2/9] =?UTF-8?q?feat:=20updateNotice=20=EC=B6=94=EA=B0=80,?= =?UTF-8?q?=20valid=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 1 + .../csereal/core/notice/api/NoticeController.kt | 12 +++++++++++- .../csereal/core/notice/database/NoticeEntity.kt | 10 +++++++++- .../core/notice/dto/CreateNoticeRequest.kt | 7 ++++++- .../csereal/core/notice/dto/NoticeDto.kt | 6 +++++- .../core/notice/dto/UpdateNoticeRequest.kt | 7 +++++++ .../csereal/core/notice/service/NoticeService.kt | 16 +++++++++++++++- 7 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt diff --git a/build.gradle.kts b/build.gradle.kts index b81e8ec6..7d5145d6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt index 91a9231d..396509af 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt @@ -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") @@ -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) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt index 5b55d6a6..cec32fc6 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt @@ -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() { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt index be251915..1e8c89ae 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt @@ -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, ) { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/NoticeDto.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/NoticeDto.kt index 65e1120c..4d6fffe1 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/NoticeDto.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/NoticeDto.kt @@ -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, ) } diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt new file mode 100644 index 00000000..a1a7d869 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt @@ -0,0 +1,7 @@ +package com.wafflestudio.csereal.core.notice.dto + +data class UpdateNoticeRequest( + val title: String?, + val description: String? +) { +} \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt index ca321282..3437d761 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt @@ -5,6 +5,7 @@ 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 @@ -12,6 +13,7 @@ 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 @@ -28,7 +30,6 @@ class NoticeServiceImpl( @Transactional override fun createNotice(request: CreateNoticeRequest): NoticeDto { - // TODO:"아직 날짜가 제대로 안 뜸" val newNotice = NoticeEntity( title = request.title, description = request.description, @@ -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) + } } \ No newline at end of file From 0382376c9bcb8e53deae1c5ee74d6f1d1c2d23de Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Wed, 26 Jul 2023 10:09:26 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat:=20deleteNotice=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../csereal/core/notice/api/NoticeController.kt | 10 ++++++++++ .../csereal/core/notice/database/NoticeEntity.kt | 4 ++++ .../csereal/core/notice/dto/NoticeDto.kt | 2 ++ .../csereal/core/notice/service/NoticeService.kt | 12 ++++++++++++ 4 files changed, 28 insertions(+) diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt index 396509af..c3f67c4f 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt @@ -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") @@ -33,4 +35,12 @@ class NoticeController( ) : NoticeDto { return noticeService.updateNotice(noticeId, request) } + + @DeleteMapping("/{noticeId}") + fun deleteNotice( + @PathVariable noticeId: Long + ) : ResponseEntity { + noticeService.deleteNotice(noticeId) + return ResponseEntity("삭제되었습니다. (noticeId: $noticeId)", HttpStatus.OK) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt index cec32fc6..afea3802 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt @@ -7,6 +7,10 @@ import jakarta.persistence.Entity @Entity(name = "notice") class NoticeEntity( + + @Column + var isDeleted: Boolean = false, + @Column var title: String, diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/NoticeDto.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/NoticeDto.kt index 4d6fffe1..a858c1dc 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/NoticeDto.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/NoticeDto.kt @@ -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, @@ -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, diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt index 3437d761..265c08d7 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt @@ -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 @@ -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) } @@ -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 @@ -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 + + } } \ No newline at end of file From 3f2e94d9d9c19a056d55b837b64135d4e64ad13b Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Wed, 26 Jul 2023 10:16:19 +0900 Subject: [PATCH 4/9] =?UTF-8?q?feat:=20enrollTag=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80,=20noticeTag=20=EC=97=B0=EA=B4=80=20?= =?UTF-8?q?=EC=97=94=ED=8B=B0=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/notice/api/NoticeController.kt | 8 ++++++++ .../core/notice/database/NoticeTagEntity.kt | 20 +++++++++++++++++++ .../csereal/core/notice/database/TagEntity.kt | 10 ++++++++++ .../core/notice/database/TagRepository.kt | 6 ++++++ .../core/notice/dto/CreateNoticeRequest.kt | 2 ++ .../core/notice/service/NoticeService.kt | 11 ++++++++++ 6 files changed, 57 insertions(+) create mode 100644 src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt create mode 100644 src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt create mode 100644 src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagRepository.kt diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt index c3f67c4f..b8f73ce4 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt @@ -43,4 +43,12 @@ class NoticeController( noticeService.deleteNotice(noticeId) return ResponseEntity("삭제되었습니다. (noticeId: $noticeId)", HttpStatus.OK) } + + @PostMapping("/tag") + fun enrollTag( + @RequestBody tagName: Map + ) : ResponseEntity { + noticeService.enrollTag(tagName["name"]!!) + return ResponseEntity("등록되었습니다. (tagName: ${tagName["name"]})", HttpStatus.OK) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt new file mode 100644 index 00000000..0a469814 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt @@ -0,0 +1,20 @@ +package com.wafflestudio.csereal.core.notice.database + +import com.wafflestudio.csereal.common.config.BaseTimeEntity +import jakarta.persistence.CascadeType +import jakarta.persistence.Entity +import jakarta.persistence.JoinColumn +import jakarta.persistence.ManyToOne + +@Entity(name="noticeTag") +class NoticeTagEntity( + @ManyToOne(cascade = [CascadeType.ALL]) + @JoinColumn(name = "notice_id") + val notice: NoticeEntity, + + @ManyToOne(cascade = [CascadeType.ALL]) + @JoinColumn(name = "tag_id") + val tag: TagEntity, + + ) : BaseTimeEntity() { +} \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt new file mode 100644 index 00000000..7a24b634 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt @@ -0,0 +1,10 @@ +package com.wafflestudio.csereal.core.notice.database + +import com.wafflestudio.csereal.common.config.BaseTimeEntity +import jakarta.persistence.Entity + +@Entity(name = "tag") +class TagEntity( + var name: String +) : BaseTimeEntity() { +} \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagRepository.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagRepository.kt new file mode 100644 index 00000000..34a50416 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagRepository.kt @@ -0,0 +1,6 @@ +package com.wafflestudio.csereal.core.notice.database + +import org.springframework.data.jpa.repository.JpaRepository + +interface TagRepository : JpaRepository { +} \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt index 1e8c89ae..4a41c66b 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt @@ -8,5 +8,7 @@ data class CreateNoticeRequest( @NotBlank(message = "내용은 비어있을 수 없습니다") val description: String, + + val tag: List = emptyList() ) { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt index 265c08d7..f37b8200 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt @@ -3,6 +3,8 @@ package com.wafflestudio.csereal.core.notice.service import com.wafflestudio.csereal.common.CserealException import com.wafflestudio.csereal.core.notice.database.NoticeEntity import com.wafflestudio.csereal.core.notice.database.NoticeRepository +import com.wafflestudio.csereal.core.notice.database.TagEntity +import com.wafflestudio.csereal.core.notice.database.TagRepository import com.wafflestudio.csereal.core.notice.dto.CreateNoticeRequest import com.wafflestudio.csereal.core.notice.dto.NoticeDto import com.wafflestudio.csereal.core.notice.dto.UpdateNoticeRequest @@ -15,11 +17,13 @@ interface NoticeService { fun createNotice(request: CreateNoticeRequest): NoticeDto fun updateNotice(noticeId: Long, request: UpdateNoticeRequest) : NoticeDto fun deleteNotice(noticeId: Long) + fun enrollTag(tagName: String) } @Service class NoticeServiceImpl( private val noticeRepository: NoticeRepository, + private val tagRepository: TagRepository, ) : NoticeService { @Transactional(readOnly = true) @@ -65,4 +69,11 @@ class NoticeServiceImpl( notice.isDeleted = true } + + override fun enrollTag(tagName: String) { + val newTag = TagEntity( + name = tagName + ) + tagRepository.save(newTag) + } } \ No newline at end of file From be6d4336e91e94906925241c5502bdbe2462737d Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Wed, 26 Jul 2023 10:30:33 +0900 Subject: [PATCH 5/9] =?UTF-8?q?feat:=20=EA=B3=B5=EC=A7=80=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EC=9E=91=EC=84=B1=ED=95=A0=20=EB=95=8C=20=ED=83=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/notice/database/NoticeEntity.kt | 5 ++++ .../core/notice/database/NoticeTagEntity.kt | 8 +++--- .../notice/database/NoticeTagRepository.kt | 7 +++++ .../csereal/core/notice/database/TagEntity.kt | 7 ++++- .../core/notice/dto/CreateNoticeRequest.kt | 2 +- .../core/notice/dto/UpdateNoticeRequest.kt | 3 ++- .../core/notice/service/NoticeService.kt | 26 +++++++++++++++---- src/main/resources/application.yaml | 8 ++++-- 8 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt index afea3802..2f4a9043 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt @@ -1,8 +1,10 @@ package com.wafflestudio.csereal.core.notice.database import com.wafflestudio.csereal.common.config.BaseTimeEntity +import jakarta.persistence.CascadeType import jakarta.persistence.Column import jakarta.persistence.Entity +import jakarta.persistence.OneToMany @Entity(name = "notice") @@ -24,6 +26,9 @@ class NoticeEntity( // var isSlide: Boolean, // // var isPinned: Boolean, + + @OneToMany(mappedBy = "notice", cascade = [CascadeType.PERSIST]) + var noticeTag: MutableSet = mutableSetOf() ): BaseTimeEntity() { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt index 0a469814..2fd7594d 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt @@ -8,13 +8,13 @@ import jakarta.persistence.ManyToOne @Entity(name="noticeTag") class NoticeTagEntity( - @ManyToOne(cascade = [CascadeType.ALL]) + @ManyToOne(cascade = [CascadeType.PERSIST]) @JoinColumn(name = "notice_id") - val notice: NoticeEntity, + var notice: NoticeEntity, - @ManyToOne(cascade = [CascadeType.ALL]) + @ManyToOne(cascade = [CascadeType.PERSIST]) @JoinColumn(name = "tag_id") - val tag: TagEntity, + var tag: TagEntity, ) : BaseTimeEntity() { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt new file mode 100644 index 00000000..39a6ce98 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt @@ -0,0 +1,7 @@ +package com.wafflestudio.csereal.core.notice.database + +import org.springframework.data.jpa.repository.JpaRepository + +interface NoticeTagRepository : JpaRepository { + fun deleteAllByNoticeId(noticeId: Long) +} \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt index 7a24b634..c1f7e285 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt @@ -1,10 +1,15 @@ package com.wafflestudio.csereal.core.notice.database import com.wafflestudio.csereal.common.config.BaseTimeEntity +import jakarta.persistence.CascadeType import jakarta.persistence.Entity +import jakarta.persistence.OneToMany @Entity(name = "tag") class TagEntity( - var name: String + var name: String, + + @OneToMany(mappedBy = "tag", cascade = [CascadeType.PERSIST]) + val noticeTag: MutableSet = mutableSetOf() ) : BaseTimeEntity() { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt index 4a41c66b..f54e8509 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt @@ -9,6 +9,6 @@ data class CreateNoticeRequest( @NotBlank(message = "내용은 비어있을 수 없습니다") val description: String, - val tag: List = emptyList() + val tag: List = emptyList() ) { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt index a1a7d869..5ccb20bc 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt @@ -2,6 +2,7 @@ package com.wafflestudio.csereal.core.notice.dto data class UpdateNoticeRequest( val title: String?, - val description: String? + val description: String?, + val tag: List = emptyList() ) { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt index f37b8200..e8c0eeb9 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt @@ -1,10 +1,7 @@ package com.wafflestudio.csereal.core.notice.service import com.wafflestudio.csereal.common.CserealException -import com.wafflestudio.csereal.core.notice.database.NoticeEntity -import com.wafflestudio.csereal.core.notice.database.NoticeRepository -import com.wafflestudio.csereal.core.notice.database.TagEntity -import com.wafflestudio.csereal.core.notice.database.TagRepository +import com.wafflestudio.csereal.core.notice.database.* import com.wafflestudio.csereal.core.notice.dto.CreateNoticeRequest import com.wafflestudio.csereal.core.notice.dto.NoticeDto import com.wafflestudio.csereal.core.notice.dto.UpdateNoticeRequest @@ -24,6 +21,7 @@ interface NoticeService { class NoticeServiceImpl( private val noticeRepository: NoticeRepository, private val tagRepository: TagRepository, + private val noticeTagRepository: NoticeTagRepository ) : NoticeService { @Transactional(readOnly = true) @@ -41,6 +39,15 @@ class NoticeServiceImpl( description = request.description, ) + request.tag.forEach { + noticeTagRepository.save( + NoticeTagEntity( + notice = newNotice, + tag = tagRepository.findByIdOrNull(it) ?: throw CserealException.Csereal400("해당하는 태그가 없습니다") + ) + ) + } + noticeRepository.save(newNotice) return NoticeDto.of(newNotice) @@ -56,7 +63,14 @@ class NoticeServiceImpl( notice.title = request.title ?: notice.title notice.description = request.description ?: notice.description - noticeRepository.save(notice) + noticeTagRepository.deleteAllByNoticeId(noticeId) + + request.tag.forEach { + noticeTagRepository.save(NoticeTagEntity( + notice = notice, + tag = tagRepository.findByIdOrNull(it) ?: throw CserealException.Csereal400("해당 태그는 존재하지 않습니다") + )) + } return NoticeDto.of(notice) } @@ -76,4 +90,6 @@ class NoticeServiceImpl( ) tagRepository.save(newTag) } + + //TODO: 이미지 등록, 페이지네이션, 검색 } \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 62f21be6..d62e3453 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -5,10 +5,14 @@ spring: --- spring: config.activate.on-profile: local + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://127.0.0.1:3306/csereal_local?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul + username: root + password: toor jpa: hibernate: - ddl-auto: create - show-sql: true + ddl-auto: update logging.level: default: INFO From c349869e7b5e5ed0ac81c7c48375561447f57c9e Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Wed, 26 Jul 2023 10:48:25 +0900 Subject: [PATCH 6/9] =?UTF-8?q?fix:=20=EB=A1=9C=EC=BB=AC=20db=20=EC=97=86?= =?UTF-8?q?=EC=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.yaml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index d62e3453..62f21be6 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -5,14 +5,10 @@ spring: --- spring: config.activate.on-profile: local - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://127.0.0.1:3306/csereal_local?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul - username: root - password: toor jpa: hibernate: - ddl-auto: update + ddl-auto: create + show-sql: true logging.level: default: INFO From b9d97562842dd16212a35c8f75bfb060434430b8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Wed, 26 Jul 2023 23:47:23 +0900 Subject: [PATCH 7/9] =?UTF-8?q?fix:=20pr=20=EB=A6=AC=EB=B7=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/notice/api/NoticeController.kt | 5 +-- .../core/notice/database/NoticeEntity.kt | 10 ++++- .../core/notice/database/NoticeTagEntity.kt | 9 ++--- .../notice/database/NoticeTagRepository.kt | 1 + .../csereal/core/notice/database/TagEntity.kt | 2 +- .../core/notice/dto/CreateNoticeRequest.kt | 4 +- .../core/notice/dto/UpdateNoticeRequest.kt | 2 +- .../core/notice/service/NoticeService.kt | 38 +++++++++++-------- src/main/resources/application.yaml | 7 +++- 9 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt index b8f73ce4..21131bff 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/api/NoticeController.kt @@ -28,7 +28,7 @@ class NoticeController( return noticeService.createNotice(request) } - @PutMapping("/{noticeId}") + @PatchMapping("/{noticeId}") fun updateNotice( @PathVariable noticeId: Long, @Valid @RequestBody request: UpdateNoticeRequest, @@ -39,9 +39,8 @@ class NoticeController( @DeleteMapping("/{noticeId}") fun deleteNotice( @PathVariable noticeId: Long - ) : ResponseEntity { + ) { noticeService.deleteNotice(noticeId) - return ResponseEntity("삭제되었습니다. (noticeId: $noticeId)", HttpStatus.OK) } @PostMapping("/tag") diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt index 2f4a9043..73892c9f 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt @@ -27,8 +27,16 @@ class NoticeEntity( // // var isPinned: Boolean, - @OneToMany(mappedBy = "notice", cascade = [CascadeType.PERSIST]) + @OneToMany(mappedBy = "notice", cascade = [CascadeType.ALL]) var noticeTag: MutableSet = mutableSetOf() ): BaseTimeEntity() { + fun setNoticeTag(noticeTag: NoticeTagEntity) { + + this.noticeTag.add(noticeTag) + noticeTag.notice = this + } + + + } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt index 2fd7594d..a9a65872 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt @@ -1,18 +1,15 @@ package com.wafflestudio.csereal.core.notice.database import com.wafflestudio.csereal.common.config.BaseTimeEntity -import jakarta.persistence.CascadeType -import jakarta.persistence.Entity -import jakarta.persistence.JoinColumn -import jakarta.persistence.ManyToOne +import jakarta.persistence.* @Entity(name="noticeTag") class NoticeTagEntity( - @ManyToOne(cascade = [CascadeType.PERSIST]) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "notice_id") var notice: NoticeEntity, - @ManyToOne(cascade = [CascadeType.PERSIST]) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "tag_id") var tag: TagEntity, diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt index 39a6ce98..52b4a4a1 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt @@ -3,5 +3,6 @@ package com.wafflestudio.csereal.core.notice.database import org.springframework.data.jpa.repository.JpaRepository interface NoticeTagRepository : JpaRepository { + fun findAllByNoticeId(noticeId: Long) fun deleteAllByNoticeId(noticeId: Long) } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt index c1f7e285..e85720a8 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt @@ -9,7 +9,7 @@ import jakarta.persistence.OneToMany class TagEntity( var name: String, - @OneToMany(mappedBy = "tag", cascade = [CascadeType.PERSIST]) + @OneToMany(mappedBy = "tag") val noticeTag: MutableSet = mutableSetOf() ) : BaseTimeEntity() { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt index f54e8509..8f73592e 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt @@ -3,10 +3,10 @@ package com.wafflestudio.csereal.core.notice.dto import jakarta.validation.constraints.NotBlank data class CreateNoticeRequest( - @NotBlank(message = "제목은 비어있을 수 없습니다") + @field:NotBlank(message = "제목은 비어있을 수 없습니다") val title: String, - @NotBlank(message = "내용은 비어있을 수 없습니다") + @field:NotBlank(message = "내용은 비어있을 수 없습니다") val description: String, val tag: List = emptyList() diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt index 5ccb20bc..9992dc20 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt @@ -3,6 +3,6 @@ package com.wafflestudio.csereal.core.notice.dto data class UpdateNoticeRequest( val title: String?, val description: String?, - val tag: List = emptyList() + val tag: List? ) { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt index e8c0eeb9..88005acd 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt @@ -12,7 +12,7 @@ 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 + fun updateNotice(noticeId: Long, request: UpdateNoticeRequest): NoticeDto fun deleteNotice(noticeId: Long) fun enrollTag(tagName: String) } @@ -28,7 +28,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)") + if (notice.isDeleted) throw CserealException.Csereal400("삭제된 공지사항입니다.(noticeId: $noticeId)") return NoticeDto.of(notice) } @@ -39,11 +39,12 @@ class NoticeServiceImpl( description = request.description, ) - request.tag.forEach { - noticeTagRepository.save( + for (i: Int in 0 until request.tag.size) { + newNotice.setNoticeTag( NoticeTagEntity( notice = newNotice, - tag = tagRepository.findByIdOrNull(it) ?: throw CserealException.Csereal400("해당하는 태그가 없습니다") + tag = tagRepository.findByIdOrNull(request.tag[i]) + ?: throw CserealException.Csereal400("해당하는 태그가 없습니다") ) ) } @@ -55,29 +56,36 @@ class NoticeServiceImpl( } @Transactional - override fun updateNotice(noticeId: Long, request: UpdateNoticeRequest) : NoticeDto { + 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") + if (notice.isDeleted) throw CserealException.Csereal400("삭제된 공지사항입니다.(noticeId: $noticeId") notice.title = request.title ?: notice.title notice.description = request.description ?: notice.description - noticeTagRepository.deleteAllByNoticeId(noticeId) - - request.tag.forEach { - noticeTagRepository.save(NoticeTagEntity( - notice = notice, - tag = tagRepository.findByIdOrNull(it) ?: throw CserealException.Csereal400("해당 태그는 존재하지 않습니다") - )) + if (request.tag != null) { + noticeTagRepository.deleteAllByNoticeId(noticeId) + notice.noticeTag = mutableSetOf() + for (i: Int in 0 until request.tag.size) { + notice.setNoticeTag( + NoticeTagEntity( + notice = notice, + tag = tagRepository.findByIdOrNull(request.tag[i]) + ?: throw CserealException.Csereal400("해당하는 태그가 없습니다") + ) + ) + } } + + return NoticeDto.of(notice) } @Transactional override fun deleteNotice(noticeId: Long) { - val notice : NoticeEntity = noticeRepository.findByIdOrNull(noticeId) + val notice: NoticeEntity = noticeRepository.findByIdOrNull(noticeId) ?: throw CserealException.Csereal400("존재하지 않는 공지사항입니다.(noticeId: $noticeId)") notice.isDeleted = true diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 62f21be6..06fa9653 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -5,9 +5,14 @@ spring: --- spring: config.activate.on-profile: local + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://127.0.0.1:3306/csereal_local?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul + username: root + password: toor jpa: hibernate: - ddl-auto: create + ddl-auto: update show-sql: true logging.level: default: INFO From 750877c0539a907e793b1688246b7d998f2d9783 Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Thu, 27 Jul 2023 19:39:43 +0900 Subject: [PATCH 8/9] =?UTF-8?q?fix:=20pr=20=EB=A6=AC=EB=B7=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/notice/database/NoticeEntity.kt | 8 +----- .../core/notice/database/NoticeTagEntity.kt | 16 ++++++++++-- .../notice/database/NoticeTagRepository.kt | 1 - .../csereal/core/notice/database/TagEntity.kt | 2 +- .../core/notice/dto/CreateNoticeRequest.kt | 2 +- .../core/notice/dto/UpdateNoticeRequest.kt | 2 +- .../core/notice/service/NoticeService.kt | 26 ++++++------------- 7 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt index 73892c9f..9fdc013a 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeEntity.kt @@ -28,15 +28,9 @@ class NoticeEntity( // var isPinned: Boolean, @OneToMany(mappedBy = "notice", cascade = [CascadeType.ALL]) - var noticeTag: MutableSet = mutableSetOf() + var noticeTags: MutableSet = mutableSetOf() ): BaseTimeEntity() { - fun setNoticeTag(noticeTag: NoticeTagEntity) { - - this.noticeTag.add(noticeTag) - noticeTag.notice = this - } - } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt index a9a65872..9b5c3586 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt @@ -3,7 +3,7 @@ package com.wafflestudio.csereal.core.notice.database import com.wafflestudio.csereal.common.config.BaseTimeEntity import jakarta.persistence.* -@Entity(name="noticeTag") +@Entity(name = "noticeTag") class NoticeTagEntity( @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "notice_id") @@ -14,4 +14,16 @@ class NoticeTagEntity( var tag: TagEntity, ) : BaseTimeEntity() { -} \ No newline at end of file + companion object { + + fun createNoticeTag(notice: NoticeEntity, tag: TagEntity) { + NoticeTagEntity(notice, tag) + notice.noticeTags.add(NoticeTagEntity(notice, tag)) + tag.noticeTags.add(NoticeTagEntity(notice,tag)) + } + } + + + +} + diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt index 52b4a4a1..39a6ce98 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagRepository.kt @@ -3,6 +3,5 @@ package com.wafflestudio.csereal.core.notice.database import org.springframework.data.jpa.repository.JpaRepository interface NoticeTagRepository : JpaRepository { - fun findAllByNoticeId(noticeId: Long) fun deleteAllByNoticeId(noticeId: Long) } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt index e85720a8..67c108b9 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/TagEntity.kt @@ -10,6 +10,6 @@ class TagEntity( var name: String, @OneToMany(mappedBy = "tag") - val noticeTag: MutableSet = mutableSetOf() + val noticeTags: MutableSet = mutableSetOf() ) : BaseTimeEntity() { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt index 8f73592e..2c660447 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/CreateNoticeRequest.kt @@ -9,6 +9,6 @@ data class CreateNoticeRequest( @field:NotBlank(message = "내용은 비어있을 수 없습니다") val description: String, - val tag: List = emptyList() + val tags: List = emptyList() ) { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt index 9992dc20..ea45d723 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/dto/UpdateNoticeRequest.kt @@ -3,6 +3,6 @@ package com.wafflestudio.csereal.core.notice.dto data class UpdateNoticeRequest( val title: String?, val description: String?, - val tag: List? + val tags: List? ) { } \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt index 88005acd..168bed68 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/service/NoticeService.kt @@ -39,14 +39,9 @@ class NoticeServiceImpl( description = request.description, ) - for (i: Int in 0 until request.tag.size) { - newNotice.setNoticeTag( - NoticeTagEntity( - notice = newNotice, - tag = tagRepository.findByIdOrNull(request.tag[i]) - ?: throw CserealException.Csereal400("해당하는 태그가 없습니다") - ) - ) + for (tagId in request.tags) { + val tag = tagRepository.findByIdOrNull(tagId) ?: throw CserealException.Csereal400("해당하는 태그가 없습니다") + NoticeTagEntity.createNoticeTag(newNotice, tag) } noticeRepository.save(newNotice) @@ -64,17 +59,12 @@ class NoticeServiceImpl( notice.title = request.title ?: notice.title notice.description = request.description ?: notice.description - if (request.tag != null) { + if (request.tags != null) { noticeTagRepository.deleteAllByNoticeId(noticeId) - notice.noticeTag = mutableSetOf() - for (i: Int in 0 until request.tag.size) { - notice.setNoticeTag( - NoticeTagEntity( - notice = notice, - tag = tagRepository.findByIdOrNull(request.tag[i]) - ?: throw CserealException.Csereal400("해당하는 태그가 없습니다") - ) - ) + notice.noticeTags.clear() + for (tagId in request.tags) { + val tag = tagRepository.findByIdOrNull(tagId) ?: throw CserealException.Csereal400("해당하는 태그가 없습니다") + NoticeTagEntity.createNoticeTag(notice, tag) } } From a8d5ec6931c5649de33db359b67f716982b3bb9e Mon Sep 17 00:00:00 2001 From: "DESKTOP-USQPRVG\\gram" Date: Fri, 28 Jul 2023 17:44:03 +0900 Subject: [PATCH 9/9] fix: noticeTag assign --- .../csereal/core/notice/database/NoticeTagEntity.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt index 9b5c3586..c910dabd 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/notice/database/NoticeTagEntity.kt @@ -17,9 +17,9 @@ class NoticeTagEntity( companion object { fun createNoticeTag(notice: NoticeEntity, tag: TagEntity) { - NoticeTagEntity(notice, tag) - notice.noticeTags.add(NoticeTagEntity(notice, tag)) - tag.noticeTags.add(NoticeTagEntity(notice,tag)) + val noticeTag = NoticeTagEntity(notice, tag) + notice.noticeTags.add(noticeTag) + tag.noticeTags.add(noticeTag) } }