diff --git a/models/authors_add_update.go b/models/authors_add_update.go index 837acd2..2ccd727 100644 --- a/models/authors_add_update.go +++ b/models/authors_add_update.go @@ -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 { diff --git a/routes/api/v1/authors_add_update.go b/routes/api/v1/authors_add_update.go index 0dc8f1f..8819baa 100644 --- a/routes/api/v1/authors_add_update.go +++ b/routes/api/v1/authors_add_update.go @@ -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