2017-11-21 10:38:52 +00:00
|
|
|
package models
|
|
|
|
|
2017-11-24 11:39:52 +00:00
|
|
|
import "fmt"
|
|
|
|
|
2017-11-21 10:38:52 +00:00
|
|
|
// 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 {
|
2017-11-24 11:39:52 +00:00
|
|
|
// Check if the author is empty, only insert it if not
|
|
|
|
if author.Forename == "" && author.Lastname == "" {
|
|
|
|
return Author{}, fmt.Errorf("Author cannot be empty")
|
|
|
|
}
|
2017-11-21 10:38:52 +00:00
|
|
|
_, err = x.Insert(&author)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Author{}, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err = x.Where("id = ?", author.ID).Update(&author)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Author{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the newly inserted author
|
|
|
|
newAuthor = author
|
|
|
|
|
|
|
|
return newAuthor, err
|
|
|
|
}
|