Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chimney library to avoid boilerplate #428

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ val anorm = "2.7.0"
val enumeratum = "1.7.2"
val scalaJavaTime = "2.5.0"
val tapir = "1.5.0"
val chimney = "0.8.0-RC1"

val consoleDisabledOptions = Seq("-Werror", "-Ywarn-unused", "-Ywarn-unused-import")

Expand Down Expand Up @@ -374,7 +375,8 @@ lazy val server = (project in file("server"))
"javax.el" % "javax.el-api" % "3.0.0",
"org.glassfish" % "javax.el" % "3.0.0",
"com.beachape" %% "enumeratum" % enumeratum,
"com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-bundle" % tapir
"com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-bundle" % tapir,
"io.scalaland" %% "chimney" % chimney
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object AdminEndpoints {
List(
AdminGetUserLogs.Response
.UserLog(
id = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6"),
userLogId = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6"),
message = "Message",
createdAt = Instant.parse("2021-01-01T00:00:00Z")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ object UsersEndpoints {
GetUserLogs.Response(
List(
GetUserLogs.Response.UserLog(
id = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6"),
userLogId = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6"),
message = "Message",
createdAt = Instant.parse("2021-01-01T00:00:00Z")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object AdminGetUserLogs {
.description("Includes the logs for a single user")

object Response {
case class UserLog(id: UUID, message: String, createdAt: Instant)
case class UserLog(userLogId: UUID, message: String, createdAt: Instant)
implicit val adminGetUserLogsResponseUserLogFormat: Format[UserLog] = Json.format[UserLog]
implicit val adminGetUserLogsResponseUserLogSchema: Schema[UserLog] = Schema
.derived[UserLog]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object GetUserLogs {
case class Response(data: List[Response.UserLog])

object Response {
case class UserLog(id: UUID, message: String, createdAt: Instant)
case class UserLog(userLogId: UUID, message: String, createdAt: Instant)
implicit val getUserLogsResponseFormat: Format[UserLog] = Json.format[UserLog]
implicit val getUserLogsResponseSchema: Schema[UserLog] =
Schema.derived[UserLog].name(Schema.SName("GetUserLogsResponseUserLog"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.wiringbits.actions

import io.scalaland.chimney.dsl.transformInto
import net.wiringbits.api.models.CreateUser
import net.wiringbits.apis.ReCaptchaApi
import net.wiringbits.config.UserTokensConfig
Expand Down Expand Up @@ -55,7 +56,7 @@ class CreateUserAction @Inject() (
),
token
)
} yield CreateUser.Response(id = createUser.id, email = createUser.email, name = createUser.name)
} yield createUser.transformInto[CreateUser.Response]
}

private def validations(request: CreateUser.Request) = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.wiringbits.actions

import io.scalaland.chimney.dsl.transformInto
import net.wiringbits.api.models.GetCurrentUser
import net.wiringbits.repositories.UsersRepository
import net.wiringbits.repositories.models.User
Expand All @@ -15,12 +16,7 @@ class GetUserAction @Inject() (
def apply(userId: UUID): Future[GetCurrentUser.Response] = {
for {
user <- unsafeUser(userId)
} yield GetCurrentUser.Response(
id = user.id,
email = user.email,
name = user.name,
createdAt = user.createdAt
)
} yield user.transformInto[GetCurrentUser.Response]
}

private def unsafeUser(userId: UUID): Future[User] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.wiringbits.actions

import io.scalaland.chimney.dsl.transformInto
import net.wiringbits.api.models.GetUserLogs
import net.wiringbits.repositories.UserLogsRepository

Expand All @@ -14,13 +15,7 @@ class GetUserLogsAction @Inject() (
def apply(userId: UUID): Future[GetUserLogs.Response] = {
for {
logs <- userLogsRepository.logs(userId)
items = logs.map { x =>
GetUserLogs.Response.UserLog(
id = x.userLogId,
message = x.message,
createdAt = x.createdAt
)
}
items = logs.map(_.transformInto[GetUserLogs.Response.UserLog])
} yield GetUserLogs.Response(items)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.wiringbits.actions

import io.scalaland.chimney.dsl.transformInto
import net.wiringbits.api.models.Login
import net.wiringbits.apis.ReCaptchaApi
import net.wiringbits.repositories.{UserLogsRepository, UsersRepository}
Expand Down Expand Up @@ -28,6 +29,6 @@ class LoginAction @Inject() (

// A login token is created
_ <- userLogsRepository.create(user.id, "Logged in successfully")
} yield Login.Response(user.id, user.name, user.email)
} yield user.transformInto[Login.Response]
}
}
18 changes: 3 additions & 15 deletions server/src/main/scala/net/wiringbits/services/AdminService.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.wiringbits.services

import io.scalaland.chimney.dsl.transformInto
import net.wiringbits.api.models.{AdminGetUserLogs, AdminGetUsers}
import net.wiringbits.repositories.{UserLogsRepository, UsersRepository}

Expand All @@ -14,27 +15,14 @@ class AdminService @Inject() (userLogsRepository: UserLogsRepository, usersRepos
def userLogs(userId: UUID): Future[AdminGetUserLogs.Response] = {
for {
logs <- userLogsRepository.logs(userId)
items = logs.map { x =>
AdminGetUserLogs.Response.UserLog(
id = x.userLogId,
createdAt = x.createdAt,
message = x.message
)
}
items = logs.map(_.transformInto[AdminGetUserLogs.Response.UserLog])
} yield AdminGetUserLogs.Response(items)
}

def users(): Future[AdminGetUsers.Response] = {
for {
users <- usersRepository.all()
items = users.map { x =>
AdminGetUsers.Response.User(
id = x.id,
name = x.name,
email = x.email,
createdAt = x.createdAt
)
}
items = users.map(_.transformInto[AdminGetUsers.Response.User])
} yield AdminGetUsers.Response(items)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object LogList {
.secondary(Formatter.instant(item.createdAt))
)
.divider(true)
.withKey(item.id.toString)
.withKey(item.userLogId.toString)
.build
}

Expand Down
Loading