Skip to content

Commit

Permalink
add avatar_url contraint and default
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Aug 10, 2024
1 parent 9dc82e6 commit aaef430
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ class TeamSchema @Inject() (override val service: TeamService)
case Some(description) => Some(description)
case None => team.description
},
avatarURL = context.arg(TeamSchema.avatarUrlArg) match {
case Some(avatarURL) => Some(avatarURL)
case None => team.avatarURL
}
avatarURL = context.arg(TeamSchema.avatarUrlArg)
),
user
)
Expand Down Expand Up @@ -208,10 +205,10 @@ object TeamSchema extends DefaultWrites {
OptionInputType(StringType),
"The description of the object"
)
val avatarUrlArg: Argument[Option[String]] =
val avatarUrlArg: Argument[String] =
Argument(
"avatarURL",
OptionInputType(StringType),
StringType,
"An avatar URL representing the object"
)
}
4 changes: 2 additions & 2 deletions app/org/maproulette/framework/model/Group.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ case class Group(
id: Long,
name: String,
description: Option[String] = None,
avatarURL: Option[String] = None,
avatarURL: String,
groupType: Int = Group.GROUP_TYPE_STANDARD,
created: DateTime = DateTime.now(),
modified: DateTime = DateTime.now()
Expand All @@ -34,7 +34,7 @@ object Group extends CommonField {
(JsPath \ "id").read[Long] and
(JsPath \ "name").read[String] and
(JsPath \ "description").readNullable[String] and
(JsPath \ "avatarURL").readNullable[String] and
(JsPath \ "avatarURL").read[String] and
(JsPath \ "groupType").read[Int] and
((JsPath \ "created").read[DateTime] or Reads.pure(DateTime.now())) and
((JsPath \ "modified").read[DateTime] or Reads.pure(DateTime.now()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LeaderboardRepository @Inject() (override val db: Database) extends Reposi
): RowParser[LeaderboardUser] = {
get[Long]("user_id") ~
get[String]("user_name") ~
get[String]("user_avatar_url").? ~
get[String]("user_avatar_url") ~
get[Int]("user_score") ~
get[Int]("user_ranking") ~
get[DateTime]("created").? ~
Expand All @@ -61,7 +61,7 @@ class LeaderboardRepository @Inject() (override val db: Database) extends Reposi
new LeaderboardUser(
userId,
name,
avatarURL.getOrElse(""),
avatarURL,
score,
rank,
completedTasks,
Expand Down
24 changes: 18 additions & 6 deletions app/org/maproulette/framework/repository/UserRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ class UserRepository @Inject() (
case None =>
val group = this.serviceManager.group
.create(
Group(-1, s"User ${follower.id} Following", groupType = Group.GROUP_TYPE_FOLLOWING)
Group(
-1,
s"User ${follower.id} Following",
Some(""),
"",
groupType = Group.GROUP_TYPE_FOLLOWING
)
)
.get
this.setupGroup("following_group", group.id, follower.id)
Expand All @@ -183,7 +189,13 @@ class UserRepository @Inject() (
case None =>
val group = this.serviceManager.group
.create(
Group(-1, s"User ${followed.id} Followers", groupType = Group.GROUP_TYPE_FOLLOWERS)
Group(
-1,
s"User ${followed.id} Followers",
Some(""),
"",
groupType = Group.GROUP_TYPE_FOLLOWERS
)
)
.get
this.setupGroup("followers_group", group.id, followed.id)
Expand Down Expand Up @@ -466,10 +478,10 @@ object UserRepository {
get[Long]("users.id") ~
get[Long]("users.osm_id") ~
get[String]("users.name") ~
get[Option[String]]("users.avatar_url") ~
get[String]("users.avatar_url") ~
get[List[Int]]("roles") map {
case projectId ~ userId ~ osmId ~ displayName ~ avatarURL ~ roles =>
ProjectManager(projectId, userId, osmId, displayName, avatarURL.getOrElse(""), roles)
ProjectManager(projectId, userId, osmId, displayName, avatarURL, roles)
}
}

Expand All @@ -493,7 +505,7 @@ object UserRepository {
get[DateTime]("users.osm_created") ~
get[String]("users.name") ~
get[Option[String]]("users.description") ~
get[Option[String]]("users.avatar_url") ~
get[String]("users.avatar_url") ~
get[Option[String]]("home") ~
get[Option[String]]("users.api_key") ~
get[String]("users.oauth_token") ~
Expand Down Expand Up @@ -544,7 +556,7 @@ object UserRepository {
osmId,
displayName,
description.getOrElse(""),
avatarURL.getOrElse(""),
avatarURL,
Location(locationWKT.getX, locationWKT.getY),
osmCreated,
oauthToken
Expand Down
24 changes: 24 additions & 0 deletions conf/evolutions/default/95.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- --- !Ups
-- Update any existing null avatar_url values to the default
UPDATE users
SET avatar_url = '/assets/images/user_no_image.png'
WHERE avatar_url IS NULL;

-- Alter the table to set avatar_url as NOT NULL with a default value
ALTER TABLE IF EXISTS users
ALTER COLUMN avatar_url SET DEFAULT '/assets/images/user_no_image.png',
ALTER COLUMN avatar_url SET NOT NULL;

-- Add a CHECK constraint to ensure avatar_url is not empty
ALTER TABLE IF EXISTS users
ADD CONSTRAINT check_avatar_url_not_empty CHECK (avatar_url <> '');

-- --- !Downs
-- Remove the CHECK constraint
ALTER TABLE IF EXISTS users
DROP CONSTRAINT IF EXISTS check_avatar_url_not_empty;

-- Revert the avatar_url column to allow null values and remove the default value
ALTER TABLE IF EXISTS users
ALTER COLUMN avatar_url DROP NOT NULL,
ALTER COLUMN avatar_url DROP DEFAULT;
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class GroupMemberRepositorySpec(implicit val application: Application) extends F
-1,
name,
Some("A test group"),
Some("http://www.gravatar.com/avatar/?d=identicon"),
"http://www.gravatar.com/avatar/?d=identicon",
groupType
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ class GroupRepositorySpec(implicit val application: Application) extends Framewo
}

"create a group" taggedAs GroupTag in {
val createdGroup = this.repository.create(Group(-1, "RandomCreateGroup")).get
val createdGroup = this.repository.create(Group(-1, "RandomCreateGroup", Some(""), "")).get
val retrievedGroup = this.repository.retrieve(createdGroup.id)
retrievedGroup.get mustEqual createdGroup
}

"update a group" taggedAs GroupTag in {
val randomGroup = this.repository.create(Group(-1, "RandomGroup")).get
val randomGroup = this.repository.create(Group(-1, "RandomGroup", Some(""), "")).get
this.repository.update(
Group(randomGroup.id, "UpdatedName", Some("Updated description"), Some("new_avatar_url"))
Group(randomGroup.id, "UpdatedName", Some("Updated description"), "new_avatar_url")
)
val group = this.service.retrieve(randomGroup.id)
group.get.name mustEqual "UpdatedName"
group.get.description.get mustEqual "Updated description"
group.get.avatarURL.get mustEqual "new_avatar_url"
group.get.avatarURL mustEqual "new_avatar_url"
}

"delete a group" taggedAs GroupTag in {
val group = this.repository.create(Group(-1, "RandomDeleteGroup")).get
val group = this.repository.create(Group(-1, "RandomDeleteGroup", Some(""), "")).get
this.repository.delete(group)
this.service.retrieve(group.id) mustEqual None
}
Expand All @@ -73,7 +73,7 @@ class GroupRepositorySpec(implicit val application: Application) extends Framewo
-1,
name,
Some("A test group"),
Some("http://www.gravatar.com/avatar/?d=identicon")
"http://www.gravatar.com/avatar/?d=identicon"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class GroupServiceSpec(implicit val application: Application) extends FrameworkH
-1,
name,
Some("A test group"),
Some("http://www.gravatar.com/avatar/?d=identicon")
"http://www.gravatar.com/avatar/?d=identicon"
)
}
}
2 changes: 1 addition & 1 deletion test/org/maproulette/framework/util/FrameworkHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ trait FrameworkHelper extends PlaySpec with BeforeAndAfterAll with MockitoSugar
-1,
name,
Some("A test team"),
Some("http://www.gravatar.com/avatar/?d=identicon")
"http://www.gravatar.com/avatar/?d=identicon"
)
}

Expand Down

0 comments on commit aaef430

Please sign in to comment.