Skip to content

Commit

Permalink
feat: notice에 attachments 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
skfotakf committed Sep 1, 2023
1 parent 2975ebd commit fa44f43
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import jakarta.validation.Valid
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile

@RequestMapping("/api/v1/notice")
@RestController
Expand All @@ -31,20 +32,22 @@ class NoticeController(
return ResponseEntity.ok(noticeService.readNotice(noticeId, tag, keyword))
}

@AuthenticatedStaff
// @AuthenticatedStaff
@PostMapping
fun createNotice(
@Valid @RequestBody request: NoticeDto
@Valid @RequestPart("request") request: NoticeDto,
@RequestPart("attachments") attachments: List<MultipartFile>?
): ResponseEntity<NoticeDto> {
return ResponseEntity.ok(noticeService.createNotice(request))
return ResponseEntity.ok(noticeService.createNotice(request, attachments))
}

@PatchMapping("/{noticeId}")
fun updateNotice(
@PathVariable noticeId: Long,
@Valid @RequestBody request: NoticeDto,
@Valid @RequestPart("request") request: NoticeDto,
@RequestPart("attachments") attachments: List<MultipartFile>?,
): ResponseEntity<NoticeDto> {
return ResponseEntity.ok(noticeService.updateNotice(noticeId, request))
return ResponseEntity.ok(noticeService.updateNotice(noticeId, request, attachments))
}

@DeleteMapping("/{noticeId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.wafflestudio.csereal.core.notice.database

import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.AttachmentContentEntityType
import com.wafflestudio.csereal.core.notice.dto.NoticeDto
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import com.wafflestudio.csereal.core.user.database.UserEntity
import jakarta.persistence.*

Expand All @@ -24,8 +26,14 @@ class NoticeEntity(

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "users_id")
val author: UserEntity
) : BaseTimeEntity() {
val author: UserEntity? = null,

@OneToMany(mappedBy = "notice", cascade = [CascadeType.ALL], orphanRemoval = true)
var attachments: MutableList<AttachmentEntity> = mutableListOf(),

) : BaseTimeEntity(), AttachmentContentEntityType {
override fun bringAttachments() = attachments

fun update(updateNoticeRequest: NoticeDto) {
this.title = updateNoticeRequest.title
this.description = updateNoticeRequest.description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.wafflestudio.csereal.core.notice.dto

import com.wafflestudio.csereal.core.notice.database.NoticeEntity
import com.wafflestudio.csereal.core.resource.attachment.dto.AttachmentResponse
import java.time.LocalDateTime

data class NoticeDto(
val id: Long,
val title: String,
val description: String,
val author: String,
// val author: String,
val tags: List<String>,
val createdAt: LocalDateTime?,
val modifiedAt: LocalDateTime?,
Expand All @@ -16,16 +17,17 @@ data class NoticeDto(
val prevId: Long?,
val prevTitle: String?,
val nextId: Long?,
val nextTitle: String?
val nextTitle: String?,
val attachments: List<AttachmentResponse>?,
) {

companion object {
fun of(entity: NoticeEntity, prevNext: Array<NoticeEntity?>?): NoticeDto = entity.run {
fun of(entity: NoticeEntity, attachmentResponses: List<AttachmentResponse>, prevNext: Array<NoticeEntity?>?): NoticeDto = entity.run {
NoticeDto(
id = this.id,
title = this.title,
description = this.description,
author = this.author.name,
// author = this.author.name,
tags = this.noticeTags.map { it.tag.name },
createdAt = this.createdAt,
modifiedAt = this.modifiedAt,
Expand All @@ -34,7 +36,8 @@ data class NoticeDto(
prevId = prevNext?.get(0)?.id,
prevTitle = prevNext?.get(0)?.title,
nextId = prevNext?.get(1)?.id,
nextTitle = prevNext?.get(1)?.title
nextTitle = prevNext?.get(1)?.title,
attachments = attachmentResponses,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.wafflestudio.csereal.core.notice.service
import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.core.notice.database.*
import com.wafflestudio.csereal.core.notice.dto.*
import com.wafflestudio.csereal.core.resource.attachment.service.AttachmentService
import com.wafflestudio.csereal.core.user.database.UserEntity
import com.wafflestudio.csereal.core.user.database.UserRepository
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -12,12 +13,13 @@ import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.context.request.RequestAttributes
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.multipart.MultipartFile

interface NoticeService {
fun searchNotice(tag: List<String>?, keyword: String?, pageNum: Long): NoticeSearchResponse
fun readNotice(noticeId: Long, tag: List<String>?, keyword: String?): NoticeDto
fun createNotice(request: NoticeDto): NoticeDto
fun updateNotice(noticeId: Long, request: NoticeDto): NoticeDto
fun createNotice(request: NoticeDto, attachments: List<MultipartFile>?): NoticeDto
fun updateNotice(noticeId: Long, request: NoticeDto, attachments: List<MultipartFile>?): NoticeDto
fun deleteNotice(noticeId: Long)
fun enrollTag(tagName: String)
}
Expand All @@ -27,7 +29,8 @@ class NoticeServiceImpl(
private val noticeRepository: NoticeRepository,
private val tagInNoticeRepository: TagInNoticeRepository,
private val noticeTagRepository: NoticeTagRepository,
private val userRepository: UserRepository
private val userRepository: UserRepository,
private val attachmentService: AttachmentService,
) : NoticeService {

@Transactional(readOnly = true)
Expand All @@ -50,13 +53,16 @@ class NoticeServiceImpl(

if (notice.isDeleted) throw CserealException.Csereal404("삭제된 공지사항입니다.(noticeId: $noticeId)")

val attachmentResponses = attachmentService.createAttachmentResponses(notice.attachments)

val prevNext = noticeRepository.findPrevNextId(noticeId, tag, keyword)

return NoticeDto.of(notice, prevNext)
return NoticeDto.of(notice, attachmentResponses, prevNext)
}

@Transactional
override fun createNotice(request: NoticeDto): NoticeDto {
override fun createNotice(request: NoticeDto, attachments: List<MultipartFile>?): NoticeDto {
/*
var user = RequestContextHolder.getRequestAttributes()?.getAttribute(
"loggedInUser",
RequestAttributes.SCOPE_REQUEST
Expand All @@ -69,33 +75,46 @@ class NoticeServiceImpl(
user = userRepository.findByUsername(username) ?: throw CserealException.Csereal404("재로그인이 필요합니다.")
}
*/

val newNotice = NoticeEntity(
title = request.title,
description = request.description,
isPublic = request.isPublic,
isPinned = request.isPinned,
author = user
// author = user
)

for (tagName in request.tags) {
val tag = tagInNoticeRepository.findByName(tagName) ?: throw CserealException.Csereal404("해당하는 태그가 없습니다")
NoticeTagEntity.createNoticeTag(newNotice, tag)
}

if(attachments != null) {
attachmentService.uploadAllAttachments(newNotice, attachments)
}

noticeRepository.save(newNotice)

return NoticeDto.of(newNotice, null)
val attachmentResponses = attachmentService.createAttachmentResponses(newNotice.attachments)

return NoticeDto.of(newNotice, attachmentResponses, null)

}

@Transactional
override fun updateNotice(noticeId: Long, request: NoticeDto): NoticeDto {
override fun updateNotice(noticeId: Long, request: NoticeDto, attachments: List<MultipartFile>?): NoticeDto {
val notice: NoticeEntity = noticeRepository.findByIdOrNull(noticeId)
?: throw CserealException.Csereal404("존재하지 않는 공지사항입니다.(noticeId: $noticeId)")
if (notice.isDeleted) throw CserealException.Csereal404("삭제된 공지사항입니다.(noticeId: $noticeId)")

notice.update(request)

if(attachments != null) {
notice.attachments.clear()
attachmentService.uploadAllAttachments(notice, attachments)
}

val oldTags = notice.noticeTags.map { it.tag.name }

val tagsToRemove = oldTags - request.tags
Expand All @@ -112,7 +131,9 @@ class NoticeServiceImpl(
NoticeTagEntity.createNoticeTag(notice, tag)
}

return NoticeDto.of(notice, null)
val attachmentResponses = attachmentService.createAttachmentResponses(notice.attachments)

return NoticeDto.of(notice, attachmentResponses, null)


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.wafflestudio.csereal.core.about.database.AboutEntity
import com.wafflestudio.csereal.core.academics.database.AcademicsEntity
import com.wafflestudio.csereal.core.academics.database.CourseEntity
import com.wafflestudio.csereal.core.news.database.NewsEntity
import com.wafflestudio.csereal.core.notice.database.NoticeEntity
import com.wafflestudio.csereal.core.research.database.LabEntity
import com.wafflestudio.csereal.core.research.database.ResearchEntity
import com.wafflestudio.csereal.core.seminar.database.SeminarEntity
Expand All @@ -24,6 +25,10 @@ class AttachmentEntity(
@JoinColumn(name = "news_id")
var news: NewsEntity? = null,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "notice_id")
var notice: NoticeEntity? = null,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "seminar_id")
var seminar: SeminarEntity? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.wafflestudio.csereal.core.about.database.AboutEntity
import com.wafflestudio.csereal.core.academics.database.AcademicsEntity
import com.wafflestudio.csereal.core.academics.database.CourseEntity
import com.wafflestudio.csereal.core.news.database.NewsEntity
import com.wafflestudio.csereal.core.notice.database.NoticeEntity
import com.wafflestudio.csereal.core.research.database.LabEntity
import com.wafflestudio.csereal.core.research.database.ResearchEntity
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
Expand Down Expand Up @@ -127,6 +128,10 @@ class AttachmentServiceImpl(
contentEntity.attachments.add(attachment)
attachment.news = contentEntity
}
is NoticeEntity -> {
contentEntity.attachments.add(attachment)
attachment.notice = contentEntity
}
is SeminarEntity -> {
contentEntity.attachments.add(attachment)
attachment.seminar = contentEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SeminarServiceImpl(

val imageURL = mainImageService.createImageURL(seminar.mainImage)
val attachmentResponses = attachmentService.createAttachmentResponses(seminar.attachments)

return SeminarDto.of(seminar, imageURL, attachmentResponses, null)
}
@Transactional
Expand Down

0 comments on commit fa44f43

Please sign in to comment.