29 lines
658 B
Go
29 lines
658 B
Go
package models
|
|
|
|
import "fmt"
|
|
|
|
// AddOrUpdatePublisher adds or updates a publisher from a publisher struct
|
|
func AddOrUpdatePublisher(publisher Publisher) (newPublisher Publisher, err error) {
|
|
if publisher.ID == 0 {
|
|
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")
|
|
}
|
|
|
|
_, err = x.Insert(&publisher)
|
|
|
|
if err != nil {
|
|
return Publisher{}, err
|
|
}
|
|
} else {
|
|
_, err = x.ID(publisher.ID).Update(&publisher)
|
|
|
|
if err != nil {
|
|
return Publisher{}, err
|
|
}
|
|
}
|
|
|
|
newPublisher, _, err = GetPublisherByID(publisher.ID)
|
|
|
|
return newPublisher, err
|
|
}
|