Skip to content

Commit

Permalink
updating branch for 6/9
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet committed Jun 10, 2021
1 parent 5e032a2 commit 5083c74
Show file tree
Hide file tree
Showing 65 changed files with 3,240 additions and 543 deletions.
6 changes: 6 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
var err error
var cursor string

data.Data = make([]interface{}, 0)
client, err := GetClientInformation()

if viper.GetString("BASE_URL") != "" {
Expand Down Expand Up @@ -100,6 +101,7 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
data = apiResponse
break
}

d := data.Data.([]interface{})
data.Data = append(d, apiResponse.Data)

Expand All @@ -120,6 +122,10 @@ func NewRequest(method string, path string, queryParameters []string, body []byt

}

if data.Data == nil {
log.Println("here")
data.Data = make([]interface{}, 0)
}
// handle json marshalling better; returns empty slice vs. null
if len(data.Data.([]interface{})) == 0 && data.Error == "" {
data.Data = make([]interface{}, 0)
Expand Down
14 changes: 9 additions & 5 deletions internal/database/_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ create table bans (
broadcaster_id text not null,
user_id text not null,
created_at text not null,
expires_at text,
primary key (broadcaster_id, user_id),
foreign key (broadcaster_id) references users(id),
foreign key (user_id) references users(id)
Expand Down Expand Up @@ -129,10 +130,10 @@ create table tags(
tag_name text not null
);
create table stream_tags(
stream_id text not null,
user_id text not null,
tag_id text not null,
primary key(stream_id, tag_id),
foreign key(stream_id) references streams(id),
primary key(user_id, tag_id),
foreign key(user_id) references users(id),
foreign key(tag_id) references tags(id)
);
create table teams(
Expand All @@ -159,9 +160,12 @@ create table videos(
view_count int not null default 0,
duration text not null,
video_language text not null default 'en',
category_id text,
type text default 'archive',
foreign key (stream_id) references streams(id),
foreign key (broadcaster_id) references users(id)
);
foreign key (broadcaster_id) references users(id),
foreign key (category_id) references categories(id)
};
create table stream_markers(
id text not null primary key,
video_id text not null,
Expand Down
2 changes: 1 addition & 1 deletion internal/database/_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (q *Query) GetPrinciple(p Principle) (*DBResponse, error) {
}
}

dbr := DBResposne{
dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
Expand Down
2 changes: 1 addition & 1 deletion internal/database/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

type DBResposne struct {
type DBResponse struct {
Cursor string `json:"cursor"`
Total int `json:"total"`
Limit int `json:"-"`
Expand Down
4 changes: 2 additions & 2 deletions internal/database/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (q *Query) CreateAuthorization(a Authorization) (Authorization, error) {
}
}

func (q *Query) GetAuthenticationClient(ac AuthenticationClient) (*DBResposne, error) {
func (q *Query) GetAuthenticationClient(ac AuthenticationClient) (*DBResponse, error) {
var r []AuthenticationClient
rows, err := q.DB.NamedQuery(generateSQL("select * from clients", ac, SEP_AND)+q.SQL, ac)
if err != nil {
Expand All @@ -105,7 +105,7 @@ func (q *Query) GetAuthenticationClient(ac AuthenticationClient) (*DBResposne, e
r = append(r, ac)
}

dbr := DBResposne{
dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
Expand Down
59 changes: 50 additions & 9 deletions internal/database/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
// SPDX-License-Identifier: Apache-2.0
package database

import (
"fmt"
)

type Category struct {
ID string `db:"id" json:"id"`
Name string `db:"category_name" json:"name"`
ID string `db:"id" json:"id"`
Name string `db:"category_name" json:"name"`
BoxartURL string `json:"boxart_url"`
ViewerCount int `db:"vc" json:"-"`
}

func (q *Query) GetCategories(cat Category) (*DBResposne, error) {
func (q *Query) GetCategories(cat Category) (*DBResponse, error) {
var r []Category
rows, err := q.DB.NamedQuery(generateSQL("select * from categories", cat, SEP_AND)+q.SQL, cat)
if err != nil {
Expand All @@ -23,7 +29,7 @@ func (q *Query) GetCategories(cat Category) (*DBResposne, error) {
r = append(r, cat)
}

dbr := DBResposne{
dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
Expand All @@ -43,11 +49,46 @@ func (q *Query) InsertCategory(category Category, upsert bool) error {
return err
}

func (q *Query) SearchCategories(query string) ([]Category, error) {
categories := []Category{}
err := q.DB.Select(&categories, `select * from categories where category_name like '%$1%'`, query)
func (q *Query) SearchCategories(query string) (*DBResponse, error) {
r := []Category{}
err := q.DB.Select(&r, `select * from categories where lower(category_name) like lower($1) `+q.SQL, fmt.Sprintf("%%%v%%", query))
if err != nil {
return categories, err
return nil, err
}
return categories, nil
dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
}

if len(r) != q.Limit {
q.PaginationCursor = ""
}

dbr.Cursor = q.PaginationCursor

return &dbr, err
}

func (q *Query) GetTopGames() (*DBResponse, error) {
r := []Category{}

err := q.DB.Select(&r, "select c.id, c.category_name, SUM(s.viewer_count) as vc from users u join streams s on s.broadcaster_id = u.id left join categories c on u.category_id = c.id group by c.id, c.category_name order by vc desc")
if err != nil {
return nil, err
}

dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
}

if len(r) != q.Limit {
q.PaginationCursor = ""
}

dbr.Cursor = q.PaginationCursor

return &dbr, err
}
4 changes: 2 additions & 2 deletions internal/database/channel_points_redemptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type ChannelPointsRedemptionRewardInfo struct {
Cost int `dbi:"false" db:"cost" json:"cost"`
}

func (q *Query) GetChannelPointsRedemption(cpr ChannelPointsRedemption, sort string) (*DBResposne, error) {
func (q *Query) GetChannelPointsRedemption(cpr ChannelPointsRedemption, sort string) (*DBResponse, error) {
var r []ChannelPointsRedemption
orderBy := ""
if sort == "" || sort == "OLDEST" {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (q *Query) GetChannelPointsRedemption(cpr ChannelPointsRedemption, sort str
r = append(r, red)
}

dbr := DBResposne{
dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
Expand Down
4 changes: 2 additions & 2 deletions internal/database/channel_points_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type DefaultImage struct {
URL4x string `json:"url_4x"`
}

func (q *Query) GetChannelPointsReward(cpr ChannelPointsReward) (*DBResposne, error) {
func (q *Query) GetChannelPointsReward(cpr ChannelPointsReward) (*DBResponse, error) {
var r []ChannelPointsReward
sql := generateSQL("select cpr.*, u1.user_login as broadcaster_login, u1.display_name as broadcaster_name from channel_points_rewards cpr join users u1 on cpr.broadcaster_id = u1.id", cpr, SEP_AND)
rows, err := q.DB.NamedQuery(sql+q.SQL, cpr)
Expand Down Expand Up @@ -81,7 +81,7 @@ func (q *Query) GetChannelPointsReward(cpr ChannelPointsReward) (*DBResposne, er
r = append(r, cpr)
}

dbr := DBResposne{
dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
Expand Down
40 changes: 25 additions & 15 deletions internal/database/drops.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@ package database
import "log"

type DropsEntitlement struct {
ID string `db:"id" json:"id" dbs:"c.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"`
}

func (q *Query) GetDropsEntitlementById(id string) (*DBResposne, error) {
func (q *Query) GetDropsEntitlements(de DropsEntitlement) (*DBResponse, error) {
var r []DropsEntitlement

err := q.DB.Get(&r, "select * from drops_entitlements where id = $1", id)
stmt := generateSQL("select * from drops_entitlements", de, SEP_AND)
stmt += " order by timestamp desc " + q.SQL
rows, err := q.DB.NamedQuery(stmt, de)
if err != nil {
log.Print(err)
return nil, err
}
log.Printf("%#v", r)
for rows.Next() {
var de DropsEntitlement
err := rows.StructScan(&de)
if err != nil {
log.Print(err)
return nil, err
}

r = append(r, de)
}

dbr := DBResposne{
dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
Expand All @@ -30,14 +46,8 @@ func (q *Query) GetDropsEntitlementById(id string) (*DBResposne, error) {

return &dbr, err
}

func (q *Query) InsertDropsEntitlement(d DropsEntitlement, upsert bool) error {
tx := q.DB.MustBegin()
tx.NamedExec(`insert into drops_entitlements values(:id, :values...)`, d)
err := tx.Commit()
if err != nil {
return err
}

return nil
func (q *Query) InsertDropsEntitlement(d DropsEntitlement) error {
stmt := generateInsertSQL("drops_entitlements", "id", d, false)
_, err := q.DB.NamedExec(stmt, d)
return err
}
4 changes: 2 additions & 2 deletions internal/database/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "log"
type Extension struct {
}

func (q *Query) GetExtensionById(id string) (*DBResposne, error) {
func (q *Query) GetExtensionById(id string) (*DBResponse, error) {
var r []Extension

err := q.DB.Get(&r, "select * from principle where id = $1", id)
Expand All @@ -16,7 +16,7 @@ func (q *Query) GetExtensionById(id string) (*DBResposne, error) {
}
log.Printf("%#v", r)

dbr := DBResposne{
dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
Expand Down
Loading

0 comments on commit 5083c74

Please sign in to comment.