Skip to content

Commit

Permalink
M3-122: Added delete duration endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed Jun 11, 2020
1 parent 9a67637 commit ceac629
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ class ObservationV1Api(controller: ObservationController)(implicit val executor:
extends V1APIStack {

get("/:uuid") {
val uuid = params.getAs[UUID]("uuid").getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide a UUID")))))
val uuid = params
.getAs[UUID]("uuid")
.getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide a UUID")))))
controller
.findByUUID(uuid)
.map({
case None =>
halt(NotFound(toJson(ErrorMsg(404, "An ImagedMoment with a UUID of $uuid was not found"))))
halt(
NotFound(toJson(ErrorMsg(404, "An ImagedMoment with a UUID of $uuid was not found")))
)
case Some(v) => toJson(v)
})
}
Expand All @@ -69,12 +73,20 @@ class ObservationV1Api(controller: ObservationController)(implicit val executor:

get("/association/:uuid") {
val uuid =
params.getAs[UUID]("uuid").getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide an Association UUID")))))
params
.getAs[UUID]("uuid")
.getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide an Association UUID")))))
controller
.findByAssociationUUID(uuid)
.map({
case None =>
halt(NotFound(toJson(ErrorMsg(404, s"No observation for association with uuid of ${uuid} was found"))))
halt(
NotFound(
toJson(
ErrorMsg(404, s"No observation for association with uuid of ${uuid} was found")
)
)
)
case Some(obs) => toJson(obs)
})
}
Expand All @@ -98,15 +110,23 @@ class ObservationV1Api(controller: ObservationController)(implicit val executor:

get("/concept/count/:concept") {
val concept =
params.get("concept").getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide a concept to search for")))))
params
.get("concept")
.getOrElse(
halt(BadRequest(toJson(ErrorMsg(400, "Please provide a concept to search for"))))
)
controller
.countByConcept(concept)
.map(n => s"""{"concept":"$concept", "count":"$n"}""")
}

get("/concept/images/count/:concept") {
val concept =
params.get("concept").getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide a concept to search for")))))
params
.get("concept")
.getOrElse(
halt(BadRequest(toJson(ErrorMsg(400, "Please provide a concept to search for"))))
)
controller
.countByConceptWithImages(concept)
.map(n => s"""{"concept":"$concept", "count":"$n"}""")
Expand Down Expand Up @@ -150,9 +170,17 @@ class ObservationV1Api(controller: ObservationController)(implicit val executor:

put("/concept/rename") {
val oldConcept =
params.get("old").getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide the concept being replaced")))))
params
.get("old")
.getOrElse(
halt(BadRequest(toJson(ErrorMsg(400, "Please provide the concept being replaced"))))
)
val newConcept =
params.get("new").getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide the replacement concept")))))
params
.get("new")
.getOrElse(
halt(BadRequest(toJson(ErrorMsg(400, "Please provide the replacement concept"))))
)
controller
.updateConcept(oldConcept, newConcept)
.map(n =>
Expand All @@ -175,19 +203,41 @@ class ObservationV1Api(controller: ObservationController)(implicit val executor:
controller
.update(uuid, concept, observer, observationDate, duration, group, activity, imagedMomentUUID)
.map({
case None => halt(NotFound(toJson(ErrorMsg(404, s"Failed. No observation with UUID of $uuid was found."))))
case None =>
halt(
NotFound(toJson(ErrorMsg(404, s"Failed. No observation with UUID of $uuid was found.")))
)
case Some(obs) => toJson(obs)
})
}

put("/delete/duration/:uuid") {
validateRequest() // Apply API security
val uuid = params
.getAs[UUID]("uuid")
.getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "A uuid parameter is required")))))
controller.deleteDuration(uuid) map ({
case None =>
halt(
NotFound(toJson(ErrorMsg(404, s"Failed. No observation with UUID of $uuid was found.")))
)
case Some(obs) => toJson(obs)
})
}

delete("/:uuid") {
validateRequest() // Apply API security
val uuid = params.getAs[UUID]("uuid").getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide a UUID")))))
val uuid = params
.getAs[UUID]("uuid")
.getOrElse(halt(BadRequest(toJson(ErrorMsg(400, "Please provide a UUID")))))
controller
.delete(uuid)
.map({
case true => halt(NoContent()) // Success!!
case false => halt(NotFound(toJson(ErrorMsg(404, s"Failed. No observation with UUID of $uuid was found."))))
case true => halt(NoContent()) // Success!!
case false =>
halt(
NotFound(toJson(ErrorMsg(404, s"Failed. No observation with UUID of $uuid was found.")))
)
})
}

Expand All @@ -204,7 +254,11 @@ class ObservationV1Api(controller: ObservationController)(implicit val executor:
halt(BadRequest(toJson(ErrorMsg(400, "No observation UUIDs were provided as JSON"))))
controller.bulkDelete(uuids)
case _ =>
halt(BadRequest(toJson(ErrorMsg(400, "bulk delete only accepts JSON (Content-Type: application/json)"))))
halt(
BadRequest(
toJson(ErrorMsg(400, "bulk delete only accepts JSON (Content-Type: application/json)"))
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ class ObservationController(
exec(fn)
}

def deleteDuration(uuid: UUID)(implicit ec: ExecutionContext): Future[Option[Observation]] = {
def fn(dao: ODAO): Option[Observation] =
dao
.findByUUID(uuid)
.map(obs => {
obs.duration = null
obs
})
exec(fn)
}

def bulkDelete(uuids: Iterable[UUID])(implicit ec: ExecutionContext): Future[Boolean] = {
def fn(dao: ODAO): Boolean =
uuids.map(deleteFunction(dao, _)).toSeq.forall(b => b)
Expand Down

0 comments on commit ceac629

Please sign in to comment.