Skip to content

Commit

Permalink
updates to the units and db structure
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet committed Jun 2, 2021
1 parent 5529f3d commit 95105e9
Show file tree
Hide file tree
Showing 12 changed files with 498 additions and 0 deletions.
90 changes: 90 additions & 0 deletions internal/database/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package database

import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/jmoiron/sqlx"
)

type Query struct {
Limit int
Cursor string
InternalPagination
DB *sqlx.DB
}

// NewQuery handles the logic for generating the pagination token to pass alongside the DB queries for easier access
func (c CLIDatabase) NewQuery(r *http.Request, max_limit int) *Query {
p := Query{DB: c.DB}
if r == nil {
return &p
}

ic := InternalCursor{}

query := r.URL.Query()
a := query.Get("after")
f := query.Get("first")
b := query.Get("before")

isBefore := false
if b != "" {
isBefore = true
}

if len(a) > 0 {
p.Cursor = a
}

first, err := strconv.Atoi(f)
if err != nil {
return &p
}
if first > max_limit || first <= 0 {
return &p
}
if first == 0 {
first = 20
}
p.Limit = int(first)

if a != "" {
b, err := base64.RawStdEncoding.DecodeString(a)
if err != nil {
return &p
}
err = json.Unmarshal(b, &ic)
if err != nil {
return &p
}

if isBefore {
ic.Offset -= first
} else {
ic.Offset += first
}
}

ic.Limit = first

if ic.Offset < 0 {
return &p
}

body, _ := json.Marshal(ic)

ip := InternalPagination{
InternalCursor: ic,
PaginationCursor: base64.RawURLEncoding.EncodeToString(body),
SQL: fmt.Sprintf(" LIMIT %v OFFSET %v", ic.Limit, ic.Offset),
}

p.InternalPagination = ip
return &p
}
42 changes: 42 additions & 0 deletions internal/mock_units/categories/categories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package categories

import (
"encoding/json"
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
)

type Endpoint struct{}

var db database.CLIDatabase

func (e Endpoint) Path() string { return "/categories" }

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
db = r.Context().Value("db").(database.CLIDatabase)

switch r.Method {
case http.MethodGet:
getCategories(w, r)
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func getCategories(w http.ResponseWriter, r *http.Request) {
c, err := db.NewQuery(r, 100).GetCategories(database.Category{})
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
j, err := json.Marshal(c)
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
w.Write(j)
}
27 changes: 27 additions & 0 deletions internal/mock_units/channel_points/channel_points.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package channel_points

import (
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
)

type Endpoint struct{}

var db database.CLIDatabase

func (e Endpoint) Path() string { return "/categories" }

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
db = r.Context().Value("db").(database.CLIDatabase)

switch r.Method {
case http.MethodGet:
w.WriteHeader(http.StatusOK)
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
27 changes: 27 additions & 0 deletions internal/mock_units/polls/polls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package mock_units

import (
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
)

type Endpoint struct{}

var db database.CLIDatabase

func (e Endpoint) Path() string { return "/categories" }

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
db = r.Context().Value("db").(database.CLIDatabase)

switch r.Method {
case http.MethodGet:
w.WriteHeader(http.StatusOK)
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
27 changes: 27 additions & 0 deletions internal/mock_units/predictions/predictions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package predictions

import (
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
)

type Endpoint struct{}

var db database.CLIDatabase

func (e Endpoint) Path() string { return "/categories" }

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
db = r.Context().Value("db").(database.CLIDatabase)

switch r.Method {
case http.MethodGet:
w.WriteHeader(http.StatusOK)
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
42 changes: 42 additions & 0 deletions internal/mock_units/streams/streams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package streams

import (
"encoding/json"
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
)

type Endpoint struct{}

var db database.CLIDatabase

func (e Endpoint) Path() string { return "/streams" }

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
db = r.Context().Value("db").(database.CLIDatabase)

switch r.Method {
case http.MethodGet:
getStreams(w, r)
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func getStreams(w http.ResponseWriter, r *http.Request) {
s, err := db.NewQuery(r, 100).GetStream(database.Stream{})
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
j, err := json.Marshal(s)
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
w.Write(j)
}
42 changes: 42 additions & 0 deletions internal/mock_units/subscriptions/subscriptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package subscriptions

import (
"encoding/json"
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
)

type Endpoint struct{}

var db database.CLIDatabase

func (e Endpoint) Path() string { return "/subscriptions" }

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
db = r.Context().Value("db").(database.CLIDatabase)

switch r.Method {
case http.MethodGet:
getStreams(w, r)
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func getStreams(w http.ResponseWriter, r *http.Request) {
s, err := db.NewQuery(r, 100).GetSubscriptions(database.Subscription{})
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
j, err := json.Marshal(s)
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
w.Write(j)
}
42 changes: 42 additions & 0 deletions internal/mock_units/tags/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package tags

import (
"encoding/json"
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
)

type Endpoint struct{}

var db database.CLIDatabase

func (e Endpoint) Path() string { return "/tags" }

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
db = r.Context().Value("db").(database.CLIDatabase)

switch r.Method {
case http.MethodGet:
getTags(w, r)
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func getTags(w http.ResponseWriter, r *http.Request) {
s, err := db.NewQuery(r, 100).GetTags(database.Tag{})
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
j, err := json.Marshal(s)
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
w.Write(j)
}
42 changes: 42 additions & 0 deletions internal/mock_units/teams/teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package teams

import (
"encoding/json"
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
)

type Endpoint struct{}

var db database.CLIDatabase

func (e Endpoint) Path() string { return "/teams" }

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
db = r.Context().Value("db").(database.CLIDatabase)

switch r.Method {
case http.MethodGet:
getTeams(w, r)
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func getTeams(w http.ResponseWriter, r *http.Request) {
u, err := db.NewQuery(r, 100).GetTeam(database.Team{})
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
j, err := json.Marshal(u)
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
}
w.Write(j)
}
Loading

0 comments on commit 95105e9

Please sign in to comment.