-
Notifications
You must be signed in to change notification settings - Fork 12
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
Changement du type pour le Mandat #815
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,15 @@ package models | |
import java.time.ZonedDateTime | ||
import java.util.UUID | ||
|
||
import anorm.Column.nonNull | ||
import anorm.{MetaDataItem, TypeDoesNotMatch} | ||
import cats.implicits.catsSyntaxEitherId | ||
import helper.Time | ||
import play.api.libs.json.{Json, Reads, Writes} | ||
import serializers.Anorm.className | ||
|
||
import serializers.JsonFormats._ | ||
|
||
case class Answer( | ||
id: UUID, | ||
applicationId: UUID, | ||
|
@@ -24,3 +33,29 @@ case class Answer( | |
} | ||
|
||
} | ||
|
||
object Answer { | ||
|
||
implicit val Reads: Reads[Answer] = Json | ||
.reads[Answer] | ||
.map(answer => | ||
answer.copy(creationDate = answer.creationDate.withZoneSameInstant(Time.timeZoneParis)) | ||
) | ||
|
||
implicit val Writes: Writes[Answer] = Json.writes[Answer] | ||
|
||
implicit val answerListParser: anorm.Column[List[Answer]] = | ||
nonNull { (value, meta) => | ||
val MetaDataItem(qualified, _, _) = meta | ||
value match { | ||
case json: org.postgresql.util.PGobject => | ||
Json.parse(json.getValue).as[List[Answer]].asRight[Nothing] | ||
case json: String => Json.parse(json).as[List[Answer]].asRight[Nothing] | ||
case _ => | ||
TypeDoesNotMatch( | ||
s"Cannot convert $value: ${className(value)} to List[Answer] for column $qualified" | ||
).asLeft[List[Answer]] | ||
} | ||
} | ||
Comment on lines
+39
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. En fait avoir la logique de serialization dans le modèle ça n'a pas de sens. Ce n'est pas clair, ça mixe de la logique métier avec de la logique de serialization. Aussi juste la dépendance "modèle dépends des serializers anorm/json" ça sens pas bon. |
||
|
||
} | ||
Comment on lines
+36
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Du coup ces modifs ne sont plus nécessaires ? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,18 @@ import java.time.ZonedDateTime | |
import java.time.temporal.ChronoUnit.MINUTES | ||
import java.util.UUID | ||
|
||
import anorm.SqlParser.{bool, get, int, str} | ||
import anorm.{~, RowParser} | ||
import cats.Eq | ||
import cats.syntax.all._ | ||
import helper.BooleanHelper.not | ||
import helper.Time | ||
import models.Application.Mandat | ||
import models.Application.Mandat.MandatType | ||
import models.Authorization.{isExpert, isHelper, isInstructor, UserRights} | ||
import serializers.Anorm.{fieldsMapLongParser, fieldsMapStringParser, fieldsMapUUIDParser} | ||
import serializers.DataModel | ||
import serializers.JsonFormats._ | ||
|
||
case class Application( | ||
id: UUID, | ||
|
@@ -21,18 +29,17 @@ case class Application( | |
invitedUsers: Map[UUID, String], | ||
area: UUID, | ||
irrelevant: Boolean, | ||
answers: List[Answer] = List(), | ||
answers: List[Answer] = List.empty[Answer], | ||
internalId: Int = -1, | ||
closed: Boolean = false, | ||
seenByUserIds: List[UUID] = List(), | ||
seenByUserIds: List[UUID] = List.empty[UUID], | ||
usefulness: Option[String] = None, | ||
closedDate: Option[ZonedDateTime] = None, | ||
closedDate: Option[ZonedDateTime] = Option.empty[ZonedDateTime], | ||
expertInvited: Boolean = false, | ||
hasSelectedSubject: Boolean = false, | ||
category: Option[String] = None, | ||
files: Map[String, Long] = Map(), | ||
mandatType: Option[Application.MandatType], | ||
mandatDate: Option[String] | ||
category: Option[String] = Option.empty[String], | ||
files: Map[String, Long] = Map.empty[String, Long], | ||
mandat: Option[Mandat] | ||
) extends AgeModel { | ||
|
||
lazy val filesAvailabilityLeftInDays: Option[Int] = if (ageInDays > 8) { | ||
|
@@ -186,14 +193,75 @@ case class Application( | |
|
||
object Application { | ||
|
||
sealed trait MandatType | ||
implicit val Parser: RowParser[Application] = | ||
(get[UUID]("id") ~ | ||
get[ZonedDateTime]("creation_date").map(_.withZoneSameInstant(Time.timeZoneParis)) ~ | ||
str(columnName = "creator_user_name") ~ | ||
get[UUID]("creator_user_id") ~ | ||
str(columnName = "subject") ~ | ||
str(columnName = "description") ~ | ||
get[Map[String, String]]("user_infos") ~ | ||
get[Map[UUID, String]]("invited_users") ~ | ||
get[UUID]("area") ~ | ||
bool(columnName = "irrelevant") ~ | ||
get[List[Answer]]("answers") ~ | ||
int("internal_id") ~ | ||
bool("closed") ~ | ||
get[List[UUID]]("seen_by_user_ids") ~ | ||
str("usefulness").? ~ | ||
get[ZonedDateTime]("closed_date").? ~ | ||
bool("expert_invited") ~ | ||
bool("has_selected_subject") ~ | ||
str("category").? ~ | ||
get[Map[String, Long]]("files") ~ | ||
Application.Mandat.Parser) | ||
.map { | ||
case id ~ creation ~ creatorName ~ creatorId ~ subject ~ description ~ userInfos ~ invitedUsers | ||
~ area ~ irrelevant ~ answers ~ internalId ~ closed ~ seen ~ usefulness ~ closedDate ~ experts ~ hasSelectedSubject ~ category ~ files ~ mandat => | ||
Application( | ||
id, | ||
creation, | ||
creatorName, | ||
creatorId, | ||
subject, | ||
description, | ||
userInfos, | ||
invitedUsers, | ||
area, | ||
irrelevant, | ||
answers, | ||
internalId, | ||
closed, | ||
seen, | ||
usefulness, | ||
closedDate, | ||
experts, | ||
hasSelectedSubject, | ||
category, | ||
files, | ||
mandat | ||
) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non mais simplement définis une |
||
|
||
final case class Mandat(_type: MandatType, date: String) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. J'aime pas trop le There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type_ c'est ok pour moi. A la base j'avais mis |
||
|
||
object Mandat { | ||
|
||
implicit val Parser: RowParser[Option[Mandat]] = | ||
(str("mandat_type").?.map( | ||
_.flatMap(DataModel.Application.MandatType.dataModelDeserialization) | ||
) ~ str("mandat_date").?).map { case _type ~ date => (_type, date).mapN(Mandat.apply) } | ||
|
||
object MandatType { | ||
case object Sms extends MandatType | ||
case object Phone extends MandatType | ||
case object Paper extends MandatType | ||
sealed trait MandatType | ||
|
||
implicit val Eq: Eq[MandatType] = (x: MandatType, y: MandatType) => x == y | ||
object MandatType { | ||
case object Sms extends MandatType | ||
case object Phone extends MandatType | ||
case object Paper extends MandatType | ||
|
||
implicit val Eq: Eq[MandatType] = (x: MandatType, y: MandatType) => x == y | ||
|
||
} | ||
|
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.