Library/models/authors_add_update.go

42 lines
907 B
Go
Raw Normal View History

package models
// AddOrUpdateAuthor adds a new author based on an author struct
2018-03-06 11:36:49 +00:00
func AddOrUpdateAuthor(author Author, doer *User) (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{}, ErrAuthorCannotBeEmpty{}
2017-11-24 11:39:52 +00:00
}
_, err = x.Insert(&author)
if err != nil {
return Author{}, err
}
2018-03-06 11:36:49 +00:00
// Log
err = logAction(ActionTypeAuthorAdded, doer, author.ID)
if err != nil {
return Author{}, err
}
} else {
_, err = x.Where("id = ?", author.ID).Update(&author)
if err != nil {
return Author{}, err
}
2018-03-06 11:36:49 +00:00
// Log
err = logAction(ActionTypeAuthorUpdated, doer, author.ID)
if err != nil {
return Author{}, err
}
}
// Get the newly inserted author
newAuthor = author
return newAuthor, err
}