feat(repo): refactor MoveToArchive to use structured data for improved readability and maintainability
This commit is contained in:
parent
a9de32bed1
commit
e34d2bf8f1
@ -172,8 +172,7 @@ func (r *pgxEpisodeRepo) MoveToArchive(ctx context.Context, ids []int64) (episod
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
type rowData struct {
|
||||
id int64
|
||||
epNum int
|
||||
epTitle string
|
||||
@ -182,12 +181,24 @@ func (r *pgxEpisodeRepo) MoveToArchive(ctx context.Context, ids []int64) (episod
|
||||
playback string
|
||||
currentEp bool
|
||||
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
|
||||
inserted bool
|
||||
)
|
||||
if err := rows.Scan(&id, &epNum, &epTitle, &seasonName, &startTime, &playback, ¤tEp, &dateCreated); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
// First try to insert with the same id; on conflict, insert with a new id.
|
||||
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)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
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) {
|
||||
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
|
||||
}
|
||||
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)
|
||||
RETURNING id
|
||||
`, epNum, epTitle, seasonName, startTime, playback, currentEp, dateCreated).Scan(&archivedID); err != nil {
|
||||
log.Printf("archive fallback insert failed for original id=%d: %v", id, err)
|
||||
`, 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", item.id, err)
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
|
||||
// Delete from current using the original id.
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM current WHERE id = $1`, id); err != nil {
|
||||
log.Printf("archive delete failed for id=%d: %v", id, err)
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM current WHERE id = $1`, item.id); err != nil {
|
||||
log.Printf("archive delete failed for id=%d: %v", item.id, err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.MovedIDs = append(res.MovedIDs, id)
|
||||
res.DeletedIDs = append(res.DeletedIDs, id)
|
||||
delete(requested, id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return res, err
|
||||
res.MovedIDs = append(res.MovedIDs, item.id)
|
||||
res.DeletedIDs = append(res.DeletedIDs, item.id)
|
||||
delete(requested, item.id)
|
||||
}
|
||||
|
||||
// Any ids not found are treated as skipped.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user