Skip to content

Commit

Permalink
Add readable error messages for Project/TaskType unique checks (#5602)
Browse files Browse the repository at this point in the history
* Add readable error messages for Project/TaskType unique checks

* format
  • Loading branch information
fm3 authored Jul 12, 2021
1 parent f5703d9 commit 61ce361
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/ProjectController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ProjectController @Inject()(projectService: ProjectService,
for {
_ <- projectDAO
.findOneByNameAndOrganization(project.name, request.identity._organization)(GlobalAccessContext)
.reverse ?~> "project.name.alreadyTaken"
.reverse ?~> Messages("project.name.alreadyTaken", project.name)
_ <- Fox
.assertTrue(userService.isTeamManagerOrAdminOf(request.identity, project._team)) ?~> "notAllowed" ~> FORBIDDEN
_ <- projectDAO.insertOne(project, request.identity._organization) ?~> "project.creation.failed"
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/TaskTypeController.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import com.mohiva.play.silhouette.api.Silhouette
import com.scalableminds.util.accesscontext.GlobalAccessContext
import com.scalableminds.util.tools.{Fox, FoxImplicits}
import com.scalableminds.webknossos.tracingstore.tracings.TracingType
import models.annotation.AnnotationSettings
Expand Down Expand Up @@ -37,6 +38,9 @@ class TaskTypeController @Inject()(taskTypeDAO: TaskTypeDAO,
withJsonBodyUsing(taskTypePublicReads) { taskType =>
for {
_ <- Fox.assertTrue(userService.isTeamManagerOrAdminOf(request.identity, taskType._team)) ?~> "notAllowed" ~> FORBIDDEN
_ <- taskTypeDAO
.findOneBySummaryAndOrganization(taskType.summary, request.identity._organization)(GlobalAccessContext)
.reverse ?~> Messages("taskType.summary.alreadyTaken", taskType.summary)
_ <- taskTypeDAO.insertOne(taskType, request.identity._organization)
js <- taskTypeService.publicWrites(taskType)
} yield Ok(js)
Expand Down
10 changes: 10 additions & 0 deletions app/models/task/TaskType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ class TaskTypeDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
parsed <- parseFirst(r, id.toString)
} yield parsed

def findOneBySummaryAndOrganization(summary: String, organizationId: ObjectId)(
implicit ctx: DBAccessContext): Fox[TaskType] =
for {
accessQuery <- readAccessQuery
r <- run(
sql"select #$columns from #$existingCollectionName where summary = '#${sanitize(summary)}' and _organization = $organizationId and #$accessQuery"
.as[TasktypesRow])
parsed <- parseFirst(r, summary)
} yield parsed

override def findAll(implicit ctx: DBAccessContext): Fox[List[TaskType]] =
for {
accessQuery <- readAccessQuery
Expand Down
3 changes: 2 additions & 1 deletion conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ priority=Priority

project=Project (optional)
projectName=Project Name
project.name.alreadyTaken=Name already used
project.name.alreadyTaken=The project name “{0}” is already in use.
project.name.invalidChars=Name contains invalid characters
project.remove.confirm=Do you really want to delete this project? All annotations and tasks connected to this project will be deleted.
project.remove.notAllowed=You are not authorized to remove this project. Talk to the owner.
Expand Down Expand Up @@ -292,6 +292,7 @@ task.notOneAnnotation=The specified base task does not have exactly one (finishe
assignment.retrieval.failed=Failed to retrieve any assignment

taskType=Task type
taskType.summary.alreadyTaken=A task type with summary “{0}” already exists. The summary needs to be unique.
taskType.editSuccess=Task type successfully edited
taskType.notFound=Selected task type doesn’t exist
taskType.deleteSuccess=Task type “{0}” successfully deleted
Expand Down

0 comments on commit 61ce361

Please sign in to comment.