Added check if author is empty

This commit is contained in:
konrad 2017-11-24 12:39:52 +01:00 committed by kolaente
parent 326e44e3a7
commit 79da356557
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 8 additions and 2 deletions

View File

@ -1,10 +1,16 @@
package models
import "fmt"
// AddOrUpdateAuthor adds a new author based on an author struct
func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) {
// If the ID is 0, insert the author, otherwise update it
if author.ID == 0 {
// Check if the author is empty, only insert it if not
if author.Forename == "" && author.Lastname == "" {
return Author{}, fmt.Errorf("Author cannot be empty")
}
_, err = x.Insert(&author)
if err != nil {

View File

@ -37,8 +37,8 @@ func AuthorAddOrUpdate(c echo.Context) error {
}
// Check if we have at least a Lastname
if authorToInsert.Lastname == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least a lastame."})
if authorToInsert.Lastname == "" && authorToInsert.Forename == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least one name."})
}
// Insert the author