Allow tag ids or tag names when replacing tags

This commit is contained in:
eikek 2021-07-25 21:24:08 +02:00
parent 90f19aec43
commit fcef52856a
6 changed files with 24 additions and 20 deletions

View File

@ -24,7 +24,7 @@ import org.log4s.getLogger
trait OItem[F[_]] {
/** Sets the given tags (removing all existing ones). */
def setTags(item: Ident, tagIds: List[Ident], collective: Ident): F[UpdateResult]
def setTags(item: Ident, tagIds: List[String], collective: Ident): F[UpdateResult]
/** Sets tags for multiple items. The tags of the items will be
* replaced with the given ones. Same as `setTags` but for multiple
@ -32,7 +32,7 @@ trait OItem[F[_]] {
*/
def setTagsMultipleItems(
items: NonEmptyList[Ident],
tags: List[Ident],
tags: List[String],
collective: Ident
): F[UpdateResult]
@ -304,19 +304,20 @@ object OItem {
def setTags(
item: Ident,
tagIds: List[Ident],
tagIds: List[String],
collective: Ident
): F[UpdateResult] =
setTagsMultipleItems(NonEmptyList.of(item), tagIds, collective)
def setTagsMultipleItems(
items: NonEmptyList[Ident],
tags: List[Ident],
tags: List[String],
collective: Ident
): F[UpdateResult] =
UpdateResult.fromUpdate(store.transact(for {
k <- RTagItem.deleteItemTags(items, collective)
res <- items.traverse(i => RTagItem.setAllTags(i, tags))
k <- RTagItem.deleteItemTags(items, collective)
rtags <- RTag.findAllByNameOrId(tags, collective)
res <- items.traverse(i => RTagItem.setAllTags(i, rtags.map(_.tagId)))
n = res.fold
} yield k + n))

View File

@ -1628,6 +1628,8 @@ paths:
Update the tags associated to an item. This will remove all
existing ones and sets the given tags, such that after this
returns, the item has exactly the tags as given.
Tags may be specified as names or ids.
security:
- authTokenHeader: []
parameters:
@ -1636,7 +1638,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/ReferenceList"
$ref: "#/components/schemas/StringList"
responses:
200:
description: Ok

View File

@ -59,9 +59,12 @@ object ItemMultiRoutes extends MultiIdSupport {
for {
json <- req.as[ItemsAndRefs]
items <- readIds[F](json.items)
tags <- json.refs.traverse(readId[F])
res <- backend.item.setTagsMultipleItems(items, tags, user.account.collective)
resp <- Ok(Conversions.basicResult(res, "Tags updated"))
res <- backend.item.setTagsMultipleItems(
items,
json.refs,
user.account.collective
)
resp <- Ok(Conversions.basicResult(res, "Tags updated"))
} yield resp
case req @ POST -> Root / "tags" =>

View File

@ -146,8 +146,8 @@ object ItemRoutes {
case req @ PUT -> Root / Ident(id) / "tags" =>
for {
tags <- req.as[ReferenceList].map(_.items)
res <- backend.item.setTags(id, tags.map(_.id), user.account.collective)
tags <- req.as[StringList].map(_.items)
res <- backend.item.setTags(id, tags, user.account.collective)
resp <- Ok(Conversions.basicResult(res, "Tags updated"))
} yield resp

View File

@ -1783,12 +1783,12 @@ itemDetail flags id receive =
}
setTags : Flags -> String -> ReferenceList -> (Result Http.Error BasicResult -> msg) -> Cmd msg
setTags : Flags -> String -> StringList -> (Result Http.Error BasicResult -> msg) -> Cmd msg
setTags flags item tags receive =
Http2.authPut
{ url = flags.config.baseUrl ++ "/api/v1/sec/item/" ++ item ++ "/tags"
, account = getAccount flags
, body = Http.jsonBody (Api.Model.ReferenceList.encode tags)
, body = Http.jsonBody (Api.Model.StringList.encode tags)
, expect = Http.expectJson receive Api.Model.BasicResult.decoder
}

View File

@ -19,11 +19,9 @@ import Api.Model.MoveAttachment exposing (MoveAttachment)
import Api.Model.OptionalDate exposing (OptionalDate)
import Api.Model.OptionalId exposing (OptionalId)
import Api.Model.OptionalText exposing (OptionalText)
import Api.Model.ReferenceList exposing (ReferenceList)
import Api.Model.Tag exposing (Tag)
import Api.Model.StringList exposing (StringList)
import Browser.Navigation as Nav
import Comp.AttachmentMeta
import Comp.ConfirmModal
import Comp.CustomFieldMultiInput
import Comp.DatePicker
import Comp.DetailEdit
@ -1628,8 +1626,8 @@ saveTags flags model =
tags =
Comp.Dropdown.getSelected model.tagModel
|> Util.List.distinct
|> List.map (\t -> IdName t.id t.name)
|> ReferenceList
|> List.map (\t -> t.id)
|> StringList
in
Api.setTags flags model.item.id tags SaveResp
@ -1762,7 +1760,7 @@ resetField : Flags -> String -> (Field -> Result Http.Error BasicResult -> msg)
resetField flags item tagger field =
case field of
Data.Fields.Tag ->
Api.setTags flags item Api.Model.ReferenceList.empty (tagger Data.Fields.Tag)
Api.setTags flags item Api.Model.StringList.empty (tagger Data.Fields.Tag)
Data.Fields.Folder ->
Api.setFolder flags item Api.Model.OptionalId.empty (tagger Data.Fields.Folder)