-
Notifications
You must be signed in to change notification settings - Fork 126
/
user_session.go
85 lines (68 loc) · 2.33 KB
/
user_session.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
package prisma
import (
"context"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
"github.com/hatchet-dev/hatchet/internal/validator"
)
type userSessionRepository struct {
client *db.PrismaClient
v validator.Validator
}
func NewUserSessionRepository(client *db.PrismaClient, v validator.Validator) repository.UserSessionRepository {
return &userSessionRepository{
client: client,
v: v,
}
}
func (r *userSessionRepository) Create(opts *repository.CreateSessionOpts) (*db.UserSessionModel, error) {
if err := r.v.Validate(opts); err != nil {
return nil, err
}
params := []db.UserSessionSetParam{
db.UserSession.ID.Set(opts.ID),
}
if opts.UserId != nil {
params = append(params, db.UserSession.User.Link(db.User.ID.Equals(*opts.UserId)))
}
if opts.Data != nil {
params = append(params, db.UserSession.Data.SetIfPresent(opts.Data))
}
return r.client.UserSession.CreateOne(
db.UserSession.ExpiresAt.Set(opts.ExpiresAt),
params...,
).Exec(context.Background())
}
func (r *userSessionRepository) Update(sessionId string, opts *repository.UpdateSessionOpts) (*db.UserSessionModel, error) {
if err := r.v.Validate(opts); err != nil {
return nil, err
}
params := []db.UserSessionSetParam{}
if opts.UserId != nil {
params = append(params, db.UserSession.User.Link(db.User.ID.Equals(*opts.UserId)))
}
if opts.Data != nil {
params = append(params, db.UserSession.Data.SetIfPresent(opts.Data))
}
return r.client.UserSession.FindUnique(
db.UserSession.ID.Equals(sessionId),
).Update(
params...,
).Exec(context.Background())
}
func (r *userSessionRepository) Delete(sessionId string) (*db.UserSessionModel, error) {
return r.client.UserSession.FindUnique(
db.UserSession.ID.Equals(sessionId),
).Delete().Exec(context.Background())
}
func (r *userSessionRepository) GetById(sessionId string) (*db.UserSessionModel, error) {
return r.client.UserSession.FindUnique(
db.UserSession.ID.Equals(sessionId),
).Exec(context.Background())
}
// type UserSessionRepository interface {
// Create(opts *CreateSessionOpts) (*db.UserSessionModel, error)
// Update(sessionId string, opts *UpdateSessionOpts) (*db.UserSessionModel, error)
// Delete(sessionId string) (*db.UserSessionModel, error)
// GetById(sessionId string) (*db.UserSessionModel, error)
// }