[BUGFIX] improve database access
This commit is contained in:
		
							parent
							
								
									a3942f79b8
								
							
						
					
					
						commit
						cfe89e7f0c
					
				|  | @ -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 | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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", | ||||
|  |  | |||
|  | @ -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
 | ||||
|  |  | |||
		Reference in New Issue