Skip to content

Commit

Permalink
refactor(domains): rename Request and Response models
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Dec 4, 2024
1 parent 9ca127c commit d37a3fa
Show file tree
Hide file tree
Showing 53 changed files with 307 additions and 305 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/net/yoshinorin/qualtet/Modules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import net.yoshinorin.qualtet.domains.tags.{TagRepository, TagService}
import net.yoshinorin.qualtet.auth.Signature
import net.yoshinorin.qualtet.domains.feeds.FeedService
import net.yoshinorin.qualtet.cache.CacheService
import net.yoshinorin.qualtet.domains.articles.ResponseArticleWithCount
import net.yoshinorin.qualtet.domains.articles.ArticleWithCountResponseModel
import net.yoshinorin.qualtet.http.{AuthProvider, CorsProvider}
import net.yoshinorin.qualtet.http.routes.HomeRoute
import net.yoshinorin.qualtet.http.routes.v1.{
Expand Down Expand Up @@ -134,9 +134,9 @@ class Modules(tx: Transactor[IO]) {
val sitemapCache: CacheModule[String, Seq[Url]] = new CacheModule[String, Seq[Url]](sitemapCaffeinCache)
val sitemapService = new SitemapService(sitemapRepository, sitemapCache)

val feedCaffeinCache: CaffeineCache[String, ResponseArticleWithCount] =
Caffeine.newBuilder().expireAfterAccess(config.cache.feed, TimeUnit.SECONDS).build[String, ResponseArticleWithCount]
val feedCache: CacheModule[String, ResponseArticleWithCount] = new CacheModule[String, ResponseArticleWithCount](feedCaffeinCache)
val feedCaffeinCache: CaffeineCache[String, ArticleWithCountResponseModel] =
Caffeine.newBuilder().expireAfterAccess(config.cache.feed, TimeUnit.SECONDS).build[String, ArticleWithCountResponseModel]
val feedCache: CacheModule[String, ArticleWithCountResponseModel] = new CacheModule[String, ArticleWithCountResponseModel](feedCaffeinCache)
val feedService = new FeedService(feedCache, articleService)

val cacheService = new CacheService(
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/net/yoshinorin/qualtet/auth/AuthService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package net.yoshinorin.qualtet.auth

import cats.effect.IO
import cats.Monad
import net.yoshinorin.qualtet.domains.authors.{AuthorId, AuthorService, BCryptPassword, ResponseAuthor}
import net.yoshinorin.qualtet.domains.authors.{AuthorId, AuthorResponseModel, AuthorService, BCryptPassword}
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import net.yoshinorin.qualtet.domains.errors.{AuthorNotFound, Unauthorized}
import net.yoshinorin.qualtet.syntax.*
Expand Down Expand Up @@ -35,7 +35,7 @@ class AuthService[F[_]: Monad](authorService: AuthorService[F], jwt: Jwt) {

}

def findAuthorFromJwtString(jwtString: String): IO[Option[ResponseAuthor]] = {
def findAuthorFromJwtString(jwtString: String): IO[Option[AuthorResponseModel]] = {
jwt.decode[IO](jwtString).flatMap {
case Right(jwtClaim: JwtClaim) =>
authorService.findById(AuthorId(jwtClaim.sub))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class ArchiveService[F[_]: Monad](
contentTypeService: ContentTypeService[F]
)(using executer: Executer[F, IO]) {

def cont(contentTypeId: ContentTypeId): ContT[F, Seq[ResponseArchive], Seq[ResponseArchive]] = {
ContT.apply[F, Seq[ResponseArchive], Seq[ResponseArchive]] { next =>
def cont(contentTypeId: ContentTypeId): ContT[F, Seq[ArchiveResponseModel], Seq[ArchiveResponseModel]] = {
ContT.apply[F, Seq[ArchiveResponseModel], Seq[ArchiveResponseModel]] { next =>
archiveRepository.get(contentTypeId).map { archives =>
archives.map(a => ResponseArchive(a.path, a.title, a.publishedAt))
archives.map(a => ArchiveResponseModel(a.path, a.title, a.publishedAt))
}
}
}

def get: IO[Seq[ResponseArchive]] = {
def get: IO[Seq[ArchiveResponseModel]] = {
for {
c <- contentTypeService.findByName("article").throwIfNone(ContentTypeNotFound(detail = "content-type not found: article"))
articles <- executer.transact(cont(c.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import com.github.plokhotnyuk.jsoniter_scala.macros.*
import com.github.plokhotnyuk.jsoniter_scala.core.*
import net.yoshinorin.qualtet.domains.Path

final case class ResponseArchive(
final case class ArchiveResponseModel(
path: Path,
title: String,
publishedAt: Long
)

object ResponseArchive {
given codecContent: JsonValueCodec[ResponseArchive] = JsonCodecMaker.make
given codecContents: JsonValueCodec[Seq[ResponseArchive]] = JsonCodecMaker.make
object ArchiveResponseModel {
given codecContent: JsonValueCodec[ArchiveResponseModel] = JsonCodecMaker.make
given codecContents: JsonValueCodec[Seq[ArchiveResponseModel]] = JsonCodecMaker.make
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class ArticleService[F[_]: Monad](
contentTypeId: ContentTypeId,
none: Unit = (),
queryParams: ArticlesQueryParameter
): ContT[F, Seq[(Int, ResponseArticle)], Seq[(Int, ResponseArticle)]] = {
ContT.apply[F, Seq[(Int, ResponseArticle)], Seq[(Int, ResponseArticle)]] { next =>
): ContT[F, Seq[(Int, ArticleResponseModel)], Seq[(Int, ArticleResponseModel)]] = {
ContT.apply[F, Seq[(Int, ArticleResponseModel)], Seq[(Int, ArticleResponseModel)]] { next =>
articleRepository.getWithCount(contentTypeId, queryParams).map { article =>
article.map { case (count, article) =>
(count, ResponseArticle(article.id, article.path, article.title, article.content, article.publishedAt, article.updatedAt))
(count, ArticleResponseModel(article.id, article.path, article.title, article.content, article.publishedAt, article.updatedAt))
}
}
}
Expand All @@ -36,11 +36,11 @@ class ArticleService[F[_]: Monad](
contentTypeId: ContentTypeId,
tagName: TagName,
queryParams: ArticlesQueryParameter
): ContT[F, Seq[(Int, ResponseArticle)], Seq[(Int, ResponseArticle)]] = {
ContT.apply[F, Seq[(Int, ResponseArticle)], Seq[(Int, ResponseArticle)]] { next =>
): ContT[F, Seq[(Int, ArticleResponseModel)], Seq[(Int, ArticleResponseModel)]] = {
ContT.apply[F, Seq[(Int, ArticleResponseModel)], Seq[(Int, ArticleResponseModel)]] { next =>
articleRepository.findByTagNameWithCount(contentTypeId, tagName, queryParams).map { article =>
article.map { case (count, article) =>
(count, ResponseArticle(article.id, article.path, article.title, article.content, article.publishedAt, article.updatedAt))
(count, ArticleResponseModel(article.id, article.path, article.title, article.content, article.publishedAt, article.updatedAt))
}
}
}
Expand All @@ -50,11 +50,11 @@ class ArticleService[F[_]: Monad](
contentTypeId: ContentTypeId,
seriesName: SeriesName,
queryParams: ArticlesQueryParameter // TODO: `Optional`
): ContT[F, Seq[(Int, ResponseArticle)], Seq[(Int, ResponseArticle)]] = {
ContT.apply[F, Seq[(Int, ResponseArticle)], Seq[(Int, ResponseArticle)]] { next =>
): ContT[F, Seq[(Int, ArticleResponseModel)], Seq[(Int, ArticleResponseModel)]] = {
ContT.apply[F, Seq[(Int, ArticleResponseModel)], Seq[(Int, ArticleResponseModel)]] { next =>
articleRepository.findBySeriesNameWithCount(contentTypeId, seriesName).map { article =>
article.map { case (count, article) =>
(count, ResponseArticle(article.id, article.path, article.title, article.content, article.publishedAt, article.updatedAt))
(count, ArticleResponseModel(article.id, article.path, article.title, article.content, article.publishedAt, article.updatedAt))
}
}
}
Expand All @@ -63,27 +63,29 @@ class ArticleService[F[_]: Monad](
def get[A](
data: A = (),
queryParam: ArticlesQueryParameter
)(f: (ContentTypeId, A, ArticlesQueryParameter) => ContT[F, Seq[(Int, ResponseArticle)], Seq[(Int, ResponseArticle)]]): IO[ResponseArticleWithCount] = {
)(
f: (ContentTypeId, A, ArticlesQueryParameter) => ContT[F, Seq[(Int, ArticleResponseModel)], Seq[(Int, ArticleResponseModel)]]
): IO[ArticleWithCountResponseModel] = {
for {
c <- contentTypeService.findByName("article").throwIfNone(ContentTypeNotFound(detail = "content-type not found: article"))
articlesWithCount <- executer.transact(f(c.id, data, queryParam))
} yield
if (articlesWithCount.nonEmpty) {
ResponseArticleWithCount(articlesWithCount.map(_._1).headOption.getOrElse(0), articlesWithCount.map(_._2))
ArticleWithCountResponseModel(articlesWithCount.map(_._1).headOption.getOrElse(0), articlesWithCount.map(_._2))
} else {
throw ArticleNotFound(detail = "articles not found")
}
}

def getWithCount(queryParam: ArticlesQueryParameter): IO[ResponseArticleWithCount] = {
def getWithCount(queryParam: ArticlesQueryParameter): IO[ArticleWithCountResponseModel] = {
this.get(queryParam = queryParam)(cont)
}

def getByTagNameWithCount(tagName: TagName, queryParam: ArticlesQueryParameter): IO[ResponseArticleWithCount] = {
def getByTagNameWithCount(tagName: TagName, queryParam: ArticlesQueryParameter): IO[ArticleWithCountResponseModel] = {
this.get(tagName, queryParam)(tagCont)
}

def getBySeriesName(seriesName: SeriesName): IO[ResponseArticleWithCount] = {
def getBySeriesName(seriesName: SeriesName): IO[ArticleWithCountResponseModel] = {
this.get(seriesName, ArticlesQueryParameter(0, 100))(seriesCont)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import net.yoshinorin.qualtet.syntax.*

import scala.util.Random

final case class ResponseArticle(
final case class ArticleResponseModel(
id: ContentId,
path: Path,
title: String,
Expand All @@ -17,14 +17,14 @@ final case class ResponseArticle(
updatedAt: Long
)

object ResponseArticle {
given codecResponseArticle: JsonValueCodec[ResponseArticle] = JsonCodecMaker.make
given codecResponseArticles: JsonValueCodec[Seq[ResponseArticle]] = JsonCodecMaker.make
object ArticleResponseModel {
given codecResponseArticle: JsonValueCodec[ArticleResponseModel] = JsonCodecMaker.make
given codecResponseArticles: JsonValueCodec[Seq[ArticleResponseModel]] = JsonCodecMaker.make

def apply(id: ContentId, path: Path, title: String, content: String, publishedAt: Long, updatedAt: Long): ResponseArticle = {
def apply(id: ContentId, path: Path, title: String, content: String, publishedAt: Long, updatedAt: Long): ArticleResponseModel = {
val stripedContent = content.stripHtmlTags
val stripedContentLen = if (stripedContent.length > 100) 100 else stripedContent.length
new ResponseArticle(
new ArticleResponseModel(
id,
path,
title,
Expand All @@ -35,12 +35,12 @@ object ResponseArticle {
}
}

final case class ResponseArticleWithCount(
final case class ArticleWithCountResponseModel(
count: Int,
articles: Seq[ResponseArticle]
articles: Seq[ArticleResponseModel]
)

object ResponseArticleWithCount {
given codecResponseArticleWithCount: JsonValueCodec[ResponseArticleWithCount] = JsonCodecMaker.make
given codecResponseArticlesWithCount: JsonValueCodec[Seq[ResponseArticleWithCount]] = JsonCodecMaker.make
object ArticleWithCountResponseModel {
given codecResponseArticleWithCount: JsonValueCodec[ArticleWithCountResponseModel] = JsonCodecMaker.make
given codecResponseArticlesWithCount: JsonValueCodec[Seq[ArticleWithCountResponseModel]] = JsonCodecMaker.make
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class AuthorService[F[_]: Monad](
}
}

def fetchCont: ContT[F, Seq[ResponseAuthor], Seq[ResponseAuthor]] = {
ContT.apply[F, Seq[ResponseAuthor], Seq[ResponseAuthor]] { next =>
def fetchCont: ContT[F, Seq[AuthorResponseModel], Seq[AuthorResponseModel]] = {
ContT.apply[F, Seq[AuthorResponseModel], Seq[AuthorResponseModel]] { next =>
authorRepository.getAll().map { authors =>
authors.map { author =>
ResponseAuthor(
AuthorResponseModel(
id = author.id,
name = author.name,
displayName = author.displayName,
Expand All @@ -40,11 +40,11 @@ class AuthorService[F[_]: Monad](
}
}

def findByIdCont(id: AuthorId): ContT[F, Option[ResponseAuthor], Option[ResponseAuthor]] = {
ContT.apply[F, Option[ResponseAuthor], Option[ResponseAuthor]] { next =>
def findByIdCont(id: AuthorId): ContT[F, Option[AuthorResponseModel], Option[AuthorResponseModel]] = {
ContT.apply[F, Option[AuthorResponseModel], Option[AuthorResponseModel]] { next =>
authorRepository.findById(id).map { author =>
author.map { a =>
ResponseAuthor(
AuthorResponseModel(
id = a.id,
name = a.name,
displayName = a.displayName,
Expand Down Expand Up @@ -75,11 +75,11 @@ class AuthorService[F[_]: Monad](
}
}

def findByNameCont(name: AuthorName): ContT[F, Option[ResponseAuthor], Option[ResponseAuthor]] = {
ContT.apply[F, Option[ResponseAuthor], Option[ResponseAuthor]] { next =>
def findByNameCont(name: AuthorName): ContT[F, Option[AuthorResponseModel], Option[AuthorResponseModel]] = {
ContT.apply[F, Option[AuthorResponseModel], Option[AuthorResponseModel]] { next =>
authorRepository.findByName(name).map { author =>
author.map { a =>
ResponseAuthor(
AuthorResponseModel(
id = a.id,
name = a.name,
displayName = a.displayName,
Expand All @@ -96,7 +96,7 @@ class AuthorService[F[_]: Monad](
* @param data Instance of Author
* @return Instance of created Author with IO
*/
def create(data: Author): IO[ResponseAuthor] = {
def create(data: Author): IO[AuthorResponseModel] = {
for {
_ <- executer.transact(upsertCont(data))
a <- this.findByName(data.name).throwIfNone(UnexpectedException("user not found"))
Expand All @@ -108,7 +108,7 @@ class AuthorService[F[_]: Monad](
*
* @return Authors
*/
def getAll: IO[Seq[ResponseAuthor]] = {
def getAll: IO[Seq[AuthorResponseModel]] = {
executer.transact(fetchCont)
}

Expand All @@ -118,7 +118,7 @@ class AuthorService[F[_]: Monad](
* @param id authorName's id
* @return Author
*/
def findById(id: AuthorId): IO[Option[ResponseAuthor]] = {
def findById(id: AuthorId): IO[Option[AuthorResponseModel]] = {
executer.transact(findByIdCont(id))
}

Expand All @@ -138,7 +138,7 @@ class AuthorService[F[_]: Monad](
* @param name authorName's name
* @return Author
*/
def findByName(name: AuthorName): IO[Option[ResponseAuthor]] = {
def findByName(name: AuthorName): IO[Option[AuthorResponseModel]] = {
executer.transact(findByNameCont(name))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import java.time.ZonedDateTime
import com.github.plokhotnyuk.jsoniter_scala.macros.*
import com.github.plokhotnyuk.jsoniter_scala.core.*

final case class ResponseAuthor(
final case class AuthorResponseModel(
id: AuthorId = AuthorId.apply(),
name: AuthorName,
displayName: AuthorDisplayName,
createdAt: Long = ZonedDateTime.now.toEpochSecond
)

object ResponseAuthor {
given codecAuthor: JsonValueCodec[ResponseAuthor] = JsonCodecMaker.make
given codecAuthors: JsonValueCodec[Seq[ResponseAuthor]] = JsonCodecMaker.make
object AuthorResponseModel {
given codecAuthor: JsonValueCodec[AuthorResponseModel] = JsonCodecMaker.make
given codecAuthors: JsonValueCodec[Seq[AuthorResponseModel]] = JsonCodecMaker.make
}
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: RequestContent): IO[Content] = {
def createContentFromRequest(authorName: AuthorName, request: ContentRequestModel): IO[Content] = {

def createContentTagging(contentId: ContentId, tags: Option[List[Tag]]): IO[Option[List[ContentTagging]]] = {
tags match {
Expand Down Expand Up @@ -271,7 +271,7 @@ class ContentService[F[_]: Monad](
* @param path a content path
* @return ResponseContent instance
*/
def findByPathWithMeta(path: Path): IO[Option[ResponseContent]] = {
def findByPathWithMeta(path: Path): IO[Option[ContentResponseModel]] = {
this.findBy(path)(findByPathWithMetaCont)
}

Expand All @@ -285,7 +285,7 @@ class ContentService[F[_]: Monad](
executer.transact(findByIdCont(id))
}

def findBy[A](data: A)(f: A => ContT[F, Option[ContentWithMeta], Option[ContentWithMeta]]): IO[Option[ResponseContent]] = {
def findBy[A](data: A)(f: A => ContT[F, Option[ContentWithMeta], Option[ContentWithMeta]]): IO[Option[ContentResponseModel]] = {
executer.transact(f(data)).flatMap {
case None => IO(None)
case Some(x) =>
Expand All @@ -295,7 +295,7 @@ class ContentService[F[_]: Monad](

IO(
Some(
ResponseContent(
ContentResponseModel(
id = x.id,
title = x.title,
robotsAttributes = x.robotsAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.yoshinorin.qualtet.domains.series.SeriesName
import net.yoshinorin.qualtet.domains.errors.{ContentTitleRequired, HtmlContentRequired, RawContentRequired}
import net.yoshinorin.qualtet.syntax.*

final case class RequestContent(
final case class ContentRequestModel(
contentType: String,
robotsAttributes: Attributes, // TODO: Consider to use `Option[Attributes]`
externalResources: List[ExternalResources] = List(),
Expand All @@ -23,10 +23,10 @@ final case class RequestContent(
htmlContent: String,
publishedAt: Long = ZonedDateTime.now.toEpochSecond,
updatedAt: Long = ZonedDateTime.now.toEpochSecond
) extends Request[RequestContent] {
) extends Request[ContentRequestModel] {
// NOTE: see `net.yoshinorin.qualtet.domains.Request` comment.
def postDecode: RequestContent = {
new RequestContent(
def postDecode: ContentRequestModel = {
new ContentRequestModel(
contentType = contentType,
robotsAttributes = this.robotsAttributes.sort,
externalResources = externalResources,
Expand All @@ -42,9 +42,9 @@ final case class RequestContent(
}
}

object RequestContent {
given codecRequestContent: JsonValueCodec[RequestContent] = JsonCodecMaker.make
given codecRequestContents: JsonValueCodec[List[RequestContent]] = JsonCodecMaker.make
object ContentRequestModel {
given codecRequestContent: JsonValueCodec[ContentRequestModel] = JsonCodecMaker.make
given codecRequestContents: JsonValueCodec[List[ContentRequestModel]] = JsonCodecMaker.make

def apply(
contentType: String,
Expand All @@ -58,8 +58,8 @@ object RequestContent {
htmlContent: String,
publishedAt: Long = ZonedDateTime.now.toEpochSecond,
updatedAt: Long = ZonedDateTime.now.toEpochSecond
): RequestContent = {
new RequestContent(
): ContentRequestModel = {
new ContentRequestModel(
contentType = contentType,
robotsAttributes = robotsAttributes,
externalResources = externalResources,
Expand Down
Loading

0 comments on commit d37a3fa

Please sign in to comment.