mirror of
https://github.com/emo2007/block-accounting.git
synced 2025-04-12 08:56:28 +00:00
tmp users repo
This commit is contained in:
parent
9f28b4bcba
commit
da6dc66fde
@ -1,6 +1,8 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,6 +17,7 @@ type User struct {
|
|||||||
Bip32Seed []byte
|
Bip32Seed []byte
|
||||||
Admin bool
|
Admin bool
|
||||||
Activated bool
|
Activated bool
|
||||||
|
CteatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUser(id uuid.UUID, seed []byte) *User {
|
func NewUser(id uuid.UUID, seed []byte) *User {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
sq "github.com/Masterminds/squirrel"
|
||||||
"github.com/emochka2007/block-accounting/internal/pkg/models"
|
"github.com/emochka2007/block-accounting/internal/pkg/models"
|
||||||
sqltools "github.com/emochka2007/block-accounting/internal/pkg/sqlutils"
|
sqltools "github.com/emochka2007/block-accounting/internal/pkg/sqlutils"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -35,6 +36,14 @@ func NewRepository(db *sql.DB) Repository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *repositorySQL) Conn(ctx context.Context) sqltools.DBTX {
|
||||||
|
if tx, ok := ctx.Value(sqltools.TxCtxKey{}).(*sql.Tx); ok {
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
|
||||||
func (r *repositorySQL) Get(ctx context.Context, params GetParams) (*models.User, error) {
|
func (r *repositorySQL) Get(ctx context.Context, params GetParams) (*models.User, error) {
|
||||||
var user *models.User
|
var user *models.User
|
||||||
|
|
||||||
@ -48,17 +57,60 @@ func (r *repositorySQL) Get(ctx context.Context, params GetParams) (*models.User
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *repositorySQL) Create(ctx context.Context, user *models.User) error {
|
func (r *repositorySQL) Create(ctx context.Context, user *models.User) error {
|
||||||
|
if err := sqltools.Transaction(ctx, r.db, func(ctx context.Context) error {
|
||||||
|
query := sq.Insert("users").Columns(
|
||||||
|
"id", "seed", "created_at",
|
||||||
|
).Values(
|
||||||
|
user.ID,
|
||||||
|
user.Bip32Seed,
|
||||||
|
user.CteatedAt,
|
||||||
|
)
|
||||||
|
|
||||||
|
if user.Activated {
|
||||||
|
query = query.Columns("activated_at").Values(user.CteatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := query.RunWith(r.Conn(ctx)).ExecContext(ctx); err != nil {
|
||||||
|
return fmt.Errorf("error insert new user. %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("error execute transactional operation. %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repositorySQL) Activate(ctx context.Context, id string) error {
|
func (r *repositorySQL) Activate(ctx context.Context, id string) error {
|
||||||
|
if err := sqltools.Transaction(ctx, r.db, func(ctx context.Context) error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("error execute transactional operation. %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repositorySQL) Update(ctx context.Context, user *models.User) error {
|
func (r *repositorySQL) Update(ctx context.Context, user *models.User) error {
|
||||||
|
if err := sqltools.Transaction(ctx, r.db, func(ctx context.Context) error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("error execute transactional operation. %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repositorySQL) Delete(ctx context.Context, id string) error {
|
func (r *repositorySQL) Delete(ctx context.Context, id string) error {
|
||||||
|
if err := sqltools.Transaction(ctx, r.db, func(ctx context.Context) error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("error execute transactional operation. %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user