forked from indes/flowerss-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
42 lines (34 loc) · 926 Bytes
/
user.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
package storage
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/nerdneilsfield/flowerss-bot/internal/model"
)
type UserStorageImpl struct {
db *gorm.DB
}
func NewUserStorageImpl(db *gorm.DB) *UserStorageImpl {
return &UserStorageImpl{db: db}
}
func (s *UserStorageImpl) Init(ctx context.Context) error {
return s.db.Migrator().AutoMigrate(&model.User{})
}
func (s *UserStorageImpl) CrateUser(ctx context.Context, user *model.User) error {
result := s.db.WithContext(ctx).Create(user)
if result.Error != nil {
return result.Error
}
return nil
}
func (s *UserStorageImpl) GetUser(ctx context.Context, id int64) (*model.User, error) {
var user = &model.User{}
result := s.db.WithContext(ctx).Where(&model.User{ID: id}).First(user)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, ErrRecordNotFound
}
return nil, result.Error
}
return user, nil
}