Add a route to add a new tag and associate it to an item

This commit is contained in:
Eike Kettner 2020-06-10 00:25:24 +02:00
parent d440247857
commit f407f08ed3
3 changed files with 255 additions and 194 deletions

View File

@ -43,8 +43,12 @@ trait OItem[F[_]] {
collective: Ident
): F[Option[AttachmentArchiveData[F]]]
/** Sets the given tags (removing all existing ones). */
def setTags(item: Ident, tagIds: List[Ident], collective: Ident): F[AddResult]
/** Create a new tag and add it to the item. */
def addNewTag(item: Ident, tag: RTag): F[AddResult]
def setDirection(item: Ident, direction: Direction, collective: Ident): F[AddResult]
def setCorrOrg(item: Ident, org: Option[Ident], collective: Ident): F[AddResult]
@ -132,8 +136,9 @@ object OItem {
}
def apply[F[_]: Effect](store: Store[F]): Resource[F, OItem[F]] =
Resource.pure[F, OItem[F]](new OItem[F] {
for {
otag <- OTag(store)
oitem <- Resource.pure[F, OItem[F]](new OItem[F] {
def moveAttachmentBefore(
itemId: Ident,
source: Ident,
@ -243,6 +248,24 @@ object OItem {
store.transact(db).attempt.map(AddResult.fromUpdate)
}
def addNewTag(item: Ident, tag: RTag): F[AddResult] =
(for {
_ <- OptionT(store.transact(RItem.getCollective(item)))
.filter(_ == tag.collective)
addres <- OptionT.liftF(otag.add(tag))
_ <- addres match {
case AddResult.Success =>
OptionT.liftF(
store.transact(RTagItem.insertItemTags(item, List(tag.tagId)))
)
case AddResult.EntityExists(_) =>
OptionT.pure[F](0)
case AddResult.Failure(_) =>
OptionT.pure[F](0)
}
} yield addres)
.getOrElse(AddResult.Failure(new Exception("Collective mismatch")))
def setDirection(
item: Ident,
direction: Direction,
@ -289,7 +312,11 @@ object OItem {
.attempt
.map(AddResult.fromUpdate)
def setNotes(item: Ident, notes: Option[String], collective: Ident): F[AddResult] =
def setNotes(
item: Ident,
notes: Option[String],
collective: Ident
): F[AddResult] =
store
.transact(RItem.updateNotes(item, collective, notes))
.attempt
@ -348,4 +375,5 @@ object OItem {
def deleteAttachment(id: Ident, collective: Ident): F[Int] =
QAttachment.deleteSingleAttachment(store)(id, collective)
})
} yield oitem
}

View File

@ -1081,6 +1081,31 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/BasicResult"
post:
tags: [ Item ]
summary: Add a new tag to an item.
description: |
Creates a new tag and associates it to the given item.
The tag's `id` and `created` are generated and not used from
the given data, so it can be left empty. Only `name` and
`category` are used, where `category` is optional.
security:
- authTokenHeader: []
parameters:
- $ref: "#/components/parameters/id"
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Tag"
responses:
200:
description: Ok
content:
application/json:
schema:
$ref: "#/components/schemas/BasicResult"
/sec/item/{id}/direction:
put:
tags: [ Item ]

View File

@ -78,6 +78,14 @@ object ItemRoutes {
resp <- Ok(Conversions.basicResult(res, "Tags updated"))
} yield resp
case req @ POST -> Root / Ident(id) / "tags" =>
for {
data <- req.as[Tag]
rtag <- Conversions.newTag(data, user.account.collective)
res <- backend.item.addNewTag(id, rtag)
resp <- Ok(Conversions.basicResult(res, "Tag added."))
} yield resp
case req @ PUT -> Root / Ident(id) / "direction" =>
for {
dir <- req.as[DirectionValue]