-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updates to the units and db structure
- Loading branch information
Showing
12 changed files
with
498 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.