Skip to content

Commit

Permalink
[fix #102] 컨텐츠 생성, 수정 response 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jimin3263 authored Aug 16, 2024
1 parent 5d6fa93 commit db2fd1b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class ContentController(
fun getContent(
@AuthenticationPrincipal user: PrincipalUser,
@PathVariable("contentId") contentId: Long
): ResponseEntity<GetContentResponse> {
): ResponseEntity<ContentResponse> {
return contentUseCase.getContent(user.id, contentId)
.toResponse()
.wrapOk()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
package com.pokit.content.dto.response

import com.pokit.content.model.CategoryInfo
import com.pokit.content.model.Content
import java.time.format.DateTimeFormatter

data class ContentResponse(
val contentId: Long,
val categoryId: Long,
val data: String,
val title: String,
val memo: String,
val alertYn: String,
val createdAt: String,
val favorites: Boolean = false
)

data class GetContentResponse(
data class ContentResponse(
val contentId: Long,
val category: CategoryInfo,
val data: String,
Expand All @@ -26,20 +15,10 @@ data class GetContentResponse(
val favorites: Boolean = false
)

fun Content.toResponse() = ContentResponse(
contentId = this.id,
categoryId = this.categoryId,
data = this.data,
title = this.title,
memo = this.memo,
alertYn = this.alertYn,
createdAt = this.createdAt.toString()
)

fun GetContentResult.toResponse(): GetContentResponse {
fun ContentResult.toResponse(): ContentResponse {
val formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd")

return GetContentResponse(
return ContentResponse(
contentId = this.contentId,
category = this.category,
data = this.data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import com.pokit.content.dto.request.ContentCommand
import com.pokit.content.dto.response.ContentsResult
import com.pokit.content.dto.request.ContentSearchCondition
import com.pokit.content.dto.response.BookMarkContentResponse
import com.pokit.content.dto.response.GetContentResult
import com.pokit.content.dto.response.ContentResult
import com.pokit.content.dto.response.RemindContentResult
import com.pokit.content.model.Content
import com.pokit.user.model.User
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice

interface ContentUseCase {
fun bookmarkContent(user: User, contentId: Long): BookMarkContentResponse

fun create(user: User, contentCommand: ContentCommand): Content
fun create(user: User, contentCommand: ContentCommand): ContentResult

fun update(user: User, contentCommand: ContentCommand, contentId: Long): Content
fun update(user: User, contentCommand: ContentCommand, contentId: Long): ContentResult

fun delete(user: User, contentId: Long)

Expand All @@ -30,7 +29,7 @@ interface ContentUseCase {

fun getContentsByCategoryName(userId: Long, categoryName: String, pageable: Pageable): Slice<ContentsResult>

fun getContent(userId: Long, contentId: Long): GetContentResult
fun getContent(userId: Long, contentId: Long): ContentResult

fun getBookmarkContents(userId: Long, pageable: Pageable): Slice<RemindContentResult>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.pokit.category.exception.CategoryErrorCode
import com.pokit.category.model.Category
import com.pokit.category.model.RemindCategory
import com.pokit.category.port.out.CategoryPort
import com.pokit.category.port.service.loadCategoryOrThrow
import com.pokit.common.exception.NotFoundCustomException
import com.pokit.content.dto.request.ContentCommand
import com.pokit.content.dto.request.ContentSearchCondition
Expand Down Expand Up @@ -47,20 +48,21 @@ class ContentService(
}

@Transactional
override fun create(user: User, contentCommand: ContentCommand): Content {
verifyCategory(contentCommand.categoryId, user.id)
override fun create(user: User, contentCommand: ContentCommand): ContentResult {
val category = categoryPort.loadCategoryOrThrow(contentCommand.categoryId, user.id)
val content = contentCommand.toDomain()
content.parseDomain()
return contentPort.persist(content)
.toGetContentResult(false, category)
}

@Transactional
override fun update(user: User, contentCommand: ContentCommand, contentId: Long): Content {
override fun update(user: User, contentCommand: ContentCommand, contentId: Long): ContentResult {
val category = categoryPort.loadCategoryOrThrow(contentCommand.categoryId, user.id)
val content = verifyContent(user.id, contentId)
verifyCategory(contentCommand.categoryId, user.id)

content.modify(contentCommand)
return contentPort.persist(content)
.toGetContentResult(bookMarkPort.isBookmarked(contentId, user.id), category)
}

@Transactional
Expand Down Expand Up @@ -94,7 +96,7 @@ class ContentService(


@Transactional
override fun getContent(userId: Long, contentId: Long): GetContentResult {
override fun getContent(userId: Long, contentId: Long): ContentResult {
val userLog = UserLog(
contentId, userId, LogType.READ
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.pokit.content.model.CategoryInfo
import com.pokit.content.model.Content
import java.time.LocalDateTime

data class GetContentResult(
data class ContentResult(
val contentId: Long,
val category: CategoryInfo,
val data: String,
Expand All @@ -16,8 +16,8 @@ data class GetContentResult(
val favorites: Boolean = false
)

fun Content.toGetContentResult(favorites: Boolean, category: Category): GetContentResult {
return GetContentResult(
fun Content.toGetContentResult(favorites: Boolean, category: Category): ContentResult {
return ContentResult(
contentId = this.id,
category = CategoryInfo(category.categoryId, category.categoryName),
data = this.data,
Expand Down

0 comments on commit db2fd1b

Please sign in to comment.