-
Notifications
You must be signed in to change notification settings - Fork 0
/
take.go
114 lines (95 loc) · 2.62 KB
/
take.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
package entgo
import (
"context"
"github.com/tinygodsdev/orrery/internal/repository/entgo/ent/take"
"github.com/google/uuid"
"github.com/tinygodsdev/orrery/internal/domain"
"github.com/tinygodsdev/orrery/internal/repository/entgo/ent"
)
func (r *EntgoRepository) FinishedTakeCount(ctx context.Context) (int, error) {
return r.client.Take.Query().Where(take.StatusEQ(take.StatusFinish)).Count(ctx)
}
func (r *EntgoRepository) GetTake(ctx context.Context, takeID uuid.UUID) (*domain.Take, error) {
tk, err := r.client.Take.
Query().
Where(take.IDEQ(takeID)).
WithTest(). // TODO maybe migrate to fk field for simpler queries
WithUser().
Only(ctx)
if err != nil {
return nil, err
}
return entToDomainTake(tk, uuid.Nil, uuid.Nil), nil
}
func (r *EntgoRepository) CreateTake(ctx context.Context, tk *domain.Take) (*domain.Take, error) {
t, err := r.client.Take.Create().
SetTestID(tk.TestID).
SetUserID(tk.UserID).
SetMeta(tk.Meta).
SetNillableStartTime(tk.StartTime).
SetInLocale(take.InLocale(tk.InLocale)).
SetSeed(tk.Seed).
Save(ctx)
if err != nil {
return nil, err
}
return entToDomainTake(t, tk.UserID, tk.TestID), nil
}
func (r *EntgoRepository) UpdateTakeMark(ctx context.Context, takeID uuid.UUID, mark int) error {
_, err := r.client.Take.UpdateOneID(takeID).SetMark(mark).Save(ctx)
if err != nil {
return err
}
return nil
}
func (r *EntgoRepository) UpdateTake(ctx context.Context, tk *domain.Take) (*domain.Take, error) {
t, err := r.client.Take.Query().
Where(take.IDEQ(tk.ID)).
WithUser().
WithTest().
Only(ctx)
if err != nil {
return nil, err
}
updatedQuery := t.Update().
SetProgress(tk.Progress).
SetPage(tk.Page).
SetInLocale(take.InLocale(tk.InLocale)).
SetStatus(take.Status(tk.Status)).
SetNillableStartTime(tk.StartTime).
SetMeta(tk.Meta).
SetSuspicious(tk.Suspicious)
if tk.EndTime != nil && !tk.EndTime.IsZero() {
updatedQuery.SetNillableEndTime(tk.EndTime)
}
t, err = updatedQuery.Save(ctx)
if err != nil {
return nil, err
}
return entToDomainTake(t, tk.UserID, tk.TestID), nil
}
func entToDomainTake(t *ent.Take, uid, tid uuid.UUID) *domain.Take {
if t.Edges.User != nil {
uid = t.Edges.User.ID
}
if t.Edges.Test != nil {
tid = t.Edges.Test.ID
}
return &domain.Take{
ID: t.ID,
Progress: t.Progress,
Status: t.Status.String(),
Page: t.Page,
Seed: t.Seed,
UserID: uid,
TestID: tid,
InLocale: t.InLocale.String(),
StartTime: t.StartTime,
EndTime: t.EndTime,
Suspicious: t.Suspicious,
CreateTime: t.CreateTime,
UpdateTime: t.UpdateTime,
Mark: t.Mark,
Meta: t.Meta,
}
}