feat(repo): refactor MoveToArchive to use structured data for improved readability and maintainability

This commit is contained in:
Nik Afiq 2025-12-11 21:53:09 +09:00
parent a9de32bed1
commit e34d2bf8f1

View File

@ -172,8 +172,7 @@ func (r *pgxEpisodeRepo) MoveToArchive(ctx context.Context, ids []int64) (episod
} }
defer rows.Close() defer rows.Close()
for rows.Next() { type rowData struct {
var (
id int64 id int64
epNum int epNum int
epTitle string epTitle string
@ -182,12 +181,24 @@ func (r *pgxEpisodeRepo) MoveToArchive(ctx context.Context, ids []int64) (episod
playback string playback string
currentEp bool currentEp bool
dateCreated time.Time dateCreated time.Time
}
var items []rowData
for rows.Next() {
var item rowData
if err := rows.Scan(&item.id, &item.epNum, &item.epTitle, &item.seasonName, &item.startTime, &item.playback, &item.currentEp, &item.dateCreated); err != nil {
return res, err
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return res, err
}
for _, item := range items {
var (
archivedID int64 archivedID int64
inserted bool inserted bool
) )
if err := rows.Scan(&id, &epNum, &epTitle, &seasonName, &startTime, &playback, &currentEp, &dateCreated); err != nil {
return res, err
}
// First try to insert with the same id; on conflict, insert with a new id. // First try to insert with the same id; on conflict, insert with a new id.
err = tx.QueryRow(ctx, ` err = tx.QueryRow(ctx, `
@ -197,9 +208,9 @@ func (r *pgxEpisodeRepo) MoveToArchive(ctx context.Context, ids []int64) (episod
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (id) DO NOTHING ON CONFLICT (id) DO NOTHING
RETURNING id RETURNING id
`, id, epNum, epTitle, seasonName, startTime, playback, currentEp, dateCreated).Scan(&archivedID) `, item.id, item.epNum, item.epTitle, item.seasonName, item.startTime, item.playback, item.currentEp, item.dateCreated).Scan(&archivedID)
if err != nil && !errors.Is(err, pgx.ErrNoRows) { if err != nil && !errors.Is(err, pgx.ErrNoRows) {
log.Printf("archive insert failed for id=%d: %v", id, err) log.Printf("archive insert failed for id=%d: %v", item.id, err)
return res, err return res, err
} }
if err == nil { if err == nil {
@ -213,24 +224,21 @@ func (r *pgxEpisodeRepo) MoveToArchive(ctx context.Context, ids []int64) (episod
) )
VALUES (nextval(pg_get_serial_sequence('current','id')), $1, $2, $3, $4, $5, $6, $7) VALUES (nextval(pg_get_serial_sequence('current','id')), $1, $2, $3, $4, $5, $6, $7)
RETURNING id RETURNING id
`, epNum, epTitle, seasonName, startTime, playback, currentEp, dateCreated).Scan(&archivedID); err != nil { `, item.epNum, item.epTitle, item.seasonName, item.startTime, item.playback, item.currentEp, item.dateCreated).Scan(&archivedID); err != nil {
log.Printf("archive fallback insert failed for original id=%d: %v", id, err) log.Printf("archive fallback insert failed for original id=%d: %v", item.id, err)
return res, err return res, err
} }
} }
// Delete from current using the original id. // Delete from current using the original id.
if _, err := tx.Exec(ctx, `DELETE FROM current WHERE id = $1`, id); err != nil { if _, err := tx.Exec(ctx, `DELETE FROM current WHERE id = $1`, item.id); err != nil {
log.Printf("archive delete failed for id=%d: %v", id, err) log.Printf("archive delete failed for id=%d: %v", item.id, err)
return res, err return res, err
} }
res.MovedIDs = append(res.MovedIDs, id) res.MovedIDs = append(res.MovedIDs, item.id)
res.DeletedIDs = append(res.DeletedIDs, id) res.DeletedIDs = append(res.DeletedIDs, item.id)
delete(requested, id) delete(requested, item.id)
}
if err := rows.Err(); err != nil {
return res, err
} }
// Any ids not found are treated as skipped. // Any ids not found are treated as skipped.