Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/services/recipe/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const (
DEFAULT_LIST_RECIPE_SORT_FIELD = "publish_date"
)

func validateTitle(title string) error {
if len(title) < 3 {
return fmt.Errorf("title must be at least 3 characters long")
}
if len(title) > 150 {
return fmt.Errorf("title must be no more than 150 characters long")
}
return nil
}

type RecipeService interface {
GetRecipeByID(ctx context.Context, id uuid.UUID) (*model.Recipe, error)
GetRecipeBySlug(ctx context.Context, slug string, displayName string) (*model.Recipe, error)
Expand Down Expand Up @@ -142,6 +152,10 @@ func (r recipeService) AddRecipeRevision(ctx context.Context, input model.AddRev
return nil, nil
}

if err := validateTitle(input.Revision.Title); err != nil {
return nil, err
}

tx, err := r.conn.Begin(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -248,6 +262,10 @@ func (r recipeService) CreateRecipe(ctx context.Context, input model.CreateRecip
return nil, nil
}

if err := validateTitle(input.Revision.Title); err != nil {
return nil, err
}

user, _ := r.authService.GetUserSessionFromCtx(ctx)

tx, err := r.conn.Begin(ctx)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE recipe_revisions
DROP CONSTRAINT title_length_check;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE recipe_revisions
ADD CONSTRAINT title_length_check
CHECK (char_length(trim(title)) >= 3 AND char_length(trim(title)) <= 150);
Loading