Skip to content

Commit

Permalink
refactor: rename implicit from transactor to executer
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jun 15, 2024
1 parent 2989b04 commit f2efc42
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import cats.Monad
class ArchiveService[F[_]: Monad](
archiveRepository: ArchiveRepository[F],
contentTypeService: ContentTypeService[F]
)(using transactor: Executer[F, IO]) {
)(using executer: Executer[F, IO]) {

def actions(contentTypeId: ContentTypeId): Action[Seq[ResponseArchive]] = {
Continue(archiveRepository.get(contentTypeId), Action.done[Seq[ResponseArchive]])
Expand All @@ -22,7 +22,7 @@ class ArchiveService[F[_]: Monad](
def get: IO[Seq[ResponseArchive]] = {
for {
c <- contentTypeService.findByName("article").throwIfNone(NotFound(detail = "content-type not found: article"))
articles <- transactor.transact(actions(c.id))
articles <- executer.transact(actions(c.id))
} yield articles
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import net.yoshinorin.qualtet.syntax.*
class ArticleService[F[_]: Monad](
articleRepository: ArticleRepository[F],
contentTypeService: ContentTypeService[F]
)(using transactor: Executer[F, IO]) {
)(using executer: Executer[F, IO]) {

def actions(
contentTypeId: ContentTypeId,
Expand Down Expand Up @@ -53,7 +53,7 @@ class ArticleService[F[_]: Monad](
)(f: (ContentTypeId, A, ArticlesQueryParameter) => Action[Seq[(Int, ResponseArticle)]]): IO[ResponseArticleWithCount] = {
for {
c <- contentTypeService.findByName("article").throwIfNone(NotFound(detail = "content-type not found: article"))
articlesWithCount <- transactor.transact(f(c.id, data, queryParam))
articlesWithCount <- executer.transact(f(c.id, data, queryParam))
} yield
if (articlesWithCount.nonEmpty) {
ResponseArticleWithCount(articlesWithCount.map(_._1).headOption.getOrElse(0), articlesWithCount.map(_._2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.yoshinorin.qualtet.syntax.*

class AuthorService[F[_]: Monad](
authorRepository: AuthorRepository[F]
)(using transactor: Executer[F, IO]) {
)(using executer: Executer[F, IO]) {

def upsertActions(data: Author): Action[Int] = {
Continue(authorRepository.upsert(data), Action.done[Int])
Expand Down Expand Up @@ -40,7 +40,7 @@ class AuthorService[F[_]: Monad](
*/
def create(data: Author): IO[ResponseAuthor] = {
for {
_ <- transactor.transact(upsertActions(data))
_ <- executer.transact(upsertActions(data))
a <- this.findByName(data.name).throwIfNone(InternalServerError("user not found"))
} yield a
}
Expand All @@ -51,7 +51,7 @@ class AuthorService[F[_]: Monad](
* @return Authors
*/
def getAll: IO[Seq[ResponseAuthor]] = {
transactor.transact(fetchActions)
executer.transact(fetchActions)
}

/**
Expand All @@ -61,7 +61,7 @@ class AuthorService[F[_]: Monad](
* @return Author
*/
def findById(id: AuthorId): IO[Option[ResponseAuthor]] = {
transactor.transact(findByIdActions(id))
executer.transact(findByIdActions(id))
}

/**
Expand All @@ -71,7 +71,7 @@ class AuthorService[F[_]: Monad](
* @return Author
*/
def findByIdWithPassword(id: AuthorId): IO[Option[Author]] = {
transactor.transact(findByIdWithPasswordActions(id))
executer.transact(findByIdWithPasswordActions(id))
}

/**
Expand All @@ -81,7 +81,7 @@ class AuthorService[F[_]: Monad](
* @return Author
*/
def findByName(name: AuthorName): IO[Option[ResponseAuthor]] = {
transactor.transact(findByNameActions(name))
executer.transact(findByNameActions(name))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.yoshinorin.qualtet.infrastructure.db.Executer

class ContentTaggingService[F[_]: Monad](
contentTaggingRepository: ContentTaggingRepository[F]
)(using transactor: Executer[F, IO]) {
)(using executer: Executer[F, IO]) {

def findByTagIdActions(id: TagId): Action[Seq[ContentTagging]] = {
Continue(contentTaggingRepository.findByTagId(id), Action.done[Seq[ContentTagging]])
Expand Down Expand Up @@ -43,6 +43,6 @@ class ContentTaggingService[F[_]: Monad](
}

def findByTagId(id: TagId): IO[Seq[ContentTagging]] = {
transactor.transact(findByTagIdActions(id))
executer.transact(findByTagIdActions(id))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import net.yoshinorin.qualtet.syntax.*
class ContentTypeService[F[_]: Monad](
contentRepository: ContentTypeRepository[F],
cache: CacheModule[String, ContentType]
)(using transactor: Executer[F, IO])
)(using executer: Executer[F, IO])
extends Cacheable {

def upsertActions(data: ContentType): Action[Int] = {
Expand All @@ -35,7 +35,7 @@ class ContentTypeService[F[_]: Monad](
case Some(x: ContentType) => IO(x)
case None =>
for {
_ <- transactor.transact(upsertActions(data))
_ <- executer.transact(upsertActions(data))
c <- this.findByName(data.name).throwIfNone(InternalServerError("contentType not found"))
} yield c
}
Expand All @@ -56,7 +56,7 @@ class ContentTypeService[F[_]: Monad](

def fromDB(name: String): IO[Option[ContentType]] = {
for {
x <- transactor.transact(actions(name))
x <- executer.transact(actions(name))
} yield (x, cache.put(name, x))._1
}

Expand All @@ -73,7 +73,7 @@ class ContentTypeService[F[_]: Monad](
* @return ContentTypes
*/
def getAll: IO[Seq[ContentType]] = {
transactor.transact(getAllActions)
executer.transact(getAllActions)
}

def invalidate(): IO[Unit] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ContentService[F[_]: Monad](
seriesService: SeriesService[F],
contentSerializingService: ContentSerializingService[F]
)(using
transactor: Executer[F, IO]
executer: Executer[F, IO]
) {

def upsertActions(data: Content): Action[Int] = {
Expand Down Expand Up @@ -124,16 +124,16 @@ class ContentService[F[_]: Monad](
val maybeExternalResources = externalResources.flatMap(a => a.values.map(v => ExternalResource(data.id, a.kind, v)))

val queries = for {
contentUpsert <- transactor.perform(upsertActions(data))
robotsUpsert <- transactor.perform(robotsService.upsertActions(Robots(data.id, robotsAttributes)))
currentTags <- transactor.perform(tagService.findByContentIdActions(data.id))
tagsDiffDelete <- transactor.perform(contentTaggingService.bulkDeleteActions(data.id, currentTags.map(_.id).diff(tags.getOrElse(List()).map(t => t.id))))
tagsBulkUpsert <- transactor.perform(tagService.bulkUpsertActions(tags))
contentUpsert <- executer.perform(upsertActions(data))
robotsUpsert <- executer.perform(robotsService.upsertActions(Robots(data.id, robotsAttributes)))
currentTags <- executer.perform(tagService.findByContentIdActions(data.id))
tagsDiffDelete <- executer.perform(contentTaggingService.bulkDeleteActions(data.id, currentTags.map(_.id).diff(tags.getOrElse(List()).map(t => t.id))))
tagsBulkUpsert <- executer.perform(tagService.bulkUpsertActions(tags))
// TODO: check diff and clean up contentTagging before upsert
contentTaggingBulkUpsert <- transactor.perform(contentTaggingService.bulkUpsertActions(contentTagging))
contentSerializingUpsert <- transactor.perform(contentSerializingService.upsertActions(contentSerializing))
contentTaggingBulkUpsert <- executer.perform(contentTaggingService.bulkUpsertActions(contentTagging))
contentSerializingUpsert <- executer.perform(contentSerializingService.upsertActions(contentSerializing))
// TODO: check diff and clean up external_resources before upsert
externalResourceBulkUpsert <- transactor.perform(externalResourceService.bulkUpsertActions(maybeExternalResources))
externalResourceBulkUpsert <- executer.perform(externalResourceService.bulkUpsertActions(maybeExternalResources))
} yield (
contentUpsert,
currentTags,
Expand All @@ -146,7 +146,7 @@ class ContentService[F[_]: Monad](
)

for {
_ <- transactor.transact8[Int, Seq[Tag], Unit, Int, Int, Int, Int, Int](queries)
_ <- executer.transact8[Int, Seq[Tag], Unit, Int, Int, Int, Int, Int](queries)
c <- this.findByPath(data.path).throwIfNone(InternalServerError("content not found")) // NOTE: 404 is better?
// TODO: Should return `ResponseContent` instead of `Content`.
} yield c
Expand All @@ -160,11 +160,11 @@ class ContentService[F[_]: Monad](
def delete(id: ContentId): IO[Unit] = {

val queries = for {
externalResourcesDelete <- transactor.perform(externalResourceService.deleteActions(id))
externalResourcesDelete <- executer.perform(externalResourceService.deleteActions(id))
// TODO: Tags should be deleted automatically after delete a content which are not refer from other contents.
contentTaggingDelete <- transactor.perform(contentTaggingService.deleteByContentIdActions(id))
robotsDelete <- transactor.perform(robotsService.deleteActions(id))
contentDelete <- transactor.perform(deleteActions(id))
contentTaggingDelete <- executer.perform(contentTaggingService.deleteByContentIdActions(id))
robotsDelete <- executer.perform(robotsService.deleteActions(id))
contentDelete <- executer.perform(deleteActions(id))
} yield (
externalResourcesDelete,
contentTaggingDelete,
Expand All @@ -174,7 +174,7 @@ class ContentService[F[_]: Monad](

for {
_ <- this.findById(id).throwIfNone(NotFound(detail = s"content not found: ${id}"))
_ <- transactor.transact4[Unit, Unit, Unit, Unit](queries)
_ <- executer.transact4[Unit, Unit, Unit, Unit](queries)
} yield ()
}

Expand All @@ -185,7 +185,7 @@ class ContentService[F[_]: Monad](
* @return ResponseContent instance
*/
def findByPath(path: Path): IO[Option[Content]] = {
transactor.transact(findByPathActions(path))
executer.transact(findByPathActions(path))
}

/**
Expand All @@ -205,11 +205,11 @@ class ContentService[F[_]: Monad](
* @return ResponseContent instance
*/
def findById(id: ContentId): IO[Option[Content]] = {
transactor.transact(findByIdActions(id))
executer.transact(findByIdActions(id))
}

def findBy[A](data: A)(f: A => Action[Option[ReadContentDbRow]]): IO[Option[ResponseContent]] = {
transactor.transact(f(data)).flatMap {
executer.transact(f(data)).flatMap {
case None => IO(None)
case Some(x) =>
val stripedContent = x.content.stripHtmlTags.replaceAll("\n", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import scala.annotation.tailrec
class SearchService[F[_]: Monad](
searchConfig: SearchConfig,
searchRepository: SearchRepository[F]
)(using transactor: Executer[F, IO]) {
)(using executer: Executer[F, IO]) {

def actions(query: List[String]): Action[Seq[(Int, ResponseSearch)]] = {
Continue(searchRepository.search(query), Action.done[Seq[(Int, ResponseSearch)]])
Expand Down Expand Up @@ -114,7 +114,7 @@ class SearchService[F[_]: Monad](
_ <- IO(if (accErrors.nonEmpty) {
throw new UnprocessableEntity(detail = "Invalid search conditions. Please see error details.", errors = Some(accErrors))
})
searchResult <- transactor.transact(actions(queryStrings))
searchResult <- executer.transact(actions(queryStrings))
} yield
if (searchResult.nonEmpty) {
val r = searchResult.map { x =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import wvlet.airframe.ulid.ULID
class SeriesService[F[_]: Monad](
seriesRepository: SeriesRepository[F],
articleService: ArticleService[F]
)(using transactor: Executer[F, IO]) {
)(using executer: Executer[F, IO]) {

def upsertActions(data: Series): Action[Int] = {
Continue(seriesRepository.upsert(data), Action.done[Int])
Expand All @@ -37,12 +37,12 @@ class SeriesService[F[_]: Monad](
this.findByName(data.name).flatMap {
case Some(s: Series) =>
for {
_ <- transactor.transact(upsertActions(Series(s.id, s.name, data.title, data.description)))
_ <- executer.transact(upsertActions(Series(s.id, s.name, data.title, data.description)))
s <- this.findByName(data.name).throwIfNone(NotFound(detail = "series not found"))
} yield s
case None =>
for {
_ <- transactor.transact(upsertActions(Series(SeriesId(ULID.newULIDString.toLower), data.name, data.title, data.description)))
_ <- executer.transact(upsertActions(Series(SeriesId(ULID.newULIDString.toLower), data.name, data.title, data.description)))
s <- this.findByName(data.name).throwIfNone(NotFound(detail = "series not found"))
} yield s
}
Expand All @@ -55,12 +55,12 @@ class SeriesService[F[_]: Monad](
* @return Series Instance
*/
def findByName(name: SeriesName): IO[Option[Series]] = {
transactor.transact(findByNameActions(name))
executer.transact(findByNameActions(name))
}

def get(name: SeriesName): IO[ResponseSeries] = {
for {
series <- transactor.transact(findByNameActions(name)).throwIfNone(NotFound(detail = s"series not found: ${name.value}"))
series <- executer.transact(findByNameActions(name)).throwIfNone(NotFound(detail = s"series not found: ${name.value}"))
seriesWithArticles <- articleService.getBySeriesName(series.name)
} yield {
ResponseSeries(series.id, series.name, series.title, series.description, seriesWithArticles.articles)
Expand All @@ -73,7 +73,7 @@ class SeriesService[F[_]: Monad](
* @return Series
*/
def getAll: IO[Seq[Series]] = {
transactor.transact(fetchActions)
executer.transact(fetchActions)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.yoshinorin.qualtet.domains.Cacheable
class SitemapService[F[_]: Monad](
sitemapRepository: SitemapsRepository[F],
cache: CacheModule[String, Seq[Url]]
)(using transactor: Executer[F, IO])
)(using executer: Executer[F, IO])
extends Cacheable {

private val cacheKey = "sitemaps-full-cache"
Expand All @@ -25,7 +25,7 @@ class SitemapService[F[_]: Monad](
case Some(x: Seq[Url]) => IO(x)
case _ =>
for {
x <- transactor.transact(getActions)
x <- executer.transact(getActions)
} yield (x, cache.put(cacheKey, x))._1
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import net.yoshinorin.qualtet.syntax.*
class TagService[F[_]: Monad](
tagRepository: TagRepository[F],
contentTaggingService: ContentTaggingService[F]
)(using transactor: Executer[F, IO]) {
)(using executer: Executer[F, IO]) {

def bulkUpsertActions(data: Option[List[Tag]]): Action[Int] = {
data match {
Expand Down Expand Up @@ -49,7 +49,7 @@ class TagService[F[_]: Monad](
* @return tags
*/
def getAll: IO[Seq[ResponseTag]] = {
transactor.transact(getAllActions)
executer.transact(getAllActions)
}

/**
Expand All @@ -59,7 +59,7 @@ class TagService[F[_]: Monad](
* @return maybe Tag
*/
def findById(id: TagId): IO[Option[Tag]] = {
transactor.transact(findByIdActions(id))
executer.transact(findByIdActions(id))
}

/**
Expand All @@ -69,7 +69,7 @@ class TagService[F[_]: Monad](
* @return maybe Tag
*/
def findByName(tagName: TagName): IO[Option[Tag]] = {
transactor.transact(findByNameActions(tagName))
executer.transact(findByNameActions(tagName))
}

/**
Expand Down Expand Up @@ -105,13 +105,13 @@ class TagService[F[_]: Monad](
*/
def delete(id: TagId): IO[Unit] = {
val queries = for {
contentTaggingDelete <- transactor.perform(contentTaggingService.deleteByTagIdActions(id))
tagDelete <- transactor.perform(deleteActions(id))
contentTaggingDelete <- executer.perform(contentTaggingService.deleteByTagIdActions(id))
tagDelete <- executer.perform(deleteActions(id))
} yield (contentTaggingDelete, tagDelete)

for {
_ <- this.findById(id).throwIfNone(NotFound(detail = s"tag not found: ${id}"))
_ <- transactor.transact2(queries)
_ <- executer.transact2(queries)
} yield ()
}
}

0 comments on commit f2efc42

Please sign in to comment.