-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
580 additions
and
49 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,27 @@ | ||
-- +migrate Up | ||
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 ( | ||
id TEXT PRIMARY KEY, | ||
nullifier TEXT REFERENCES balances (nullifier), | ||
reward BIGINT NOT NULL, | ||
usage_count BIGINT NOT NULL DEFAULT 0, | ||
infinity BOOLEAN NOT NULL DEFAULT FALSE, | ||
|
||
updated_at TIMESTAMP NOT NULL DEFAULT NOW(), | ||
created_at TIMESTAMP NOT NULL DEFAULT NOW() | ||
); | ||
|
||
DROP TRIGGER IF EXISTS set_updated_at ON qr_codes; | ||
CREATE TRIGGER set_updated_at | ||
BEFORE UPDATE | ||
ON qr_codes | ||
FOR EACH ROW | ||
EXECUTE FUNCTION trigger_set_updated_at_ts(); | ||
|
||
|
||
-- +migrate Down | ||
DROP TABLE IF EXISTS qr_codes; | ||
DROP FUNCTION IF EXISTS trigger_set_updated_at_ts(); |
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
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
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
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,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 qrCodesTable = "qr_codes" | ||
|
||
type qrCodesQ struct { | ||
db *pgdb.DB | ||
selector squirrel.SelectBuilder | ||
updater squirrel.UpdateBuilder | ||
counter squirrel.SelectBuilder | ||
} | ||
|
||
func NewQRCodesQ(db *pgdb.DB) data.QRCodesQ { | ||
return &qrCodesQ{ | ||
db: db, | ||
selector: squirrel.Select("id", qrCodesTable+".nullifier AS nullifier", "usage_count", "infinity", "reward").From(qrCodesTable), | ||
updater: squirrel.Update(qrCodesTable), | ||
counter: squirrel.Select("COUNT(*) as count").From(qrCodesTable), | ||
} | ||
} | ||
|
||
func (q *qrCodesQ) New() data.QRCodesQ { | ||
return NewQRCodesQ(q.db) | ||
} | ||
|
||
func (q *qrCodesQ) Insert(qrCodes ...data.QRCode) error { | ||
if len(qrCodes) == 0 { | ||
return nil | ||
} | ||
|
||
stmt := squirrel.Insert(qrCodesTable).Columns("id", "nullifier", "reward", "usage_count", "infinity") | ||
for _, qr := range qrCodes { | ||
stmt = stmt.Values(qr.ID, qr.Nullifier, qr.Reward, qr.UsageCount, qr.Infinity) | ||
} | ||
|
||
if err := q.db.Exec(stmt); err != nil { | ||
return fmt.Errorf("insert qr codes: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (q *qrCodesQ) Update(values map[string]any) error { | ||
|
||
if err := q.db.Exec(q.updater.SetMap(values)); err != nil { | ||
return fmt.Errorf("update qrCode: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (q *qrCodesQ) Select() ([]data.QRCode, error) { | ||
var res []data.QRCode | ||
|
||
if err := q.db.Select(&res, q.selector); err != nil { | ||
return nil, fmt.Errorf("select qrCodes: %w", err) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (q *qrCodesQ) Get() (*data.QRCode, error) { | ||
var res data.QRCode | ||
|
||
if err := q.db.Get(&res, q.selector); err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return nil, nil | ||
} | ||
return nil, fmt.Errorf("get qrCode: %w", err) | ||
} | ||
|
||
return &res, nil | ||
} | ||
|
||
func (q *qrCodesQ) Page(page *pgdb.OffsetPageParams) data.QRCodesQ { | ||
q.selector = page.ApplyTo(q.selector, "updated_at") | ||
return q | ||
} | ||
|
||
func (q *qrCodesQ) 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 qrCodes: %w", err) | ||
} | ||
|
||
return res.Count, nil | ||
} | ||
|
||
func (q *qrCodesQ) FilterByNullifier(nullifiers ...string) data.QRCodesQ { | ||
return q.applyCondition(squirrel.Eq{fmt.Sprintf("%s.nullifier", qrCodesTable): nullifiers}) | ||
} | ||
|
||
func (q *qrCodesQ) FilterByID(ids ...string) data.QRCodesQ { | ||
return q.applyCondition(squirrel.Eq{fmt.Sprintf("%s.id", qrCodesTable): ids}) | ||
} | ||
|
||
func (q *qrCodesQ) applyCondition(cond squirrel.Sqlizer) data.QRCodesQ { | ||
q.selector = q.selector.Where(cond) | ||
q.updater = q.updater.Where(cond) | ||
q.counter = q.counter.Where(cond) | ||
return q | ||
} |
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,40 @@ | ||
package data | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"gitlab.com/distributed_lab/kit/pgdb" | ||
) | ||
|
||
const ( | ||
ColNullifier = "nullifier" | ||
ColUsageCount = "usage_count" | ||
ColInfinity = "infinity" | ||
) | ||
|
||
type QRCode struct { | ||
ID string `db:"id"` | ||
Nullifier sql.NullString `db:"nullifier"` | ||
Reward int `db:"reward"` | ||
UsageCount int `db:"usage_count"` | ||
Infinity bool `db:"infinity"` | ||
|
||
UpdatedAt time.Time `db:"updated_at"` | ||
CreatedAt time.Time `db:"created_at"` | ||
} | ||
|
||
type QRCodesQ interface { | ||
New() QRCodesQ | ||
Insert(...QRCode) error | ||
Update(map[string]any) error | ||
|
||
Page(*pgdb.OffsetPageParams) QRCodesQ | ||
|
||
Get() (*QRCode, error) | ||
Select() ([]QRCode, error) | ||
Count() (uint64, error) | ||
|
||
FilterByID(...string) QRCodesQ | ||
FilterByNullifier(...string) QRCodesQ | ||
} |
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,79 @@ | ||
package handlers | ||
|
||
import ( | ||
"crypto/rand" | ||
"database/sql" | ||
"encoding/hex" | ||
"net/http" | ||
|
||
"github.com/rarimo/geo-auth-svc/pkg/auth" | ||
"github.com/rarimo/geo-points-svc/internal/data" | ||
"github.com/rarimo/geo-points-svc/internal/service/requests" | ||
"github.com/rarimo/geo-points-svc/resources" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
) | ||
|
||
func CreateQRCode(w http.ResponseWriter, r *http.Request) { | ||
req, err := requests.NewCreateQRCode(r) | ||
if err != nil { | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
if !auth.Authenticates(UserClaims(r), auth.AdminGrant) { | ||
ape.RenderErr(w, problems.Unauthorized()) | ||
return | ||
} | ||
|
||
var ( | ||
_ = req.Data.Attributes.Nullifier | ||
dataUsageCount = req.Data.Attributes.UsageCount | ||
dataReward = req.Data.Attributes.Reward | ||
usageCount = 1 | ||
reward = 10 | ||
) | ||
|
||
qrValue := make([]byte, 10) | ||
_, err = rand.Read(qrValue[:]) | ||
if err != nil { | ||
Log(r).WithError(err).Error("Failed to get rand bytes") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
if dataUsageCount != nil { | ||
usageCount = *dataUsageCount | ||
} | ||
|
||
if dataReward != nil { | ||
reward = *dataReward | ||
} | ||
|
||
qr := data.QRCode{ | ||
ID: "one_time_" + hex.EncodeToString(qrValue), | ||
Nullifier: sql.NullString{}, | ||
UsageCount: usageCount, | ||
Reward: reward, | ||
} | ||
|
||
if err = QRCodesQ(r).Insert(qr); err != nil { | ||
Log(r).WithError(err).Error("Failed to insert qr code") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
ape.Render(w, resources.QrCodeRequest{ | ||
Data: resources.QrCode{ | ||
Key: resources.Key{ | ||
ID: qr.ID, | ||
Type: resources.QR_CODE, | ||
}, | ||
Attributes: resources.QrCodeAttributes{ | ||
Reward: &reward, | ||
UsageCount: &usageCount, | ||
}, | ||
}, | ||
}) | ||
|
||
} |
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
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
Oops, something went wrong.