Bugfix: when editing a book, all authors passed had been inserted again
the build failed Details

This commit is contained in:
konrad 2017-11-20 12:49:25 +01:00 committed by kolaente
parent 3c656bb8be
commit 7b864ff958
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 8 additions and 7 deletions

View File

@ -39,6 +39,7 @@ func GetAuthorByID(id int64) (author Author, exists bool, err error) {
// GetAuthorsByBook get all authors of a book
func GetAuthorsByBook(bookID int64) (authors []Author, err error) {
err = x.
Select("authors.*").
Table("authors_books").
Join("INNER", "authors", "authors_books.author_id = authors.id").
Where("book_id = ?", bookID).

View File

@ -9,7 +9,7 @@ func AddAuthor(author Author) (newAuthor Author, err error) {
}
// Get the newly inserted author
newAuthor, _, err = GetAuthorByID(author.ID)
newAuthor = author
return newAuthor, err
}

View File

@ -69,6 +69,9 @@ func AddOrUpdateBook(book Book) (newBook Book, err error) {
return Book{}, err
}
// Delete all (maybe) previously existing author relations
x.Delete(AuthorBook{BookID:book.ID})
// Take the authors, look if they exist, if they don't create them
authorBookRelation := make([]AuthorBook, 0)
@ -82,19 +85,16 @@ func AddOrUpdateBook(book Book) (newBook Book, err error) {
if !exists {
// We have to insert authors on this inperformant way, because we need the ne ids afterwards
authorToBeInserted := new(Author)
authorToBeInserted.Forename = author.Forename
authorToBeInserted.Lastname = author.Lastname
_, err := x.Insert(authorToBeInserted)
insertedAuthor, err := AddAuthor(author)
if err != nil {
return Book{}, err
}
author.ID = authorToBeInserted.ID
author.ID = insertedAuthor.ID
}
// Prepare new author Relateion
// Prepare new author Relation
authorBookRelation = append(authorBookRelation, AuthorBook{BookID: book.ID, AuthorID: author.ID})
}