-
Notifications
You must be signed in to change notification settings - Fork 3
/
shitpost.go
136 lines (117 loc) · 2.85 KB
/
shitpost.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
package data
import (
"github.com/ritsec/ops-bot-iii/ent"
"github.com/ritsec/ops-bot-iii/ent/shitpost"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
// Shitpost is the interface for interacting with the shitpost table
type shitpost_s struct{}
// Get gets a shitpost by its ID
func (*shitpost_s) Get(id string, ctx ddtrace.SpanContext) (*ent.Shitpost, error) {
span := tracer.StartSpan(
"data.shitpost:Get",
tracer.ResourceName("Data.Shitpost.Get"),
tracer.ChildOf(ctx),
)
defer span.Finish()
return Client.Shitpost.Query().
Where(
shitpost.IDEQ(id),
).
Only(Ctx)
}
// Create creates a new shitpost
func (*shitpost_s) Create(message_id string, channel_id string, user_id string, count int, ctx ddtrace.SpanContext) (*ent.Shitpost, error) {
span := tracer.StartSpan(
"data.shitpost:Create",
tracer.ResourceName("Data.Shitpost.Create"),
tracer.ChildOf(ctx),
)
defer span.Finish()
ent_user, err := User.Get(user_id, span.Context())
if err != nil {
return nil, err
}
return Client.Shitpost.Create().
SetID(message_id).
SetUser(ent_user).
SetChannelID(channel_id).
SetCount(count).
Save(Ctx)
}
// Delete deletes a shitpost by its ID
func (*shitpost_s) Delete(id string, ctx ddtrace.SpanContext) error {
span := tracer.StartSpan(
"data.shitpost:Delete",
tracer.ResourceName("Data.Shitpost.Delete"),
tracer.ChildOf(ctx),
)
defer span.Finish()
_, err := Client.Shitpost.Delete().
Where(
shitpost.IDEQ(id),
).
Exec(Ctx)
return err
}
// Update updates a shitpost by its ID
func (*shitpost_s) Update(message_id string, channel_id string, user_id string, count int, ctx ddtrace.SpanContext) (*ent.Shitpost, error) {
span := tracer.StartSpan(
"data.shitpost:Update",
tracer.ResourceName("Data.Shitpost.Update"),
tracer.ChildOf(ctx),
)
defer span.Finish()
exists, err := Client.Shitpost.Query().
Where(
shitpost.IDEQ(message_id),
).
Exist(Ctx)
if err != nil {
return nil, err
}
if !exists {
return Shitposts.Create(
message_id,
channel_id,
user_id,
count,
span.Context(),
)
}
entUser, err := User.Get(user_id, span.Context())
if err != nil {
return nil, err
}
entShitpost, err := Client.Shitpost.
UpdateOneID(message_id).
SetUser(entUser).
SetCount(count).
Save(Ctx)
if err != nil {
return nil, err
}
return entShitpost, err
}
func (*shitpost_s) GetTopXShitposts(amount int, ctx ddtrace.SpanContext) ([]*ent.Shitpost, error) {
span := tracer.StartSpan(
"data.shitpost:GetTopXShitposts",
tracer.ResourceName("Data.Shitpost.GetTopXShitposts"),
tracer.ChildOf(ctx),
)
defer span.Finish()
_, err := Client.Shitpost.Delete().
Where(
shitpost.CountLT(10),
).
Exec(Ctx)
if err != nil {
return nil, err
}
return Client.Shitpost.Query().
WithUser().
Order(ent.Desc(shitpost.FieldCount)).
Limit(amount).
All(Ctx)
}