Added check for list title when creating a new list

This commit is contained in:
kolaente 2018-07-26 10:29:09 +02:00
parent a9094506fa
commit 935aef8a38
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
4 changed files with 48 additions and 0 deletions

27
REST-Tests/lists.http Normal file
View file

@ -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}}
###

View file

@ -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
// ================

View file

@ -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 {

View file

@ -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.")
}