Skip to content

Commit 5529f3d

Browse files
committed
updates to the units and db structure
1 parent ecce80d commit 5529f3d

27 files changed

+536
-273
lines changed

internal/database/_template.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import "log"
77
type Principle struct {
88
}
99

10-
func (c CLIDatabase) GetPrinciple(p Principle) (Principle, error) {
10+
func (q *Query) GetPrinciple(p Principle) (*DBResponse, error) {
1111
var r Principle
1212

1313
sql := generateSQL("select * from principle", u, SEP_AND)
1414
sql = fmt.Sprintf("%v LIMIT 1", sql)
15-
rows, err := c.DB.NamedQuery(sql, u)
15+
rows, err := q.DB.NamedQuery(sql, u)
1616
if err != nil {
1717
return r, err
1818
}
@@ -24,11 +24,23 @@ func (c CLIDatabase) GetPrinciple(p Principle) (Principle, error) {
2424
}
2525
}
2626

27-
return r, err
27+
dbr := DBResposne{
28+
Data: r,
29+
Limit: q.Limit,
30+
Total: len(r),
31+
}
32+
33+
if len(r) != q.Limit {
34+
q.PaginationCursor = ""
35+
}
36+
37+
dbr.Cursor = q.PaginationCursor
38+
39+
return &dbr, err
2840
}
2941

30-
func (c CLIDatabase) InsertPrinciple(p Principle, upsert bool) error {
31-
tx := c.DB.MustBegin()
42+
func (q *Query) InsertPrinciple(p Principle, upsert bool) error {
43+
tx := q.DB.MustBegin()
3244
tx.NamedExec(`insert into principle values(:id, :values...)`, p)
3345
err := tx.Commit()
3446
if err != nil {

internal/database/api.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
package database
44

55
type DBResposne struct {
6-
Cursor string
7-
Total string
8-
Limit int
9-
Data interface{}
10-
}
11-
12-
type DBPagination struct {
13-
Limit int
14-
Cursor string
6+
Cursor string `json:"cursor"`
7+
Total int `json:"total"`
8+
Limit int `json:"-"`
9+
Data interface{} `json:"data"`
1510
}

internal/database/authentication.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ type Authorization struct {
2828
Scopes sql.NullString `db:"scopes"`
2929
}
3030

31-
func (c CLIDatabase) GetAuthorizationByToken(token string) (Authorization, error) {
31+
func (q *Query) GetAuthorizationByToken(token string) (Authorization, error) {
3232
var r Authorization
33-
db := c.DB
33+
db := q.DB
3434

3535
err := db.Get(&r, "select * from authorizations where token = $1", token)
3636
if errors.Is(err, sql.ErrNoRows) {
@@ -42,8 +42,8 @@ func (c CLIDatabase) GetAuthorizationByToken(token string) (Authorization, error
4242
return r, err
4343
}
4444

45-
func (c CLIDatabase) InsertOrUpdateAuthenticationClient(client AuthenticationClient, upsert bool) (AuthenticationClient, error) {
46-
db := c.DB
45+
func (q *Query) InsertOrUpdateAuthenticationClient(client AuthenticationClient, upsert bool) (AuthenticationClient, error) {
46+
db := q.DB
4747

4848
stmt := `insert into clients values(:id, :secret, :is_extension, :name)`
4949
if upsert == true {
@@ -64,8 +64,8 @@ func (c CLIDatabase) InsertOrUpdateAuthenticationClient(client AuthenticationCli
6464
}
6565
}
6666

67-
func (c CLIDatabase) CreateAuthorization(a Authorization) (Authorization, error) {
68-
db := c.DB
67+
func (q *Query) CreateAuthorization(a Authorization) (Authorization, error) {
68+
db := q.DB
6969

7070
a.Token = generateString(15)
7171
a.ExpiresAt = util.GetTimestamp().Add(24 * 30 * time.Hour).Format(time.RFC3339Nano)

internal/database/categories.go

+25-11
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,45 @@ type Category struct {
77
Name string `db:"category_name" json:"name"`
88
}
99

10-
func (c CLIDatabase) GetCategory(cat Category) (Category, error) {
11-
var r Category
12-
rows, err := c.DB.NamedQuery(generateSQL("select * from categories", cat, SEP_AND), cat)
10+
func (q *Query) GetCategories(cat Category) (*DBResposne, error) {
11+
var r []Category
12+
rows, err := q.DB.NamedQuery(generateSQL("select * from categories", cat, SEP_AND)+q.SQL, cat)
1313
if err != nil {
14-
return r, err
14+
return nil, err
1515
}
1616

1717
for rows.Next() {
18-
err := rows.StructScan(&r)
18+
var cat Category
19+
err := rows.StructScan(&cat)
1920
if err != nil {
20-
return r, err
21+
return nil, err
2122
}
23+
r = append(r, cat)
2224
}
2325

24-
return r, err
26+
dbr := DBResposne{
27+
Data: r,
28+
Limit: q.Limit,
29+
Total: len(r),
30+
}
31+
32+
if len(r) != q.Limit {
33+
q.PaginationCursor = ""
34+
}
35+
36+
dbr.Cursor = q.PaginationCursor
37+
38+
return &dbr, err
2539
}
2640

27-
func (c CLIDatabase) InsertCategory(category Category, upsert bool) error {
28-
_, err := c.DB.NamedExec(`insert into categories values(:id, :category_name)`, category)
41+
func (q *Query) InsertCategory(category Category, upsert bool) error {
42+
_, err := q.DB.NamedExec(`insert into categories values(:id, :category_name)`, category)
2943
return err
3044
}
3145

32-
func (c CLIDatabase) SearchCategories(query string) ([]Category, error) {
46+
func (q *Query) SearchCategories(query string) ([]Category, error) {
3347
categories := []Category{}
34-
err := c.DB.Select(&categories, `select * from categories where category_name like '%$1%'`, query)
48+
err := q.DB.Select(&categories, `select * from categories where category_name like '%$1%'`, query)
3549
if err != nil {
3650
return categories, err
3751
}

internal/database/channel_points_redemptions.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import "log"
77
type ChannelPointsRedemption struct {
88
}
99

10-
func (c CLIDatabase) GetChannelPointsRedemptionById(id string) (ChannelPointsRedemption, error) {
11-
db := c.DB
10+
func (q *Query) GetChannelPointsRedemptionById(id string) (ChannelPointsRedemption, error) {
11+
db := q.DB
1212
var r ChannelPointsRedemption
1313

1414
err := db.Get(&r, "select * from channel_points_redemptions where id = $1", id)
@@ -20,8 +20,8 @@ func (c CLIDatabase) GetChannelPointsRedemptionById(id string) (ChannelPointsRed
2020
return r, err
2121
}
2222

23-
func (c CLIDatabase) InsertChannelPointsRedemption(r ChannelPointsRedemption, upsert bool) error {
24-
db := c.DB
23+
func (q *Query) InsertChannelPointsRedemption(r ChannelPointsRedemption, upsert bool) error {
24+
db := q.DB
2525

2626
tx := db.MustBegin()
2727
tx.NamedExec(`insert into channel_points_redemptions values(:id, :values...)`, r)

internal/database/channel_points_rewards.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import "log"
77
type ChannelPointsReward struct {
88
}
99

10-
func (c CLIDatabase) GetChannelPointsRewardById(id string) (ChannelPointsReward, error) {
10+
func (q *Query) GetChannelPointsRewardById(id string) (ChannelPointsReward, error) {
1111
var r ChannelPointsReward
1212

13-
err := c.DB.Get(&r, "select * from channel_points_rewards where id = $1", id)
13+
err := q.DB.Get(&r, "select * from channel_points_rewards where id = $1", id)
1414
if err != nil {
1515
return r, err
1616
}
@@ -19,8 +19,8 @@ func (c CLIDatabase) GetChannelPointsRewardById(id string) (ChannelPointsReward,
1919
return r, err
2020
}
2121

22-
func (c CLIDatabase) InsertChannelPointsReward(r ChannelPointsReward, upsert bool) error {
23-
tx := c.DB.MustBegin()
22+
func (q *Query) InsertChannelPointsReward(r ChannelPointsReward, upsert bool) error {
23+
tx := q.DB.MustBegin()
2424
tx.NamedExec(`insert into channel_points_rewards values(:id, :values...)`, r)
2525
err := tx.Commit()
2626
if err != nil {

internal/database/database_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ func TestRetriveFromDB(t *testing.T) {
5454
Timestamp: util.GetTimestamp().Format(time.RFC3339Nano),
5555
}
5656

57-
err = db.InsertIntoDB(ecParams)
57+
q := Query{DB: db.DB}
58+
59+
err = q.InsertIntoDB(ecParams)
5860
a.Nil(err)
5961

60-
dbResponse, err := db.GetEventByID(ecParams.ID)
62+
dbResponse, err := q.GetEventByID(ecParams.ID)
6163
a.Nil(err)
6264

6365
println(dbResponse.ID)

internal/database/drops.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,32 @@ import "log"
77
type DropsEntitlement struct {
88
}
99

10-
func (c CLIDatabase) GetDropsEntitlementById(id string) (DropsEntitlement, error) {
11-
var r DropsEntitlement
10+
func (q *Query) GetDropsEntitlementById(id string) (*DBResposne, error) {
11+
var r []DropsEntitlement
1212

13-
err := c.DB.Get(&r, "select * from drops_entitlements where id = $1", id)
13+
err := q.DB.Get(&r, "select * from drops_entitlements where id = $1", id)
1414
if err != nil {
15-
return r, err
15+
return nil, err
1616
}
1717
log.Printf("%#v", r)
1818

19-
return r, err
19+
dbr := DBResposne{
20+
Data: r,
21+
Limit: q.Limit,
22+
Total: len(r),
23+
}
24+
25+
if len(r) != q.Limit {
26+
q.PaginationCursor = ""
27+
}
28+
29+
dbr.Cursor = q.PaginationCursor
30+
31+
return &dbr, err
2032
}
2133

22-
func (c CLIDatabase) InsertDropsEntitlement(d DropsEntitlement, upsert bool) error {
23-
tx := c.DB.MustBegin()
34+
func (q *Query) InsertDropsEntitlement(d DropsEntitlement, upsert bool) error {
35+
tx := q.DB.MustBegin()
2436
tx.NamedExec(`insert into drops_entitlements values(:id, :values...)`, d)
2537
err := tx.Commit()
2638
if err != nil {

internal/database/events.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ type EventCacheResponse struct {
2323
}
2424

2525
// InsertIntoDB inserts an event into the database for replay functions later.
26-
func (c CLIDatabase) InsertIntoDB(p EventCacheParameters) error {
27-
db := c.DB
26+
func (q *Query) InsertIntoDB(p EventCacheParameters) error {
27+
db := q.DB
2828

2929
tx := db.MustBegin()
3030
tx.NamedExec(`insert into events(id, event, json, from_user, to_user, transport, timestamp) values(:id, :event, :json, :from_user, :to_user, :transport, :timestamp)`, p)
@@ -37,8 +37,8 @@ func (c CLIDatabase) InsertIntoDB(p EventCacheParameters) error {
3737
}
3838

3939
// GetEventByID returns an event based on an ID provided for replay.
40-
func (c CLIDatabase) GetEventByID(id string) (EventCacheResponse, error) {
41-
db := c.DB
40+
func (q *Query) GetEventByID(id string) (EventCacheResponse, error) {
41+
db := q.DB
4242
var r EventCacheResponse
4343

4444
err := db.Get(&r, "select id, json, transport, event from events where id = $1", id)

internal/database/extensions.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,32 @@ import "log"
77
type Extension struct {
88
}
99

10-
func (c CLIDatabase) GetExtensionById(id string) (Extension, error) {
11-
var r Extension
10+
func (q *Query) GetExtensionById(id string) (*DBResposne, error) {
11+
var r []Extension
1212

13-
err := c.DB.Get(&r, "select * from principle where id = $1", id)
13+
err := q.DB.Get(&r, "select * from principle where id = $1", id)
1414
if err != nil {
15-
return r, err
15+
return nil, err
1616
}
1717
log.Printf("%#v", r)
1818

19-
return r, err
19+
dbr := DBResposne{
20+
Data: r,
21+
Limit: q.Limit,
22+
Total: len(r),
23+
}
24+
25+
if len(r) != q.Limit {
26+
q.PaginationCursor = ""
27+
}
28+
29+
dbr.Cursor = q.PaginationCursor
30+
31+
return &dbr, err
2032
}
2133

22-
func (c CLIDatabase) InsertExtension(p Extension, upsert bool) error {
23-
tx := c.DB.MustBegin()
34+
func (q *Query) InsertExtension(p Extension, upsert bool) error {
35+
tx := q.DB.MustBegin()
2436
tx.NamedExec(`insert into principle values(:id, :values...)`, p)
2537
return tx.Commit()
2638
}

0 commit comments

Comments
 (0)