aboutsummaryrefslogtreecommitdiff
path: root/user/member.go
blob: 9068a6f7226e01713e1f0fe1ede559108570f013 (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
package user

import (
	"time"

	"github.com/anhgelus/gokord"
)

type Copaing struct {
	ID         uint        `gorm:"primarykey"`
	DiscordID  string      `gorm:"not null"`
	CopaingXPs []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"`
	GuildID    string      `gorm:"not null"`
}

type CopaingXP struct {
	ID        uint `gorm:"primarykey"`
	XP        uint `gorm:"default:0"`
	CopaingID uint
	GuildID   string `gorm:"not null;"`
	CreatedAt time.Time
}

type CopaingAccess interface {
	ToCopaing() *Copaing
	GetXP() uint
}

func GetCopaing(discordID string, guildID string) *Copaing {
	c := Copaing{DiscordID: discordID, GuildID: guildID}
	if err := c.Load(); err != nil {
		panic(err)
	}
	return &c
}

func (c *Copaing) Load() error {
	return gokord.DB.
		Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).
		Preload("CopaingXPs").
		FirstOrCreate(c).
		Error
}

func (c *Copaing) Save() error {
	return gokord.DB.Save(c).Error
}

func (c *Copaing) Delete() error {
	return gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c).Error
}