-
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 all commits
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,104 @@ | ||||||
package models.sql | ||||||
|
||||||
import java.time.ZonedDateTime | ||||||
import java.util.UUID | ||||||
|
||||||
import anorm.{Macro, RowParser} | ||||||
import cats.implicits.catsSyntaxTuple2Semigroupal | ||||||
import helper.Time | ||||||
import models.Application.Mandat | ||||||
import models.{Answer, Application} | ||||||
import serializers.DataModel | ||||||
|
||||||
final case class ApplicationRow( | ||||||
id: UUID, | ||||||
creationDate: ZonedDateTime, | ||||||
creatorUserName: String, | ||||||
creatorUserId: UUID, | ||||||
subject: String, | ||||||
description: String, | ||||||
userInfos: Map[String, String], | ||||||
invitedUsers: Map[UUID, String], | ||||||
area: UUID, | ||||||
irrelevant: Boolean, | ||||||
answers: List[Answer] = List.empty[Answer], | ||||||
internalId: Int = -1, | ||||||
closed: Boolean = false, | ||||||
seenByUserIds: List[UUID] = List.empty[UUID], | ||||||
usefulness: Option[String] = Option.empty[String], | ||||||
closedDate: Option[ZonedDateTime] = Option.empty[ZonedDateTime], | ||||||
expertInvited: Boolean = false, | ||||||
hasSelectedSubject: Boolean = false, | ||||||
category: Option[String] = Option.empty[String], | ||||||
files: Map[String, Long] = Map.empty[String, Long], | ||||||
mandatType: Option[Application.Mandat.MandatType], | ||||||
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. Pour postgres c'est un
Suggested change
|
||||||
mandatDate: Option[String] | ||||||
) | ||||||
|
||||||
object ApplicationRow { | ||||||
|
||||||
import serializers.Anorm._ | ||||||
import DataModel.Application.Mandat.MandatType.MandatTypeParser | ||||||
|
||||||
val ApplicationRowParser: RowParser[ApplicationRow] = Macro | ||||||
.parser[ApplicationRow]( | ||||||
"id", | ||||||
"creation_date", | ||||||
"creator_user_name", | ||||||
"creator_user_id", | ||||||
"subject", | ||||||
"description", | ||||||
"user_infos", | ||||||
"invited_users", | ||||||
"area", | ||||||
"irrelevant", | ||||||
"answers", | ||||||
"internal_id", | ||||||
"closed", | ||||||
"seen_by_user_ids", | ||||||
"usefulness", | ||||||
"closed_date", | ||||||
"expert_invited", | ||||||
"has_selected_subject", | ||||||
"category", | ||||||
"files", | ||||||
"mandat_type", | ||||||
"mandat_date" | ||||||
) | ||||||
.map(application => | ||||||
application.copy( | ||||||
creationDate = application.creationDate.withZoneSameInstant(Time.timeZoneParis), | ||||||
answers = application.answers.map(answer => | ||||||
answer.copy(creationDate = answer.creationDate.withZoneSameInstant(Time.timeZoneParis)) | ||||||
) | ||||||
) | ||||||
) | ||||||
|
||||||
def toApplication(row: ApplicationRow): Application = { | ||||||
import row._ | ||||||
Application( | ||||||
id, | ||||||
creationDate, | ||||||
creatorUserName, | ||||||
creatorUserId, | ||||||
subject, | ||||||
description = description, | ||||||
userInfos = userInfos, | ||||||
invitedUsers = invitedUsers, | ||||||
area = area, | ||||||
irrelevant = irrelevant, | ||||||
answers = answers, | ||||||
internalId = internalId, | ||||||
closed = closed, | ||||||
seenByUserIds = seenByUserIds, | ||||||
usefulness = usefulness, | ||||||
closedDate = closedDate, | ||||||
expertInvited = expertInvited, | ||||||
hasSelectedSubject = hasSelectedSubject, | ||||||
category = category, | ||||||
files = files, | ||||||
mandat = (row.mandatType, row.mandatDate).mapN(Mandat.apply) | ||||||
) | ||||||
} | ||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
package serializers | ||
|
||
import models.Application.MandatType | ||
import anorm.Column | ||
import cats.implicits.catsSyntaxOptionId | ||
import models.Application.Mandat.MandatType | ||
import play.api.libs.json._ | ||
|
||
object DataModel { | ||
|
||
object Application { | ||
|
||
object MandatType { | ||
import models.Application.MandatType._ | ||
object Mandat { | ||
|
||
def dataModelSerialization(entity: MandatType): String = | ||
entity match { | ||
case Sms => "sms" | ||
case Phone => "phone" | ||
case Paper => "paper" | ||
} | ||
object MandatType { | ||
|
||
def dataModelDeserialization(raw: String): Option[MandatType] = | ||
raw match { | ||
case "sms" => Some(Sms) | ||
case "phone" => Some(Phone) | ||
case "paper" => Some(Paper) | ||
case _ => None | ||
} | ||
import models.Application.Mandat.MandatType._ | ||
|
||
implicit val MandatTypeParser: Column[Option[MandatType]] = | ||
implicitly[Column[Option[String]]] | ||
.map(_.flatMap(dataModelDeserialization)) | ||
Comment on lines
+18
to
+20
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 utilisé ? |
||
|
||
def dataModelSerialization(entity: MandatType): String = | ||
entity match { | ||
case Sms => "sms" | ||
case Phone => "phone" | ||
case Paper => "paper" | ||
} | ||
|
||
def dataModelDeserialization(raw: String): Option[MandatType] = | ||
raw match { | ||
case "sms" => Sms.some | ||
case "phone" => Phone.some | ||
case "paper" => Paper.some | ||
case _ => None | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
|
@@ -53,20 +64,18 @@ object DataModel { | |
} | ||
|
||
implicit val smsApiWrites: Writes[Sms] = | ||
Writes( | ||
_ match { | ||
case sms: Sms.Outgoing => | ||
smsOutgoingFormat.writes(sms) match { | ||
case obj: JsObject => obj + ("tag" -> JsString("outgoing")) | ||
case other => other | ||
} | ||
case sms: Sms.Incoming => | ||
smsIncomingFormat.writes(sms) match { | ||
case obj: JsObject => obj + ("tag" -> JsString("incoming")) | ||
case other => other | ||
} | ||
} | ||
) | ||
Writes { | ||
case sms: Sms.Outgoing => | ||
smsOutgoingFormat.writes(sms) match { | ||
case obj: JsObject => obj + ("tag" -> JsString("outgoing")) | ||
case other => other | ||
} | ||
case sms: Sms.Incoming => | ||
smsIncomingFormat.writes(sms) match { | ||
case obj: JsObject => obj + ("tag" -> JsString("incoming")) | ||
case other => other | ||
} | ||
} | ||
|
||
} | ||
|
||
|
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.
tu es sûr que c'est nécessaire ?