Added check if author is empty
This commit is contained in:
parent
326e44e3a7
commit
79da356557
|
@ -1,10 +1,16 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// AddOrUpdateAuthor adds a new author based on an author struct
|
// AddOrUpdateAuthor adds a new author based on an author struct
|
||||||
func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) {
|
func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) {
|
||||||
|
|
||||||
// If the ID is 0, insert the author, otherwise update it
|
// If the ID is 0, insert the author, otherwise update it
|
||||||
if author.ID == 0 {
|
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)
|
_, err = x.Insert(&author)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -37,8 +37,8 @@ func AuthorAddOrUpdate(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we have at least a Lastname
|
// Check if we have at least a Lastname
|
||||||
if authorToInsert.Lastname == "" {
|
if authorToInsert.Lastname == "" && authorToInsert.Forename == "" {
|
||||||
return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least a lastame."})
|
return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least one name."})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert the author
|
// Insert the author
|
||||||
|
|
Loading…
Reference in New Issue