-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
139 lines (111 loc) · 3.39 KB
/
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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package infrastructure
import (
"errors"
"github.com/scarlet0725/prism-api/infrastructure/repository"
"github.com/scarlet0725/prism-api/model"
"github.com/scarlet0725/prism-api/schema"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type userRepository struct {
db *gorm.DB
}
func NewUserRepository(db *gorm.DB) repository.User {
return &userRepository{
db: db,
}
}
func (u *userRepository) GetUser(id string) (*model.User, error) {
var user model.User
err := u.db.Preload(clause.Associations).Where("user_id = ?", id).First(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}
func (u *userRepository) CreateUser(user *model.User) (*model.User, error) {
var schema schema.User
schema.User = *user
result := u.db.Create(&schema)
err := result.Error
if err != nil {
return nil, err
}
return user, nil
}
func (u *userRepository) UpdateUser(user *model.User) (*model.User, error) {
var schema schema.User
schema.User = *user
err := u.db.Save(user).Error
if err != nil {
return nil, err
}
return &schema.User, nil
}
func (u *userRepository) DeleteUser(user *model.User) error {
return u.db.Delete(user).Error
}
func (u *userRepository) GetUserByAPIKey(apiKey string) (*model.User, error) {
var user model.User
err := u.db.Preload(clause.Associations).Where("api_key = ?", apiKey).First(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}
func (u *userRepository) GetUserCalendarByUserID(id int) (*model.ExternalCalendar, error) {
var calendar model.ExternalCalendar
err := u.db.Where("user_id = ?", id).First(&calendar).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, repository.ErrNotFound
}
return nil, err
}
return &calendar, nil
}
func (u *userRepository) GetGoogleCalendarConfig(id int) (*model.GoogleCalenderConfig, error) {
var config *model.GoogleCalenderConfig
err := u.db.Table("google_oauth_tokens").Select("*").Joins("INNER JOIN external_calendars ON google_oauth_tokens.user_id = external_calendars.user_id").Where("google_oauth_tokens.user_id = ? AND external_calendars.user_id = ?", id, id).Take(&config).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, repository.ErrNotFound
}
return nil, err
}
return config, nil
}
func (u *userRepository) GetGoogleOAuthToken(id int) (*model.GoogleOAuthToken, error) {
token := &model.GoogleOAuthToken{}
err := u.db.Table("google_oauth_tokens").Select("*").Where("user_id = ?", id).Take(&token).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, repository.ErrNotFound
}
return nil, err
}
return token, nil
}
func (u *userRepository) SaveExternalCalendar(cal *model.ExternalCalendar) (*model.ExternalCalendar, error) {
err := u.db.Create(cal).Error
if err != nil {
return nil, err
}
return cal, nil
}
func (u *userRepository) AddRegistrationEvent(user *model.User, event *model.Event) error {
err := u.db.Model(user).Association("Events").Append(event)
return err
}
func (u *userRepository) RemoveRegistrationEvent(user *model.User, event *model.Event) error {
err := u.db.Model(user).Association("Events").Delete(event)
return err
}
func (u *userRepository) GetUserByUsername(username string) (*model.User, error) {
var user model.User
err := u.db.Preload(clause.Associations).Where("username = ?", username).First(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}