Skip to content

Commit

Permalink
refactor(models): divide write and others
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Nov 19, 2024
1 parent cad204a commit 344d5d7
Show file tree
Hide file tree
Showing 36 changed files with 175 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ class AuthorService[F[_]: Monad](

def upsertCont(data: Author): ContT[F, Int, Int] = {
ContT.apply[F, Int, Int] { next =>
authorRepository.upsert(data)
val w = AuthorWriteModel(
id = data.id,
name = data.name,
displayName = data.displayName,
password = data.password,
createdAt = data.createdAt
)
authorRepository.upsert(w)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.yoshinorin.qualtet.domains.authors

final case class AuthorWriteModel(
id: AuthorId,
name: AuthorName,
displayName: AuthorDisplayName,
password: BCryptPassword,
createdAt: Long
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import doobie.util.update.Update

object AuthorQuery {

def upsert: Write[Author] ?=> Update[Author] = {
def upsert: Write[AuthorWriteModel] ?=> Update[AuthorWriteModel] = {
val q = s"""
INSERT INTO authors (id, name, display_name, password, created_at)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
display_name = VALUES(display_name),
password = VALUES(password)
"""
Update[Author](q)
Update[AuthorWriteModel](q)
}

def getAll: Read[AuthorWithoutPasswordReadModel] ?=> Query0[AuthorWithoutPasswordReadModel] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ trait AuthorRepository[F[_]] {
/**
* create a authorName
*
* @param data Instance of Author
* @param data Instance of AuthorWriteModel
* @return dummy long id (Doobie return Int)
*/
def upsert(data: Author): F[Int]
def upsert(data: AuthorWriteModel): F[Int]

/**
* find a Author by id
Expand Down Expand Up @@ -70,14 +70,14 @@ object AuthorRepository {
Some(AuthorReadModel(AuthorId(id), AuthorName(name), AuthorDisplayName(displayName), BCryptPassword(password), createdAt))
}

given authorWrite: Write[Author] =
given authorWrite: Write[AuthorWriteModel] =
Write[(String, String, String, String, Long)]
.contramap(a => (a.id.value, a.name.value, a.displayName.value, a.password.value, a.createdAt))

override def getAll(): ConnectionIO[Seq[AuthorWithoutPasswordReadModel]] = AuthorQuery.getAll.to[Seq]

// TODO: Do not do `run` here
override def upsert(data: Author): ConnectionIO[Int] = AuthorQuery.upsert.run(data)
override def upsert(data: AuthorWriteModel): ConnectionIO[Int] = AuthorQuery.upsert.run(data)

override def findById(id: AuthorId): ConnectionIO[Option[AuthorWithoutPasswordReadModel]] = AuthorQuery.findById(id).option

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class ContentSerializingService[F[_]: Monad](
def upsertCont(data: Option[ContentSerializing]): ContT[F, Int, Int] = {
ContT.apply[F, Int, Int] { next =>
data match {
case Some(d) => contentSerializingRepository.upsert(d)
case Some(d) => {
val w = ContentSerializingWriteModel(seriesId = d.seriesId, contentId = d.contentId)
contentSerializingRepository.upsert(w)
}
case None => contentSerializingRepository.fakeRequestInt
}
}
Expand All @@ -30,7 +33,10 @@ class ContentSerializingService[F[_]: Monad](
def bulkUpsertCont(data: Option[List[ContentSerializing]]): ContT[F, Int, Int] = {
ContT.apply[F, Int, Int] { next =>
data match {
case Some(d) => contentSerializingRepository.bulkUpsert(d)
case Some(d) => {
val ws = d.map(w => ContentSerializingWriteModel(seriesId = w.seriesId, contentId = w.contentId))
contentSerializingRepository.bulkUpsert(ws)
}
case None => contentSerializingRepository.fakeRequestInt
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.yoshinorin.qualtet.domains.contentSerializing

import net.yoshinorin.qualtet.domains.contents.ContentId
import net.yoshinorin.qualtet.domains.series.SeriesId

final case class ContentSerializingWriteModel(
seriesId: SeriesId,
contentId: ContentId
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ object ContentSerializingQuery {
.query[ContentSerializingReadModel]
}

def bulkUpsert: Write[ContentSerializing] ?=> Update[ContentSerializing] = {
def bulkUpsert: Write[ContentSerializingWriteModel] ?=> Update[ContentSerializingWriteModel] = {
val q = s"""
INSERT INTO contents_serializing (series_id, content_id)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE
series_id = VALUES(series_id),
content_id = VALUES(content_id)
"""
Update[ContentSerializing](q)
Update[ContentSerializingWriteModel](q)
}

def deleteBySeriesId(id: SeriesId): Update0 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import net.yoshinorin.qualtet.domains.contents.ContentId
import net.yoshinorin.qualtet.domains.series.SeriesId

trait ContentSerializingRepository[F[_]] {
def upsert(data: ContentSerializing): F[Int]
def bulkUpsert(data: List[ContentSerializing]): F[Int]
def upsert(data: ContentSerializingWriteModel): F[Int]
def bulkUpsert(data: List[ContentSerializingWriteModel]): F[Int]
def findBySeriesId(id: SeriesId): F[Seq[ContentSerializingReadModel]]
def deleteBySeriesId(id: SeriesId): F[Unit]
def deleteByContentId(id: ContentId): F[Unit]
Expand All @@ -29,14 +29,14 @@ object ContentSerializingRepository {
given contentSerializingWithOptionRead: Read[Option[ContentSerializing]] =
Read[(String, String)].map { case (seriesId, contentId) => Some(ContentSerializing(SeriesId(seriesId), ContentId(contentId))) }

given contentSerializingWrite: Write[ContentSerializing] =
given contentSerializingWrite: Write[ContentSerializingWriteModel] =
Write[(String, String)].contramap(s => (s.seriesId.value, s.contentId.value))

override def upsert(data: ContentSerializing): ConnectionIO[Int] = {
override def upsert(data: ContentSerializingWriteModel): ConnectionIO[Int] = {
ContentSerializingQuery.bulkUpsert.run(data)
}

override def bulkUpsert(data: List[ContentSerializing]): ConnectionIO[Int] = {
override def bulkUpsert(data: List[ContentSerializingWriteModel]): ConnectionIO[Int] = {
ContentSerializingQuery.bulkUpsert.updateMany(data)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class ContentTaggingService[F[_]: Monad](
def bulkUpsertCont(data: Option[List[ContentTagging]]): ContT[F, Int, Int] = {
ContT.apply[F, Int, Int] { next =>
data match {
case Some(d) => contentTaggingRepository.bulkUpsert(d)
case Some(d) => {
val ws = d.map(c => ContentTaggingWriteModel(c.contentId, c.tagId))
contentTaggingRepository.bulkUpsert(ws)
}
case None => contentTaggingRepository.fakeRequestInt
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.yoshinorin.qualtet.domains.contentTaggings

import net.yoshinorin.qualtet.domains.contents.ContentId
import net.yoshinorin.qualtet.domains.tags.TagId

final case class ContentTaggingWriteModel(
contentId: ContentId,
tagId: TagId
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ object ContentTaggingQuery {
.query[ContentTaggingReadModel]
}

def bulkUpsert: Write[ContentTagging] ?=> Update[ContentTagging] = {
def bulkUpsert: Write[ContentTaggingWriteModel] ?=> Update[ContentTaggingWriteModel] = {
val q = s"""
INSERT INTO contents_tagging (content_id, tag_id)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE
content_id = VALUES(content_id),
tag_id = VALUES(tag_id)
"""
Update[ContentTagging](q)
Update[ContentTaggingWriteModel](q)
}

def deleteByContentId(id: ContentId): Update0 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import net.yoshinorin.qualtet.domains.contents.ContentId
import net.yoshinorin.qualtet.domains.tags.TagId

trait ContentTaggingRepository[F[_]] {
def bulkUpsert(data: List[ContentTagging]): F[Int]
def bulkUpsert(data: List[ContentTaggingWriteModel]): F[Int]
def findByTagId(id: TagId): F[Seq[ContentTaggingReadModel]]
def deleteByContentId(id: ContentId): F[Unit]
def deleteByTagId(id: TagId): F[Unit]
Expand All @@ -29,10 +29,10 @@ object ContentTaggingRepository {
given contentTaggingWithOptionRead: Read[Option[ContentTaggingReadModel]] =
Read[(String, String)].map { case (contentId, tagId) => Some(ContentTaggingReadModel(ContentId(contentId), TagId(tagId))) }

given contentTaggingWrite: Write[ContentTagging] =
given contentTaggingWrite: Write[ContentTaggingWriteModel] =
Write[(String, String)].contramap(c => (c.contentId.value, c.tagId.value))

override def bulkUpsert(data: List[ContentTagging]): ConnectionIO[Int] = {
override def bulkUpsert(data: List[ContentTaggingWriteModel]): ConnectionIO[Int] = {
ContentTaggingQuery.bulkUpsert.updateMany(data)
}
override def findByTagId(id: TagId): ConnectionIO[Seq[ContentTaggingReadModel]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ContentTypeService[F[_]: Monad](

def upsertCont(data: ContentType): ContT[F, Int, Int] = {
ContT.apply[F, Int, Int] { next =>
contentRepository.upsert(data)
val w = ContentTypeWriteModel(id = data.id, name = data.name)
contentRepository.upsert(w)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.yoshinorin.qualtet.domains.contentTypes

final case class ContentTypeWriteModel(
id: ContentTypeId,
name: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import doobie.util.update.Update

object ContentTypeQuery {

def upsert: Write[ContentType] ?=> Update[ContentType] = {
def upsert: Write[ContentTypeWriteModel] ?=> Update[ContentTypeWriteModel] = {
val q = s"""
INSERT INTO content_types (id, name)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE
name = VALUES(name)
"""
Update[ContentType](q)
Update[ContentTypeWriteModel](q)
}

def getAll: Read[ContentTypeReadModel] ?=> Query0[ContentTypeReadModel] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.yoshinorin.qualtet.domains.contentTypes

trait ContentTypeRepository[F[_]] {
def upsert(data: ContentType): F[Int]
def upsert(data: ContentTypeWriteModel): F[Int]
def getAll(): F[Seq[ContentTypeReadModel]]
def findByName(name: String): F[Option[ContentTypeReadModel]]
}
Expand All @@ -19,11 +19,11 @@ object ContentTypeRepository {
given contentTypeWithOptionRead: Read[Option[ContentTypeReadModel]] =
Read[(String, String)].map { case (id, name) => Some(ContentTypeReadModel(ContentTypeId(id), name)) }

given contentTypeWrite: Write[ContentType] =
given contentTypeWrite: Write[ContentTypeWriteModel] =
Write[(String, String)].contramap(c => (c.id.value, c.name))

// TODO: do not `run` here
override def upsert(data: ContentType): ConnectionIO[Int] = {
override def upsert(data: ContentTypeWriteModel): ConnectionIO[Int] = {
ContentTypeQuery.upsert.run(data)
}
override def getAll(): ConnectionIO[Seq[ContentTypeReadModel]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ class ContentService[F[_]: Monad](

def upsertCont(data: Content): ContT[F, Int, Int] = {
ContT.apply[F, Int, Int] { next =>
contentRepository.upsert(data)
val w = ContentWriteModel(
id = data.id,
authorId = data.authorId,
contentTypeId = data.contentTypeId,
path = data.path,
title = data.title,
rawContent = data.rawContent,
htmlContent = data.htmlContent,
publishedAt = data.publishedAt,
updatedAt = data.updatedAt
)
contentRepository.upsert(w)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.yoshinorin.qualtet.domains.contents

import net.yoshinorin.qualtet.domains.authors.{AuthorId, AuthorName}
import net.yoshinorin.qualtet.domains.contentTypes.ContentTypeId
import net.yoshinorin.qualtet.domains.robots.Attributes

final case class ContentWriteModel(
id: ContentId,
authorId: AuthorId,
contentTypeId: ContentTypeId,
path: Path,
title: String,
rawContent: String,
htmlContent: String,
publishedAt: Long,
updatedAt: Long
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import doobie.util.update.{Update, Update0}

object ContentQuery {

def upsert: Write[Content] ?=> Update[Content] = {
def upsert: Write[ContentWriteModel] ?=> Update[ContentWriteModel] = {
val q = s"""
INSERT INTO contents (id, author_id, content_type_id, path, title, raw_content, html_content, published_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
Expand All @@ -19,7 +19,7 @@ object ContentQuery {
published_at = VALUES(published_at),
updated_at = VALUES(updated_at)
"""
Update[Content](q)
Update[ContentWriteModel](q)
}

def delete(id: ContentId): Update0 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.yoshinorin.qualtet.domains.contents
import net.yoshinorin.qualtet.domains.contents.{ContentId, Path}

trait ContentRepository[F[_]] {
def upsert(data: Content): F[Int]
def upsert(data: ContentWriteModel): F[Int]
def findById(id: ContentId): F[Option[ContentReadModel]]
def findByPath(path: Path): F[Option[ContentReadModel]]
def findByPathWithMeta(path: Path): F[Option[ContentWithMetaReadModel]]
Expand Down Expand Up @@ -84,7 +84,7 @@ object ContentRepository {
)
}

given contentWrite: Write[Content] =
given contentWrite: Write[ContentWriteModel] =
Write[(String, String, String, String, String, String, String, Long, Long)].contramap(c =>
(
c.id.value,
Expand All @@ -100,7 +100,7 @@ object ContentRepository {
)

// TODO: do not `run` here
override def upsert(data: Content): ConnectionIO[Int] = {
override def upsert(data: ContentWriteModel): ConnectionIO[Int] = {
ContentQuery.upsert.run(data)
}
override def findById(id: ContentId): ConnectionIO[Option[ContentReadModel]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class ExternalResourceService[F[_]: Monad](

def bulkUpsertCont(data: List[ExternalResource]): ContT[F, Int, Int] = {
ContT.apply[F, Int, Int] { next =>
externalResourceRepository.bulkUpsert(data)
val ws = data.map { d =>
ExternalResourceWriteModel(contentId = d.contentId, kind = d.kind, name = d.name)
}
externalResourceRepository.bulkUpsert(ws)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.yoshinorin.qualtet.domains.externalResources

import net.yoshinorin.qualtet.domains.contents.ContentId

final case class ExternalResourceWriteModel(
contentId: ContentId,
kind: ExternalResourceKind,
name: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import net.yoshinorin.qualtet.domains.contents.ContentId

object ExternalResourceQuery {

def bulkUpsert: Write[ExternalResource] ?=> Update[ExternalResource] = {
def bulkUpsert: Write[ExternalResourceWriteModel] ?=> Update[ExternalResourceWriteModel] = {
val q = s"""
INSERT INTO external_resources (content_id, kind, name)
VALUES (?, ?, ?)
Expand All @@ -16,7 +16,7 @@ object ExternalResourceQuery {
kind = VALUES(kind),
name = VALUES(name)
"""
Update[ExternalResource](q)
Update[ExternalResourceWriteModel](q)
}

def delete(id: ContentId): Update0 = {
Expand Down
Loading

0 comments on commit 344d5d7

Please sign in to comment.