Store configuration owner by MariaDB username

This commit is contained in:
Mikhail Chusavitin
2026-02-04 12:20:41 +03:00
parent f42b850734
commit f4f92dea66
15 changed files with 281 additions and 223 deletions

View File

@@ -43,13 +43,16 @@ func (r *ConfigurationRepository) Delete(id uint) error {
return r.db.Delete(&models.Configuration{}, id).Error
}
func (r *ConfigurationRepository) ListByUser(userID uint, offset, limit int) ([]models.Configuration, int64, error) {
func (r *ConfigurationRepository) ListByUser(ownerUsername string, offset, limit int) ([]models.Configuration, int64, error) {
var configs []models.Configuration
var total int64
r.db.Model(&models.Configuration{}).Where("user_id = ?", userID).Count(&total)
ownerScope := "owner_username = ? OR (COALESCE(owner_username, '') = '' AND user_id IN (SELECT id FROM qt_users WHERE username = ?))"
r.db.Model(&models.Configuration{}).Where(ownerScope, ownerUsername, ownerUsername).Count(&total)
err := r.db.
Where("user_id = ?", userID).
Preload("User").
Where(ownerScope, ownerUsername, ownerUsername).
Order("created_at DESC").
Offset(offset).
Limit(limit).
@@ -81,6 +84,7 @@ func (r *ConfigurationRepository) ListAll(offset, limit int) ([]models.Configura
r.db.Model(&models.Configuration{}).Count(&total)
err := r.db.
Preload("User").
Order("created_at DESC").
Offset(offset).
Limit(limit).