diff --git a/http/good_show.go b/http/good_show.go index e6ce652..94a09fa 100644 --- a/http/good_show.go +++ b/http/good_show.go @@ -28,7 +28,7 @@ func listGoods(w http.ResponseWriter, r *http.Request) { } log = log.WithField("productid", id) var list []*models.Good - result := database.Read.Where("product_id = ?", id).Find(&list) + result := &models.Good.FilterAvailable(database.Read).Where("product_id = ?", id).Find(&list) if result.RowsAffected == 0 { log.Warn("no goods found") http.Error(w, "no goods found for this product", http.StatusNotFound) @@ -60,8 +60,7 @@ func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logr return -1, log } var count float64 - var g models.Good - g.FilterAvailable(database.Read).Where("product_id = ?", id).Count(&count) + &models.Good.FilterAvailable(database.Read).Where("product_id = ?", id).Count(&count) return int(count), log } diff --git a/http/status.go b/http/status.go index 8a3ad46..1a57940 100644 --- a/http/status.go +++ b/http/status.go @@ -13,10 +13,10 @@ import ( // Function to get the status of the microservice, the database and the goods func status(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) - var goods []*models.Good + var good models.Good var count int64 var avg float64 - database.Read.Find(&goods).Count(&count) + &models.Good.FilterAvailable(database.Read).Count(&count) database.Read.Raw("SELECT avg(g.gcount) as avg FROM (select count(*) as gcount FROM good g WHERE deleted_at is NULL GROUP BY g.product_id) g").Row().Scan(&avg) lib.Write(w, map[string]interface{}{ "status": "running", diff --git a/models/good.go b/models/good.go index 866d2c5..9aecd6b 100644 --- a/models/good.go +++ b/models/good.go @@ -30,7 +30,7 @@ type Good struct { // Function to generate a database and select locked goods with a filter func (g *Good) FilterAvailable(db *gorm.DB) *gorm.DB { - return db.Model(g).Where("locked_secret == '' OR locked_secret is NULL") + return db.Model(g).Where("locked_secret = '' OR locked_secret is NULL") } // Function to lock a good, so that it cannot be locked (bought) by other users