Skip to content

Commit

Permalink
Added igdb_id to /games and /games/top; Updated /clips fields; Added …
Browse files Browse the repository at this point in the history
…updated_at to /entitlements/drops
  • Loading branch information
Xemdo committed Jan 6, 2023
1 parent ba22e4c commit 975d223
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 24 deletions.
5 changes: 3 additions & 2 deletions internal/database/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Category struct {
Name string `db:"category_name" json:"name"`
BoxartURL string `json:"box_art_url"`
ViewerCount int `db:"vc" json:"-"`
IGDB string `db:"igdb_id" json:"igdb_id"`
}

func (q *Query) GetCategories(cat Category) (*DBResponse, error) {
Expand Down Expand Up @@ -45,7 +46,7 @@ func (q *Query) GetCategories(cat Category) (*DBResponse, error) {
}

func (q *Query) InsertCategory(category Category, upsert bool) error {
_, err := q.DB.NamedExec(`insert into categories values(:id, :category_name)`, category)
_, err := q.DB.NamedExec(`insert into categories values(:id, :category_name, :igdb_id)`, category)
return err
}

Expand Down Expand Up @@ -73,7 +74,7 @@ func (q *Query) SearchCategories(query string) (*DBResponse, error) {
func (q *Query) GetTopGames() (*DBResponse, error) {
r := []Category{}

err := q.DB.Select(&r, "select c.id, c.category_name, IFNULL(SUM(s.viewer_count),0) as vc from categories c left join users u on c.id = u.category_id left join streams s on s.broadcaster_id = u.id group by c.id, c.category_name order by vc desc")
err := q.DB.Select(&r, "select c.id, c.category_name, c.igdb_id, IFNULL(SUM(s.viewer_count),0) as vc from categories c left join users u on c.id = u.category_id left join streams s on s.broadcaster_id = u.id group by c.id, c.category_name order by vc desc"+q.SQL)
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions internal/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const TEST_USER_LOGIN = "testing_user1"
const TEST_USER_ID_2 = "2"
const TEST_USER_LOGIN_2 = "second_user"
const CATEGORY_ID = "1"
const IGDB_ID = "123"

var db CLIDatabase
var q *Query
Expand All @@ -49,7 +50,7 @@ func TestMain(m *testing.M) {
}
q = db.NewQuery(nil, 100)

err = q.InsertCategory(Category{Name: "test", ID: CATEGORY_ID, ViewerCount: 0, BoxartURL: ""}, false)
err = q.InsertCategory(Category{Name: "test", ID: CATEGORY_ID, IGDB: IGDB_ID, ViewerCount: 0, BoxartURL: ""}, false)
log.Print(err)

err = q.InsertUser(User{
Expand Down Expand Up @@ -170,7 +171,7 @@ func TestAPI(t *testing.T) {
func TestCategories(t *testing.T) {
a := test_setup.SetupTestEnv(t)

c := Category{Name: "test", ID: CATEGORY_ID}
c := Category{Name: "test", ID: CATEGORY_ID, IGDB: IGDB_ID}
err := q.InsertCategory(c, false)
a.NotNil(err)

Expand All @@ -180,6 +181,7 @@ func TestCategories(t *testing.T) {
categories := dbr.Data.([]Category)
a.Len(categories, 1)
a.Equal(c.ID, categories[0].ID)
a.Equal(c.IGDB, categories[0].IGDB)

// search
dbr, err = q.SearchCategories("es")
Expand Down Expand Up @@ -771,6 +773,7 @@ func TestVideos(t *testing.T) {
ViewCount: 100,
Duration: 1234.5,
CreatedAt: util.GetTimestamp().Format(time.RFC3339),
VodOffset: int(util.RandomInt(3000)),
}

err = q.InsertClip(c)
Expand Down
13 changes: 7 additions & 6 deletions internal/database/drops.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package database
import "log"

type DropsEntitlement struct {
ID string `db:"id" json:"id" dbs:"de.id"`
UserID string `db:"user_id" json:"user_id"`
BenefitID string `db:"benefit_id" json:"benefit_id"`
GameID string `db:"game_id" json:"game_id"`
Timestamp string `db:"timestamp" json:"timestamp"`
Status string `db:"status" json:"fulfillment_status"`
ID string `db:"id" json:"id" dbs:"de.id"`
UserID string `db:"user_id" json:"user_id"`
BenefitID string `db:"benefit_id" json:"benefit_id"`
GameID string `db:"game_id" json:"game_id"`
Timestamp string `db:"timestamp" json:"timestamp"`
Status string `db:"status" json:"fulfillment_status"`
LastUpdated string `db:"last_updated" json:"last_updated"`
}

func (q *Query) GetDropsEntitlements(de DropsEntitlement) (*DBResponse, error) {
Expand Down
8 changes: 6 additions & 2 deletions internal/database/init.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions internal/database/videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ type Clip struct {
BroadcasterID string `db:"broadcaster_id" json:"broadcaster_id"`
BroadcasterName string `db:"broadcaster_name" json:"broadcaster_name" dbi:"false"`
CreatorID string `db:"creator_id" json:"creator_id"`
CreatorName string `db:"creator_name" json:"creator_login" dbi:"false"`
CreatorName string `db:"creator_name" json:"creator_name" dbi:"false"`
VideoID string `db:"video_id" json:"video_id"`
GameID string `db:"game_id" json:"game_id"`
Language string `db:"language" dbi:"false" json:"language"`
Title string `db:"title" json:"title"`
ViewCount int `db:"view_count" json:"view_count"`
CreatedAt string `db:"created_at" json:"created_at"`
Duration float64 `db:"duration" json:"duration"`
VodOffset int `db:"vod_offset" json:"vod_offset"`
// calculated fields
URL string `json:"url"`
ThumbnailURL string `json:"thumbnail_url"`
Expand Down Expand Up @@ -146,7 +147,7 @@ func (q *Query) InsertClip(c Clip) error {

func (q *Query) GetClips(c Clip, startDate string, endDate string) (*DBResponse, error) {
var r []Clip
sql := generateSQL("select c.id, c.broadcaster_id, c.creator_id, c.video_id, c.game_id, c.title, c.view_count, c.duration, datetime(c.created_at) as created_at, u1.display_name as broadcaster_name, u1.stream_language as language, u2.display_name as creator_name from clips c join users u1 on c.broadcaster_id = u1.id join users u2 on c.creator_id = u2.id ", c, SEP_AND)
sql := generateSQL("select c.id, c.broadcaster_id, c.creator_id, c.video_id, c.game_id, c.title, c.view_count, c.duration, c.vod_offset, datetime(c.created_at) as created_at, u1.display_name as broadcaster_name, u1.stream_language as language, u2.display_name as creator_name from clips c join users u1 on c.broadcaster_id = u1.id join users u2 on c.creator_id = u2.id ", c, SEP_AND)
if startDate != "" {
c.StartedAt = startDate
c.EndedAt = endDate
Expand Down
14 changes: 12 additions & 2 deletions internal/mock_api/endpoints/categories/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ func getGames(w http.ResponseWriter, r *http.Request) {
games := []database.Category{}
ids := r.URL.Query()["id"]
names := r.URL.Query()["name"]
igdb_ids := r.URL.Query()["igdb_id"]

if len(ids) == 0 && len(names) == 0 {
mock_errors.WriteBadRequest(w, "at least one name or id is required")
if len(ids) == 0 && len(names) == 0 && len(igdb_ids) == 0 {
mock_errors.WriteBadRequest(w, "at least one name, id, or igdb_id is required")
return
}

Expand All @@ -85,6 +86,15 @@ func getGames(w http.ResponseWriter, r *http.Request) {
game := dbr.Data.([]database.Category)
games = append(games, game...)
}
for _, igdb_id := range igdb_ids {
dbr, err := db.NewQuery(r, 100).GetCategories(database.Category{IGDB: igdb_id})
if err != nil {
mock_errors.WriteServerError(w, "error getting category")
return
}
game := dbr.Data.([]database.Category)
games = append(games, game...)
}
for i, g := range games {
games[i].BoxartURL = fmt.Sprintf("https://static-cdn.jtvnw.net/ttv-boxart/%v-{width}x{height}.jpg", url.PathEscape(g.Name))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/endpoints/categories/top_games.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (e TopGames) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func getTopGames(w http.ResponseWriter, r *http.Request) {
dbr, err := db.NewQuery(r, 1000).GetTopGames()
dbr, err := db.NewQuery(r, 100).GetTopGames()
if err != nil {
mock_errors.WriteServerError(w, "error fetching entitlements")
return
Expand Down
1 change: 1 addition & 0 deletions internal/mock_api/endpoints/clips/clips.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func postClips(w http.ResponseWriter, r *http.Request) {
VideoID: "",
Duration: 33.3,
CreatedAt: util.GetTimestamp().Format(time.RFC3339),
VodOffset: int(util.RandomInt(3000)),
}

err = db.NewQuery(r, 100).InsertClip(clip)
Expand Down
11 changes: 10 additions & 1 deletion internal/mock_api/endpoints/drops/entitlements.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package drops
import (
"encoding/json"
"net/http"
"time"

"github.com/twitchdev/twitch-cli/internal/database"
"github.com/twitchdev/twitch-cli/internal/mock_api/authentication"
"github.com/twitchdev/twitch-cli/internal/mock_api/mock_errors"
"github.com/twitchdev/twitch-cli/internal/models"
"github.com/twitchdev/twitch-cli/internal/util"
)

var dropsEntitlementsMethodsSupported = map[string]bool{
Expand Down Expand Up @@ -140,7 +142,14 @@ func patchEntitlements(w http.ResponseWriter, r *http.Request) {
continue
}

err = db.NewQuery(nil, 100).UpdateDropsEntitlement(database.DropsEntitlement{ID: e, UserID: entitlement[0].UserID, Status: body.FulfillmentStatus})
err = db.NewQuery(nil, 100).UpdateDropsEntitlement(
database.DropsEntitlement{
ID: e,
UserID: entitlement[0].UserID,
Status: body.FulfillmentStatus,
LastUpdated: util.GetTimestamp().Format(time.RFC3339Nano),
},
)
if err != nil {
fail.IDs = append(fail.IDs, e)
continue
Expand Down
15 changes: 9 additions & 6 deletions internal/mock_api/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func generateUsers(ctx context.Context, count int) error {
category := database.Category{
ID: fmt.Sprintf("%v", util.RandomInt(10*100*100)),
Name: c,
IGDB: fmt.Sprintf("%v", util.RandomInt(10*100*100)),
}

err := db.NewQuery(nil, 100).InsertCategory(category, false)
Expand Down Expand Up @@ -181,12 +182,13 @@ func generateUsers(ctx context.Context, count int) error {
}

entitlement := database.DropsEntitlement{
ID: util.RandomGUID(),
BenefitID: benefitID,
GameID: dropsGameID,
UserID: broadcaster.ID,
Timestamp: util.GetTimestamp().Format(time.RFC3339Nano),
Status: "CLAIMED",
ID: util.RandomGUID(),
BenefitID: benefitID,
GameID: dropsGameID,
UserID: broadcaster.ID,
Timestamp: util.GetTimestamp().Format(time.RFC3339Nano),
Status: "CLAIMED",
LastUpdated: util.GetTimestamp().Format(time.RFC3339Nano),
}
err = db.NewQuery(nil, 1000).InsertDropsEntitlement(entitlement)
if err != nil {
Expand Down Expand Up @@ -500,6 +502,7 @@ func generateUsers(ctx context.Context, count int) error {
ViewCount: 0,
CreatedAt: util.GetTimestamp().Format(time.RFC3339),
Duration: 30.1,
VodOffset: int(util.RandomInt(3000)),
}
err = db.NewQuery(nil, 100).InsertClip(c)
if err != nil {
Expand Down

0 comments on commit 975d223

Please sign in to comment.