/
balance.go
73 lines (56 loc) · 1.74 KB
/
balance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package model
import (
"context"
"errors"
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var ErrRecordNotFound = errors.New("record not found")
type Profile struct {
ID int64 `json:"id"`
PhoneNumber string `json:"phone_number"`
Balance float64 `json:"balance"`
}
type Transaction struct {
ID int `json:"id"`
ProfileID int64 `json:"profile_id"`
Amount float64 `json:"amount"`
}
type ProfileRepo interface {
FindByPhone(ctx context.Context, phoneNumber string) (*Profile, error)
Create(context.Context, *Profile) error
UpdateBalance(context.Context, string, float64) error
}
type SQLProfileRepo struct {
DB *gorm.DB
}
func (p *SQLProfileRepo) Create(ctx context.Context, profile *Profile) error {
return p.DB.WithContext(ctx).Create(profile).Error
}
func (p *SQLProfileRepo) UpdateBalance(ctx context.Context, phoneNumber string, amount float64) error {
return p.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
profile := &Profile{PhoneNumber: phoneNumber}
err := tx.Model(profile).Clauses(
clause.Returning{Columns: []clause.Column{{Name: "id"}}},
).
Where("phone_number = ?", profile.PhoneNumber).
UpdateColumn("balance", profile.Balance+amount).Error
if err != nil {
return err
}
transaction := &Transaction{Amount: amount, ProfileID: profile.ID}
return tx.Create(transaction).Error
})
}
func (p *SQLProfileRepo) FindByPhone(ctx context.Context, phoneNumber string) (*Profile, error) {
result := &Profile{}
err := p.DB.WithContext(ctx).Where("phone_number = ?", phoneNumber).First(result).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("failed to find profile: %w", ErrRecordNotFound)
}
return nil, err
}
return result, nil
}