From e48521868859ceb05df3bd06f3bc3a8bda014132 Mon Sep 17 00:00:00 2001 From: konrad Date: Thu, 30 Nov 2017 15:48:03 +0100 Subject: [PATCH] Cleanup --- models/book.go | 10 +++------- models/books_add_update.go | 2 +- models/books_list.go | 13 +++++-------- models/publisher.go | 5 ++--- models/publishers_delete.go | 4 ++-- models/quantity.go | 8 ++++---- models/status.go | 4 ++-- models/user.go | 2 -- routes/api/v1/authors_add_update.go | 2 -- routes/api/v1/books_add_update.go | 2 -- routes/api/v1/items_add_update.go | 2 -- 11 files changed, 19 insertions(+), 35 deletions(-) diff --git a/models/book.go b/models/book.go index db2a1dc..dcbc4e8 100644 --- a/models/book.go +++ b/models/book.go @@ -1,9 +1,5 @@ package models -import ( - "fmt" -) - // Book holds a book type Book struct { ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` @@ -36,19 +32,19 @@ func GetBookByID(ID int64) (book Book, exists bool, err error) { // Get the books quantity. We can't join it because xorm ignores the Quantity option in struct book.Quantity, err = book.getQuantity() if err != nil { - fmt.Println("Error getting quantity:", err) + return Book{}, false, err } // Get publisher. We can't join it because xorm ignores the PublisherID option in struct book.Publisher, _, err = GetPublisherByID(book.PublisherID) if err != nil { - fmt.Println("Error getting publisher:", err) + return Book{}, false, err } // Get all authors book.Authors, err = GetAuthorsByBook(book) if err != nil { - fmt.Println("Error getting authors:", err) + return Book{}, false, err } } diff --git a/models/books_add_update.go b/models/books_add_update.go index 8240dad..9398972 100644 --- a/models/books_add_update.go +++ b/models/books_add_update.go @@ -21,7 +21,7 @@ sie in die Datenbank eingetragen und mit dem Buch verknüpft. func AddOrUpdateBook(book Book) (newBook Book, err error) { // Check if we have at least a booktitle when we're inserting a new book - if book.Title == "" && book.ID == 0{ + if book.Title == "" && book.ID == 0 { return Book{}, fmt.Errorf("the book should at least have a title") } diff --git a/models/books_list.go b/models/books_list.go index e983efd..1b17e50 100644 --- a/models/books_list.go +++ b/models/books_list.go @@ -1,7 +1,5 @@ package models -import "fmt" - // BookPublisher struct to join books with publishers type BookPublisher struct { Book `xorm:"extends"` @@ -13,15 +11,14 @@ func ListBooks(searchterm string) (books []*Book, err error) { if searchterm == "" { err = x.Table("books"). - //Join("INNER", "publishers", "books.publisher = publishers.id"). Find(&books) if err != nil { - fmt.Println("Error getting Books", err) + return []*Book{}, err } } else { err = x.Where("title LIKE ?", "%"+searchterm+"%").Find(&books) if err != nil { - fmt.Println("Error getting Books", err) + return []*Book{}, err } } @@ -31,19 +28,19 @@ func ListBooks(searchterm string) (books []*Book, err error) { // Get quantities books[i].Quantity, err = book.getQuantity() if err != nil { - fmt.Println("Error getting quantity:", err) + return []*Book{}, err } // Get publisher books[i].Publisher, _, err = GetPublisherByID(book.PublisherID) if err != nil { - fmt.Println("Error getting publisher:", err) + return []*Book{}, err } // Get all authors books[i].Authors, err = GetAuthorsByBook(*book) if err != nil { - fmt.Println("Error getting authors:", err) + return []*Book{}, err } } diff --git a/models/publisher.go b/models/publisher.go index dbd2029..88fcc21 100644 --- a/models/publisher.go +++ b/models/publisher.go @@ -15,7 +15,6 @@ func (Publisher) TableName() string { // GetPublisherByID returns a publisher by its ID func GetPublisherByID(id int64) (publisher Publisher, exists bool, err error) { - has, err := x.Id(id).Get(&publisher) - - return publisher, has, err + exists, err = x.Id(id).Get(&publisher) + return } diff --git a/models/publishers_delete.go b/models/publishers_delete.go index e559647..b317f74 100644 --- a/models/publishers_delete.go +++ b/models/publishers_delete.go @@ -18,8 +18,8 @@ func DeletePublisherByID(id int64) error { // Set all publisher to 0 on all book with this publisher _, err = x.Table("books"). - Where("publisher_id = ?", id). - Update(map[string]interface{}{"publisher_id": 0}) + Where("publisher_id = ?", id). + Update(map[string]interface{}{"publisher_id": 0}) return err } diff --git a/models/quantity.go b/models/quantity.go index 80e3179..f2e19c8 100644 --- a/models/quantity.go +++ b/models/quantity.go @@ -109,10 +109,10 @@ func (item Item) setQuantity(quantity int64) (err error) { func (book Book) getQuantity() (quantity int64, err error) { qty := Quantity{} _, err = x.Table("quantities"). - Select("quantities.id, quantity_relations.item_id, quantities.quantity, quantities.created"). - Join("INNER", "quantity_relations", "quantities.item_id = quantity_relations.id"). - Where("quantity_relations.book_id = ?", book.ID). - Desc("quantities.created").Get(&qty) + Select("quantities.id, quantity_relations.item_id, quantities.quantity, quantities.created"). + Join("INNER", "quantity_relations", "quantities.item_id = quantity_relations.id"). + Where("quantity_relations.book_id = ?", book.ID). + Desc("quantities.created").Get(&qty) return qty.Quantity, err } diff --git a/models/status.go b/models/status.go index a590083..a1547b4 100644 --- a/models/status.go +++ b/models/status.go @@ -9,11 +9,11 @@ type Status struct { // GetStatusList returns an array with all status func GetStatusList() (status []Status, err error) { err = x.Find(&status) - return status, err + return } // GetStatusByID returns a status object with all its infos func GetStatusByID(id int64) (status Status, err error) { _, err = x.Where("id = ?", id).Get(&status) - return status, err + return } diff --git a/models/user.go b/models/user.go index 3142a52..5eead47 100644 --- a/models/user.go +++ b/models/user.go @@ -29,8 +29,6 @@ func (User) TableName() string { // GetUserByID gets informations about a user by its ID func GetUserByID(id int64) (user User, exists bool, err error) { - /*exists, err = x.Id(id).Get(&user) - return user, exists, err*/ return GetUser(User{ID: id}) } diff --git a/routes/api/v1/authors_add_update.go b/routes/api/v1/authors_add_update.go index 587756f..6c162d8 100644 --- a/routes/api/v1/authors_add_update.go +++ b/routes/api/v1/authors_add_update.go @@ -2,7 +2,6 @@ package v1 import ( "encoding/json" - "fmt" "git.mowie.cc/konrad/Library/models" "github.com/labstack/echo" "net/http" @@ -23,7 +22,6 @@ func AuthorAddOrUpdate(c echo.Context) error { if authorFromString == "" { b := new(authorPayload) if err := c.Bind(b); err != nil { - fmt.Println(err) return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"}) } datAuthor = b.Author diff --git a/routes/api/v1/books_add_update.go b/routes/api/v1/books_add_update.go index 4bb3814..6bb5ded 100644 --- a/routes/api/v1/books_add_update.go +++ b/routes/api/v1/books_add_update.go @@ -2,7 +2,6 @@ package v1 import ( "encoding/json" - "fmt" "git.mowie.cc/konrad/Library/models" "github.com/labstack/echo" "net/http" @@ -23,7 +22,6 @@ func BookAddOrUpdate(c echo.Context) error { if bookFromString == "" { b := new(bookPayload) if err := c.Bind(b); err != nil { - fmt.Println(err) return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"}) } datBook = b.Book diff --git a/routes/api/v1/items_add_update.go b/routes/api/v1/items_add_update.go index 200ce63..1b8062e 100644 --- a/routes/api/v1/items_add_update.go +++ b/routes/api/v1/items_add_update.go @@ -7,7 +7,6 @@ import ( "net/http" "strconv" "strings" - "fmt" ) type itemPayload struct { @@ -23,7 +22,6 @@ func ItemAddOrUpdate(c echo.Context) error { if itemFromString == "" { b := new(itemPayload) if err := c.Bind(b); err != nil { - fmt.Println(err) return c.JSON(http.StatusBadRequest, models.Message{"No item model provided"}) } datItem = b.Item