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

Upgraded backend libraries: #6

Merged
merged 1 commit into from
Oct 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
22 changes: 11 additions & 11 deletions backend/app/actors/scheduler/gitlab/GitlabModels.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@

package actors.scheduler.gitlab

import ai.x.play.json.Encoders.encoder
import ai.x.play.json.Jsonx
import org.joda.time.DateTime
import play.api.libs.json._

import java.util.Date
import models.BaseFormat._

case class GitlabMilestone(
due_date: Option[Date],
Expand Down Expand Up @@ -85,23 +83,24 @@ case class GitlabIssue(
assignees: Seq[GitlabUser],
assignee: Option[GitlabUser],
labels: Seq[String],
upvotes: Int,
downvotes: Int,
merge_requests_count: Int,
// remove to not exceed limit of 22 fields
toggm marked this conversation as resolved.
Show resolved Hide resolved
// upvotes: Int,
// downvotes: Int,
// merge_requests_count: Int,
id: Int,
title: String,
created_at: DateTime,
updated_at: DateTime,
closed_at: Option[DateTime],
closed_by: Option[GitlabUser],
subscribed: Option[Boolean],
user_notes_count: Int,
// subscribed: Option[Boolean],
// user_notes_count: Int,
due_date: Option[Date],
web_url: String,
references: Option[GitlabReference],
time_stats: Option[GitlabTimeslot],
confidential: Option[Boolean],
discussion_locked: Option[Boolean],
// discussion_locked: Option[Boolean],
_links: Option[GitlabLinks],
task_completion_status: Option[GitlabTaskCompletionStatus]
)
Expand All @@ -117,6 +116,7 @@ case class GitlabIssuesSearchResult(
)

object GitlabMilestone {
import models.BaseFormat._
implicit val jsonFormat: Format[GitlabMilestone] =
Json.format[GitlabMilestone]
}
Expand All @@ -138,8 +138,8 @@ object GitlabTaskCompletionStatus {
Json.format[GitlabTaskCompletionStatus]
}
object GitlabIssue {
implicit val jsonFormat: Format[GitlabIssue] =
Jsonx.formatCaseClass[GitlabIssue]
import models.BaseFormat._
implicit val jsonFormat: Format[GitlabIssue] = Json.format[GitlabIssue]
}
object GitlabIssuesSearchResult {
implicit val jsonFormat: Format[GitlabIssuesSearchResult] =
Expand Down
2 changes: 1 addition & 1 deletion backend/app/controllers/ApplicationController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import scala.concurrent.{ExecutionContext, Future}
case class LoginForm(email: String, password: String)

object LoginForm {
implicit val loginFormFormat = Json.format[LoginForm]
implicit val loginFormFormat: OFormat[LoginForm] = Json.format[LoginForm]
}

class ApplicationController @Inject() (
Expand Down
5 changes: 3 additions & 2 deletions backend/app/domain/views/UserTimeBookingStatisticsView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import play.modules.reactivemongo.ReactiveMongoApi
import repositories._

import scala.annotation.tailrec
import scala.concurrent.Await
import scala.concurrent.{Await, ExecutionContextExecutor}
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.util.{Failure, Success}
Expand Down Expand Up @@ -81,7 +81,8 @@ class UserTimeBookingStatisticsView(

private val waitTime = 5 seconds

private implicit val executionContext = context.dispatcher
private implicit val executionContext: ExecutionContextExecutor =
context.dispatcher

override def restoreViewFromState(snapshot: UserTimeBooking): Unit = {
println(
Expand Down
41 changes: 26 additions & 15 deletions backend/app/models/BaseFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
package models

import com.tegonal.play.json.TypedId._
import julienrf.json.derived
import julienrf.json.derived.{DerivedReads, TypeTag}
import models.BaseFormat.CompositeBaseId
import org.joda.time.format.{DateTimeFormat, ISODateTimeFormat}
import org.joda.time.{
DateTime,
DateTimeZone,
Duration,
LocalDate,
LocalDateTime
}
import org.joda.time.format.DateTimeFormat
import org.joda.time._
import play.api.libs.json._
import reactivemongo.api.bson._
import shapeless.Lazy

import java.net.{URI, URL}
import java.util.UUID
Expand Down Expand Up @@ -125,6 +122,20 @@ object BaseFormat {

val defaultTypeFormat: OFormat[String] = (__ \ "type").format[String]

private val selfTypeFormat: OFormat[String] = __.format[String]

private def toStringWrites[T]: Writes[T] =
Writes[T](obj => JsString(obj.toString))

def enumFormat[T](implicit
derivedReads: Lazy[DerivedReads[T, TypeTag.ShortClassName]]
): Format[T] = {
implicit val reads: Reads[T] = derived.flat.reads(selfTypeFormat)
implicit val writes: Writes[T] = toStringWrites[T]

Format[T](reads, writes)
}

val dateTimePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"
implicit val dateFormat: Format[DateTime] = Format[DateTime](
JodaReads.jodaDateReads(dateTimePattern),
Expand All @@ -133,9 +144,9 @@ object BaseFormat {
Format.optionWithNull

val localDateTimePattern = "yyyy-MM-dd'T'HH:mm:ss.SSS"
val localDateTimeReads: Reads[LocalDateTime] = {
private val localDateTimeReads: Reads[LocalDateTime] = {
new Reads[LocalDateTime] {
val df =
private val df =
DateTimeFormat.forPattern(localDateTimePattern)

def reads(json: JsValue): JsResult[LocalDateTime] = json match {
Expand All @@ -160,7 +171,7 @@ object BaseFormat {
.opt(LocalDateTime.parse(input, df))
}
}
val localDateTimeWrites: Writes[LocalDateTime] = {
private val localDateTimeWrites: Writes[LocalDateTime] = {
val df =
org.joda.time.format.DateTimeFormat.forPattern(localDateTimePattern)
Writes[LocalDateTime] { d =>
Expand All @@ -171,9 +182,9 @@ object BaseFormat {
Format[LocalDateTime](localDateTimeReads, localDateTimeWrites)

val localDatePattern = "yyyy-MM-dd"
val localDateReads: Reads[LocalDate] = {
private val localDateReads: Reads[LocalDate] = {
new Reads[LocalDate] {
val df =
private val df =
DateTimeFormat.forPattern(localDatePattern)

def reads(json: JsValue): JsResult[LocalDate] = json match {
Expand All @@ -198,7 +209,7 @@ object BaseFormat {
.opt(LocalDate.parse(input, df))
}
}
val localDateWrites: Writes[LocalDate] = {
private val localDateWrites: Writes[LocalDate] = {
val df =
org.joda.time.format.DateTimeFormat.forPattern(localDatePattern)
Writes[LocalDate] { d =>
Expand All @@ -210,7 +221,7 @@ object BaseFormat {

implicit object DateTimeZoneFormat extends Format[DateTimeZone] {
def writes(zone: DateTimeZone): JsValue = {
JsString(zone.getID())
JsString(zone.getID)
}

def reads(json: JsValue): JsResult[DateTimeZone] = json match {
Expand Down
10 changes: 3 additions & 7 deletions backend/app/models/Granularity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@

package models

import ai.x.play.json.Jsonx
import play.api.libs.json.Format
import models.BaseFormat.enumFormat
import play.api.libs.json._

sealed trait Granularity {}

object Granularity {
import ai.x.play.json.SingletonEncoder.simpleName
import ai.x.play.json.implicits.formatSingleton

implicit val format: Format[Granularity] =
Jsonx.formatSealed[Granularity]
implicit val format: Format[Granularity] = enumFormat[Granularity]
}

case object All extends Granularity
Expand Down
4 changes: 2 additions & 2 deletions backend/app/models/Invitation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import models.ProjectId.ProjectReference
import models.UserId.UserReference
import org.joda.time.DateTime
import play.api.libs.json._
import models.BaseFormat._
import play.api.libs.json.Format
import models.BaseFormat._

sealed trait Invitation extends BaseEntity[InvitationId] {
val createDate: DateTime
Expand All @@ -39,7 +39,7 @@ sealed trait Invitation extends BaseEntity[InvitationId] {
}

object Invitation {
implicit val format =
implicit val format: OFormat[Invitation] =
derived.flat.oformat[Invitation](BaseFormat.defaultTypeFormat)
}

Expand Down
7 changes: 2 additions & 5 deletions backend/app/models/InvitationOutcomeStatus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@

package models

import ai.x.play.json._
import models.BaseFormat.enumFormat
import play.api.libs.json.Format

sealed trait InvitationOutcomeStatus

object InvitationOutcomeStatus {
import ai.x.play.json.SingletonEncoder.simpleName
import ai.x.play.json.implicits.formatSingleton

implicit val format: Format[InvitationOutcomeStatus] =
Jsonx.formatSealed[InvitationOutcomeStatus]
enumFormat[InvitationOutcomeStatus]
}

case object InvitationAccepted extends InvitationOutcomeStatus
Expand Down
9 changes: 2 additions & 7 deletions backend/app/models/InvitationStatusResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@

package models

import ai.x.play.json.Jsonx
import models.BaseFormat.enumFormat
import play.api.libs.json.{Format, Json}

sealed trait InvitationStatus

object InvitationStatus {

import ai.x.play.json.SingletonEncoder.simpleName
import ai.x.play.json.implicits.formatSingleton

implicit val format: Format[InvitationStatus] =
Jsonx.formatSealed[InvitationStatus]
implicit val format: Format[InvitationStatus] = enumFormat[InvitationStatus]
}

case object UnregisteredUser extends InvitationStatus
Expand Down
8 changes: 2 additions & 6 deletions backend/app/models/OrganisationRole.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

package models
import ai.x.play.json.Jsonx
import models.BaseFormat.enumFormat
import play.api.libs.json.Format

sealed trait OrganisationRole
Expand All @@ -31,9 +31,5 @@ case object OrganisationAdministrator extends OrganisationRole

object OrganisationRole {

import ai.x.play.json.SingletonEncoder.simpleName
import ai.x.play.json.implicits.formatSingleton

implicit val format: Format[OrganisationRole] =
Jsonx.formatSealed[OrganisationRole]
implicit val format: Format[OrganisationRole] = enumFormat[OrganisationRole]
}
7 changes: 4 additions & 3 deletions backend/app/models/Tags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ object SimpleTag {
}

object Tag {
implicit val tagWrites =
implicit val tagWrites: OWrites[Tag] =
derived.flat.owrites[Tag](BaseFormat.defaultTypeFormat)
val defaultTagReads = derived.flat.reads[Tag](BaseFormat.defaultTypeFormat)
val tagReads = (JsPath \ "type").readNullable[String].flatMap {
private val defaultTagReads =
derived.flat.reads[Tag](BaseFormat.defaultTypeFormat)
private val tagReads = (JsPath \ "type").readNullable[String].flatMap {
// by default map to "SimpleTag" to be compliant with old bookings based on TagId only
case None =>
JsPath().read[String].map[Tag](tag => SimpleTag(TagId(tag)))
Expand Down
8 changes: 2 additions & 6 deletions backend/app/models/UserProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

package models

import ai.x.play.json.Jsonx
import models.BaseFormat.enumFormat
import models.OrganisationId.OrganisationReference
import models.ProjectId.ProjectReference
import play.api.libs.json._
Expand All @@ -33,11 +33,7 @@ case object ProjectMember extends ProjectRole
case object ProjectAdministrator extends ProjectRole

object ProjectRole {

import ai.x.play.json.SingletonEncoder.simpleName
import ai.x.play.json.implicits.formatSingleton

implicit val format: Format[ProjectRole] = Jsonx.formatSealed[ProjectRole]
implicit val format: Format[ProjectRole] = enumFormat[ProjectRole]
}

case class UserProject(
Expand Down
6 changes: 2 additions & 4 deletions backend/app/models/UserRole.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@

package models

import ai.x.play.json.Jsonx
import models.BaseFormat.enumFormat
import play.api.libs.json.Format

sealed trait UserRole
case object FreeUser extends UserRole
case object Administrator extends UserRole

object UserRole {
import ai.x.play.json.SingletonEncoder.simpleName
import ai.x.play.json.implicits.formatSingleton
implicit val format: Format[UserRole] = Jsonx.formatSealed[UserRole]
implicit val format: Format[UserRole] = enumFormat[UserRole]
}
4 changes: 2 additions & 2 deletions backend/app/models/adapters/PersistedEventAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import play.modules.reactivemongo.ReactiveMongoApi
import repositories.{ProjectRepository, UserRepository}

import scala.annotation.nowarn
import scala.concurrent.{Await, Awaitable}
import scala.concurrent.{Await, Awaitable, ExecutionContextExecutor}
import scala.concurrent.duration.DurationInt
import scala.language.postfixOps

class PersistedEventAdapter(system: ExtendedActorSystem)
extends ReadEventAdapter {

implicit val executionContext = system.dispatcher
implicit val executionContext: ExecutionContextExecutor = system.dispatcher

lazy val allUsers = {
val reactiveMongoApi = PlayAkkaExtension(system)
Expand Down
Loading
Loading