blob: db39911ed183a28800a83cbf8f3136a4edb12464 (
plain)
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
|
package user
import (
"context"
"time"
"git.anhgelus.world/anhgelus/les-copaings-bot/common"
)
type Copaing struct {
ID uint `gorm:"primarykey"`
DiscordID uint64 `gorm:"not null"`
CopaingXPs []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"`
GuildID uint64 `gorm:"not null"`
}
type CopaingXP struct {
ID uint `gorm:"primarykey"`
XP uint `gorm:"default:0"`
CopaingID uint
GuildID uint64 `gorm:"not null;"`
CreatedAt time.Time
}
type CopaingAccess interface {
Copaing() *Copaing
GetXP() uint
}
func GetCopaing(ctx context.Context, discordID, guildID uint64) *CopaingCached {
state := GetState(ctx)
cc, err := state.Copaing(guildID, discordID)
if err != nil {
c := Copaing{DiscordID: discordID, GuildID: guildID}
if err := c.load(ctx); err != nil {
panic(err)
}
cc = FromCopaing(&c)
}
return cc
}
func (c *Copaing) load(ctx context.Context) error {
err := common.GetDB(ctx).
Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).
Preload("CopaingXPs").
FirstOrCreate(c).
Error
if err != nil {
return err
}
return err
}
func (c *Copaing) Save(ctx context.Context) error {
return common.GetDB(ctx).Save(c).Error
}
func (c *Copaing) Delete(ctx context.Context) error {
db := common.GetDB(ctx)
err := db.
Where("copaing_id = ? and guild_id = ?", c.ID, c.GuildID).
Delete(&CopaingXP{}).
Error
if err != nil {
return err
}
return db.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c).Error
}
|