Skip to content

Commit a33aeca

Browse files
authored
Merge pull request #77 from forkd-app/task/72-empty-recipe-revision-titles
2 parents 13acb45 + c47f3f9 commit a33aeca

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

api/services/recipe/recipe.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ const (
2424
DEFAULT_LIST_RECIPE_SORT_FIELD = "publish_date"
2525
)
2626

27+
func validateTitle(title string) error {
28+
if len(title) < 3 {
29+
return fmt.Errorf("title must be at least 3 characters long")
30+
}
31+
if len(title) > 150 {
32+
return fmt.Errorf("title must be no more than 150 characters long")
33+
}
34+
return nil
35+
}
36+
2737
type RecipeService interface {
2838
GetRecipeByID(ctx context.Context, id uuid.UUID) (*model.Recipe, error)
2939
GetRecipeBySlug(ctx context.Context, slug string, displayName string) (*model.Recipe, error)
@@ -142,6 +152,10 @@ func (r recipeService) AddRecipeRevision(ctx context.Context, input model.AddRev
142152
return nil, nil
143153
}
144154

155+
if err := validateTitle(input.Revision.Title); err != nil {
156+
return nil, err
157+
}
158+
145159
tx, err := r.conn.Begin(ctx)
146160
if err != nil {
147161
return nil, err
@@ -248,6 +262,10 @@ func (r recipeService) CreateRecipe(ctx context.Context, input model.CreateRecip
248262
return nil, nil
249263
}
250264

265+
if err := validateTitle(input.Revision.Title); err != nil {
266+
return nil, err
267+
}
268+
251269
user, _ := r.authService.GetUserSessionFromCtx(ctx)
252270

253271
tx, err := r.conn.Begin(ctx)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE recipe_revisions
2+
DROP CONSTRAINT title_length_check;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE recipe_revisions
2+
ADD CONSTRAINT title_length_check
3+
CHECK (char_length(trim(title)) >= 3 AND char_length(trim(title)) <= 150);

0 commit comments

Comments
 (0)