Skip to content

Commit

Permalink
feat: 공지사항 작성할 때 태그 생성 및 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
skfotakf committed Jul 26, 2023
1 parent 3f2e94d commit be6d433
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -24,6 +26,9 @@ class NoticeEntity(
// var isSlide: Boolean,
//
// var isPinned: Boolean,

@OneToMany(mappedBy = "notice", cascade = [CascadeType.PERSIST])
var noticeTag: MutableSet<NoticeTagEntity> = mutableSetOf()
): BaseTimeEntity() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wafflestudio.csereal.core.notice.database

import org.springframework.data.jpa.repository.JpaRepository

interface NoticeTagRepository : JpaRepository<NoticeTagEntity, Long> {
fun deleteAllByNoticeId(noticeId: Long)
}
Original file line number Diff line number Diff line change
@@ -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<NoticeTagEntity> = mutableSetOf()
) : BaseTimeEntity() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ data class CreateNoticeRequest(
@NotBlank(message = "내용은 비어있을 수 없습니다")
val description: String,

val tag: List<String> = emptyList()
val tag: List<Long> = emptyList()
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> = emptyList()
) {
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,6 +21,7 @@ interface NoticeService {
class NoticeServiceImpl(
private val noticeRepository: NoticeRepository,
private val tagRepository: TagRepository,
private val noticeTagRepository: NoticeTagRepository
) : NoticeService {

@Transactional(readOnly = true)
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -76,4 +90,6 @@ class NoticeServiceImpl(
)
tagRepository.save(newTag)
}

//TODO: 이미지 등록, 페이지네이션, 검색
}
8 changes: 6 additions & 2 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit be6d433

Please sign in to comment.