Skip to content

Commit

Permalink
implement new response on some playback routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibbp committed Aug 6, 2024
1 parent fab5725 commit 4614c88
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion internal/playback/playback.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type GetPlayback struct {
Vod *ent.Vod `json:"vod"`
}

var ErrorPlaybackNotFound = fmt.Errorf("playback not found")

func (s *Service) UpdateProgress(c *auth.CustomContext, vID uuid.UUID, time int) error {
uID := c.User.ID

Expand Down Expand Up @@ -64,7 +66,7 @@ func (s *Service) GetProgress(c *auth.CustomContext, vID uuid.UUID) (*ent.Playba
playbackEntry, err := s.Store.Client.Playback.Query().Where(playback.UserID(uID)).Where(playback.VodID(vID)).Only(c.Request().Context())
if err != nil {
if _, ok := err.(*ent.NotFoundError); ok {
return nil, fmt.Errorf("playback not found")
return nil, ErrorPlaybackNotFound
}
return nil, fmt.Errorf("error getting playback: %v", err)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/transport/http/playback.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"errors"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -86,9 +87,13 @@ func (h *Handler) GetProgress(c echo.Context) error {
}
playbackEntry, err := h.Service.PlaybackService.GetProgress(cc, vID)
if err != nil {
if errors.Is(err, playback.ErrorPlaybackNotFound) {
return ErrorResponse(c, http.StatusOK, "playback not found")
}
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, playbackEntry)

return SuccessResponse(c, playbackEntry, fmt.Sprintf("playback data for %s", vID))
}

// GetAllProgress godoc
Expand Down
8 changes: 8 additions & 0 deletions internal/transport/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ func SuccessResponse(c echo.Context, data interface{}, message string) error {
Message: message,
})
}

func ErrorResponse(c echo.Context, statusCode int, message string) error {
return c.JSON(statusCode, Response{
Success: false,
Data: nil,
Message: message,
})
}

0 comments on commit 4614c88

Please sign in to comment.