Skip to content

Commit

Permalink
refactor(content): separating Response case class from Content class
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Dec 4, 2024
1 parent 41f6cf7 commit 6b29ae7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ContentService[F[_]: Monad](
* @param request RequestContent
* @return created Content with IO
*/
def createContentFromRequest(authorName: AuthorName, request: ContentRequestModel): IO[Content] = {
def createContentFromRequest(authorName: AuthorName, request: ContentRequestModel): IO[ContentResponseModel] = {

def createContentTagging(contentId: ContentId, tags: Option[List[Tag]]): IO[Option[List[ContentTagging]]] = {
tags match {
Expand Down Expand Up @@ -180,7 +180,17 @@ class ContentService[F[_]: Monad](
maybeContentSerializing,
request.externalResources
)
} yield createdContent
} yield ContentResponseModel(
id = createdContent.id,
authorId = a.id,
contentTypeId = c.id,
path = createdContent.path,
title = createdContent.title,
rawContent = createdContent.rawContent,
htmlContent = createdContent.htmlContent,
publishedAt = createdContent.publishedAt,
updatedAt = createdContent.updatedAt
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ final case class Content(
updatedAt: Long = ZonedDateTime.now.toEpochSecond
)

object Content {
given codecContent: JsonValueCodec[Content] = JsonCodecMaker.make
given codecContents: JsonValueCodec[List[Content]] = JsonCodecMaker.make
}

final case class ContentWithMeta(
id: ContentId,
title: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@ package net.yoshinorin.qualtet.domains.contents

import com.github.plokhotnyuk.jsoniter_scala.macros.*
import com.github.plokhotnyuk.jsoniter_scala.core.*
import net.yoshinorin.qualtet.domains.authors.AuthorName
import net.yoshinorin.qualtet.domains.authors.{AuthorId, AuthorName}
import net.yoshinorin.qualtet.domains.contentTypes.ContentTypeId
import net.yoshinorin.qualtet.domains.Path
import net.yoshinorin.qualtet.domains.externalResources.ExternalResources
import net.yoshinorin.qualtet.domains.robots.Attributes
import net.yoshinorin.qualtet.domains.tags.Tag

final case class ContentResponseModel(
id: ContentId,
authorId: AuthorId,
contentTypeId: ContentTypeId,
path: Path,
title: String,
rawContent: String,
htmlContent: String,
publishedAt: Long,
updatedAt: Long
)

object ContentResponseModel {
given codecContent: JsonValueCodec[ContentResponseModel] = JsonCodecMaker.make
}

final case class ContentDetailResponseModel(
id: ContentId,
title: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import org.http4s.{ContextRequest, Request}
import org.slf4j.LoggerFactory
import net.yoshinorin.qualtet.domains.Path
import net.yoshinorin.qualtet.domains.authors.AuthorResponseModel
import net.yoshinorin.qualtet.domains.contents.{ContentId, ContentService}
import net.yoshinorin.qualtet.domains.contents.ContentRequestModel
import net.yoshinorin.qualtet.domains.contents.{ContentDetailResponseModel, ContentId, ContentRequestModel, ContentResponseModel, ContentService}
import net.yoshinorin.qualtet.http.{AuthProvider, RequestDecoder}
import net.yoshinorin.qualtet.syntax.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.typelevel.ci.*
import net.yoshinorin.qualtet.auth.RequestToken
import net.yoshinorin.qualtet.domains.authors.AuthorResponseModel
import net.yoshinorin.qualtet.domains.Path
import net.yoshinorin.qualtet.domains.contents.{Content, ContentDetailResponseModel, ContentId, ContentRequestModel}
import net.yoshinorin.qualtet.domains.contents.{ContentDetailResponseModel, ContentId, ContentRequestModel, ContentResponseModel}
import net.yoshinorin.qualtet.domains.robots.Attributes
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.*
Expand Down Expand Up @@ -51,7 +51,7 @@ class ContentRouteV1Spec extends AnyWordSpec {
assert(response.status === Created)
assert(response.contentType.get === `Content-Type`(MediaType.application.json))

val maybeContent = unsafeDecode[Content](response)
val maybeContent = unsafeDecode[ContentResponseModel](response)
assert(maybeContent.authorId === validAuthor.id)
// assert(maybeContent.contentTypeId === 'TODO') TODO: assert contentTypeId
assert(maybeContent.htmlContent === "<p>this is a html ContentRouteSpec1<p>")
Expand Down

0 comments on commit 6b29ae7

Please sign in to comment.