Added delete function

This commit is contained in:
Nik Afiq 2025-11-22 19:37:06 +09:00
parent a24b72740b
commit 70c76c62c3
3 changed files with 42 additions and 0 deletions

View File

@ -1,7 +1,9 @@
package httpapi
import (
"errors"
"net/http"
"strconv"
"time"
"watch-party-backend/internal/repo"
@ -118,5 +120,26 @@ func NewRouter(svc service.EpisodeService) *gin.Engine {
}
c.JSON(http.StatusOK, items)
})
// DELETE /v1/shows/:id — delete a row from "current" by id
v1.DELETE("/shows/:id", func(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
err = svc.Delete(c.Request.Context(), id)
if err != nil {
if errors.Is(err, repo.ErrNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "id not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "delete failed"})
return
}
c.Status(http.StatusNoContent)
})
return r
}

View File

@ -32,6 +32,7 @@ type EpisodeRepository interface {
SetCurrent(ctx context.Context, id int64, startHHMMSS string) error
MoveToArchive(ctx context.Context, ids []int64) (MoveResult, error)
ListAll(ctx context.Context) ([]Episode, error)
Delete(ctx context.Context, id int64) error
}
type pgxEpisodeRepo struct {
@ -199,3 +200,14 @@ func (r *pgxEpisodeRepo) MoveToArchive(ctx context.Context, ids []int64) (MoveRe
}
return res, nil
}
func (r *pgxEpisodeRepo) Delete(ctx context.Context, id int64) error {
cmdTag, err := r.pool.Exec(ctx, `DELETE FROM current WHERE id = $1`, id)
if err != nil {
return err
}
if cmdTag.RowsAffected() == 0 {
return ErrNotFound
}
return nil
}

View File

@ -19,6 +19,7 @@ type EpisodeService interface {
SetCurrent(ctx context.Context, id int64, start string) (repo.Episode, error)
MoveToArchive(ctx context.Context, ids []int64) (repo.MoveResult, error)
ListAll(ctx context.Context) ([]repo.Episode, error)
Delete(ctx context.Context, id int64) error
}
type episodeService struct {
@ -72,3 +73,9 @@ func (s *episodeService) MoveToArchive(ctx context.Context, ids []int64) (repo.M
defer cancel()
return s.repo.MoveToArchive(c, uniq)
}
func (s *episodeService) Delete(ctx context.Context, id int64) error {
c, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return s.repo.Delete(c, id)
}