Skip to content

Commit

Permalink
Added is_featured to /mock/clips
Browse files Browse the repository at this point in the history
  • Loading branch information
Xemdo committed Nov 3, 2023
1 parent 1b3a1c4 commit e85a245
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/database/videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Clip struct {
CreatedAt string `db:"created_at" json:"created_at"`
Duration float64 `db:"duration" json:"duration"`
VodOffset int `db:"vod_offset" json:"vod_offset"`
IsFeatured bool `json:"is_featured"`
// calculated fields
URL string `json:"url"`
ThumbnailURL string `json:"thumbnail_url"`
Expand Down
21 changes: 21 additions & 0 deletions internal/mock_api/endpoints/clips/clips.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"

"github.com/twitchdev/twitch-cli/internal/database"
Expand Down Expand Up @@ -70,6 +72,8 @@ func getClips(w http.ResponseWriter, r *http.Request) {
startedAt := r.URL.Query().Get("started_at")
endedAt := r.URL.Query().Get("ended_at")

isFeatured := r.URL.Query().Get("is_featured")

if broadcasterID == "" && gameID == "" && id == "" {
mock_errors.WriteBadRequest(w, "one of broadcaster_id, game_id, or id is required")
return
Expand All @@ -80,6 +84,11 @@ func getClips(w http.ResponseWriter, r *http.Request) {
return
}

if isFeatured != "" && (!strings.EqualFold(isFeatured, "false") && !strings.EqualFold(isFeatured, "true")) {
mock_errors.WriteBadRequest(w, "is_featured must be true or false")
return
}

if startedAt != "" && endedAt == "" {
sa, _ := time.Parse(time.RFC3339, startedAt)
endedAt = sa.Add(7 * 24 * time.Hour).Format(time.RFC3339)
Expand All @@ -92,6 +101,18 @@ func getClips(w http.ResponseWriter, r *http.Request) {
}

clips := dbr.Data.([]database.Clip)

if isFeatured != "" {
newClips := []database.Clip{}
for _, clip := range clips {
featured, _ := strconv.ParseBool(isFeatured)
if clip.IsFeatured == featured {
newClips = append(newClips, clip)
}
}
clips = newClips
}

apiResponse := models.APIResponse{
Data: clips,
}
Expand Down

0 comments on commit e85a245

Please sign in to comment.