2017-10-11 16:06:51 +00:00
|
|
|
package models
|
|
|
|
|
2018-01-16 13:28:02 +00:00
|
|
|
import "fmt"
|
|
|
|
|
2017-11-21 11:09:45 +00:00
|
|
|
// AddOrUpdatePublisher adds or updates a publisher from a publisher struct
|
|
|
|
func AddOrUpdatePublisher(publisher Publisher) (newPublisher Publisher, err error) {
|
|
|
|
if publisher.ID == 0 {
|
2018-01-16 13:28:02 +00:00
|
|
|
if publisher.Name == "" { // Only insert it if the name is not empty
|
|
|
|
return Publisher{}, fmt.Errorf("You need at least a name to insert a new publisher")
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:28:44 +00:00
|
|
|
_, err = x.Insert(&publisher)
|
2017-10-11 19:45:31 +00:00
|
|
|
|
2018-01-16 13:28:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return Publisher{}, err
|
|
|
|
}
|
2017-11-21 11:09:45 +00:00
|
|
|
} else {
|
2017-11-29 11:25:52 +00:00
|
|
|
_, err = x.ID(publisher.ID).Update(&publisher)
|
2017-11-21 11:09:45 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Publisher{}, err
|
|
|
|
}
|
2017-10-11 19:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newPublisher, _, err = GetPublisherByID(publisher.ID)
|
|
|
|
|
|
|
|
return newPublisher, err
|
2017-11-07 15:35:10 +00:00
|
|
|
}
|