Skip to content

Commit

Permalink
Change qr_code to bonus_code
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaptoss committed Oct 8, 2024
1 parent 704b0c1 commit 3527587
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 249 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
allOf:
- $ref: '#/components/schemas/QRCodeKey'
- $ref: '#/components/schemas/BonusCodeKey'
- type: object
x-go-is-request: true
required:
Expand All @@ -11,15 +11,15 @@ allOf:
reward:
type: integer
format: int
description: Reward for this qr-code
description: Reward for this bonus code
default: 10
example: 10
usage_count:
type: integer
format: int
description: Specify how many times qr-code can be scaned. Omit if qr-code must have infinity usage count
description: Specify how many times bonus code can be scaned. Omit if bonus code must have infinity usage count
example: 1
nullifier:
type: string
description: For creating personal qr-codes
description: For creating personal bonus codes
example: "0xabc...123"
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ required:
properties:
id:
type: string
description: QR code value
description: Bonus code value
example: "one_time_abcdefg..xyz"
type:
type: string
enum: [ qr_code ]
enum: [ bonus_code ]
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
post:
tags:
- QRCodes
summary: Create QR code
description: Create custom qr code
operationId: createQRCode
- Bonus Codes
summary: Createbonus code
description: Create custom bonus code
operationId: createCode
security:
- BearerAuth: []
requestBody:
Expand All @@ -16,7 +16,7 @@ post:
- data
properties:
data:
$ref: '#/components/schemas/QRCode'
$ref: '#/components/schemas/BonusCode'
responses:
200:
description: Success
Expand All @@ -28,7 +28,7 @@ post:
- data
properties:
data:
$ref: '#/components/schemas/QRCode'
$ref: '#/components/schemas/BonusCode'
400:
$ref: '#/components/responses/invalidParameter'
401:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
post:
tags:
- QRCodes
summary: Send QR code
description: Send a QR code and get a reward
operationId: submitQRCode
- Bonus Codes
summary: Send code
description: Send a code and get a reward
operationId: submitCode
security:
- BearerAuth: []
requestBody:
Expand All @@ -16,7 +16,7 @@ post:
- data
properties:
data:
$ref: '#/components/schemas/QRCodeKey'
$ref: '#/components/schemas/BonusCodeKey'
responses:
200:
description: Success
Expand Down
8 changes: 4 additions & 4 deletions internal/assets/migrations/006_one_time_qr_code.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE OR REPLACE FUNCTION trigger_set_updated_at_ts() RETURNS trigger
LANGUAGE plpgsql
AS $$ BEGIN NEW.updated_at = (NOW() AT TIME ZONE 'utc'); RETURN NEW; END; $$;

CREATE TABLE IF NOT EXISTS qr_codes (
CREATE TABLE IF NOT EXISTS bonus_codes (
id TEXT PRIMARY KEY,
nullifier TEXT REFERENCES balances (nullifier),
reward BIGINT NOT NULL,
Expand All @@ -14,14 +14,14 @@ CREATE TABLE IF NOT EXISTS qr_codes (
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

DROP TRIGGER IF EXISTS set_updated_at ON qr_codes;
DROP TRIGGER IF EXISTS set_updated_at ON bonus_codes;
CREATE TRIGGER set_updated_at
BEFORE UPDATE
ON qr_codes
ON bonus_codes
FOR EACH ROW
EXECUTE FUNCTION trigger_set_updated_at_ts();


-- +migrate Down
DROP TABLE IF EXISTS qr_codes;
DROP TABLE IF EXISTS bonus_codes;
DROP FUNCTION IF EXISTS trigger_set_updated_at_ts();
18 changes: 9 additions & 9 deletions internal/data/qr_codes.go → internal/data/bonus_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
ColInfinity = "infinity"
)

type QRCode struct {
type BonusCode struct {
ID string `db:"id"`
Nullifier sql.NullString `db:"nullifier"`
Reward int `db:"reward"`
Expand All @@ -24,17 +24,17 @@ type QRCode struct {
CreatedAt time.Time `db:"created_at"`
}

type QRCodesQ interface {
New() QRCodesQ
Insert(...QRCode) error
type BonusCodesQ interface {
New() BonusCodesQ
Insert(...BonusCode) error
Update(map[string]any) error

Page(*pgdb.OffsetPageParams) QRCodesQ
Page(*pgdb.OffsetPageParams) BonusCodesQ

Get() (*QRCode, error)
Select() ([]QRCode, error)
Get() (*BonusCode, error)
Select() ([]BonusCode, error)
Count() (uint64, error)

FilterByID(...string) QRCodesQ
FilterByNullifier(...string) QRCodesQ
FilterByID(...string) BonusCodesQ
FilterByNullifier(...string) BonusCodesQ
}
2 changes: 1 addition & 1 deletion internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type EventsQ interface {
FilterByType(...string) EventsQ
FilterByNotType(types ...string) EventsQ
FilterByUpdatedAtBefore(int64) EventsQ
FilterByQRCode(string) EventsQ
FilterByBonusCode(string) EventsQ

FilterTodayEvents(offset int) EventsQ

Expand Down
2 changes: 1 addition & 1 deletion internal/data/evtypes/models/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
TypePollParticipation = "poll_participation"
TypeEarlyTest = "early_test"
TypeDailyQuestion = "daily_question"
TypeQRCode = "qr_code"
TypeBonusCode = "bonus_code"
)

const (
Expand Down
114 changes: 114 additions & 0 deletions internal/data/pg/bonus_codes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package pg

import (
"database/sql"
"errors"
"fmt"

"github.com/Masterminds/squirrel"
"github.com/rarimo/geo-points-svc/internal/data"
"gitlab.com/distributed_lab/kit/pgdb"
)

const bonusCodesTable = "bonus_codes"

type bonusCodesQ struct {
db *pgdb.DB
selector squirrel.SelectBuilder
updater squirrel.UpdateBuilder
counter squirrel.SelectBuilder
}

func NewBonusCodesQ(db *pgdb.DB) data.BonusCodesQ {
return &bonusCodesQ{
db: db,
selector: squirrel.Select("id", bonusCodesTable+".nullifier AS nullifier", "usage_count", "infinity", "reward").From(bonusCodesTable),
updater: squirrel.Update(bonusCodesTable),
counter: squirrel.Select("COUNT(*) as count").From(bonusCodesTable),
}
}

func (q *bonusCodesQ) New() data.BonusCodesQ {
return NewBonusCodesQ(q.db)
}

func (q *bonusCodesQ) Insert(bonusCodes ...data.BonusCode) error {
if len(bonusCodes) == 0 {
return nil
}

stmt := squirrel.Insert(bonusCodesTable).Columns("id", "nullifier", "reward", "usage_count", "infinity")
for _, bonusCode := range bonusCodes {
stmt = stmt.Values(bonusCode.ID, bonusCode.Nullifier, bonusCode.Reward, bonusCode.UsageCount, bonusCode.Infinity)
}

if err := q.db.Exec(stmt); err != nil {
return fmt.Errorf("insert bonus codes: %w", err)
}

return nil
}

func (q *bonusCodesQ) Update(values map[string]any) error {

if err := q.db.Exec(q.updater.SetMap(values)); err != nil {
return fmt.Errorf("update bonusCode: %w", err)
}

return nil
}

func (q *bonusCodesQ) Select() ([]data.BonusCode, error) {
var res []data.BonusCode

if err := q.db.Select(&res, q.selector); err != nil {
return nil, fmt.Errorf("select bonusCodes: %w", err)
}

return res, nil
}

func (q *bonusCodesQ) Get() (*data.BonusCode, error) {
var res data.BonusCode

if err := q.db.Get(&res, q.selector); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("get bonusCode: %w", err)
}

return &res, nil
}

func (q *bonusCodesQ) Page(page *pgdb.OffsetPageParams) data.BonusCodesQ {
q.selector = page.ApplyTo(q.selector, "updated_at")
return q
}

func (q *bonusCodesQ) Count() (uint64, error) {
var res struct {
Count uint64 `db:"count"`
}

if err := q.db.Get(&res, q.counter); err != nil {
return 0, fmt.Errorf("count bonusCodes: %w", err)
}

return res.Count, nil
}

func (q *bonusCodesQ) FilterByNullifier(nullifiers ...string) data.BonusCodesQ {
return q.applyCondition(squirrel.Eq{fmt.Sprintf("%s.nullifier", bonusCodesTable): nullifiers})
}

func (q *bonusCodesQ) FilterByID(ids ...string) data.BonusCodesQ {
return q.applyCondition(squirrel.Eq{fmt.Sprintf("%s.id", bonusCodesTable): ids})
}

func (q *bonusCodesQ) applyCondition(cond squirrel.Sqlizer) data.BonusCodesQ {
q.selector = q.selector.Where(cond)
q.updater = q.updater.Where(cond)
q.counter = q.counter.Where(cond)
return q
}
4 changes: 2 additions & 2 deletions internal/data/pg/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ func (q *events) FilterByQuestionID(id int) data.EventsQ {
return q.applyCondition(squirrel.Eq{"meta->>'question_id'": id})
}

func (q *events) FilterByQRCode(qrCode string) data.EventsQ {
return q.applyCondition(squirrel.Eq{"meta->>'qr_code'": qrCode})
func (q *events) FilterByBonusCode(bonusCode string) data.EventsQ {
return q.applyCondition(squirrel.Eq{"meta->>'bonus_code'": bonusCode})
}

func (q *events) FilterInactiveNotClaimed(types ...string) data.EventsQ {
Expand Down
Loading

0 comments on commit 3527587

Please sign in to comment.