feat(observabillity): #153 instrument sqlx
This commit was merged in pull request #160.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -12,11 +13,11 @@ import (
|
||||
)
|
||||
|
||||
type Account interface {
|
||||
Add(user *types.User, name string) (*types.Account, error)
|
||||
UpdateName(user *types.User, id string, name string) (*types.Account, error)
|
||||
Get(user *types.User, id string) (*types.Account, error)
|
||||
GetAll(user *types.User) ([]*types.Account, error)
|
||||
Delete(user *types.User, id string) error
|
||||
Add(ctx context.Context, user *types.User, name string) (*types.Account, error)
|
||||
UpdateName(ctx context.Context, user *types.User, id string, name string) (*types.Account, error)
|
||||
Get(ctx context.Context, user *types.User, id string) (*types.Account, error)
|
||||
GetAll(ctx context.Context, user *types.User) ([]*types.Account, error)
|
||||
Delete(ctx context.Context, user *types.User, id string) error
|
||||
}
|
||||
|
||||
type AccountImpl struct {
|
||||
@@ -33,7 +34,7 @@ func NewAccount(db *sqlx.DB, random Random, clock Clock) Account {
|
||||
}
|
||||
}
|
||||
|
||||
func (s AccountImpl) Add(user *types.User, name string) (*types.Account, error) {
|
||||
func (s AccountImpl) Add(ctx context.Context, user *types.User, name string) (*types.Account, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -64,7 +65,7 @@ func (s AccountImpl) Add(user *types.User, name string) (*types.Account, error)
|
||||
UpdatedBy: nil,
|
||||
}
|
||||
|
||||
r, err := s.db.NamedExec(`
|
||||
r, err := s.db.NamedExecContext(ctx, `
|
||||
INSERT INTO account (id, user_id, name, current_balance, oink_balance, created_at, created_by)
|
||||
VALUES (:id, :user_id, :name, :current_balance, :oink_balance, :created_at, :created_by)`, account)
|
||||
err = db.TransformAndLogDbError("account Insert", r, err)
|
||||
@@ -75,7 +76,7 @@ func (s AccountImpl) Add(user *types.User, name string) (*types.Account, error)
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func (s AccountImpl) UpdateName(user *types.User, id string, name string) (*types.Account, error) {
|
||||
func (s AccountImpl) UpdateName(ctx context.Context, user *types.User, id string, name string) (*types.Account, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -89,7 +90,7 @@ func (s AccountImpl) UpdateName(user *types.User, id string, name string) (*type
|
||||
return nil, fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("account Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -99,7 +100,7 @@ func (s AccountImpl) UpdateName(user *types.User, id string, name string) (*type
|
||||
}()
|
||||
|
||||
var account types.Account
|
||||
err = tx.Get(&account, `SELECT * FROM account WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = tx.GetContext(ctx, &account, `SELECT * FROM account WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("account Update", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -113,7 +114,7 @@ func (s AccountImpl) UpdateName(user *types.User, id string, name string) (*type
|
||||
account.UpdatedAt = ×tamp
|
||||
account.UpdatedBy = &user.Id
|
||||
|
||||
r, err := tx.NamedExec(`
|
||||
r, err := tx.NamedExecContext(ctx, `
|
||||
UPDATE account
|
||||
SET
|
||||
name = :name,
|
||||
@@ -135,7 +136,7 @@ func (s AccountImpl) UpdateName(user *types.User, id string, name string) (*type
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func (s AccountImpl) Get(user *types.User, id string) (*types.Account, error) {
|
||||
func (s AccountImpl) Get(ctx context.Context, user *types.User, id string) (*types.Account, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -146,7 +147,7 @@ func (s AccountImpl) Get(user *types.User, id string) (*types.Account, error) {
|
||||
}
|
||||
|
||||
var account types.Account
|
||||
err = s.db.Get(&account, `
|
||||
err = s.db.GetContext(ctx, &account, `
|
||||
SELECT * FROM account WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("account Get", nil, err)
|
||||
if err != nil {
|
||||
@@ -157,13 +158,13 @@ func (s AccountImpl) Get(user *types.User, id string) (*types.Account, error) {
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func (s AccountImpl) GetAll(user *types.User) ([]*types.Account, error) {
|
||||
func (s AccountImpl) GetAll(ctx context.Context, user *types.User) ([]*types.Account, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
accounts := make([]*types.Account, 0)
|
||||
err := s.db.Select(&accounts, `
|
||||
err := s.db.SelectContext(ctx, &accounts, `
|
||||
SELECT * FROM account WHERE user_id = ? ORDER BY name`, user.Id)
|
||||
err = db.TransformAndLogDbError("account GetAll", nil, err)
|
||||
if err != nil {
|
||||
@@ -173,7 +174,7 @@ func (s AccountImpl) GetAll(user *types.User) ([]*types.Account, error) {
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (s AccountImpl) Delete(user *types.User, id string) error {
|
||||
func (s AccountImpl) Delete(ctx context.Context, user *types.User, id string) error {
|
||||
if user == nil {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
@@ -183,7 +184,7 @@ func (s AccountImpl) Delete(user *types.User, id string) error {
|
||||
return fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("account Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -193,7 +194,7 @@ func (s AccountImpl) Delete(user *types.User, id string) error {
|
||||
}()
|
||||
|
||||
transactionsCount := 0
|
||||
err = tx.Get(&transactionsCount, `SELECT COUNT(*) FROM "transaction" WHERE user_id = ? AND account_id = ?`, user.Id, uuid)
|
||||
err = tx.GetContext(ctx, &transactionsCount, `SELECT COUNT(*) FROM "transaction" WHERE user_id = ? AND account_id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("account Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -202,7 +203,7 @@ func (s AccountImpl) Delete(user *types.User, id string) error {
|
||||
return fmt.Errorf("account has transactions, cannot delete: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
res, err := tx.Exec("DELETE FROM account WHERE id = ? and user_id = ?", uuid, user.Id)
|
||||
res, err := tx.ExecContext(ctx, "DELETE FROM account WHERE id = ? and user_id = ?", uuid, user.Id)
|
||||
err = db.TransformAndLogDbError("account Delete", res, err)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -26,24 +26,24 @@ var (
|
||||
)
|
||||
|
||||
type Auth interface {
|
||||
SignUp(email string, password string) (*types.User, error)
|
||||
SendVerificationMail(userId uuid.UUID, email string)
|
||||
VerifyUserEmail(token string) error
|
||||
SignUp(ctx context.Context, email string, password string) (*types.User, error)
|
||||
SendVerificationMail(ctx context.Context, userId uuid.UUID, email string)
|
||||
VerifyUserEmail(ctx context.Context, token string) error
|
||||
|
||||
SignIn(session *types.Session, email string, password string) (*types.Session, *types.User, error)
|
||||
SignInSession(sessionId string) (*types.Session, *types.User, error)
|
||||
SignInAnonymous() (*types.Session, error)
|
||||
SignOut(sessionId string) error
|
||||
SignIn(ctx context.Context, session *types.Session, email string, password string) (*types.Session, *types.User, error)
|
||||
SignInSession(ctx context.Context, sessionId string) (*types.Session, *types.User, error)
|
||||
SignInAnonymous(ctx context.Context) (*types.Session, error)
|
||||
SignOut(ctx context.Context, sessionId string) error
|
||||
|
||||
DeleteAccount(user *types.User, currPass string) error
|
||||
DeleteAccount(ctx context.Context, user *types.User, currPass string) error
|
||||
|
||||
ChangePassword(user *types.User, sessionId string, currPass, newPass string) error
|
||||
ChangePassword(ctx context.Context, user *types.User, sessionId string, currPass, newPass string) error
|
||||
|
||||
SendForgotPasswordMail(email string) error
|
||||
ForgotPassword(token string, newPass string) error
|
||||
SendForgotPasswordMail(ctx context.Context, email string) error
|
||||
ForgotPassword(ctx context.Context, token string, newPass string) error
|
||||
|
||||
IsCsrfTokenValid(tokenStr string, sessionId string) bool
|
||||
GetCsrfToken(session *types.Session) (string, error)
|
||||
IsCsrfTokenValid(ctx context.Context, tokenStr string, sessionId string) bool
|
||||
GetCsrfToken(ctx context.Context, session *types.Session) (string, error)
|
||||
}
|
||||
|
||||
type AuthImpl struct {
|
||||
@@ -64,8 +64,8 @@ func NewAuth(db db.Auth, random Random, clock Clock, mail Mail, serverSettings *
|
||||
}
|
||||
}
|
||||
|
||||
func (service AuthImpl) SignIn(session *types.Session, email string, password string) (*types.Session, *types.User, error) {
|
||||
user, err := service.db.GetUserByEmail(email)
|
||||
func (service AuthImpl) SignIn(ctx context.Context, session *types.Session, email string, password string) (*types.Session, *types.User, error) {
|
||||
user, err := service.db.GetUserByEmail(ctx, email)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, nil, ErrInvalidCredentials
|
||||
@@ -80,12 +80,12 @@ func (service AuthImpl) SignIn(session *types.Session, email string, password st
|
||||
return nil, nil, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
err = service.cleanUpSessionWithTokens(session)
|
||||
err = service.cleanUpSessionWithTokens(ctx, session)
|
||||
if err != nil {
|
||||
return nil, nil, types.ErrInternal
|
||||
}
|
||||
|
||||
session, err = service.createSession(user.Id)
|
||||
session, err = service.createSession(ctx, user.Id)
|
||||
if err != nil {
|
||||
return nil, nil, types.ErrInternal
|
||||
}
|
||||
@@ -93,17 +93,17 @@ func (service AuthImpl) SignIn(session *types.Session, email string, password st
|
||||
return session, user, nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) SignInSession(sessionId string) (*types.Session, *types.User, error) {
|
||||
func (service AuthImpl) SignInSession(ctx context.Context, sessionId string) (*types.Session, *types.User, error) {
|
||||
if sessionId == "" {
|
||||
return nil, nil, ErrSessionIdInvalid
|
||||
}
|
||||
|
||||
session, err := service.db.GetSession(sessionId)
|
||||
session, err := service.db.GetSession(ctx, sessionId)
|
||||
if err != nil {
|
||||
return nil, nil, types.ErrInternal
|
||||
}
|
||||
if session.ExpiresAt.Before(service.clock.Now()) {
|
||||
_ = service.db.DeleteSession(sessionId)
|
||||
_ = service.db.DeleteSession(ctx, sessionId)
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ func (service AuthImpl) SignInSession(sessionId string) (*types.Session, *types.
|
||||
return session, nil, nil
|
||||
}
|
||||
|
||||
user, err := service.db.GetUser(session.UserId)
|
||||
user, err := service.db.GetUser(ctx, session.UserId)
|
||||
if err != nil {
|
||||
return nil, nil, types.ErrInternal
|
||||
}
|
||||
@@ -119,8 +119,8 @@ func (service AuthImpl) SignInSession(sessionId string) (*types.Session, *types.
|
||||
return session, user, nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) SignInAnonymous() (*types.Session, error) {
|
||||
session, err := service.createSession(uuid.Nil)
|
||||
func (service AuthImpl) SignInAnonymous(ctx context.Context) (*types.Session, error) {
|
||||
session, err := service.createSession(ctx, uuid.Nil)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -130,7 +130,7 @@ func (service AuthImpl) SignInAnonymous() (*types.Session, error) {
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) SignUp(email string, password string) (*types.User, error) {
|
||||
func (service AuthImpl) SignUp(ctx context.Context, email string, password string) (*types.User, error) {
|
||||
_, err := mail.ParseAddress(email)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidEmail
|
||||
@@ -154,7 +154,7 @@ func (service AuthImpl) SignUp(email string, password string) (*types.User, erro
|
||||
|
||||
user := types.NewUser(userId, email, false, nil, false, hash, salt, service.clock.Now())
|
||||
|
||||
err = service.db.InsertUser(user)
|
||||
err = service.db.InsertUser(ctx, user)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrAlreadyExists) {
|
||||
return nil, ErrAccountExists
|
||||
@@ -166,8 +166,8 @@ func (service AuthImpl) SignUp(email string, password string) (*types.User, erro
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
||||
tokens, err := service.db.GetTokensByUserIdAndType(userId, types.TokenTypeEmailVerify)
|
||||
func (service AuthImpl) SendVerificationMail(ctx context.Context, userId uuid.UUID, email string) {
|
||||
tokens, err := service.db.GetTokensByUserIdAndType(ctx, userId, types.TokenTypeEmailVerify)
|
||||
if err != nil && !errors.Is(err, db.ErrNotFound) {
|
||||
return
|
||||
}
|
||||
@@ -192,7 +192,7 @@ func (service AuthImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
||||
service.clock.Now(),
|
||||
service.clock.Now().Add(24*time.Hour))
|
||||
|
||||
err = service.db.InsertToken(token)
|
||||
err = service.db.InsertToken(ctx, token)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -208,17 +208,17 @@ func (service AuthImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
||||
service.mail.SendMail(email, "Welcome to spend-sparrow", w.String())
|
||||
}
|
||||
|
||||
func (service AuthImpl) VerifyUserEmail(tokenStr string) error {
|
||||
func (service AuthImpl) VerifyUserEmail(ctx context.Context, tokenStr string) error {
|
||||
if tokenStr == "" {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
token, err := service.db.GetToken(tokenStr)
|
||||
token, err := service.db.GetToken(ctx, tokenStr)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
user, err := service.db.GetUser(token.UserId)
|
||||
user, err := service.db.GetUser(ctx, token.UserId)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -236,21 +236,21 @@ func (service AuthImpl) VerifyUserEmail(tokenStr string) error {
|
||||
user.EmailVerified = true
|
||||
user.EmailVerifiedAt = &now
|
||||
|
||||
err = service.db.UpdateUser(user)
|
||||
err = service.db.UpdateUser(ctx, user)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
_ = service.db.DeleteToken(token.Token)
|
||||
_ = service.db.DeleteToken(ctx, token.Token)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) SignOut(sessionId string) error {
|
||||
return service.db.DeleteSession(sessionId)
|
||||
func (service AuthImpl) SignOut(ctx context.Context, sessionId string) error {
|
||||
return service.db.DeleteSession(ctx, sessionId)
|
||||
}
|
||||
|
||||
func (service AuthImpl) DeleteAccount(user *types.User, currPass string) error {
|
||||
userDb, err := service.db.GetUser(user.Id)
|
||||
func (service AuthImpl) DeleteAccount(ctx context.Context, user *types.User, currPass string) error {
|
||||
userDb, err := service.db.GetUser(ctx, user.Id)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -260,7 +260,7 @@ func (service AuthImpl) DeleteAccount(user *types.User, currPass string) error {
|
||||
return ErrInvalidCredentials
|
||||
}
|
||||
|
||||
err = service.db.DeleteUser(user.Id)
|
||||
err = service.db.DeleteUser(ctx, user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -270,7 +270,7 @@ func (service AuthImpl) DeleteAccount(user *types.User, currPass string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) ChangePassword(user *types.User, sessionId string, currPass, newPass string) error {
|
||||
func (service AuthImpl) ChangePassword(ctx context.Context, user *types.User, sessionId string, currPass, newPass string) error {
|
||||
if !isPasswordValid(newPass) {
|
||||
return ErrInvalidPassword
|
||||
}
|
||||
@@ -288,18 +288,18 @@ func (service AuthImpl) ChangePassword(user *types.User, sessionId string, currP
|
||||
newHash := GetHashPassword(newPass, user.Salt)
|
||||
user.Password = newHash
|
||||
|
||||
err := service.db.UpdateUser(user)
|
||||
err := service.db.UpdateUser(ctx, user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sessions, err := service.db.GetSessions(user.Id)
|
||||
sessions, err := service.db.GetSessions(ctx, user.Id)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
for _, s := range sessions {
|
||||
if s.Id != sessionId {
|
||||
err = service.db.DeleteSession(s.Id)
|
||||
err = service.db.DeleteSession(ctx, s.Id)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -309,13 +309,13 @@ func (service AuthImpl) ChangePassword(user *types.User, sessionId string, currP
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) SendForgotPasswordMail(email string) error {
|
||||
func (service AuthImpl) SendForgotPasswordMail(ctx context.Context, email string) error {
|
||||
tokenStr, err := service.random.String(32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := service.db.GetUserByEmail(email)
|
||||
user, err := service.db.GetUserByEmail(ctx, email)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil
|
||||
@@ -332,7 +332,7 @@ func (service AuthImpl) SendForgotPasswordMail(email string) error {
|
||||
service.clock.Now(),
|
||||
service.clock.Now().Add(15*time.Minute))
|
||||
|
||||
err = service.db.InsertToken(token)
|
||||
err = service.db.InsertToken(ctx, token)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -348,17 +348,17 @@ func (service AuthImpl) SendForgotPasswordMail(email string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||
func (service AuthImpl) ForgotPassword(ctx context.Context, tokenStr string, newPass string) error {
|
||||
if !isPasswordValid(newPass) {
|
||||
return ErrInvalidPassword
|
||||
}
|
||||
|
||||
token, err := service.db.GetToken(tokenStr)
|
||||
token, err := service.db.GetToken(ctx, tokenStr)
|
||||
if err != nil {
|
||||
return ErrTokenInvalid
|
||||
}
|
||||
|
||||
err = service.db.DeleteToken(tokenStr)
|
||||
err = service.db.DeleteToken(ctx, tokenStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -368,7 +368,7 @@ func (service AuthImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||
return ErrTokenInvalid
|
||||
}
|
||||
|
||||
user, err := service.db.GetUser(token.UserId)
|
||||
user, err := service.db.GetUser(ctx, token.UserId)
|
||||
if err != nil {
|
||||
slog.Error("Could not get user from token", "err", err)
|
||||
return types.ErrInternal
|
||||
@@ -377,18 +377,18 @@ func (service AuthImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||
passHash := GetHashPassword(newPass, user.Salt)
|
||||
|
||||
user.Password = passHash
|
||||
err = service.db.UpdateUser(user)
|
||||
err = service.db.UpdateUser(ctx, user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sessions, err := service.db.GetSessions(user.Id)
|
||||
sessions, err := service.db.GetSessions(ctx, user.Id)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
for _, session := range sessions {
|
||||
err = service.db.DeleteSession(session.Id)
|
||||
err = service.db.DeleteSession(ctx, session.Id)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -397,8 +397,8 @@ func (service AuthImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) IsCsrfTokenValid(tokenStr string, sessionId string) bool {
|
||||
token, err := service.db.GetToken(tokenStr)
|
||||
func (service AuthImpl) IsCsrfTokenValid(ctx context.Context, tokenStr string, sessionId string) bool {
|
||||
token, err := service.db.GetToken(ctx, tokenStr)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -412,12 +412,12 @@ func (service AuthImpl) IsCsrfTokenValid(tokenStr string, sessionId string) bool
|
||||
return true
|
||||
}
|
||||
|
||||
func (service AuthImpl) GetCsrfToken(session *types.Session) (string, error) {
|
||||
func (service AuthImpl) GetCsrfToken(ctx context.Context, session *types.Session) (string, error) {
|
||||
if session == nil {
|
||||
return "", types.ErrInternal
|
||||
}
|
||||
|
||||
tokens, _ := service.db.GetTokensBySessionIdAndType(session.Id, types.TokenTypeCsrf)
|
||||
tokens, _ := service.db.GetTokensBySessionIdAndType(ctx, session.Id, types.TokenTypeCsrf)
|
||||
|
||||
if len(tokens) > 0 {
|
||||
return tokens[0].Token, nil
|
||||
@@ -435,7 +435,7 @@ func (service AuthImpl) GetCsrfToken(session *types.Session) (string, error) {
|
||||
types.TokenTypeCsrf,
|
||||
service.clock.Now(),
|
||||
service.clock.Now().Add(8*time.Hour))
|
||||
err = service.db.InsertToken(token)
|
||||
err = service.db.InsertToken(ctx, token)
|
||||
if err != nil {
|
||||
return "", types.ErrInternal
|
||||
}
|
||||
@@ -445,22 +445,22 @@ func (service AuthImpl) GetCsrfToken(session *types.Session) (string, error) {
|
||||
return tokenStr, nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) cleanUpSessionWithTokens(session *types.Session) error {
|
||||
func (service AuthImpl) cleanUpSessionWithTokens(ctx context.Context, session *types.Session) error {
|
||||
if session == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := service.db.DeleteSession(session.Id)
|
||||
err := service.db.DeleteSession(ctx, session.Id)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
tokens, err := service.db.GetTokensBySessionIdAndType(session.Id, types.TokenTypeCsrf)
|
||||
tokens, err := service.db.GetTokensBySessionIdAndType(ctx, session.Id, types.TokenTypeCsrf)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
for _, token := range tokens {
|
||||
err = service.db.DeleteToken(token.Token)
|
||||
err = service.db.DeleteToken(ctx, token.Token)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -469,13 +469,13 @@ func (service AuthImpl) cleanUpSessionWithTokens(session *types.Session) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) createSession(userId uuid.UUID) (*types.Session, error) {
|
||||
func (service AuthImpl) createSession(ctx context.Context, userId uuid.UUID) (*types.Session, error) {
|
||||
sessionId, err := service.random.String(32)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
err = service.db.DeleteOldSessions(userId)
|
||||
err = service.db.DeleteOldSessions(ctx, userId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -485,7 +485,7 @@ func (service AuthImpl) createSession(userId uuid.UUID) (*types.Session, error)
|
||||
|
||||
session := types.NewSession(sessionId, userId, createAt, expiresAt)
|
||||
|
||||
err = service.db.InsertSession(session)
|
||||
err = service.db.InsertSession(ctx, session)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -13,13 +14,13 @@ import (
|
||||
)
|
||||
|
||||
type Transaction interface {
|
||||
Add(tx *sqlx.Tx, user *types.User, transaction types.Transaction) (*types.Transaction, error)
|
||||
Update(user *types.User, transaction types.Transaction) (*types.Transaction, error)
|
||||
Get(user *types.User, id string) (*types.Transaction, error)
|
||||
GetAll(user *types.User, filter types.TransactionItemsFilter) ([]*types.Transaction, error)
|
||||
Delete(user *types.User, id string) error
|
||||
Add(ctx context.Context, tx *sqlx.Tx, user *types.User, transaction types.Transaction) (*types.Transaction, error)
|
||||
Update(ctx context.Context, user *types.User, transaction types.Transaction) (*types.Transaction, error)
|
||||
Get(ctx context.Context, user *types.User, id string) (*types.Transaction, error)
|
||||
GetAll(ctx context.Context, user *types.User, filter types.TransactionItemsFilter) ([]*types.Transaction, error)
|
||||
Delete(ctx context.Context, user *types.User, id string) error
|
||||
|
||||
RecalculateBalances(user *types.User) error
|
||||
RecalculateBalances(ctx context.Context, user *types.User) error
|
||||
}
|
||||
|
||||
type TransactionImpl struct {
|
||||
@@ -36,7 +37,7 @@ func NewTransaction(db *sqlx.DB, random Random, clock Clock) Transaction {
|
||||
}
|
||||
}
|
||||
|
||||
func (s TransactionImpl) Add(tx *sqlx.Tx, user *types.User, transactionInput types.Transaction) (*types.Transaction, error) {
|
||||
func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *types.User, transactionInput types.Transaction) (*types.Transaction, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -45,7 +46,7 @@ func (s TransactionImpl) Add(tx *sqlx.Tx, user *types.User, transactionInput typ
|
||||
ownsTransaction := false
|
||||
if tx == nil {
|
||||
ownsTransaction = true
|
||||
tx, err = s.db.Beginx()
|
||||
tx, err = s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transaction Add", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -55,12 +56,12 @@ func (s TransactionImpl) Add(tx *sqlx.Tx, user *types.User, transactionInput typ
|
||||
}()
|
||||
}
|
||||
|
||||
transaction, err := s.validateAndEnrichTransaction(tx, nil, user.Id, transactionInput)
|
||||
transaction, err := s.validateAndEnrichTransaction(ctx, tx, nil, user.Id, transactionInput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r, err := tx.NamedExec(`
|
||||
r, err := tx.NamedExecContext(ctx, `
|
||||
INSERT INTO "transaction" (id, user_id, account_id, treasure_chest_id, value, timestamp,
|
||||
party, description, error, created_at, created_by)
|
||||
VALUES (:id, :user_id, :account_id, :treasure_chest_id, :value, :timestamp,
|
||||
@@ -71,8 +72,8 @@ func (s TransactionImpl) Add(tx *sqlx.Tx, user *types.User, transactionInput typ
|
||||
}
|
||||
|
||||
if transaction.Error == nil && transaction.AccountId != nil {
|
||||
r, err = tx.Exec(`
|
||||
UPDATE account
|
||||
r, err = tx.ExecContext(ctx, `
|
||||
UPDATE actx context.Context,ccount
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Add", r, err)
|
||||
@@ -82,7 +83,7 @@ func (s TransactionImpl) Add(tx *sqlx.Tx, user *types.User, transactionInput typ
|
||||
}
|
||||
|
||||
if transaction.Error == nil && transaction.TreasureChestId != nil {
|
||||
r, err = tx.Exec(`
|
||||
r, err = tx.ExecContext(ctx, `
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
@@ -103,12 +104,12 @@ func (s TransactionImpl) Add(tx *sqlx.Tx, user *types.User, transactionInput typ
|
||||
return transaction, nil
|
||||
}
|
||||
|
||||
func (s TransactionImpl) Update(user *types.User, input types.Transaction) (*types.Transaction, error) {
|
||||
func (s TransactionImpl) Update(ctx context.Context, user *types.User, input types.Transaction) (*types.Transaction, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transaction Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -118,7 +119,7 @@ func (s TransactionImpl) Update(user *types.User, input types.Transaction) (*typ
|
||||
}()
|
||||
|
||||
transaction := &types.Transaction{}
|
||||
err = tx.Get(transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, input.Id)
|
||||
err = tx.GetContext(ctx, transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, input.Id)
|
||||
err = db.TransformAndLogDbError("transaction Update", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -128,7 +129,7 @@ func (s TransactionImpl) Update(user *types.User, input types.Transaction) (*typ
|
||||
}
|
||||
|
||||
if transaction.Error == nil && transaction.AccountId != nil {
|
||||
r, err := tx.Exec(`
|
||||
r, err := tx.ExecContext(ctx, `
|
||||
UPDATE account
|
||||
SET current_balance = current_balance - ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
@@ -138,7 +139,7 @@ func (s TransactionImpl) Update(user *types.User, input types.Transaction) (*typ
|
||||
}
|
||||
}
|
||||
if transaction.Error == nil && transaction.TreasureChestId != nil {
|
||||
r, err := tx.Exec(`
|
||||
r, err := tx.ExecContext(ctx, `
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance - ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
@@ -148,13 +149,13 @@ func (s TransactionImpl) Update(user *types.User, input types.Transaction) (*typ
|
||||
}
|
||||
}
|
||||
|
||||
transaction, err = s.validateAndEnrichTransaction(tx, transaction, user.Id, input)
|
||||
transaction, err = s.validateAndEnrichTransaction(ctx, tx, transaction, user.Id, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if transaction.Error == nil && transaction.AccountId != nil {
|
||||
r, err := tx.Exec(`
|
||||
r, err := tx.ExecContext(ctx, `
|
||||
UPDATE account
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
@@ -164,7 +165,7 @@ func (s TransactionImpl) Update(user *types.User, input types.Transaction) (*typ
|
||||
}
|
||||
}
|
||||
if transaction.Error == nil && transaction.TreasureChestId != nil {
|
||||
r, err := tx.Exec(`
|
||||
r, err := tx.ExecContext(ctx, `
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
@@ -174,7 +175,7 @@ func (s TransactionImpl) Update(user *types.User, input types.Transaction) (*typ
|
||||
}
|
||||
}
|
||||
|
||||
r, err := tx.NamedExec(`
|
||||
r, err := tx.NamedExecContext(ctx, `
|
||||
UPDATE "transaction"
|
||||
SET
|
||||
account_id = :account_id,
|
||||
@@ -202,7 +203,7 @@ func (s TransactionImpl) Update(user *types.User, input types.Transaction) (*typ
|
||||
return transaction, nil
|
||||
}
|
||||
|
||||
func (s TransactionImpl) Get(user *types.User, id string) (*types.Transaction, error) {
|
||||
func (s TransactionImpl) Get(ctx context.Context, user *types.User, id string) (*types.Transaction, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -213,7 +214,7 @@ func (s TransactionImpl) Get(user *types.User, id string) (*types.Transaction, e
|
||||
}
|
||||
|
||||
var transaction types.Transaction
|
||||
err = s.db.Get(&transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = s.db.GetContext(ctx, &transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("transaction Get", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -225,13 +226,13 @@ func (s TransactionImpl) Get(user *types.User, id string) (*types.Transaction, e
|
||||
return &transaction, nil
|
||||
}
|
||||
|
||||
func (s TransactionImpl) GetAll(user *types.User, filter types.TransactionItemsFilter) ([]*types.Transaction, error) {
|
||||
func (s TransactionImpl) GetAll(ctx context.Context, user *types.User, filter types.TransactionItemsFilter) ([]*types.Transaction, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
transactions := make([]*types.Transaction, 0)
|
||||
err := s.db.Select(&transactions, `
|
||||
err := s.db.SelectContext(ctx, &transactions, `
|
||||
SELECT *
|
||||
FROM "transaction"
|
||||
WHERE user_id = ?
|
||||
@@ -254,7 +255,7 @@ func (s TransactionImpl) GetAll(user *types.User, filter types.TransactionItemsF
|
||||
return transactions, nil
|
||||
}
|
||||
|
||||
func (s TransactionImpl) Delete(user *types.User, id string) error {
|
||||
func (s TransactionImpl) Delete(ctx context.Context, user *types.User, id string) error {
|
||||
if user == nil {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
@@ -264,7 +265,7 @@ func (s TransactionImpl) Delete(user *types.User, id string) error {
|
||||
return fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transaction Delete", nil, err)
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -274,14 +275,14 @@ func (s TransactionImpl) Delete(user *types.User, id string) error {
|
||||
}()
|
||||
|
||||
var transaction types.Transaction
|
||||
err = tx.Get(&transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = tx.GetContext(ctx, &transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("transaction Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if transaction.Error == nil && transaction.AccountId != nil {
|
||||
r, err := tx.Exec(`
|
||||
r, err := tx.ExecContext(ctx, `
|
||||
UPDATE account
|
||||
SET current_balance = current_balance - ?
|
||||
WHERE id = ?
|
||||
@@ -293,7 +294,7 @@ func (s TransactionImpl) Delete(user *types.User, id string) error {
|
||||
}
|
||||
|
||||
if transaction.Error == nil && transaction.TreasureChestId != nil {
|
||||
r, err := tx.Exec(`
|
||||
r, err := tx.ExecContext(ctx, `
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance - ?
|
||||
WHERE id = ?
|
||||
@@ -304,7 +305,7 @@ func (s TransactionImpl) Delete(user *types.User, id string) error {
|
||||
}
|
||||
}
|
||||
|
||||
r, err := tx.Exec("DELETE FROM \"transaction\" WHERE id = ? AND user_id = ?", uuid, user.Id)
|
||||
r, err := tx.ExecContext(ctx, "DELETE FROM \"transaction\" WHERE id = ? AND user_id = ?", uuid, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Delete", r, err)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -319,12 +320,12 @@ func (s TransactionImpl) Delete(user *types.User, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s TransactionImpl) RecalculateBalances(user *types.User) error {
|
||||
func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.User) error {
|
||||
if user == nil {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -333,7 +334,7 @@ func (s TransactionImpl) RecalculateBalances(user *types.User) error {
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
|
||||
r, err := tx.Exec(`
|
||||
r, err := tx.ExecContext(ctx, `
|
||||
UPDATE account
|
||||
SET current_balance = 0
|
||||
WHERE user_id = ?`, user.Id)
|
||||
@@ -342,7 +343,7 @@ func (s TransactionImpl) RecalculateBalances(user *types.User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
r, err = tx.Exec(`
|
||||
r, err = tx.ExecContext(ctx, `
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = 0
|
||||
WHERE user_id = ?`, user.Id)
|
||||
@@ -351,7 +352,7 @@ func (s TransactionImpl) RecalculateBalances(user *types.User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, err := tx.Queryx(`
|
||||
rows, err := tx.QueryxContext(ctx, `
|
||||
SELECT *
|
||||
FROM "transaction"
|
||||
WHERE user_id = ?`, user.Id)
|
||||
@@ -375,7 +376,7 @@ func (s TransactionImpl) RecalculateBalances(user *types.User) error {
|
||||
}
|
||||
|
||||
s.updateErrors(&transaction)
|
||||
r, err = tx.Exec(`
|
||||
r, err = tx.ExecContext(ctx, `
|
||||
UPDATE "transaction"
|
||||
SET error = ?
|
||||
WHERE user_id = ?
|
||||
@@ -390,7 +391,7 @@ func (s TransactionImpl) RecalculateBalances(user *types.User) error {
|
||||
}
|
||||
|
||||
if transaction.AccountId != nil {
|
||||
r, err = tx.Exec(`
|
||||
r, err = tx.ExecContext(ctx, `
|
||||
UPDATE account
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
@@ -400,7 +401,7 @@ func (s TransactionImpl) RecalculateBalances(user *types.User) error {
|
||||
}
|
||||
}
|
||||
if transaction.TreasureChestId != nil {
|
||||
r, err = tx.Exec(`
|
||||
r, err = tx.ExecContext(ctx, `
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
@@ -420,7 +421,7 @@ func (s TransactionImpl) RecalculateBalances(user *types.User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s TransactionImpl) validateAndEnrichTransaction(tx *sqlx.Tx, oldTransaction *types.Transaction, userId uuid.UUID, input types.Transaction) (*types.Transaction, error) {
|
||||
func (s TransactionImpl) validateAndEnrichTransaction(ctx context.Context, tx *sqlx.Tx, oldTransaction *types.Transaction, userId uuid.UUID, input types.Transaction) (*types.Transaction, error) {
|
||||
var (
|
||||
id uuid.UUID
|
||||
createdAt time.Time
|
||||
@@ -449,7 +450,7 @@ func (s TransactionImpl) validateAndEnrichTransaction(tx *sqlx.Tx, oldTransactio
|
||||
}
|
||||
|
||||
if input.AccountId != nil {
|
||||
err = tx.Get(&rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, input.AccountId, userId)
|
||||
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, input.AccountId, userId)
|
||||
err = db.TransformAndLogDbError("transaction validate", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -462,7 +463,7 @@ func (s TransactionImpl) validateAndEnrichTransaction(tx *sqlx.Tx, oldTransactio
|
||||
|
||||
if input.TreasureChestId != nil {
|
||||
var treasureChest types.TreasureChest
|
||||
err = tx.Get(&treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, input.TreasureChestId, userId)
|
||||
err = tx.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, input.TreasureChestId, userId)
|
||||
err = db.TransformAndLogDbError("transaction validate", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -15,14 +16,14 @@ import (
|
||||
)
|
||||
|
||||
type TransactionRecurring interface {
|
||||
Add(user *types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error)
|
||||
Update(user *types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error)
|
||||
GetAll(user *types.User) ([]*types.TransactionRecurring, error)
|
||||
GetAllByAccount(user *types.User, accountId string) ([]*types.TransactionRecurring, error)
|
||||
GetAllByTreasureChest(user *types.User, treasureChestId string) ([]*types.TransactionRecurring, error)
|
||||
Delete(user *types.User, id string) error
|
||||
Add(ctx context.Context, user *types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error)
|
||||
Update(ctx context.Context, user *types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error)
|
||||
GetAll(ctx context.Context, user *types.User) ([]*types.TransactionRecurring, error)
|
||||
GetAllByAccount(ctx context.Context, user *types.User, accountId string) ([]*types.TransactionRecurring, error)
|
||||
GetAllByTreasureChest(ctx context.Context, user *types.User, treasureChestId string) ([]*types.TransactionRecurring, error)
|
||||
Delete(ctx context.Context, user *types.User, id string) error
|
||||
|
||||
GenerateTransactions(user *types.User) error
|
||||
GenerateTransactions(ctx context.Context, user *types.User) error
|
||||
}
|
||||
|
||||
type TransactionRecurringImpl struct {
|
||||
@@ -41,7 +42,7 @@ func NewTransactionRecurring(db *sqlx.DB, random Random, clock Clock, transactio
|
||||
}
|
||||
}
|
||||
|
||||
func (s TransactionRecurringImpl) Add(
|
||||
func (s TransactionRecurringImpl) Add(ctx context.Context,
|
||||
user *types.User,
|
||||
transactionRecurringInput types.TransactionRecurringInput,
|
||||
) (*types.TransactionRecurring, error) {
|
||||
@@ -49,7 +50,7 @@ func (s TransactionRecurringImpl) Add(
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transactionRecurring Add", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -58,12 +59,12 @@ func (s TransactionRecurringImpl) Add(
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
|
||||
transactionRecurring, err := s.validateAndEnrichTransactionRecurring(tx, nil, user.Id, transactionRecurringInput)
|
||||
transactionRecurring, err := s.validateAndEnrichTransactionRecurring(ctx, tx, nil, user.Id, transactionRecurringInput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r, err := tx.NamedExec(`
|
||||
r, err := tx.NamedExecContext(ctx, `
|
||||
INSERT INTO "transaction_recurring" (id, user_id, interval_months,
|
||||
next_execution, party, description, account_id, treasure_chest_id, value, created_at, created_by)
|
||||
VALUES (:id, :user_id, :interval_months,
|
||||
@@ -83,7 +84,7 @@ func (s TransactionRecurringImpl) Add(
|
||||
return transactionRecurring, nil
|
||||
}
|
||||
|
||||
func (s TransactionRecurringImpl) Update(
|
||||
func (s TransactionRecurringImpl) Update(ctx context.Context,
|
||||
user *types.User,
|
||||
input types.TransactionRecurringInput,
|
||||
) (*types.TransactionRecurring, error) {
|
||||
@@ -96,7 +97,7 @@ func (s TransactionRecurringImpl) Update(
|
||||
return nil, fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transactionRecurring Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -106,7 +107,7 @@ func (s TransactionRecurringImpl) Update(
|
||||
}()
|
||||
|
||||
transactionRecurring := &types.TransactionRecurring{}
|
||||
err = tx.Get(transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = tx.GetContext(ctx, transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("transactionRecurring Update", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -115,12 +116,12 @@ func (s TransactionRecurringImpl) Update(
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
transactionRecurring, err = s.validateAndEnrichTransactionRecurring(tx, transactionRecurring, user.Id, input)
|
||||
transactionRecurring, err = s.validateAndEnrichTransactionRecurring(ctx, tx, transactionRecurring, user.Id, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r, err := tx.NamedExec(`
|
||||
r, err := tx.NamedExecContext(ctx, `
|
||||
UPDATE transaction_recurring
|
||||
SET
|
||||
interval_months = :interval_months,
|
||||
@@ -148,13 +149,13 @@ func (s TransactionRecurringImpl) Update(
|
||||
return transactionRecurring, nil
|
||||
}
|
||||
|
||||
func (s TransactionRecurringImpl) GetAll(user *types.User) ([]*types.TransactionRecurring, error) {
|
||||
func (s TransactionRecurringImpl) GetAll(ctx context.Context, user *types.User) ([]*types.TransactionRecurring, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
transactionRecurrings := make([]*types.TransactionRecurring, 0)
|
||||
err := s.db.Select(&transactionRecurrings, `
|
||||
err := s.db.SelectContext(ctx, &transactionRecurrings, `
|
||||
SELECT *
|
||||
FROM transaction_recurring
|
||||
WHERE user_id = ?
|
||||
@@ -168,7 +169,7 @@ func (s TransactionRecurringImpl) GetAll(user *types.User) ([]*types.Transaction
|
||||
return transactionRecurrings, nil
|
||||
}
|
||||
|
||||
func (s TransactionRecurringImpl) GetAllByAccount(user *types.User, accountId string) ([]*types.TransactionRecurring, error) {
|
||||
func (s TransactionRecurringImpl) GetAllByAccount(ctx context.Context, user *types.User, accountId string) ([]*types.TransactionRecurring, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -179,7 +180,7 @@ func (s TransactionRecurringImpl) GetAllByAccount(user *types.User, accountId st
|
||||
return nil, fmt.Errorf("could not parse accountId: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transactionRecurring GetAllByAccount", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -189,7 +190,7 @@ func (s TransactionRecurringImpl) GetAllByAccount(user *types.User, accountId st
|
||||
}()
|
||||
|
||||
var rowCount int
|
||||
err = tx.Get(&rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, user.Id)
|
||||
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, user.Id)
|
||||
err = db.TransformAndLogDbError("transactionRecurring GetAllByAccount", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -199,7 +200,7 @@ func (s TransactionRecurringImpl) GetAllByAccount(user *types.User, accountId st
|
||||
}
|
||||
|
||||
transactionRecurrings := make([]*types.TransactionRecurring, 0)
|
||||
err = tx.Select(&transactionRecurrings, `
|
||||
err = tx.SelectContext(ctx, &transactionRecurrings, `
|
||||
SELECT *
|
||||
FROM transaction_recurring
|
||||
WHERE user_id = ?
|
||||
@@ -220,7 +221,7 @@ func (s TransactionRecurringImpl) GetAllByAccount(user *types.User, accountId st
|
||||
return transactionRecurrings, nil
|
||||
}
|
||||
|
||||
func (s TransactionRecurringImpl) GetAllByTreasureChest(
|
||||
func (s TransactionRecurringImpl) GetAllByTreasureChest(ctx context.Context,
|
||||
user *types.User,
|
||||
treasureChestId string,
|
||||
) ([]*types.TransactionRecurring, error) {
|
||||
@@ -234,7 +235,7 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(
|
||||
return nil, fmt.Errorf("could not parse treasureChestId: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transactionRecurring GetAllByTreasureChest", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -244,7 +245,7 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(
|
||||
}()
|
||||
|
||||
var rowCount int
|
||||
err = tx.Get(&rowCount, `SELECT COUNT(*) FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestId, user.Id)
|
||||
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestId, user.Id)
|
||||
err = db.TransformAndLogDbError("transactionRecurring GetAllByTreasureChest", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -254,7 +255,7 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(
|
||||
}
|
||||
|
||||
transactionRecurrings := make([]*types.TransactionRecurring, 0)
|
||||
err = tx.Select(&transactionRecurrings, `
|
||||
err = tx.SelectContext(ctx, &transactionRecurrings, `
|
||||
SELECT *
|
||||
FROM transaction_recurring
|
||||
WHERE user_id = ?
|
||||
@@ -275,7 +276,7 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(
|
||||
return transactionRecurrings, nil
|
||||
}
|
||||
|
||||
func (s TransactionRecurringImpl) Delete(user *types.User, id string) error {
|
||||
func (s TransactionRecurringImpl) Delete(ctx context.Context, user *types.User, id string) error {
|
||||
if user == nil {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
@@ -285,7 +286,7 @@ func (s TransactionRecurringImpl) Delete(user *types.User, id string) error {
|
||||
return fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transactionRecurring Delete", nil, err)
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -295,13 +296,13 @@ func (s TransactionRecurringImpl) Delete(user *types.User, id string) error {
|
||||
}()
|
||||
|
||||
var transactionRecurring types.TransactionRecurring
|
||||
err = tx.Get(&transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = tx.GetContext(ctx, &transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("transactionRecurring Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r, err := tx.Exec("DELETE FROM transaction_recurring WHERE id = ? AND user_id = ?", uuid, user.Id)
|
||||
r, err := tx.ExecContext(ctx, "DELETE FROM transaction_recurring WHERE id = ? AND user_id = ?", uuid, user.Id)
|
||||
err = db.TransformAndLogDbError("transactionRecurring Delete", r, err)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -316,13 +317,13 @@ func (s TransactionRecurringImpl) Delete(user *types.User, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s TransactionRecurringImpl) GenerateTransactions(user *types.User) error {
|
||||
func (s TransactionRecurringImpl) GenerateTransactions(ctx context.Context, user *types.User) error {
|
||||
if user == nil {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
now := s.clock.Now()
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transactionRecurring GenerateTransactions", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -332,7 +333,7 @@ func (s TransactionRecurringImpl) GenerateTransactions(user *types.User) error {
|
||||
}()
|
||||
|
||||
recurringTransactions := make([]*types.TransactionRecurring, 0)
|
||||
err = tx.Select(&recurringTransactions, `
|
||||
err = tx.SelectContext(ctx, &recurringTransactions, `
|
||||
SELECT * FROM transaction_recurring WHERE user_id = ? AND next_execution <= ?`,
|
||||
user.Id, now)
|
||||
err = db.TransformAndLogDbError("transactionRecurring GenerateTransactions", nil, err)
|
||||
@@ -350,13 +351,13 @@ func (s TransactionRecurringImpl) GenerateTransactions(user *types.User) error {
|
||||
Value: transactionRecurring.Value,
|
||||
}
|
||||
|
||||
_, err = s.transaction.Add(tx, user, transaction)
|
||||
_, err = s.transaction.Add(ctx, tx, user, transaction)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
nextExecution := transactionRecurring.NextExecution.AddDate(0, int(transactionRecurring.IntervalMonths), 0)
|
||||
r, err := tx.Exec(`UPDATE transaction_recurring SET next_execution = ? WHERE id = ? AND user_id = ?`,
|
||||
r, err := tx.ExecContext(ctx, `UPDATE transaction_recurring SET next_execution = ? WHERE id = ? AND user_id = ?`,
|
||||
nextExecution, transactionRecurring.Id, user.Id)
|
||||
err = db.TransformAndLogDbError("transactionRecurring GenerateTransactions", r, err)
|
||||
if err != nil {
|
||||
@@ -373,6 +374,7 @@ func (s TransactionRecurringImpl) GenerateTransactions(user *types.User) error {
|
||||
}
|
||||
|
||||
func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring(
|
||||
ctx context.Context,
|
||||
tx *sqlx.Tx,
|
||||
oldTransactionRecurring *types.TransactionRecurring,
|
||||
userId uuid.UUID,
|
||||
@@ -417,7 +419,7 @@ func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring(
|
||||
return nil, fmt.Errorf("could not parse accountId: %w", ErrBadRequest)
|
||||
}
|
||||
accountUuid = &temp
|
||||
err = tx.Get(&rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, userId)
|
||||
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, userId)
|
||||
err = db.TransformAndLogDbError("transactionRecurring validate", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -438,7 +440,7 @@ func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring(
|
||||
}
|
||||
treasureChestUuid = &temp
|
||||
var treasureChest types.TreasureChest
|
||||
err = tx.Get(&treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestUuid, userId)
|
||||
err = tx.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestUuid, userId)
|
||||
err = db.TransformAndLogDbError("transactionRecurring validate", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -13,11 +14,11 @@ import (
|
||||
)
|
||||
|
||||
type TreasureChest interface {
|
||||
Add(user *types.User, parentId, name string) (*types.TreasureChest, error)
|
||||
Update(user *types.User, id, parentId, name string) (*types.TreasureChest, error)
|
||||
Get(user *types.User, id string) (*types.TreasureChest, error)
|
||||
GetAll(user *types.User) ([]*types.TreasureChest, error)
|
||||
Delete(user *types.User, id string) error
|
||||
Add(ctx context.Context, user *types.User, parentId, name string) (*types.TreasureChest, error)
|
||||
Update(ctx context.Context, user *types.User, id, parentId, name string) (*types.TreasureChest, error)
|
||||
Get(ctx context.Context, user *types.User, id string) (*types.TreasureChest, error)
|
||||
GetAll(ctx context.Context, user *types.User) ([]*types.TreasureChest, error)
|
||||
Delete(ctx context.Context, user *types.User, id string) error
|
||||
}
|
||||
|
||||
type TreasureChestImpl struct {
|
||||
@@ -34,7 +35,7 @@ func NewTreasureChest(db *sqlx.DB, random Random, clock Clock) TreasureChest {
|
||||
}
|
||||
}
|
||||
|
||||
func (s TreasureChestImpl) Add(user *types.User, parentId, name string) (*types.TreasureChest, error) {
|
||||
func (s TreasureChestImpl) Add(ctx context.Context, user *types.User, parentId, name string) (*types.TreasureChest, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -51,7 +52,7 @@ func (s TreasureChestImpl) Add(user *types.User, parentId, name string) (*types.
|
||||
|
||||
var parentUuid *uuid.UUID
|
||||
if parentId != "" {
|
||||
parent, err := s.Get(user, parentId)
|
||||
parent, err := s.Get(ctx, user, parentId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -76,7 +77,7 @@ func (s TreasureChestImpl) Add(user *types.User, parentId, name string) (*types.
|
||||
UpdatedBy: nil,
|
||||
}
|
||||
|
||||
r, err := s.db.NamedExec(`
|
||||
r, err := s.db.NamedExecContext(ctx, `
|
||||
INSERT INTO treasure_chest (id, parent_id, user_id, name, current_balance, created_at, created_by)
|
||||
VALUES (:id, :parent_id, :user_id, :name, :current_balance, :created_at, :created_by)`, treasureChest)
|
||||
err = db.TransformAndLogDbError("treasureChest Insert", r, err)
|
||||
@@ -87,7 +88,7 @@ func (s TreasureChestImpl) Add(user *types.User, parentId, name string) (*types.
|
||||
return treasureChest, nil
|
||||
}
|
||||
|
||||
func (s TreasureChestImpl) Update(user *types.User, idStr, parentId, name string) (*types.TreasureChest, error) {
|
||||
func (s TreasureChestImpl) Update(ctx context.Context, user *types.User, idStr, parentId, name string) (*types.TreasureChest, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -101,7 +102,7 @@ func (s TreasureChestImpl) Update(user *types.User, idStr, parentId, name string
|
||||
return nil, fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("treasureChest Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -111,7 +112,7 @@ func (s TreasureChestImpl) Update(user *types.User, idStr, parentId, name string
|
||||
}()
|
||||
|
||||
treasureChest := &types.TreasureChest{}
|
||||
err = tx.Get(treasureChest, `SELECT * FROM treasure_chest WHERE user_id = ? AND id = ?`, user.Id, id)
|
||||
err = tx.GetContext(ctx, treasureChest, `SELECT * FROM treasure_chest WHERE user_id = ? AND id = ?`, user.Id, id)
|
||||
err = db.TransformAndLogDbError("treasureChest Update", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -122,12 +123,12 @@ func (s TreasureChestImpl) Update(user *types.User, idStr, parentId, name string
|
||||
|
||||
var parentUuid *uuid.UUID
|
||||
if parentId != "" {
|
||||
parent, err := s.Get(user, parentId)
|
||||
parent, err := s.Get(ctx, user, parentId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var childCount int
|
||||
err = tx.Get(&childCount, `SELECT COUNT(*) FROM treasure_chest WHERE user_id = ? AND parent_id = ?`, user.Id, id)
|
||||
err = tx.GetContext(ctx, &childCount, `SELECT COUNT(*) FROM treasure_chest WHERE user_id = ? AND parent_id = ?`, user.Id, id)
|
||||
err = db.TransformAndLogDbError("treasureChest Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -145,7 +146,7 @@ func (s TreasureChestImpl) Update(user *types.User, idStr, parentId, name string
|
||||
treasureChest.UpdatedAt = ×tamp
|
||||
treasureChest.UpdatedBy = &user.Id
|
||||
|
||||
r, err := tx.NamedExec(`
|
||||
r, err := tx.NamedExecContext(ctx, `
|
||||
UPDATE treasure_chest
|
||||
SET
|
||||
parent_id = :parent_id,
|
||||
@@ -169,7 +170,7 @@ func (s TreasureChestImpl) Update(user *types.User, idStr, parentId, name string
|
||||
return treasureChest, nil
|
||||
}
|
||||
|
||||
func (s TreasureChestImpl) Get(user *types.User, id string) (*types.TreasureChest, error) {
|
||||
func (s TreasureChestImpl) Get(ctx context.Context, user *types.User, id string) (*types.TreasureChest, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
@@ -180,7 +181,7 @@ func (s TreasureChestImpl) Get(user *types.User, id string) (*types.TreasureChes
|
||||
}
|
||||
|
||||
var treasureChest types.TreasureChest
|
||||
err = s.db.Get(&treasureChest, `SELECT * FROM treasure_chest WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = s.db.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("treasureChest Get", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -192,13 +193,13 @@ func (s TreasureChestImpl) Get(user *types.User, id string) (*types.TreasureChes
|
||||
return &treasureChest, nil
|
||||
}
|
||||
|
||||
func (s TreasureChestImpl) GetAll(user *types.User) ([]*types.TreasureChest, error) {
|
||||
func (s TreasureChestImpl) GetAll(ctx context.Context, user *types.User) ([]*types.TreasureChest, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
treasureChests := make([]*types.TreasureChest, 0)
|
||||
err := s.db.Select(&treasureChests, `SELECT * FROM treasure_chest WHERE user_id = ?`, user.Id)
|
||||
err := s.db.SelectContext(ctx, &treasureChests, `SELECT * FROM treasure_chest WHERE user_id = ?`, user.Id)
|
||||
err = db.TransformAndLogDbError("treasureChest GetAll", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -207,7 +208,7 @@ func (s TreasureChestImpl) GetAll(user *types.User) ([]*types.TreasureChest, err
|
||||
return sortTree(treasureChests), nil
|
||||
}
|
||||
|
||||
func (s TreasureChestImpl) Delete(user *types.User, idStr string) error {
|
||||
func (s TreasureChestImpl) Delete(ctx context.Context, user *types.User, idStr string) error {
|
||||
if user == nil {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
@@ -217,7 +218,7 @@ func (s TreasureChestImpl) Delete(user *types.User, idStr string) error {
|
||||
return fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.Beginx()
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("treasureChest Delete", nil, err)
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -227,7 +228,7 @@ func (s TreasureChestImpl) Delete(user *types.User, idStr string) error {
|
||||
}()
|
||||
|
||||
childCount := 0
|
||||
err = tx.Get(&childCount, `SELECT COUNT(*) FROM treasure_chest WHERE user_id = ? AND parent_id = ?`, user.Id, id)
|
||||
err = tx.GetContext(ctx, &childCount, `SELECT COUNT(*) FROM treasure_chest WHERE user_id = ? AND parent_id = ?`, user.Id, id)
|
||||
err = db.TransformAndLogDbError("treasureChest Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -238,7 +239,7 @@ func (s TreasureChestImpl) Delete(user *types.User, idStr string) error {
|
||||
}
|
||||
|
||||
transactionsCount := 0
|
||||
err = tx.Get(&transactionsCount,
|
||||
err = tx.GetContext(ctx, &transactionsCount,
|
||||
`SELECT COUNT(*) FROM "transaction" WHERE user_id = ? AND treasure_chest_id = ?`,
|
||||
user.Id, id)
|
||||
err = db.TransformAndLogDbError("treasureChest Delete", nil, err)
|
||||
@@ -250,7 +251,7 @@ func (s TreasureChestImpl) Delete(user *types.User, idStr string) error {
|
||||
}
|
||||
|
||||
recurringCount := 0
|
||||
err = tx.Get(&recurringCount, `
|
||||
err = tx.GetContext(ctx, &recurringCount, `
|
||||
SELECT COUNT(*) FROM transaction_recurring WHERE user_id = ? AND treasure_chest_id = ?`,
|
||||
user.Id, id)
|
||||
err = db.TransformAndLogDbError("treasureChest Delete", nil, err)
|
||||
@@ -261,7 +262,7 @@ func (s TreasureChestImpl) Delete(user *types.User, idStr string) error {
|
||||
return fmt.Errorf("cannot delete treasure chest with existing recurring transactions: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
r, err := tx.Exec(`DELETE FROM treasure_chest WHERE id = ? AND user_id = ?`, id, user.Id)
|
||||
r, err := tx.ExecContext(ctx, `DELETE FROM treasure_chest WHERE id = ? AND user_id = ?`, id, user.Id)
|
||||
err = db.TransformAndLogDbError("treasureChest Delete", r, err)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user