Skip to content

Commit

Permalink
Merge pull request #33 from MEOGO-DSM/32-modify-community-post
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-32) modify community post
  • Loading branch information
meltapplee authored Sep 14, 2024
2 parents 0fc1a4c + 3b31882 commit 359a997
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 13 deletions.
19 changes: 14 additions & 5 deletions src/main/kotlin/org/meogo/domain/post/domain/Post.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class Post(
val id: Long = 0,

@Column(nullable = false)
val title: String,
var title: String,

@Column(nullable = false)
val content: String,
var content: String,

@Column(name = "user_id", columnDefinition = "BINARY(16)")
val userId: UUID,
Expand All @@ -32,13 +32,22 @@ class Post(
val date: LocalDateTime,

@Column(name = "school_id")
val schoolId: Int?,
var schoolId: Int?,

@Column(name = "key_word")
val keyWord: String?,
var keyWord: String?,

val image: String?
var image: String?
) {
fun update(title: String, content: String, schoolId: Int? = null, keyWord: String? = null, image: String?): Post {
this.title = title
this.content = content
this.schoolId = schoolId
this.keyWord = keyWord
this.image = image
return this
}

fun addGood() {
this.good += 1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.meogo.domain.post.present
package org.meogo.domain.post.presentation

import lombok.RequiredArgsConstructor
import org.meogo.domain.post.present.dto.request.PostRequest
import org.meogo.domain.post.presentation.dto.request.PostRequest
import org.meogo.domain.post.service.CreatePostService
import org.meogo.domain.post.service.DeletePostService
import org.meogo.domain.post.service.ModifyPostService
import org.meogo.domain.post.service.QueryAllPostService
import org.meogo.domain.post.service.QuerySchoolPostService
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
Expand All @@ -25,7 +27,8 @@ class PostController(
private val createPostService: CreatePostService,
private val queryAllPostService: QueryAllPostService,
private val querySchoolPostService: QuerySchoolPostService,
private val deletePostService: DeletePostService
private val deletePostService: DeletePostService,
private val modifyPostService: ModifyPostService
) {

@PostMapping
Expand All @@ -43,6 +46,15 @@ class PostController(
fun delete(@RequestParam("post_id") id: Long) =
deletePostService.execute(id)

@PatchMapping("/modify")
fun modify(
@RequestParam("post_id") id: Long,
@Valid
@RequestPart("request")
request: PostRequest,
@RequestPart("image") image: MultipartFile?
) = modifyPostService.execute(id, request, image)

@GetMapping("/query/all")
fun queryAll() = queryAllPostService.execute()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.meogo.domain.post.present.dto.request
package org.meogo.domain.post.presentation.dto.request

import javax.validation.constraints.Size

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.meogo.domain.post.present.dto.response
package org.meogo.domain.post.presentation.dto.response

import org.meogo.domain.post.domain.Post
import org.meogo.global.s3.FileUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.meogo.domain.post.service

import org.meogo.domain.post.domain.Post
import org.meogo.domain.post.domain.PostRepository
import org.meogo.domain.post.present.dto.request.PostRequest
import org.meogo.domain.post.presentation.dto.request.PostRequest
import org.meogo.domain.user.exception.UserNotFoundException
import org.meogo.domain.user.facade.UserFacade
import org.meogo.global.s3.FileUtil
Expand Down
42 changes: 42 additions & 0 deletions src/main/kotlin/org/meogo/domain/post/service/ModifyPostService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.meogo.domain.post.service

import org.meogo.domain.post.domain.PostRepository
import org.meogo.domain.post.exception.PostNotFoundException
import org.meogo.domain.post.presentation.dto.request.PostRequest
import org.meogo.domain.user.exception.UserMisMatchException
import org.meogo.domain.user.facade.UserFacade
import org.meogo.global.s3.FileUtil
import org.meogo.global.s3.Path
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile

@Service
class ModifyPostService(
private val postRepository: PostRepository,
private val userFacade: UserFacade,
private val fileUtil: FileUtil
) {
@Transactional
fun execute(postId: Long, request: PostRequest, image: MultipartFile?) {
val user = userFacade.currentUser()
val post = postRepository.findById(postId) ?: throw PostNotFoundException

if (user!!.id != post.userId) throw UserMisMatchException

val updateImage = handleImage(post.image, image)
val schoolId = if (!request.isOk || user.enrolledSchool == null) null else user.enrolledSchool
val keyWord = request.keyWord?.joinToString(separator = ",")

postRepository.save(post.update(request.title, request.content, schoolId, keyWord, updateImage))
}

private fun handleImage(image: String?, newImage: MultipartFile?): String? {
image?.let { fileUtil.delete(it, Path.COMMUNITY) }
return if (newImage == null) {
null
} else {
fileUtil.upload(newImage, Path.COMMUNITY)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.meogo.domain.post.service

import org.meogo.domain.post.domain.PostRepository
import org.meogo.domain.post.present.dto.response.PostResponse
import org.meogo.domain.post.presentation.dto.response.PostResponse
import org.meogo.global.s3.FileUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.meogo.domain.post.service

import org.meogo.domain.post.domain.PostRepository
import org.meogo.domain.post.present.dto.response.PostResponse
import org.meogo.domain.post.presentation.dto.response.PostResponse
import org.meogo.global.s3.FileUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand Down

0 comments on commit 359a997

Please sign in to comment.