-
Notifications
You must be signed in to change notification settings - Fork 15
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
1 parent
d750014
commit 1cb5c2d
Showing
7 changed files
with
207 additions
and
174 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,16 @@ | ||
package datastore | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rafael-piovesan/go-rocket-ride/entity" | ||
) | ||
|
||
func (s *sqlStore) CreateAuditRecord(ctx context.Context, ar *entity.AuditRecord) (*entity.AuditRecord, error) { | ||
_, err := s.db.NewInsert(). | ||
Model(ar). | ||
Returning("*"). | ||
Exec(ctx) | ||
|
||
return ar, err | ||
} |
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,43 @@ | ||
package datastore | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rafael-piovesan/go-rocket-ride/entity" | ||
) | ||
|
||
func (s *sqlStore) CreateIdempotencyKey( | ||
ctx context.Context, | ||
ik *entity.IdempotencyKey, | ||
) (*entity.IdempotencyKey, error) { | ||
_, err := s.db.NewInsert(). | ||
Model(ik). | ||
Returning("*"). | ||
Exec(ctx) | ||
|
||
return ik, err | ||
} | ||
|
||
func (s *sqlStore) GetIdempotencyKey(ctx context.Context, key string, userID int64) (*entity.IdempotencyKey, error) { | ||
ik := entity.IdempotencyKey{} | ||
err := s.db.NewSelect(). | ||
Model(&ik). | ||
Where("idempotency_key = ? AND user_id = ?", key, userID). | ||
Limit(1). | ||
Scan(ctx) | ||
|
||
return &ik, err | ||
} | ||
|
||
func (s *sqlStore) UpdateIdempotencyKey( | ||
ctx context.Context, | ||
ik *entity.IdempotencyKey, | ||
) (*entity.IdempotencyKey, error) { | ||
_, err := s.db.NewUpdate(). | ||
Model(ik). | ||
WherePK(). | ||
Returning("*"). | ||
Exec(ctx) | ||
|
||
return ik, err | ||
} |
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,37 @@ | ||
package datastore | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rafael-piovesan/go-rocket-ride/entity" | ||
) | ||
|
||
func (s *sqlStore) CreateRide(ctx context.Context, rd *entity.Ride) (*entity.Ride, error) { | ||
_, err := s.db.NewInsert(). | ||
Model(rd). | ||
Returning("*"). | ||
Exec(ctx) | ||
|
||
return rd, err | ||
} | ||
|
||
func (s *sqlStore) GetRideByIdempotencyKeyID(ctx context.Context, keyID int64) (*entity.Ride, error) { | ||
r := entity.Ride{} | ||
err := s.db.NewSelect(). | ||
Model(&r). | ||
Where("idempotency_key_id = ?", keyID). | ||
Limit(1). | ||
Scan(ctx) | ||
|
||
return &r, err | ||
} | ||
|
||
func (s *sqlStore) UpdateRide(ctx context.Context, rd *entity.Ride) (*entity.Ride, error) { | ||
_, err := s.db.NewUpdate(). | ||
Model(rd). | ||
WherePK(). | ||
Returning("*"). | ||
Exec(ctx) | ||
|
||
return rd, err | ||
} |
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,53 @@ | ||
package datastore | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/uptrace/bun" | ||
|
||
rocketride "github.com/rafael-piovesan/go-rocket-ride" | ||
) | ||
|
||
type sqlStore struct { | ||
conn *bun.DB | ||
db bun.IDB | ||
} | ||
|
||
func NewStore(db *bun.DB) rocketride.Datastore { | ||
return &sqlStore{ | ||
conn: db, | ||
db: db, | ||
} | ||
} | ||
|
||
func (s *sqlStore) Atomic(ctx context.Context, fn func(store rocketride.Datastore) error) (err error) { | ||
tx, err := s.conn.BeginTx(ctx, &sql.TxOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if p := recover(); p != nil { | ||
err = fmt.Errorf("panic err: %v", p) | ||
} | ||
if err != nil { | ||
if rbErr := tx.Rollback(); rbErr != nil { | ||
err = fmt.Errorf("tx err: %v, rb err: %v", err, rbErr) | ||
} | ||
} else { | ||
err = tx.Commit() | ||
} | ||
}() | ||
|
||
// TODO: check if it works for nested transactions as well | ||
newStore := &sqlStore{ | ||
conn: s.conn, | ||
db: tx, | ||
} | ||
err = fn(newStore) | ||
return err | ||
} | ||
|
||
var _ rocketride.Datastore = (*sqlStore)(nil) |
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,16 @@ | ||
package datastore | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rafael-piovesan/go-rocket-ride/entity" | ||
) | ||
|
||
func (s *sqlStore) CreateStagedJob(ctx context.Context, sj *entity.StagedJob) (*entity.StagedJob, error) { | ||
_, err := s.db.NewInsert(). | ||
Model(sj). | ||
Returning("*"). | ||
Exec(ctx) | ||
|
||
return sj, err | ||
} |
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.