Upstream sync

This commit is contained in:
Senad Uka
2023-10-13 11:48:14 +02:00
parent 7369739bdd
commit d01c1a0232
29 changed files with 563 additions and 444 deletions

View File

@@ -15,8 +15,9 @@ import (
func ListProductTemplates(c *gin.Context) {
// Get limit and offset from query parameter with defaults
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999")
limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0")
iDsStr := c.QueryArray("ids[]")
// Convert limit and offset to int
limit, err := strconv.Atoi(limitStr)
@@ -34,18 +35,36 @@ func ListProductTemplates(c *gin.Context) {
}
// Create a slice to hold the product templates
var productTemplates []models.ProductTemplate
productTemplates := []models.ProductTemplate{}
// Convert ids to []int64
var productIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
productIDs = append(productIDs, id)
}
countDb := shared.GetDb()
db := shared.GetDb()
if len(productIDs) > 0 {
countDb = countDb.Where("id IN (?)", productIDs)
db = db.Where("id IN (?)", productIDs)
}
// Count the total number of product templates
var total int64
if err := shared.GetDb().Model(&models.ProductTemplate{}).Count(&total).Error; err != nil {
if err := countDb.Model(&models.ProductTemplate{}).Count(&total).Error; err != nil {
log.Printf("ListProductTemplates Error: Database error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
return
}
// Fetch the product templates from the database with LIMIT and OFFSET
if err := shared.GetDb().Order("created_at desc").Limit(limit).Offset(offset).Find(&productTemplates).Error; err != nil {
if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&productTemplates).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("ListProductTemplates Error: No product templates found: %v", err)
c.JSON(http.StatusNotFound, gin.H{"error": "No product templates found"})