From 7d4c70abadf98db732f877840b095e5efcc94587 Mon Sep 17 00:00:00 2001 From: Mercier Mateo Date: Wed, 11 Dec 2024 00:37:55 +0100 Subject: [PATCH] Move tag not found error handling to validate in order to get proper http 400 status code --- app/actions/post.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/actions/post.go b/app/actions/post.go index 18255a655..061b0d27f 100644 --- a/app/actions/post.go +++ b/app/actions/post.go @@ -3,7 +3,6 @@ package actions import ( "context" "time" - "fmt" "github.com/getfider/fider/app/models/dto" "github.com/getfider/fider/app/models/entity" @@ -31,16 +30,15 @@ type CreateNewPost struct { // OnPreExecute prefetches Tags for later use func (input *CreateNewPost) OnPreExecute(ctx context.Context) error { - fmt.Println(env.Config.PostCreationWithTagsEnabled) if env.Config.PostCreationWithTagsEnabled { - input.Tags = make([]*entity.Tag, len(input.TagSlugs)) - for i, slug := range input.TagSlugs { + input.Tags = make([]*entity.Tag, 0, len(input.TagSlugs)) + for _, slug := range input.TagSlugs { getTag := &query.GetTagBySlug{Slug: slug} if err := bus.Dispatch(ctx, getTag); err != nil { - return err + break } - input.Tags[i] = getTag.Result + input.Tags = append(input.Tags, getTag.Result) } } @@ -71,6 +69,8 @@ func (action *CreateNewPost) Validate(ctx context.Context, user *entity.User) *v result.AddFieldFailure("title", i18n.T(ctx, "validation.custom.descriptivetitle")) } else if len(action.Title) > 100 { result.AddFieldFailure("title", propertyMaxStringLen(ctx, "title", 100)) + } else if env.Config.PostCreationWithTagsEnabled && len(action.TagSlugs) != len(action.Tags) { + result.AddFieldFailure("tags", propertyIsInvalid(ctx, "tags")) } else { err := bus.Dispatch(ctx, &query.GetPostBySlug{Slug: slug.Make(action.Title)}) if err != nil && errors.Cause(err) != app.ErrNotFound {