Skip to content

Commit

Permalink
Refactor to have the widget layout type be a jsonb and JsValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ljdelight committed Oct 14, 2023
1 parent d10f57a commit 2ab35a0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ trait MRSchemaTypes {
implicit val ChallengePriorityType: ObjectType[Unit, ChallengePriority] =
deriveObjectType[Unit, ChallengePriority](ObjectTypeName("ChallengePriority"))
implicit val ChallengeExtraType: ObjectType[Unit, ChallengeExtra] =
deriveObjectType[Unit, ChallengeExtra](ObjectTypeName("ChallengeExtra"))
deriveObjectType[Unit, ChallengeExtra](
ObjectTypeName("ChallengeExtra"),
ReplaceField(
"widgetLayout",
Field("widgetLayout", StringType, resolve = _.value.widgetLayout.getOrElse("").toString)
)
)
// Comment Types
implicit val CommentType: ObjectType[Unit, Comment] =
deriveObjectType[Unit, Comment](ObjectTypeName("Comment"))
Expand Down
2 changes: 1 addition & 1 deletion app/org/maproulette/framework/model/Challenge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ case class ChallengeExtra(
taskBundleIdProperty: Option[String] = None,
isArchived: Boolean = false,
reviewSetting: Int = Challenge.REVIEW_SETTING_NOT_REQUIRED,
widgetLayout: Option[String] = None,
widgetLayout: Option[JsValue] = None,
systemArchivedAt: Option[DateTime] = None,
presets: Option[List[String]] = None
) extends DefaultWrites
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ package org.maproulette.framework.repository
import java.sql.Connection
import anorm.SqlParser._
import anorm.{RowParser, SQL, ~}

import anorm.postgresql.jsValueColumn
import javax.inject.{Inject, Singleton}
import org.apache.commons.lang3.StringUtils
import org.joda.time.DateTime
import org.maproulette.framework.model._
import org.maproulette.framework.psql.{GroupField, Grouping, OR, Query}
import org.maproulette.framework.psql.filter._
import play.api.db.Database
import play.api.libs.json.JsValue

/**
* The challenge repository handles all the querying with the databases related to challenge objects
Expand Down Expand Up @@ -262,7 +263,7 @@ object ChallengeRepository {
get[Option[List[Long]]]("virtual_parent_ids") ~
get[Boolean]("challenges.is_archived") ~
get[Int]("challenges.review_setting") ~
get[Option[String]]("challenges.widget_layout") ~
get[Option[JsValue]]("challenges.widget_layout") ~
get[Option[DateTime]]("challenges.system_archived_at") map {
case id ~ name ~ created ~ modified ~ description ~ infoLink ~ ownerId ~ parentId ~ instruction ~
difficulty ~ blurb ~ enabled ~ featured ~ cooperativeType ~ popularity ~ checkin_comment ~
Expand All @@ -283,6 +284,7 @@ object ChallengeRepository {
case Some(c) if StringUtils.isEmpty(c) || StringUtils.equals(c, "{}") => None
case r => r
}

new Challenge(
id,
name,
Expand Down
24 changes: 15 additions & 9 deletions app/org/maproulette/models/dal/ChallengeDAL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package org.maproulette.models.dal

import java.sql.Connection

import anorm.SqlParser._
import anorm._
import anorm.postgresql.{asJson, jsValueColumn}

import javax.inject.{Inject, Singleton}
import org.apache.commons.lang3.StringUtils
import org.joda.time.DateTime
Expand All @@ -17,9 +18,9 @@ import org.maproulette.data.{Actions, ChallengeType, ProjectType}
import org.maproulette.exception.{InvalidException, NotFoundException, UniqueViolationException}
import org.maproulette.framework.model._
import org.maproulette.framework.repository.{
ChallengeListingRepository,
ProjectRepository,
TaskRepository,
ChallengeListingRepository
TaskRepository
}
import org.maproulette.framework.service.{ServiceManager, TagService}
import org.maproulette.models.dal.mixin.{OwnerMixin, TagDALMixin}
Expand Down Expand Up @@ -121,7 +122,7 @@ class ChallengeDAL @Inject() (
get[Boolean]("deleted") ~
get[Boolean]("challenges.is_archived") ~
get[Int]("challenges.review_setting") ~
get[Option[String]]("challenges.widget_layout") ~
get[Option[JsValue]]("challenges.widget_layout") ~
get[Option[Int]]("challenges.completion_percentage") ~
get[Option[Int]]("challenges.tasks_remaining") map {
case id ~ name ~ created ~ modified ~ description ~ infoLink ~ ownerId ~ parentId ~ instruction ~
Expand All @@ -144,6 +145,7 @@ class ChallengeDAL @Inject() (
case Some(c) if StringUtils.isEmpty(c) || StringUtils.equals(c, "{}") => None
case r => r
}

new Challenge(
id,
name,
Expand Down Expand Up @@ -257,7 +259,7 @@ class ChallengeDAL @Inject() (
get[Option[List[String]]]("presets") ~
get[Boolean]("challenges.is_archived") ~
get[Int]("challenges.review_setting") ~
get[Option[String]]("challenges.widget_layout") ~
get[Option[JsValue]]("challenges.widget_layout") ~
get[Option[DateTime]]("challenges.system_archived_at") ~
get[Option[Int]]("challenges.completion_percentage") ~
get[Option[Int]]("challenges.tasks_remaining") map {
Expand All @@ -282,6 +284,7 @@ class ChallengeDAL @Inject() (
case Some(c) if StringUtils.isEmpty(c) || StringUtils.equals(c, "{}") => None
case r => r
}

new Challenge(
id,
name,
Expand Down Expand Up @@ -492,7 +495,8 @@ class ChallengeDAL @Inject() (
${challenge.dataOriginDate.getOrElse(DateTime.now()).toString}::timestamptz,
${challenge.extra.preferredTags}, ${challenge.extra.preferredReviewTags}, ${challenge.extra.limitTags},
${challenge.extra.limitReviewTags}, ${challenge.extra.taskStyles}, ${challenge.general.requiresLocal}, ${challenge.extra.isArchived},
${challenge.extra.reviewSetting}, ${challenge.extra.widgetLayout})
${challenge.extra.reviewSetting},
${asJson(challenge.extra.widgetLayout.getOrElse(Json.parse("{}")))}
ON CONFLICT(parent_id, LOWER(name)) DO NOTHING RETURNING #${this.retrieveColumns}"""
.as(this.parser.*)
.headOption
Expand Down Expand Up @@ -680,8 +684,8 @@ class ChallengeDAL @Inject() (
.getOrElse(cachedItem.extra.reviewSetting)

val widgetLayout = (updates \ "widgetLayout")
.asOpt[String]
.getOrElse(cachedItem.extra.widgetLayout.getOrElse(""))
.asOpt[JsValue]
.getOrElse(cachedItem.extra.widgetLayout.getOrElse(Json.parse("{}")))

val presets: List[String] = (updates \ "presets")
.asOpt[List[String]]
Expand Down Expand Up @@ -712,7 +716,9 @@ class ChallengeDAL @Inject() (
custom_basemap = $customBasemap, updatetasks = $updateTasks, exportable_properties = $exportableProperties,
osm_id_property = $osmIdProperty, task_bundle_id_property = $taskBundleIdProperty, preferred_tags = $preferredTags, preferred_review_tags = $preferredReviewTags,
limit_tags = $limitTags, limit_review_tags = $limitReviewTags, task_styles = $taskStyles,
requires_local = $requiresLocal, is_archived = $isArchived, review_setting = $reviewSetting, widget_layout = $widgetLayout
requires_local = $requiresLocal, is_archived = $isArchived, review_setting = $reviewSetting, widget_layout = ${asJson(
widgetLayout
)}
WHERE id = $id RETURNING #${this.retrieveColumns}""".as(parser.*).headOption

updatedChallenge match {
Expand Down
4 changes: 2 additions & 2 deletions conf/evolutions/default/93.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- --- !Ups
ALTER TABLE IF EXISTS challenges
ADD COLUMN widget_layout text DEFAULT '';
ADD COLUMN widget_layout jsonb NOT NULL DEFAULT '{}'::jsonb;

-- --- !Downs
ALTER TABLE IF EXISTS challenges
DROP COLUMN widget_layout;
DROP COLUMN widget_layout;

0 comments on commit 2ab35a0

Please sign in to comment.