Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
fix #72: use "upsert" for register license logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Nov 24, 2018
1 parent c6e3d7e commit f22f3e3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion env/client/rest.http
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ X-Request-ID: 10000000-2000-4000-8000-160000000000

### Maintenance

POST http://localhost:8093/install
POST http://localhost:8093/api/v1/install
Content-Type: application/json
X-Request-ID: 10000000-2000-4000-8000-160000000000

Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/internal/postgres/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (scope licenseManager) Update(token *types.Token, data query.UpdateLicense)
"user %q of account %q with token %q tried to update license %q with new contract %s",
token.UserID, token.User.AccountID, token.ID, entity.ID, after)
}
if prev == nil || !prev.Equal(*entity.UpdatedAt) {
if entity.UpdatedAt != nil && (prev == nil || !prev.Equal(*entity.UpdatedAt)) {
audit := `INSERT INTO "license_audit" ("license_id", "contract", "what", "when", "who", "with")
VALUES ($1, $2, $3, $4, $5, $6)`
if _, execErr := scope.conn.ExecContext(scope.ctx, audit, entity.ID, before,
Expand Down
65 changes: 50 additions & 15 deletions pkg/storage/protected.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,42 @@ import (

"github.com/kamilsk/guard/pkg/storage/query"
"github.com/kamilsk/guard/pkg/storage/types"
"github.com/pkg/errors"
)

// RegisterLicense TODO issue#docs
func (storage *Storage) RegisterLicense(ctx context.Context, id domain.Token, data query.RegisterLicense) (types.License, error) {
return storage.CreateLicense(ctx, id, query.CreateLicense{ID: &data.ID, Contract: data.Contract})
var license types.License

conn, closer, connErr := storage.connection(ctx)
if connErr != nil {
return license, connErr
}
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
return license, authErr
}

tx, txErr := conn.BeginTx(ctx, &sql.TxOptions{})
if txErr != nil {
return license, txErr
}
defer func() { _ = tx.Rollback() }()

manager := storage.exec.LicenseManager(ctx, conn)
license, execErr := manager.Read(token, query.ReadLicense{ID: data.ID})
if execErr == nil {
license, execErr = manager.Update(token, query.UpdateLicense{ID: data.ID, Contract: data.Contract})
} else if errors.Cause(execErr) == sql.ErrNoRows {
license, execErr = manager.Create(token, query.CreateLicense{ID: &data.ID, Contract: data.Contract})
}
if execErr != nil {
return license, execErr
}

return license, tx.Commit()
}

// CreateLicense TODO issue#docs
Expand All @@ -23,7 +54,7 @@ func (storage *Storage) CreateLicense(ctx context.Context, id domain.Token, data
if connErr != nil {
return license, connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -34,9 +65,10 @@ func (storage *Storage) CreateLicense(ctx context.Context, id domain.Token, data
if txErr != nil {
return license, txErr
}
defer func() { _ = tx.Rollback() }()

license, execErr := storage.exec.LicenseManager(ctx, conn).Create(token, data)
if execErr != nil {
_ = tx.Rollback() // TODO issue#composite
return license, execErr
}
return license, tx.Commit()
Expand All @@ -50,7 +82,7 @@ func (storage *Storage) ReadLicense(ctx context.Context, id domain.Token, data q
if connErr != nil {
return license, connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -68,7 +100,7 @@ func (storage *Storage) UpdateLicense(ctx context.Context, id domain.Token, data
if connErr != nil {
return license, connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -79,9 +111,10 @@ func (storage *Storage) UpdateLicense(ctx context.Context, id domain.Token, data
if txErr != nil {
return license, txErr
}
defer func() { _ = tx.Rollback() }()

license, execErr := storage.exec.LicenseManager(ctx, conn).Update(token, data)
if execErr != nil {
_ = tx.Rollback() // TODO issue#composite
return license, execErr
}
return license, tx.Commit()
Expand All @@ -95,7 +128,7 @@ func (storage *Storage) DeleteLicense(ctx context.Context, id domain.Token, data
if connErr != nil {
return license, connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -106,9 +139,10 @@ func (storage *Storage) DeleteLicense(ctx context.Context, id domain.Token, data
if txErr != nil {
return license, txErr
}
defer func() { _ = tx.Rollback() }()

license, execErr := storage.exec.LicenseManager(ctx, conn).Delete(token, data)
if execErr != nil {
_ = tx.Rollback() // TODO issue#composite
return license, execErr
}
return license, tx.Commit()
Expand All @@ -122,7 +156,7 @@ func (storage *Storage) RestoreLicense(ctx context.Context, id domain.Token, dat
if connErr != nil {
return license, connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -133,9 +167,10 @@ func (storage *Storage) RestoreLicense(ctx context.Context, id domain.Token, dat
if txErr != nil {
return license, txErr
}
defer func() { _ = tx.Rollback() }()

license, execErr := storage.exec.LicenseManager(ctx, conn).Restore(token, data)
if execErr != nil {
_ = tx.Rollback() // TODO issue#composite
return license, execErr
}
return license, tx.Commit()
Expand All @@ -149,7 +184,7 @@ func (storage *Storage) AddEmployee(ctx context.Context, id domain.Token, data q
if connErr != nil {
return connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -165,7 +200,7 @@ func (storage *Storage) DeleteEmployee(ctx context.Context, id domain.Token, dat
if connErr != nil {
return connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -181,7 +216,7 @@ func (storage *Storage) AddWorkplace(ctx context.Context, id domain.Token, data
if connErr != nil {
return connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -197,7 +232,7 @@ func (storage *Storage) DeleteWorkplace(ctx context.Context, id domain.Token, da
if connErr != nil {
return connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand All @@ -213,7 +248,7 @@ func (storage *Storage) PushWorkplace(ctx context.Context, id domain.Token, data
if connErr != nil {
return connErr
}
defer closer()
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (storage *Storage) LicenseByID(ctx context.Context, id domain.ID) (types.Li
if connErr != nil {
return license, connErr
}
defer closer()
defer func() { _ = closer() }()

return storage.exec.LicenseReader(ctx, conn).GetByID(query.GetLicenseWithID{ID: id})
}
Expand All @@ -31,7 +31,7 @@ func (storage *Storage) LicenseByEmployee(ctx context.Context, employee domain.I
if connErr != nil {
return license, connErr
}
defer closer()
defer func() { _ = closer() }()

return storage.exec.LicenseReader(ctx, conn).GetByEmployee(query.GetEmployeeLicense{Employee: employee})
}
Expand All @@ -46,7 +46,7 @@ func (storage *Storage) RegisterAccount(ctx context.Context, data *query.Registe
if connErr != nil {
return nil, connErr
}
defer closer()
defer func() { _ = closer() }()

tx, txErr := conn.BeginTx(ctx, &sql.TxOptions{})
if txErr != nil {
Expand Down

0 comments on commit f22f3e3

Please sign in to comment.