diff --git a/REST-Tests/lists.http b/REST-Tests/lists.http new file mode 100644 index 0000000000..4f58c0b316 --- /dev/null +++ b/REST-Tests/lists.http @@ -0,0 +1,27 @@ +# Get all lists +GET http://localhost:8080/api/v1/lists +Authorization: Bearer {{auth_token}} + +### + +# Get one list +GET http://localhost:8080/api/v1/lists/28 +Authorization: Bearer {{auth_token}} + +### + +# Add a new list +PUT http://localhost:8080/api/v1/namespaces/1/lists +Authorization: Bearer {{auth_token}} +Content-Type: application/json + +{} + +### + +# Delete a list from a list +DELETE http://localhost:8080/api/v1/lists/28 +Authorization: Bearer {{auth_token}} + +### + diff --git a/models/error.go b/models/error.go index 90cc6aa003..2981182257 100644 --- a/models/error.go +++ b/models/error.go @@ -191,6 +191,19 @@ func (err ErrNeedToHaveListReadAccess) Error() string { return fmt.Sprintf("You need to be List owner to do that [ListID: %d, UserID: %d]", err.ListID, err.UserID) } +// ErrListTitleCannotBeEmpty represents a "ErrListTitleCannotBeEmpty" kind of error. Used if the list does not exist. +type ErrListTitleCannotBeEmpty struct{} + +// IsErrListTitleCannotBeEmpty checks if an error is a ErrListTitleCannotBeEmpty. +func IsErrListTitleCannotBeEmpty(err error) bool { + _, ok := err.(ErrListTitleCannotBeEmpty) + return ok +} + +func (err ErrListTitleCannotBeEmpty) Error() string { + return fmt.Sprintf("List item text cannot be empty.") +} + // ================ // List item errors // ================ diff --git a/models/list_create_update.go b/models/list_create_update.go index 1031b4a04e..1d36d63502 100644 --- a/models/list_create_update.go +++ b/models/list_create_update.go @@ -3,6 +3,11 @@ package models // CreateOrUpdateList updates a list or creates it if it doesn't exist func CreateOrUpdateList(list *List) (err error) { + // Check we have at least a title + if list.Title == "" { + return ErrListTitleCannotBeEmpty{} + } + if list.ID == 0 { _, err = x.Insert(list) } else { diff --git a/routes/crud/create.go b/routes/crud/create.go index 866ad6c607..590057d15b 100644 --- a/routes/crud/create.go +++ b/routes/crud/create.go @@ -35,6 +35,9 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error { if models.IsErrListDoesNotExist(err) { return echo.NewHTTPError(http.StatusBadRequest, "The list does not exist.") } + if models.IsErrListTitleCannotBeEmpty(err) { + return echo.NewHTTPError(http.StatusBadRequest, "You must provide at least a list title.") + } if models.IsErrListItemCannotBeEmpty(err) { return echo.NewHTTPError(http.StatusBadRequest, "You must provide at least a list item text.") }