@@ -7,20 +7,20 @@ import (
77
88 "github.com/jmoiron/sqlx"
99
10- pgutil "github.com/code-payments/code-server/pkg/database/postgres"
1110 "github.com/code-payments/code-server/pkg/code/data/rendezvous"
11+ pgutil "github.com/code-payments/code-server/pkg/database/postgres"
1212)
1313
1414const (
1515 tableName = "codewallet__core_rendezvous"
1616)
1717
1818type model struct {
19- Id sql.NullInt64 `db:"id"`
20- Key string `db:"key"`
21- Location string `db:"location "`
22- CreatedAt time.Time `db:"created_at"`
23- LastUpdatedAt time.Time `db:"last_updated_at "`
19+ Id sql.NullInt64 `db:"id"`
20+ Key string `db:"key"`
21+ Address string `db:"address "`
22+ CreatedAt time.Time `db:"created_at"`
23+ ExpiresAt time.Time `db:"expires_at "`
2424}
2525
2626func toModel (obj * rendezvous.Record ) (* model , error ) {
@@ -29,69 +29,93 @@ func toModel(obj *rendezvous.Record) (*model, error) {
2929 }
3030
3131 return & model {
32- Key : obj .Key ,
33- Location : obj .Location ,
34- CreatedAt : obj .CreatedAt ,
35- LastUpdatedAt : obj .LastUpdatedAt ,
32+ Key : obj .Key ,
33+ Address : obj .Address ,
34+ CreatedAt : obj .CreatedAt ,
35+ ExpiresAt : obj .ExpiresAt ,
3636 }, nil
3737}
3838
3939func fromModel (obj * model ) * rendezvous.Record {
4040 return & rendezvous.Record {
41- Id : uint64 (obj .Id .Int64 ),
42- Key : obj .Key ,
43- Location : obj .Location ,
44- CreatedAt : obj .CreatedAt ,
45- LastUpdatedAt : obj .LastUpdatedAt ,
41+ Id : uint64 (obj .Id .Int64 ),
42+ Key : obj .Key ,
43+ Address : obj .Address ,
44+ CreatedAt : obj .CreatedAt ,
45+ ExpiresAt : obj .ExpiresAt ,
4646 }
4747}
4848
49- func (m * model ) dbSave (ctx context.Context , db * sqlx.DB ) error {
49+ func (m * model ) dbPut (ctx context.Context , db * sqlx.DB ) error {
5050 query := `INSERT INTO ` + tableName + `
51- (key, location , created_at, last_updated_at )
51+ (key, address , created_at, expires_at )
5252 VALUES ($1, $2, $3, $4)
5353
5454 ON CONFLICT (key)
5555 DO UPDATE
56- SET location = $2, last_updated_at = $4
57- WHERE ` + tableName + `.key = $1
56+ SET address = $2, expires_at = $4
57+ WHERE ` + tableName + `.key = $1 AND ` + tableName + `.expires_at < NOW()
5858
59- RETURNING id, key, location , created_at, last_updated_at
59+ RETURNING id, key, address , created_at, expires_at
6060 `
6161
6262 if m .CreatedAt .IsZero () {
6363 m .CreatedAt = time .Now ()
6464 }
65- m .LastUpdatedAt = time .Now ()
6665
67- return db .QueryRowxContext (
66+ err := db .QueryRowxContext (
6867 ctx ,
6968 query ,
7069 m .Key ,
71- m .Location ,
70+ m .Address ,
7271 m .CreatedAt ,
73- m .LastUpdatedAt ,
72+ m .ExpiresAt ,
7473 ).StructScan (m )
74+
75+ if err != nil {
76+ return pgutil .CheckNoRows (err , rendezvous .ErrExists )
77+ }
78+
79+ return nil
7580}
7681
77- func dbGetByKey (ctx context.Context , db * sqlx.DB , key string ) ( * model , error ) {
78- var res model
79- query := `SELECT id, key, location, created_at, last_updated_at FROM ` + tableName + `
80- WHERE key = $1
82+ func dbExtendExpiry (ctx context.Context , db * sqlx.DB , key , address string , expiry time. Time ) error {
83+ query := `UPDATE ` + tableName + `
84+ SET expires_at = $1
85+ WHERE key = $2 AND address = $3 AND expires_at > NOW()
8186 `
8287
83- err := db .GetContext (ctx , & res , query , key )
88+ res , err := db .ExecContext (ctx , query , expiry , key , address )
8489 if err != nil {
85- return nil , pgutil . CheckNoRows ( err , rendezvous . ErrNotFound )
90+ return err
8691 }
87- return & res , nil
92+ rowsAffected , err := res .RowsAffected ()
93+ if err != nil {
94+ return err
95+ } else if rowsAffected == 0 {
96+ return rendezvous .ErrNotFound
97+ }
98+ return nil
8899}
89100
90- func dbDelete (ctx context.Context , db * sqlx.DB , key string ) error {
101+ func dbDelete (ctx context.Context , db * sqlx.DB , key , address string ) error {
91102 query := `DELETE FROM ` + tableName + `
92- WHERE key = $1
103+ WHERE key = $1 AND address = $2
93104 `
94105
95- _ , err := db .ExecContext (ctx , query , key )
106+ _ , err := db .ExecContext (ctx , query , key , address )
96107 return err
97108}
109+
110+ func dbGetByKey (ctx context.Context , db * sqlx.DB , key string ) (* model , error ) {
111+ var res model
112+ query := `SELECT id, key, address, created_at, expires_at FROM ` + tableName + `
113+ WHERE key = $1 AND expires_at > NOW()
114+ `
115+
116+ err := db .GetContext (ctx , & res , query , key )
117+ if err != nil {
118+ return nil , pgutil .CheckNoRows (err , rendezvous .ErrNotFound )
119+ }
120+ return & res , nil
121+ }
0 commit comments