feat: merge conflict resolution, site management, UI fixes

This commit is contained in:
ShukeBta
2026-05-16 17:57:34 +08:00
parent 0be8c7952e
commit cbb4b806be
84 changed files with 13826 additions and 202 deletions
@@ -0,0 +1,92 @@
// Package repository 实现下载客户端配置的数据访问层。
package repository
import (
"context"
"errors"
"time"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// DownloadClientRepository persists model.DownloadClient records.
type DownloadClientRepository struct{ db *gorm.DB }
// Create inserts a new download client.
func (r *DownloadClientRepository) Create(ctx context.Context, c *model.DownloadClient) error {
return r.db.WithContext(ctx).Create(c).Error
}
// FindByID returns the download client by ID, or (nil, nil) when absent.
func (r *DownloadClientRepository) FindByID(ctx context.Context, id string) (*model.DownloadClient, error) {
var c model.DownloadClient
err := r.db.WithContext(ctx).Where("id = ?", id).First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &c, nil
}
// FindDefault returns the default download client, or (nil, nil).
func (r *DownloadClientRepository) FindDefault(ctx context.Context) (*model.DownloadClient, error) {
var c model.DownloadClient
err := r.db.WithContext(ctx).Where("is_default = ? AND enabled = ?", true, true).First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &c, nil
}
// List returns all download clients ordered by creation time.
func (r *DownloadClientRepository) List(ctx context.Context) ([]model.DownloadClient, error) {
var rows []model.DownloadClient
err := r.db.WithContext(ctx).Order("created_at asc").Find(&rows).Error
return rows, err
}
// ListEnabled returns all enabled download clients.
func (r *DownloadClientRepository) ListEnabled(ctx context.Context) ([]model.DownloadClient, error) {
var rows []model.DownloadClient
err := r.db.WithContext(ctx).Where("enabled = ?", true).Order("created_at asc").Find(&rows).Error
return rows, err
}
// Update persists changes to a download client.
func (r *DownloadClientRepository) Update(ctx context.Context, c *model.DownloadClient) error {
return r.db.WithContext(ctx).Save(c).Error
}
// Delete removes a download client (soft-delete).
func (r *DownloadClientRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.DownloadClient{}, "id = ?", id).Error
}
// ClearDefault unsets the default flag for all clients.
func (r *DownloadClientRepository) ClearDefault(ctx context.Context) error {
return r.db.WithContext(ctx).Model(&model.DownloadClient{}).
Where("is_default = ?", true).Update("is_default", false).Error
}
// SetDefault sets a specific client as default and clears others.
func (r *DownloadClientRepository) SetDefault(ctx context.Context, id string) error {
now := time.Now()
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&model.DownloadClient{}).
Where("is_default = ?", true).Update("is_default", false).Error; err != nil {
return err
}
return tx.Model(&model.DownloadClient{}).
Where("id = ?", id).Updates(map[string]any{
"is_default": true,
"updated_at": now,
}).Error
})
}
@@ -0,0 +1,67 @@
// Package repository 实现通知渠道配置的数据访问层。
package repository
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// NotifyChannelRepository persists model.NotifyChannel records.
type NotifyChannelRepository struct{ db *gorm.DB }
// Create inserts a new notification channel.
func (r *NotifyChannelRepository) Create(ctx context.Context, c *model.NotifyChannel) error {
return r.db.WithContext(ctx).Create(c).Error
}
// FindByID returns the notification channel by ID, or (nil, nil) when absent.
func (r *NotifyChannelRepository) FindByID(ctx context.Context, id string) (*model.NotifyChannel, error) {
var c model.NotifyChannel
err := r.db.WithContext(ctx).Where("id = ?", id).First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &c, nil
}
// List returns all notification channels ordered by creation time.
func (r *NotifyChannelRepository) List(ctx context.Context) ([]model.NotifyChannel, error) {
var rows []model.NotifyChannel
err := r.db.WithContext(ctx).Order("created_at asc").Find(&rows).Error
return rows, err
}
// ListEnabled returns all enabled notification channels.
func (r *NotifyChannelRepository) ListEnabled(ctx context.Context) ([]model.NotifyChannel, error) {
var rows []model.NotifyChannel
err := r.db.WithContext(ctx).Where("enabled = ?", true).Order("created_at asc").Find(&rows).Error
return rows, err
}
// ListByEvent returns all enabled channels that subscribe to the given event type.
func (r *NotifyChannelRepository) ListByEvent(ctx context.Context, eventType string) ([]model.NotifyChannel, error) {
var rows []model.NotifyChannel
// Events is a JSON array stored as text; use LIKE for simple matching.
// This works for exact event type matches within the JSON array.
err := r.db.WithContext(ctx).
Where("enabled = ? AND events LIKE ?", true, "%\""+eventType+"\"%").
Find(&rows).Error
return rows, err
}
// Update persists changes to a notification channel.
func (r *NotifyChannelRepository) Update(ctx context.Context, c *model.NotifyChannel) error {
return r.db.WithContext(ctx).Save(c).Error
}
// Delete removes a notification channel (soft-delete).
func (r *NotifyChannelRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.NotifyChannel{}, "id = ?", id).Error
}
+180 -10
View File
@@ -1,13 +1,14 @@
// Package repository implements a thin GORM-based data-access layer over the
// types declared in internal/model. Each method takes a context.Context so we
// can plug in cancellation / tracing later.
// Package repository 实现基于 GORM 的数据访问层。
// 每个方法接受 context.Context 以便后续插入取消/追踪。
//
// Repositories are intentionally narrow: they only know how to persist data,
// not how to interpret it. Domain logic lives in internal/service.
// Repository 故意保持精简:它们只负责持久化数据,不处理业务逻辑。
// 业务逻辑位于 internal/service。
package repository
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"time"
@@ -16,7 +17,7 @@ import (
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// Container is the registry of all repositories injected into services.
// Container 是所有 repositories 的注册表,注入到 services 中。
type Container struct {
DB *gorm.DB
User *UserRepository
@@ -30,9 +31,16 @@ type Container struct {
Subscription *SubscriptionRepository
Setting *SettingRepository
Log *AccessLogRepository
Permission *PermissionRepository
RefreshToken *RefreshTokenRepository
ApiConfig *ApiConfigRepository
DownloadClient *DownloadClientRepository
NotifyChannel *NotifyChannelRepository
Site *SiteRepository
STRM *STRMRepository
}
// New wires every repository to a single *gorm.DB.
// New 将每个 repository 连接到单个 *gorm.DB。
func New(db *gorm.DB) *Container {
return &Container{
DB: db,
@@ -47,6 +55,13 @@ func New(db *gorm.DB) *Container {
Subscription: &SubscriptionRepository{db: db},
Setting: &SettingRepository{db: db},
Log: &AccessLogRepository{db: db},
Permission: &PermissionRepository{db: db},
RefreshToken: &RefreshTokenRepository{db: db},
ApiConfig: &ApiConfigRepository{db: db},
DownloadClient: &DownloadClientRepository{db: db},
NotifyChannel: &NotifyChannelRepository{db: db},
Site: &SiteRepository{db: db},
STRM: &STRMRepository{db: db},
}
}
@@ -267,7 +282,7 @@ func (r *HistoryRepository) ListByUser(ctx context.Context, userID string, limit
return rows, err
}
// ─── Favorite ────────────────────────────────────────────────────────────────
// ─── Favorite ───────────────────────────────────────────────────────────────
// FavoriteRepository persists model.Favorite records.
type FavoriteRepository struct{ db *gorm.DB }
@@ -311,7 +326,7 @@ func (r *PlaylistRepository) ListByUser(ctx context.Context, userID string) ([]m
return rows, err
}
// ─── Download ────────────────────────────────────────────────────────────────
// ─── Download ───────────────────────────────────────────────────────────────
// DownloadRepository persists model.DownloadTask records.
type DownloadRepository struct{ db *gorm.DB }
@@ -328,7 +343,7 @@ func (r *DownloadRepository) List(ctx context.Context) ([]model.DownloadTask, er
return rows, err
}
// ─── Subscription ────────────────────────────────────────────────────────────
// ─── Subscription ───────────────────────────────────────────────────────────
// SubscriptionRepository persists model.Subscription records.
type SubscriptionRepository struct{ db *gorm.DB }
@@ -389,3 +404,158 @@ func (r *AccessLogRepository) Recent(ctx context.Context, limit int) ([]model.Ac
err := r.db.WithContext(ctx).Order("created_at desc").Limit(limit).Find(&rows).Error
return rows, err
}
// ─── Permission ──────────────────────────────────────────────────────────────
// PermissionRepository persists model.UserPermission records.
type PermissionRepository struct{ db *gorm.DB }
// Create inserts a new permission record.
func (r *PermissionRepository) Create(ctx context.Context, p *model.UserPermission) error {
return r.db.WithContext(ctx).Create(p).Error
}
// FindByUserID returns the permission record for a user, or (nil, nil) when absent.
func (r *PermissionRepository) FindByUserID(ctx context.Context, userID string) (*model.UserPermission, error) {
var p model.UserPermission
err := r.db.WithContext(ctx).Where("user_id = ?", userID).First(&p).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &p, nil
}
// Update updates permission fields for a user.
func (r *PermissionRepository) Update(ctx context.Context, userID string, updates map[string]bool) error {
return r.db.WithContext(ctx).Model(&model.UserPermission{}).
Where("user_id = ?", userID).Updates(updates).Error
}
// Upsert creates or updates a permission record.
func (r *PermissionRepository) Upsert(ctx context.Context, p *model.UserPermission) error {
return r.db.WithContext(ctx).Where("user_id = ?", p.UserID).
Assign(*p).FirstOrCreate(p).Error
}
// Delete removes a permission record.
func (r *PermissionRepository) Delete(ctx context.Context, userID string) error {
return r.db.WithContext(ctx).Where("user_id = ?", userID).Delete(&model.UserPermission{}).Error
}
// ─── Refresh Token ───────────────────────────────────────────────────────────
// RefreshTokenRepository persists model.RefreshToken records.
type RefreshTokenRepository struct{ db *gorm.DB }
// Create inserts a new refresh token record.
func (r *RefreshTokenRepository) Create(ctx context.Context, t *model.RefreshToken) error {
return r.db.WithContext(ctx).Create(t).Error
}
// FindByHash returns the refresh token matching the hash, or (nil, nil).
func (r *RefreshTokenRepository) FindByHash(ctx context.Context, hash string) (*model.RefreshToken, error) {
var t model.RefreshToken
err := r.db.WithContext(ctx).Where("token_hash = ?", hash).First(&t).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &t, nil
}
// RevokeByUserID revokes all refresh tokens for a user.
func (r *RefreshTokenRepository) RevokeByUserID(ctx context.Context, userID string) error {
return r.db.WithContext(ctx).Model(&model.RefreshToken{}).
Where("user_id = ?", userID).Update("revoked", true).Error
}
// DeleteExpired removes all expired refresh tokens.
func (r *RefreshTokenRepository) DeleteExpired(ctx context.Context) error {
return r.db.WithContext(ctx).Where("expires_at < ?", time.Now()).Delete(&model.RefreshToken{}).Error
}
// Revoke revokes a specific refresh token.
func (r *RefreshTokenRepository) Revoke(ctx context.Context, hash string) error {
return r.db.WithContext(ctx).Model(&model.RefreshToken{}).
Where("token_hash = ?", hash).Update("revoked", true).Error
}
// HashToken returns the SHA256 hash of a token.
func HashToken(token string) string {
h := sha256.Sum256([]byte(token))
return hex.EncodeToString(h[:])
}
// ─── API Config ──────────────────────────────────────────────────────────────
// ApiConfigRepository persists model.ApiConfig records.
type ApiConfigRepository struct{ db *gorm.DB }
// Create inserts a new API config record.
func (r *ApiConfigRepository) Create(ctx context.Context, c *model.ApiConfig) error {
return r.db.WithContext(ctx).Create(c).Error
}
// FindByProvider returns the API config for a provider, or (nil, nil).
func (r *ApiConfigRepository) FindByProvider(ctx context.Context, provider string) (*model.ApiConfig, error) {
var c model.ApiConfig
err := r.db.WithContext(ctx).Where("provider = ?", provider).First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &c, nil
}
// List returns all API configs.
func (r *ApiConfigRepository) List(ctx context.Context) ([]model.ApiConfig, error) {
var rows []model.ApiConfig
err := r.db.WithContext(ctx).Order("provider asc").Find(&rows).Error
return rows, err
}
// Upsert creates or updates an API config.
func (r *ApiConfigRepository) Upsert(ctx context.Context, c *model.ApiConfig) error {
return r.db.WithContext(ctx).Where("provider = ?", c.Provider).
Assign(model.ApiConfig{
APIKey: c.APIKey,
BaseURL: c.BaseURL,
Extra: c.Extra,
Enabled: c.Enabled,
UpdatedAt: time.Now(),
}).FirstOrCreate(c).Error
}
// Update updates an API config.
func (r *ApiConfigRepository) Update(ctx context.Context, c *model.ApiConfig) error {
return r.db.WithContext(ctx).Model(&model.ApiConfig{}).
Where("provider = ?", c.Provider).Updates(map[string]any{
"api_key": c.APIKey,
"base_url": c.BaseURL,
"extra": c.Extra,
"enabled": c.Enabled,
"updated_at": time.Now(),
}).Error
}
// Delete removes an API config.
func (r *ApiConfigRepository) Delete(ctx context.Context, provider string) error {
return r.db.WithContext(ctx).Where("provider = ?", provider).Delete(&model.ApiConfig{}).Error
}
// UpdateTestResult 更新测试结果。
func (r *ApiConfigRepository) UpdateTestResult(ctx context.Context, provider, result string) error {
now := time.Now()
return r.db.WithContext(ctx).Model(&model.ApiConfig{}).
Where("provider = ?", provider).Updates(map[string]any{
"test_result": result,
"last_tested_at": &now,
}).Error
}
+56
View File
@@ -0,0 +1,56 @@
// Package repository — PT 站点数据访问层。
package repository
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// SiteRepository persists model.Site records.
type SiteRepository struct{ db *gorm.DB }
// Create inserts a new site.
func (r *SiteRepository) Create(ctx context.Context, s *model.Site) error {
return r.db.WithContext(ctx).Create(s).Error
}
// FindByID returns the site by ID, or (nil, nil) when absent.
func (r *SiteRepository) FindByID(ctx context.Context, id string) (*model.Site, error) {
var s model.Site
err := r.db.WithContext(ctx).Where("id = ?", id).First(&s).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &s, nil
}
// List returns all sites ordered by name.
func (r *SiteRepository) List(ctx context.Context) ([]model.Site, error) {
var rows []model.Site
err := r.db.WithContext(ctx).Order("name asc").Find(&rows).Error
return rows, err
}
// ListEnabled returns all enabled sites.
func (r *SiteRepository) ListEnabled(ctx context.Context) ([]model.Site, error) {
var rows []model.Site
err := r.db.WithContext(ctx).Where("enabled = ?", true).Order("name asc").Find(&rows).Error
return rows, err
}
// Update updates site fields.
func (r *SiteRepository) Update(ctx context.Context, s *model.Site) error {
return r.db.WithContext(ctx).Save(s).Error
}
// Delete removes a site (soft-delete).
func (r *SiteRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.Site{}, "id = ?", id).Error
}
+82
View File
@@ -0,0 +1,82 @@
// Package repository — STRM 文件记录数据访问层。
package repository
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// STRMRepository persists model.STRMRecord records.
type STRMRepository struct{ db *gorm.DB }
// Create inserts a new STRM record.
func (r *STRMRepository) Create(ctx context.Context, s *model.STRMRecord) error {
return r.db.WithContext(ctx).Create(s).Error
}
// CreateBatch inserts multiple STRM records.
func (r *STRMRepository) CreateBatch(ctx context.Context, records []model.STRMRecord) error {
if len(records) == 0 {
return nil
}
return r.db.WithContext(ctx).CreateInBatches(records, 100).Error
}
// FindByID returns the STRM record by ID, or (nil, nil) when absent.
func (r *STRMRepository) FindByID(ctx context.Context, id string) (*model.STRMRecord, error) {
var s model.STRMRecord
err := r.db.WithContext(ctx).Where("id = ?", id).First(&s).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &s, nil
}
// List returns STRM records with optional filters. Supports pagination.
// Filters: media_id, media_type, protocol
func (r *STRMRepository) List(ctx context.Context, filters map[string]string, offset, limit int) ([]model.STRMRecord, int64, error) {
q := r.db.WithContext(ctx).Model(&model.STRMRecord{})
if mediaID, ok := filters["media_id"]; ok && mediaID != "" {
q = q.Where("media_id = ?", mediaID)
}
if mediaType, ok := filters["media_type"]; ok && mediaType != "" {
q = q.Where("media_type = ?", mediaType)
}
if protocol, ok := filters["protocol"]; ok && protocol != "" {
q = q.Where("protocol = ?", protocol)
}
var total int64
if err := q.Count(&total).Error; err != nil {
return nil, 0, err
}
var rows []model.STRMRecord
err := q.Order("created_at desc").Offset(offset).Limit(limit).Find(&rows).Error
return rows, total, err
}
// Update updates a STRM record.
func (r *STRMRepository) Update(ctx context.Context, s *model.STRMRecord) error {
return r.db.WithContext(ctx).Save(s).Error
}
// Delete removes a STRM record (soft-delete).
func (r *STRMRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.STRMRecord{}, "id = ?", id).Error
}
// FindByMediaID returns STRM records for a given media ID.
func (r *STRMRepository) FindByMediaID(ctx context.Context, mediaID string) ([]model.STRMRecord, error) {
var rows []model.STRMRecord
err := r.db.WithContext(ctx).Where("media_id = ?", mediaID).Find(&rows).Error
return rows, err
}