mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-03 04:26:36 +08:00
8e06b7e149
- Remove all license-related code (handler/service/repository/model) License authorization is managed by separate server: https://github.com/ShukeBta/MediaStationLicenseServer - Fix compilation errors: model field alignment, method name fixes, route conflicts, struct literal corrections (7 files) - Add Chinese README.md as primary, English README_EN.md - Update .gitignore: exclude .workbuddy/, editor backups - Add new repository files: assistant, play_profile, storage_config
64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
|
)
|
|
|
|
// PlayProfileRepository persists model.PlayProfile records.
|
|
type PlayProfileRepository struct{ db *gorm.DB }
|
|
|
|
// Create inserts a new play profile.
|
|
func (r *PlayProfileRepository) Create(ctx context.Context, p *model.PlayProfile) error {
|
|
return r.db.WithContext(ctx).Create(p).Error
|
|
}
|
|
|
|
// FindByID returns the profile or (nil, nil).
|
|
func (r *PlayProfileRepository) FindByID(ctx context.Context, id string) (*model.PlayProfile, error) {
|
|
var p model.PlayProfile
|
|
err := r.db.WithContext(ctx).Where("id = ?", id).First(&p).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &p, nil
|
|
}
|
|
|
|
// List returns every profile (admin view).
|
|
func (r *PlayProfileRepository) List(ctx context.Context) ([]model.PlayProfile, error) {
|
|
var rows []model.PlayProfile
|
|
err := r.db.WithContext(ctx).Order("created_at desc").Find(&rows).Error
|
|
return rows, err
|
|
}
|
|
|
|
// ListByUser returns profiles owned by a user.
|
|
func (r *PlayProfileRepository) ListByUser(ctx context.Context, userID string) ([]model.PlayProfile, error) {
|
|
var rows []model.PlayProfile
|
|
err := r.db.WithContext(ctx).Where("user_id = ?", userID).
|
|
Order("created_at desc").Find(&rows).Error
|
|
return rows, err
|
|
}
|
|
|
|
// Update applies a partial update to a profile row.
|
|
func (r *PlayProfileRepository) Update(ctx context.Context, id string, patch map[string]any) error {
|
|
return r.db.WithContext(ctx).Model(&model.PlayProfile{}).
|
|
Where("id = ?", id).Updates(patch).Error
|
|
}
|
|
|
|
// Delete soft-deletes a profile.
|
|
func (r *PlayProfileRepository) Delete(ctx context.Context, id string) error {
|
|
return r.db.WithContext(ctx).Delete(&model.PlayProfile{}, "id = ?", id).Error
|
|
}
|
|
|
|
// ClearDefaultsFor resets is_default for all of a user's profiles.
|
|
func (r *PlayProfileRepository) ClearDefaultsFor(ctx context.Context, userID string) error {
|
|
return r.db.WithContext(ctx).Model(&model.PlayProfile{}).
|
|
Where("user_id = ?", userID).Update("is_default", false).Error
|
|
}
|