update caching and server creation

This commit is contained in:
Fran Jurmanović
2025-06-01 19:48:39 +02:00
parent 8a3b11b1ef
commit d57013bb50
26 changed files with 888 additions and 249 deletions

View File

@@ -0,0 +1,58 @@
package repository
import (
"acc-server-manager/local/model"
"context"
"time"
"gorm.io/gorm"
)
type SystemConfigRepository struct {
db *gorm.DB
}
func NewSystemConfigRepository(db *gorm.DB) *SystemConfigRepository {
return &SystemConfigRepository{
db: db,
}
}
func (r *SystemConfigRepository) Initialize(ctx context.Context) error {
// Migration and seeding are now handled in the db package
return nil
}
func (r *SystemConfigRepository) Get(ctx context.Context, key string) (*model.SystemConfig, error) {
var config model.SystemConfig
err := r.db.Where("key = ?", key).First(&config).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
return &config, nil
}
func (r *SystemConfigRepository) GetAll(ctx context.Context) (*[]model.SystemConfig, error) {
var configs []model.SystemConfig
if err := r.db.Find(&configs).Error; err != nil {
return nil, err
}
return &configs, nil
}
func (r *SystemConfigRepository) Update(ctx context.Context, config *model.SystemConfig) error {
if err := config.Validate(); err != nil {
return err
}
config.DateModified = time.Now().UTC().Format(time.RFC3339)
return r.db.Model(&model.SystemConfig{}).
Where("key = ?", config.Key).
Updates(map[string]interface{}{
"value": config.Value,
"date_modified": config.DateModified,
}).Error
}