From b56465e996b6d5cc025b14693cf03d187a38660f Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 28 Nov 2017 16:19:59 +0100 Subject: [PATCH] Fixed a bug where a new quantity was inserted when it hasnt changed --- models/book.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/models/book.go b/models/book.go index 01eee5c..6167908 100644 --- a/models/book.go +++ b/models/book.go @@ -57,9 +57,19 @@ func GetQuantityByBook(book Book) (quantity int64, err error) { // SetBookQuantity sets a new quantity for a book func (book Book) setBookQuantity(quantity int64) (err error) { - q := Quantity{BookID: book.ID, Quantity: quantity} - _, err = x.Insert(q) - return err + // Check if the quantity already exists and only insert it if not + qty, err := GetQuantityByBook(book) + if err != nil { + return err + } + + if qty != quantity { + q := Quantity{BookID: book.ID, Quantity: quantity} + _, err = x.Insert(q) + return err + } + + return nil } // GetBookByID gets a Book by its ID