preset.go
| 1 | package repository |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | |
| 6 | "vidarchive/internal/models" |
| 7 | ) |
| 8 | |
| 9 | type PresetRepository struct { |
| 10 | db *sql.DB |
| 11 | } |
| 12 | |
| 13 | func NewPresetRepository(db *sql.DB) *PresetRepository { |
| 14 | return &PresetRepository{db: db} |
| 15 | } |
| 16 | |
| 17 | func (r *PresetRepository) Create(p *models.Preset) error { |
| 18 | result, err := r.db.Exec( |
| 19 | `INSERT INTO presets (name, description, is_default, format_mode, format, quality, custom_format, extract_audio, audio_format, embed_subs, sub_langs, embed_thumbnail, embed_metadata, write_info_json, output_template, custom_flags) |
| 20 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, |
| 21 | p.Name, p.Description, boolToInt(p.IsDefault), p.FormatMode, p.Format, p.Quality, p.CustomFormat, |
| 22 | boolToInt(p.ExtractAudio), p.AudioFormat, boolToInt(p.EmbedSubs), p.SubLangs, |
| 23 | boolToInt(p.EmbedThumbnail), boolToInt(p.EmbedMetadata), boolToInt(p.WriteInfoJSON), |
| 24 | p.OutputTemplate, p.CustomFlags, |
| 25 | ) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | p.ID, _ = result.LastInsertId() |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | func (r *PresetRepository) GetByID(id int64) (*models.Preset, error) { |
| 34 | row := r.db.QueryRow( |
| 35 | `SELECT id, name, description, is_default, format_mode, format, quality, custom_format, extract_audio, audio_format, embed_subs, sub_langs, embed_thumbnail, embed_metadata, write_info_json, output_template, custom_flags, created_at |
| 36 | FROM presets WHERE id = ?`, id, |
| 37 | ) |
| 38 | return scanPreset(row) |
| 39 | } |
| 40 | |
| 41 | func (r *PresetRepository) GetAll() ([]*models.Preset, error) { |
| 42 | rows, err := r.db.Query( |
| 43 | `SELECT id, name, description, is_default, format_mode, format, quality, custom_format, extract_audio, audio_format, embed_subs, sub_langs, embed_thumbnail, embed_metadata, write_info_json, output_template, custom_flags, created_at |
| 44 | FROM presets ORDER BY name`, |
| 45 | ) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | defer rows.Close() |
| 50 | |
| 51 | var presets []*models.Preset |
| 52 | for rows.Next() { |
| 53 | p, err := scanPreset(rows) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | presets = append(presets, p) |
| 58 | } |
| 59 | return presets, rows.Err() |
| 60 | } |
| 61 | |
| 62 | func (r *PresetRepository) GetDefault() (*models.Preset, error) { |
| 63 | row := r.db.QueryRow( |
| 64 | `SELECT id, name, description, is_default, format_mode, format, quality, custom_format, extract_audio, audio_format, embed_subs, sub_langs, embed_thumbnail, embed_metadata, write_info_json, output_template, custom_flags, created_at |
| 65 | FROM presets WHERE is_default = 1 LIMIT 1`, |
| 66 | ) |
| 67 | return scanPreset(row) |
| 68 | } |
| 69 | |
| 70 | func (r *PresetRepository) Update(p *models.Preset) error { |
| 71 | _, err := r.db.Exec( |
| 72 | `UPDATE presets SET name=?, description=?, is_default=?, format_mode=?, format=?, quality=?, custom_format=?, extract_audio=?, audio_format=?, embed_subs=?, sub_langs=?, embed_thumbnail=?, embed_metadata=?, write_info_json=?, output_template=?, custom_flags=? |
| 73 | WHERE id=?`, |
| 74 | p.Name, p.Description, boolToInt(p.IsDefault), p.FormatMode, p.Format, p.Quality, p.CustomFormat, |
| 75 | boolToInt(p.ExtractAudio), p.AudioFormat, boolToInt(p.EmbedSubs), p.SubLangs, |
| 76 | boolToInt(p.EmbedThumbnail), boolToInt(p.EmbedMetadata), boolToInt(p.WriteInfoJSON), |
| 77 | p.OutputTemplate, p.CustomFlags, p.ID, |
| 78 | ) |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | func (r *PresetRepository) Delete(id int64) error { |
| 83 | _, err := r.db.Exec(`DELETE FROM presets WHERE id = ?`, id) |
| 84 | return err |
| 85 | } |
| 86 | |
| 87 | func (r *PresetRepository) ClearDefault() error { |
| 88 | _, err := r.db.Exec(`UPDATE presets SET is_default = 0`) |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | func scanPreset(row interface{ Scan(...interface{}) error }) (*models.Preset, error) { |
| 93 | var p models.Preset |
| 94 | var isDefault, extractAudio, embedSubs, embedThumbnail, embedMetadata, writeInfoJSON int |
| 95 | var description, formatMode, format, quality, customFormat, audioFormat, subLangs, outputTemplate, customFlags sql.NullString |
| 96 | err := row.Scan( |
| 97 | &p.ID, &p.Name, &description, &isDefault, &formatMode, &format, &quality, &customFormat, |
| 98 | &extractAudio, &audioFormat, &embedSubs, &subLangs, |
| 99 | &embedThumbnail, &embedMetadata, &writeInfoJSON, |
| 100 | &outputTemplate, &customFlags, &p.CreatedAt, |
| 101 | ) |
| 102 | if err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | p.Description = description.String |
| 106 | p.FormatMode = formatMode.String |
| 107 | p.Format = format.String |
| 108 | p.Quality = quality.String |
| 109 | p.CustomFormat = customFormat.String |
| 110 | p.AudioFormat = audioFormat.String |
| 111 | p.SubLangs = subLangs.String |
| 112 | p.OutputTemplate = outputTemplate.String |
| 113 | p.CustomFlags = customFlags.String |
| 114 | p.IsDefault = isDefault == 1 |
| 115 | p.ExtractAudio = extractAudio == 1 |
| 116 | p.EmbedSubs = embedSubs == 1 |
| 117 | p.EmbedThumbnail = embedThumbnail == 1 |
| 118 | p.EmbedMetadata = embedMetadata == 1 |
| 119 | p.WriteInfoJSON = writeInfoJSON == 1 |
| 120 | return &p, nil |
| 121 | } |
| 122 | |
| 123 | func boolToInt(b bool) int { |
| 124 | if b { |
| 125 | return 1 |
| 126 | } |
| 127 | return 0 |
| 128 | } |
| 129 |