Added check for list title when creating a new list
This commit is contained in:
parent
a9094506fa
commit
935aef8a38
4 changed files with 48 additions and 0 deletions
27
REST-Tests/lists.http
Normal file
27
REST-Tests/lists.http
Normal 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}}
|
||||||
|
|
||||||
|
###
|
||||||
|
|
|
@ -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)
|
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
|
// List item errors
|
||||||
// ================
|
// ================
|
||||||
|
|
|
@ -3,6 +3,11 @@ package models
|
||||||
// CreateOrUpdateList updates a list or creates it if it doesn't exist
|
// CreateOrUpdateList updates a list or creates it if it doesn't exist
|
||||||
func CreateOrUpdateList(list *List) (err error) {
|
func CreateOrUpdateList(list *List) (err error) {
|
||||||
|
|
||||||
|
// Check we have at least a title
|
||||||
|
if list.Title == "" {
|
||||||
|
return ErrListTitleCannotBeEmpty{}
|
||||||
|
}
|
||||||
|
|
||||||
if list.ID == 0 {
|
if list.ID == 0 {
|
||||||
_, err = x.Insert(list)
|
_, err = x.Insert(list)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -35,6 +35,9 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
||||||
if models.IsErrListDoesNotExist(err) {
|
if models.IsErrListDoesNotExist(err) {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "The list does not exist.")
|
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) {
|
if models.IsErrListItemCannotBeEmpty(err) {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "You must provide at least a list item text.")
|
return echo.NewHTTPError(http.StatusBadRequest, "You must provide at least a list item text.")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue