Upstream sync
This commit is contained in:
@@ -15,8 +15,9 @@ import (
|
||||
|
||||
func ListTextTemplates(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)
|
||||
@@ -33,19 +34,36 @@ func ListTextTemplates(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Convert ids to []int64
|
||||
var templateIDs []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
|
||||
}
|
||||
templateIDs = append(templateIDs, id)
|
||||
}
|
||||
|
||||
// Create a slice to hold the text templates
|
||||
var textTemplates []models.TextTemplate
|
||||
textTemplates := []models.TextTemplate{}
|
||||
|
||||
// Count the total number of text templates
|
||||
var total int64
|
||||
if err := shared.GetDb().Model(&models.TextTemplate{}).Count(&total).Error; err != nil {
|
||||
countDb := shared.GetDb()
|
||||
db := shared.GetDb()
|
||||
if len(templateIDs) > 0 {
|
||||
countDb = countDb.Where("id IN (?)", templateIDs)
|
||||
db = db.Where("id IN (?)", templateIDs)
|
||||
}
|
||||
if err := countDb.Model(&models.TextTemplate{}).Count(&total).Error; err != nil {
|
||||
log.Printf("ListTextTemplates Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the text templates from the database with LIMIT and OFFSET
|
||||
if err := shared.GetDb().Order("created_at desc").Limit(limit).Offset(offset).Find(&textTemplates).Error; err != nil {
|
||||
if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&textTemplates).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("ListTextTemplates Error: No text templates found: %v", err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No text templates found"})
|
||||
@@ -82,4 +100,3 @@ func CreateTextTemplate(c *gin.Context) {
|
||||
log.Printf("Successfully received and saved text template: %v", textTemplate)
|
||||
c.JSON(http.StatusOK, gin.H{"data": textTemplate})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user