diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/authors/AuthorService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/authors/AuthorService.scala index 19135cb6..f4ab4280 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/authors/AuthorService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/authors/AuthorService.scala @@ -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) } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/authors/models/AuthorWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/authors/models/AuthorWriteModel.scala new file mode 100644 index 00000000..f67ac602 --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/authors/models/AuthorWriteModel.scala @@ -0,0 +1,9 @@ +package net.yoshinorin.qualtet.domains.authors + +final case class AuthorWriteModel( + id: AuthorId, + name: AuthorName, + displayName: AuthorDisplayName, + password: BCryptPassword, + createdAt: Long +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/authors/repositories/AuthorQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/authors/repositories/AuthorQuery.scala index c6c1a5cd..5e45b5e7 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/authors/repositories/AuthorQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/authors/repositories/AuthorQuery.scala @@ -7,7 +7,7 @@ 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 (?, ?, ?, ?, ?) @@ -15,7 +15,7 @@ object AuthorQuery { display_name = VALUES(display_name), password = VALUES(password) """ - Update[Author](q) + Update[AuthorWriteModel](q) } def getAll: Read[AuthorWithoutPasswordReadModel] ?=> Query0[AuthorWithoutPasswordReadModel] = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/authors/repositories/AuthorRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/authors/repositories/AuthorRepository.scala index c52c08b7..118b1049 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/authors/repositories/AuthorRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/authors/repositories/AuthorRepository.scala @@ -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 @@ -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 diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/ContentSerializingService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/ContentSerializingService.scala index 413856f2..9777e4a4 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/ContentSerializingService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/ContentSerializingService.scala @@ -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 } } @@ -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 } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/models/ContentSerializingWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/models/ContentSerializingWriteModel.scala new file mode 100644 index 00000000..11f97c99 --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/models/ContentSerializingWriteModel.scala @@ -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 +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/repositories/ContentSerializingQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/repositories/ContentSerializingQuery.scala index a6ff99dd..3f160130 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/repositories/ContentSerializingQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/repositories/ContentSerializingQuery.scala @@ -15,7 +15,7 @@ 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 (?, ?) @@ -23,7 +23,7 @@ object ContentSerializingQuery { series_id = VALUES(series_id), content_id = VALUES(content_id) """ - Update[ContentSerializing](q) + Update[ContentSerializingWriteModel](q) } def deleteBySeriesId(id: SeriesId): Update0 = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/repositories/ContentSerializingRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/repositories/ContentSerializingRepository.scala index 4f210232..6308b468 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/repositories/ContentSerializingRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentSerializing/repositories/ContentSerializingRepository.scala @@ -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] @@ -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) } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/ContentTaggingService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/ContentTaggingService.scala index 4bc22ccb..318d298b 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/ContentTaggingService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/ContentTaggingService.scala @@ -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 } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/models/ContentTaggingWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/models/ContentTaggingWriteModel.scala new file mode 100644 index 00000000..01a2c184 --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/models/ContentTaggingWriteModel.scala @@ -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 +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/repositories/ContentTaggingQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/repositories/ContentTaggingQuery.scala index 13314a5e..3c409d44 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/repositories/ContentTaggingQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/repositories/ContentTaggingQuery.scala @@ -15,7 +15,7 @@ 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 (?, ?) @@ -23,7 +23,7 @@ object ContentTaggingQuery { content_id = VALUES(content_id), tag_id = VALUES(tag_id) """ - Update[ContentTagging](q) + Update[ContentTaggingWriteModel](q) } def deleteByContentId(id: ContentId): Update0 = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/repositories/ContentTaggingRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/repositories/ContentTaggingRepository.scala index 17c6829f..9243f955 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/repositories/ContentTaggingRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentTaggings/repositories/ContentTaggingRepository.scala @@ -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] @@ -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]] = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/ContentTypeService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/ContentTypeService.scala index 7a27ffc4..83b34816 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/ContentTypeService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/ContentTypeService.scala @@ -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) } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/models/ContentTypeWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/models/ContentTypeWriteModel.scala new file mode 100644 index 00000000..e911d83b --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/models/ContentTypeWriteModel.scala @@ -0,0 +1,6 @@ +package net.yoshinorin.qualtet.domains.contentTypes + +final case class ContentTypeWriteModel( + id: ContentTypeId, + name: String +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/repositories/ContentTypeQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/repositories/ContentTypeQuery.scala index 14149c41..423955b6 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/repositories/ContentTypeQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/repositories/ContentTypeQuery.scala @@ -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] = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/repositories/ContentTypeRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/repositories/ContentTypeRepository.scala index e7da5a19..68598c74 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/repositories/ContentTypeRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contentTypes/repositories/ContentTypeRepository.scala @@ -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]] } @@ -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]] = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contents/ContentService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contents/ContentService.scala index 5112a553..0e22e571 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contents/ContentService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contents/ContentService.scala @@ -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) } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contents/models/ContentWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contents/models/ContentWriteModel.scala new file mode 100644 index 00000000..a7d3b7e8 --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contents/models/ContentWriteModel.scala @@ -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 +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contents/repositories/ContentQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contents/repositories/ContentQuery.scala index 98e05b7a..02b1c407 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contents/repositories/ContentQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contents/repositories/ContentQuery.scala @@ -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 (?, ?, ?, ?, ?, ?, ?, ?, ?) @@ -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 = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/contents/repositories/ContentRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/contents/repositories/ContentRepository.scala index 0b6cf14f..f2ffed5e 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/contents/repositories/ContentRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/contents/repositories/ContentRepository.scala @@ -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]] @@ -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, @@ -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]] = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/ExternalResourceService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/ExternalResourceService.scala index 1b5ff285..bebb09ca 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/ExternalResourceService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/ExternalResourceService.scala @@ -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) } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/models/ExternalResourcesWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/models/ExternalResourcesWriteModel.scala new file mode 100644 index 00000000..664a45b1 --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/models/ExternalResourcesWriteModel.scala @@ -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 +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/repositories/ExternalResourceQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/repositories/ExternalResourceQuery.scala index 7734ba58..0f20d536 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/repositories/ExternalResourceQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/repositories/ExternalResourceQuery.scala @@ -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 (?, ?, ?) @@ -16,7 +16,7 @@ object ExternalResourceQuery { kind = VALUES(kind), name = VALUES(name) """ - Update[ExternalResource](q) + Update[ExternalResourceWriteModel](q) } def delete(id: ContentId): Update0 = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/repositories/ExternalResourceRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/repositories/ExternalResourceRepository.scala index 17e787f7..81204fa2 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/repositories/ExternalResourceRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/externalResources/repositories/ExternalResourceRepository.scala @@ -3,7 +3,7 @@ package net.yoshinorin.qualtet.domains.externalResources import net.yoshinorin.qualtet.domains.contents.ContentId trait ExternalResourceRepository[F[_]] { - def bulkUpsert(data: List[ExternalResource]): F[Int] + def bulkUpsert(data: List[ExternalResourceWriteModel]): F[Int] def delete(contentId: ContentId): F[Unit] def fakeRequest(): F[Int] } @@ -17,10 +17,10 @@ object ExternalResourceRepository { given ExternalResourceRepository: ExternalResourceRepository[ConnectionIO] = { new ExternalResourceRepository[ConnectionIO] { - given tagWrite: Write[ExternalResource] = + given tagWrite: Write[ExternalResourceWriteModel] = Write[(String, String, String)].contramap(p => (p.contentId.value, p.kind.value, p.name)) - override def bulkUpsert(data: List[ExternalResource]): ConnectionIO[Int] = ExternalResourceQuery.bulkUpsert.updateMany(data) + override def bulkUpsert(data: List[ExternalResourceWriteModel]): ConnectionIO[Int] = ExternalResourceQuery.bulkUpsert.updateMany(data) override def delete(contentId: ContentId): ConnectionIO[Unit] = ExternalResourceQuery.delete(contentId).run.map(_ => ()) override def fakeRequest(): ConnectionIO[Int] = 0.pure[ConnectionIO] } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/robots/RobotsService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/robots/RobotsService.scala index bb2d4c6f..ab15bbbe 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/robots/RobotsService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/robots/RobotsService.scala @@ -9,7 +9,8 @@ class RobotsService[F[_]: Monad]( ) { def upsertCont(data: Robots): ContT[F, Int, Int] = { ContT.apply[F, Int, Int] { next => - robotsRepository.upsert(data) + val w = RobotsWriteModel(contentId = data.contentId, attributes = data.attributes) + robotsRepository.upsert(w) } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/robots/models/RobotsWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/robots/models/RobotsWriteModel.scala new file mode 100644 index 00000000..119f85a4 --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/robots/models/RobotsWriteModel.scala @@ -0,0 +1,8 @@ +package net.yoshinorin.qualtet.domains.robots + +import net.yoshinorin.qualtet.domains.contents.ContentId + +final case class RobotsWriteModel( + contentId: ContentId, + attributes: Attributes +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/robots/repositories/RobotsQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/robots/repositories/RobotsQuery.scala index 1375ae6e..2d762173 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/robots/repositories/RobotsQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/robots/repositories/RobotsQuery.scala @@ -7,14 +7,14 @@ import net.yoshinorin.qualtet.domains.contents.ContentId object RobotsQuery { - def upsert: Write[Robots] ?=> Update[Robots] = { + def upsert: Write[RobotsWriteModel] ?=> Update[RobotsWriteModel] = { val q = s""" INSERT INTO robots (content_id, attributes) VALUES (?, ?) ON DUPLICATE KEY UPDATE attributes = VALUES(attributes) """ - Update[Robots](q) + Update[RobotsWriteModel](q) } def delete(id: ContentId): Update0 = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/robots/repositories/RobotsRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/robots/repositories/RobotsRepository.scala index 619d76d5..f270b313 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/robots/repositories/RobotsRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/robots/repositories/RobotsRepository.scala @@ -3,7 +3,7 @@ package net.yoshinorin.qualtet.domains.robots import net.yoshinorin.qualtet.domains.contents.ContentId trait RobotsRepository[F[_]] { - def upsert(data: Robots): F[Int] + def upsert(data: RobotsWriteModel): F[Int] def delete(contentId: ContentId): F[Unit] } @@ -15,10 +15,10 @@ object RobotsRepository { given RobotsRepository: RobotsRepository[ConnectionIO] = { new RobotsRepository[ConnectionIO] { - given robotsWrite: Write[Robots] = + given robotsWrite: Write[RobotsWriteModel] = Write[(String, String)].contramap(p => (p.contentId.value, p.attributes.value)) - override def upsert(data: Robots): ConnectionIO[Int] = RobotsQuery.upsert.run(data) + override def upsert(data: RobotsWriteModel): ConnectionIO[Int] = RobotsQuery.upsert.run(data) override def delete(contentId: ContentId): ConnectionIO[Unit] = RobotsQuery.delete(contentId).run.map(_ => ()) } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/series/SeriesService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/series/SeriesService.scala index 95af93d3..4ecf1ee4 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/series/SeriesService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/series/SeriesService.scala @@ -17,7 +17,8 @@ class SeriesService[F[_]: Monad]( def upsertCont(data: Series): ContT[F, Int, Int] = { ContT.apply[F, Int, Int] { next => - seriesRepository.upsert(data) + val w = SeriesWriteModel(id = data.id, name = data.name, title = data.title, description = data.description) + seriesRepository.upsert(w) } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/series/models/SeriesWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/series/models/SeriesWriteModel.scala new file mode 100644 index 00000000..4db7c558 --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/series/models/SeriesWriteModel.scala @@ -0,0 +1,8 @@ +package net.yoshinorin.qualtet.domains.series + +final case class SeriesWriteModel( + id: SeriesId, + name: SeriesName, + title: String, + description: Option[String] +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/series/repositories/SeriesQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/series/repositories/SeriesQuery.scala index b607917f..c800af51 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/series/repositories/SeriesQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/series/repositories/SeriesQuery.scala @@ -7,7 +7,7 @@ import doobie.util.update.Update object SeriesQuery { - def upsert: doobie.Write[Series] ?=> doobie.Update[Series] = { + def upsert: doobie.Write[SeriesWriteModel] ?=> doobie.Update[SeriesWriteModel] = { val q = s""" INSERT INTO series (id, name, title, description) VALUES (?, ?, ?, ?) @@ -15,7 +15,7 @@ object SeriesQuery { title = VALUES(title), description = VALUES(description) """ - Update[Series](q) + Update[SeriesWriteModel](q) } def findByName(name: SeriesName): Read[SeriesReadModel] ?=> Query0[SeriesReadModel] = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/series/repositories/SeriesRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/series/repositories/SeriesRepository.scala index f3e96c96..2ec135e0 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/series/repositories/SeriesRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/series/repositories/SeriesRepository.scala @@ -1,7 +1,7 @@ package net.yoshinorin.qualtet.domains.series trait SeriesRepository[F[_]] { - def upsert(data: Series): F[Int] + def upsert(data: SeriesWriteModel): F[Int] def findByName(name: SeriesName): F[Option[SeriesReadModel]] def getAll(): F[Seq[SeriesReadModel]] } @@ -14,7 +14,7 @@ object SeriesRepository { given SeriesRepository: SeriesRepository[ConnectionIO] = { new SeriesRepository[ConnectionIO] { - given seriesWrite: Write[Series] = + given seriesWrite: Write[SeriesWriteModel] = Write[(String, String, String, Option[String])].contramap { s => (s.id.value, s.name.value, s.title, s.description) } @@ -42,7 +42,7 @@ object SeriesRepository { } // TODO: do not `run` here - override def upsert(data: Series): ConnectionIO[Int] = { + override def upsert(data: SeriesWriteModel): ConnectionIO[Int] = { SeriesQuery.upsert.run(data) } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/tags/TagService.scala b/src/main/scala/net/yoshinorin/qualtet/domains/tags/TagService.scala index 501323b4..bb6cda7c 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/tags/TagService.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/tags/TagService.scala @@ -18,7 +18,10 @@ class TagService[F[_]: Monad]( def bulkUpsertCont(data: Option[List[Tag]]): ContT[F, Int, Int] = { ContT.apply[F, Int, Int] { next => data match { - case Some(d) => tagRepository.bulkUpsert(d) + case Some(d) => { + val ws = d.map { t => TagWriteModel(id = t.id, name = t.name) } + tagRepository.bulkUpsert(ws) + } case None => tagRepository.fakeRequest() } } diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/tags/models/TagWriteModel.scala b/src/main/scala/net/yoshinorin/qualtet/domains/tags/models/TagWriteModel.scala new file mode 100644 index 00000000..6419c04d --- /dev/null +++ b/src/main/scala/net/yoshinorin/qualtet/domains/tags/models/TagWriteModel.scala @@ -0,0 +1,6 @@ +package net.yoshinorin.qualtet.domains.tags + +final case class TagWriteModel( + id: TagId, + name: TagName +) diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/tags/repositories/TagQuery.scala b/src/main/scala/net/yoshinorin/qualtet/domains/tags/repositories/TagQuery.scala index 472529a7..06b248c7 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/tags/repositories/TagQuery.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/tags/repositories/TagQuery.scala @@ -49,14 +49,14 @@ object TagQuery { .query[TagReadModel] } - def bulkUpsert: Write[Tag] ?=> Update[Tag] = { + def bulkUpsert: Write[TagWriteModel] ?=> Update[TagWriteModel] = { val q = s""" INSERT INTO tags (id, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name) """ - Update[Tag](q) + Update[TagWriteModel](q) } def delete(id: TagId): Update0 = { diff --git a/src/main/scala/net/yoshinorin/qualtet/domains/tags/repositories/TagRepository.scala b/src/main/scala/net/yoshinorin/qualtet/domains/tags/repositories/TagRepository.scala index 7211a2e4..f935d0e3 100644 --- a/src/main/scala/net/yoshinorin/qualtet/domains/tags/repositories/TagRepository.scala +++ b/src/main/scala/net/yoshinorin/qualtet/domains/tags/repositories/TagRepository.scala @@ -3,7 +3,7 @@ package net.yoshinorin.qualtet.domains.tags import net.yoshinorin.qualtet.domains.contents.ContentId trait TagRepository[F[_]] { - def bulkUpsert(data: List[Tag]): F[Int] + def bulkUpsert(data: List[TagWriteModel]): F[Int] def getAll(): F[Seq[TagWithCountReadModel]] def findById(id: TagId): F[Option[TagReadModel]] def findByName(id: TagName): F[Option[TagReadModel]] @@ -33,10 +33,10 @@ object TagRepository { given tagReadWithOption: Read[Option[TagReadModel]] = Read[(String, String)].map { case (id, name) => Some(TagReadModel(TagId(id), TagName(name))) } - given tagWrite: Write[Tag] = + given tagWrite: Write[TagWriteModel] = Write[(String, String)].contramap(p => (p.id.value, p.name.value)) - override def bulkUpsert(data: List[Tag]): ConnectionIO[Int] = { + override def bulkUpsert(data: List[TagWriteModel]): ConnectionIO[Int] = { TagQuery.bulkUpsert.updateMany(data) } override def getAll(): ConnectionIO[Seq[TagWithCountReadModel]] = {