2017-10-09 19:06:40 +00:00
package models
2017-11-24 13:40:35 +00:00
import "fmt"
2017-10-11 19:42:16 +00:00
/ * *
: : USAGE : :
Erwatet ein Struct mit einem Buch .
Wenn dieses Struct einen Publisher in Book . Publisher enthält und dieser existiert , wird diese
ID verwendet . Wenn die ID nicht existiert oder 0 ist , wird geguckt , ob der Publisher unter
2017-11-28 09:43:23 +00:00
Book . Publisher bereits existtiert ( über die ID ) , ist das nicht der Fall , wird er in
2017-10-11 19:42:16 +00:00
die Datenbank eingetragen und mit dem Buch verknüpft .
2017-11-08 09:55:17 +00:00
Bei den Autoren wird ebenfalls überprüft , ob sie bereits existieren , wenn dem nicht so ist werden
2017-10-11 19:42:16 +00:00
sie in die Datenbank eingetragen und mit dem Buch verknüpft .
2017-11-07 15:35:10 +00:00
* /
2017-10-11 19:42:16 +00:00
2017-11-18 08:32:30 +00:00
// AddOrUpdateBook adds a new book or updates an existing one, it takes a book struct with author and publisher. Inserts them if they don't already exist
func AddOrUpdateBook ( book Book ) ( newBook Book , err error ) {
2017-10-11 19:42:16 +00:00
2017-11-29 14:59:34 +00:00
// Check if we have at least a booktitle when we're inserting a new book
2017-11-30 14:48:03 +00:00
if book . Title == "" && book . ID == 0 {
2017-11-24 13:40:35 +00:00
return Book { } , fmt . Errorf ( "the book should at least have a title" )
}
2017-10-11 19:42:16 +00:00
// Take Publisher, check if it exists. If not, insert it
exists := false
2017-11-28 09:43:23 +00:00
publisherid := book . PublisherID
2017-10-11 19:42:16 +00:00
if publisherid == 0 {
2017-11-28 09:43:23 +00:00
if book . Publisher . ID != 0 {
publisherid = book . Publisher . ID
2017-10-11 19:42:16 +00:00
}
}
2017-11-14 15:33:01 +00:00
_ , exists , err = GetPublisherByID ( publisherid )
2017-10-11 19:42:16 +00:00
if err != nil {
return Book { } , err
}
2017-11-28 11:27:00 +00:00
// If the publisher exists, make it the new publisher of the book
if exists {
book . PublisherID = publisherid
} else {
// Otherwise insert it and make it the new publisher afterwards
2017-11-28 09:43:23 +00:00
newPublisher , err := AddOrUpdatePublisher ( Publisher { Name : book . Publisher . Name } )
2017-10-11 19:42:16 +00:00
if err != nil {
return Book { } , err
}
2017-11-28 09:43:23 +00:00
book . PublisherID = newPublisher . ID
2017-10-11 19:42:16 +00:00
}
2017-11-16 18:07:32 +00:00
// Save the quantity for later use
qty := book . Quantity
2017-11-18 08:32:30 +00:00
// If we have an ID, try to update the book, otherwise create it
if book . ID == 0 {
// Insert the book
_ , err = x . Insert ( & book )
2017-10-11 16:17:51 +00:00
2017-11-18 08:32:30 +00:00
if err != nil {
return Book { } , err
}
} else {
// Update the book
_ , err := x . Id ( book . ID ) . Update ( book )
if err != nil {
return Book { } , err
}
2017-11-16 18:07:32 +00:00
}
// Set the Quantity
2017-11-29 11:25:52 +00:00
err = book . setQuantity ( qty )
2017-11-16 18:07:32 +00:00
if err != nil {
2017-10-11 16:17:51 +00:00
return Book { } , err
}
2017-11-20 11:49:25 +00:00
// Delete all (maybe) previously existing author relations
2017-11-21 10:40:07 +00:00
x . Delete ( AuthorBook { BookID : book . ID } )
2017-11-20 11:49:25 +00:00
2017-10-11 19:42:16 +00:00
// Take the authors, look if they exist, if they don't create them
authorBookRelation := make ( [ ] AuthorBook , 0 )
for _ , author := range book . Authors {
_ , exists , err := GetAuthorByID ( author . ID )
if err != nil {
return Book { } , err
}
if ! exists {
// We have to insert authors on this inperformant way, because we need the ne ids afterwards
2017-11-21 10:38:52 +00:00
insertedAuthor , err := AddOrUpdateAuthor ( author )
2017-10-11 19:42:16 +00:00
if err != nil {
return Book { } , err
}
2017-11-20 11:49:25 +00:00
author . ID = insertedAuthor . ID
2017-10-11 19:42:16 +00:00
}
2017-11-20 11:49:25 +00:00
// Prepare new author Relation
2017-10-11 19:42:16 +00:00
authorBookRelation = append ( authorBookRelation , AuthorBook { BookID : book . ID , AuthorID : author . ID } )
}
// Insert the connection between the author and the book
_ , err = x . Insert ( & authorBookRelation )
if err != nil {
return Book { } , err
}
2017-10-11 16:17:51 +00:00
// Get the newly inserted book
2017-11-08 09:57:38 +00:00
newBook , _ , err = GetBookByID ( book . ID )
2017-10-11 16:17:51 +00:00
2017-10-11 19:42:16 +00:00
return newBook , err
}