[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) | 	log = log.WithField("productid", id) | ||||||
| 	var list []*models.Good | 	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 { | 	if result.RowsAffected == 0 { | ||||||
| 		log.Warn("no goods found") | 		log.Warn("no goods found") | ||||||
| 		http.Error(w, "no goods found for this product", http.StatusNotFound) | 		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 | 		return -1, log | ||||||
| 	} | 	} | ||||||
| 	var count float64 | 	var count float64 | ||||||
| 	var g models.Good | 	&models.Good.FilterAvailable(database.Read).Where("product_id = ?", id).Count(&count) | ||||||
| 	g.FilterAvailable(database.Read).Where("product_id = ?", id).Count(&count) |  | ||||||
| 	return int(count), log | 	return int(count), log | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -13,10 +13,10 @@ import ( | ||||||
| // Function to get the status of the microservice, the database and the goods
 | // Function to get the status of the microservice, the database and the goods
 | ||||||
| func status(w http.ResponseWriter, r *http.Request) { | func status(w http.ResponseWriter, r *http.Request) { | ||||||
| 	log := logger.HTTP(r) | 	log := logger.HTTP(r) | ||||||
| 	var goods []*models.Good | 	var good models.Good | ||||||
| 	var count int64 | 	var count int64 | ||||||
| 	var avg float64 | 	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) | 	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{}{ | 	lib.Write(w, map[string]interface{}{ | ||||||
| 		"status": "running", | 		"status": "running", | ||||||
|  |  | ||||||
|  | @ -30,7 +30,7 @@ type Good struct { | ||||||
| 
 | 
 | ||||||
| // Function to generate a database and select locked goods with a filter
 | // Function to generate a database and select locked goods with a filter
 | ||||||
| func (g *Good) FilterAvailable(db *gorm.DB) *gorm.DB { | 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
 | // Function to lock a good, so that it cannot be locked (bought) by other users
 | ||||||
|  |  | ||||||
		Reference in New Issue