Files
MeBox/internal/repository/storage_config_repo.go
T
truewhile b0fe40142a Rebrand MMTL to MeBox (name, logo, Docker image) (#17)
* Rebrand MMTL to MeBox across codebase and assets

Rename the project display name, Go module path, environment variable
prefix (MEBOX_*), Docker image references, and UI branding from MMTL/mmtl
to MeBox/mebox. Replace logo assets with the new MeBox icon and keep
legacy SQLite migration support for existing mmtl.db deployments.

Co-authored-by: truewhile <truewhile@users.noreply.github.com>

* Fix logo icons: use cube-only crop without truncated text

Previous icon generation cropped too much of the source image, including
partial MeBox wordmark text that was cut off in square icon containers.
Regenerate logo-64/192/512, favicon, and SVG from cube-only region.

Co-authored-by: truewhile <truewhile@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: truewhile <truewhile@users.noreply.github.com>
2026-09-02 16:26:28 +08:00

83 lines
2.3 KiB
Go

package repository
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
"github.com/truewhile/MeBox/internal/model"
)
// StorageConfigRepository persists model.StorageConfig records.
type StorageConfigRepository struct{ db *gorm.DB }
// Get returns the config row by type, or (nil, nil).
func (r *StorageConfigRepository) Get(ctx context.Context, kind string) (*model.StorageConfig, error) {
var c model.StorageConfig
err := r.db.WithContext(ctx).Where("type = ?", kind).First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &c, nil
}
// List returns all storage configs.
func (r *StorageConfigRepository) List(ctx context.Context) ([]model.StorageConfig, error) {
var rows []model.StorageConfig
err := r.db.WithContext(ctx).Order("type asc").Find(&rows).Error
return rows, err
}
// Upsert creates or replaces a storage config keyed by Type.
func (r *StorageConfigRepository) Upsert(ctx context.Context, c *model.StorageConfig) error {
db := r.db.WithContext(ctx)
var existing model.StorageConfig
err := db.Where("type = ?", c.Type).First(&existing).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
now := time.Now()
c.ID = uuid.NewString()
c.CreatedAt = now
c.UpdatedAt = now
return db.Model(&model.StorageConfig{}).Create(map[string]any{
"id": c.ID,
"type": c.Type,
"config": c.Config,
"enabled": c.Enabled,
"last_error": c.LastError,
"created_at": c.CreatedAt,
"updated_at": c.UpdatedAt,
}).Error
}
if err != nil {
return err
}
c.ID = existing.ID
return db.Model(&model.StorageConfig{}).Where("id = ?", existing.ID).Updates(map[string]any{
"config": c.Config,
"enabled": c.Enabled,
"last_error": c.LastError,
"updated_at": time.Now(),
}).Error
}
// Delete 物理删除存储配置。
func (r *StorageConfigRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Unscoped().Where("id = ?", id).Delete(&model.StorageConfig{}).Error
}
// FindByID returns a storage config by ID.
func (r *StorageConfigRepository) FindByID(ctx context.Context, id string) (*model.StorageConfig, error) {
var c model.StorageConfig
err := r.db.WithContext(ctx).Where("id = ?", id).First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &c, err
}