2023-10-31 22:40:14 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2023-11-01 21:51:14 +00:00
|
|
|
"github.com/ectrc/snow/aid"
|
2023-10-31 22:40:14 +00:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
2023-11-20 21:20:26 +00:00
|
|
|
"gorm.io/gorm/logger"
|
2023-10-31 22:40:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PostgresStorage struct {
|
|
|
|
Postgres *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostgresStorage() *PostgresStorage {
|
2023-12-10 00:52:59 +00:00
|
|
|
l := logger.Default
|
2023-11-20 23:20:42 +00:00
|
|
|
if aid.Config.Output.Level == "time" {
|
|
|
|
l = logger.Default.LogMode(logger.Info)
|
|
|
|
}
|
|
|
|
|
2023-11-20 21:20:26 +00:00
|
|
|
db, err := gorm.Open(postgres.Open(aid.Config.Database.URI), &gorm.Config{
|
2023-11-20 23:20:42 +00:00
|
|
|
Logger: l,
|
2023-11-20 21:20:26 +00:00
|
|
|
})
|
2023-10-31 22:40:14 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PostgresStorage{
|
|
|
|
Postgres: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) Migrate(table interface{}, tableName string) {
|
|
|
|
s.Postgres.Table(tableName).AutoMigrate(table)
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:53:19 +00:00
|
|
|
func (s *PostgresStorage) MigrateAll() {
|
|
|
|
s.Migrate(&DB_Person{}, "Persons")
|
2024-02-04 02:05:31 +00:00
|
|
|
s.Migrate(&DB_Relationship{}, "Relationships")
|
2023-11-09 18:53:19 +00:00
|
|
|
s.Migrate(&DB_Profile{}, "Profiles")
|
2024-02-04 02:05:31 +00:00
|
|
|
s.Migrate(&DB_Attribute{}, "Attributes")
|
2023-11-20 23:20:42 +00:00
|
|
|
s.Migrate(&DB_Loadout{}, "Loadouts")
|
2024-02-04 02:05:31 +00:00
|
|
|
s.Migrate(&DB_Item{}, "Items")
|
|
|
|
s.Migrate(&DB_Purchase{}, "Purchases")
|
|
|
|
s.Migrate(&DB_PurchaseLoot{}, "PurchaseLoot")
|
2023-11-09 18:53:19 +00:00
|
|
|
s.Migrate(&DB_VariantChannel{}, "Variants")
|
2024-02-04 02:05:31 +00:00
|
|
|
s.Migrate(&DB_Quest{}, "Quests")
|
|
|
|
s.Migrate(&DB_Gift{}, "Gifts")
|
|
|
|
s.Migrate(&DB_GiftLoot{}, "GiftLoot")
|
2023-12-10 23:54:31 +00:00
|
|
|
s.Migrate(&DB_DiscordPerson{}, "Discords")
|
2024-02-04 15:21:16 +00:00
|
|
|
s.Migrate(&DB_BanStatus{}, "Bans")
|
2023-12-10 23:54:31 +00:00
|
|
|
s.Migrate(&DB_SeasonStat{}, "Stats")
|
2023-11-09 18:53:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DropTables() {
|
|
|
|
s.Postgres.Exec(`DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;`)
|
|
|
|
}
|
|
|
|
|
2024-02-04 15:21:16 +00:00
|
|
|
func (s *PostgresStorage) PreloadPerson() (tx *gorm.DB) {
|
|
|
|
return s.Postgres.
|
2023-12-10 00:52:59 +00:00
|
|
|
Model(&DB_Person{}).
|
2023-10-31 22:40:14 +00:00
|
|
|
Preload("Profiles").
|
2023-11-20 23:20:42 +00:00
|
|
|
Preload("Profiles.Loadouts").
|
2023-10-31 22:40:14 +00:00
|
|
|
Preload("Profiles.Attributes").
|
|
|
|
Preload("Profiles.Items").
|
2024-02-04 01:25:44 +00:00
|
|
|
Preload("Profiles.Items.Variants").
|
2023-10-31 22:40:14 +00:00
|
|
|
Preload("Profiles.Gifts").
|
2024-02-04 01:25:44 +00:00
|
|
|
Preload("Profiles.Gifts.Loot").
|
2023-10-31 22:40:14 +00:00
|
|
|
Preload("Profiles.Quests").
|
2024-02-04 15:21:16 +00:00
|
|
|
Preload("Profiles.Purchases").
|
|
|
|
Preload("Profiles.Purchases.Loot").
|
2023-12-11 16:49:45 +00:00
|
|
|
Preload("Discord").
|
2024-02-04 15:21:16 +00:00
|
|
|
Preload("BanHistory").
|
|
|
|
Preload("Stats")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) GetPerson(personId string) *DB_Person {
|
|
|
|
var dbPerson DB_Person
|
|
|
|
s.PreloadPerson().Where("id = ?", personId).Find(&dbPerson)
|
2023-10-31 22:40:14 +00:00
|
|
|
|
|
|
|
if dbPerson.ID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dbPerson
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:48:50 +00:00
|
|
|
func (s *PostgresStorage) GetPersonByDisplay(displayName string) *DB_Person {
|
|
|
|
var dbPerson DB_Person
|
2024-02-04 15:21:16 +00:00
|
|
|
s.PreloadPerson().Where("display_name = ?", displayName).Find(&dbPerson)
|
2023-11-03 23:48:50 +00:00
|
|
|
|
|
|
|
if dbPerson.ID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dbPerson
|
|
|
|
}
|
|
|
|
|
2024-01-03 23:25:17 +00:00
|
|
|
func (s *PostgresStorage) GetPersonsByPartialDisplay(displayName string) []*DB_Person {
|
|
|
|
var dbPersons []*DB_Person
|
2024-02-04 15:21:16 +00:00
|
|
|
s.PreloadPerson().Where("display_name LIKE ?", "%" + displayName + "%").Find(&dbPersons)
|
2024-01-03 23:25:17 +00:00
|
|
|
|
|
|
|
if len(dbPersons) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbPersons
|
|
|
|
}
|
|
|
|
|
2023-12-11 16:49:45 +00:00
|
|
|
func (s *PostgresStorage) GetPersonByDiscordID(discordId string) *DB_Person {
|
2023-12-10 00:52:59 +00:00
|
|
|
var discordEntry DB_DiscordPerson
|
2023-12-11 16:49:45 +00:00
|
|
|
s.Postgres.Model(&DB_DiscordPerson{}).Where("id = ?", discordId).Find(&discordEntry)
|
2023-12-10 00:52:59 +00:00
|
|
|
|
|
|
|
if discordEntry.ID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.GetPerson(discordEntry.PersonID)
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:40:14 +00:00
|
|
|
func (s *PostgresStorage) GetAllPersons() []*DB_Person {
|
|
|
|
var dbPersons []*DB_Person
|
2024-02-04 15:21:16 +00:00
|
|
|
s.PreloadPerson().Find(&dbPersons)
|
2023-10-31 22:40:14 +00:00
|
|
|
|
|
|
|
return dbPersons
|
|
|
|
}
|
|
|
|
|
2023-12-13 22:52:16 +00:00
|
|
|
func (s *PostgresStorage) GetPersonsCount() int {
|
|
|
|
var count int64
|
|
|
|
s.Postgres.Model(&DB_Person{}).Count(&count)
|
|
|
|
return int(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) TotalVBucks() int {
|
|
|
|
var total int64
|
|
|
|
s.Postgres.Model(&DB_Item{}).Select("sum(quantity)").Where("template_id = ?", "Currency:MtxPurchased").Find(&total)
|
|
|
|
return int(total)
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:40:14 +00:00
|
|
|
func (s *PostgresStorage) SavePerson(person *DB_Person) {
|
|
|
|
s.Postgres.Save(person)
|
|
|
|
}
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
func (s *PostgresStorage) DeletePerson(personId string) {
|
2024-02-04 15:21:16 +00:00
|
|
|
s.PreloadPerson().Delete(&DB_Person{}, "id = ?", personId)
|
2023-11-05 01:58:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 23:46:22 +00:00
|
|
|
func (s *PostgresStorage) GetIncomingRelationships(personId string) []*DB_Relationship {
|
|
|
|
var dbRelationships []*DB_Relationship
|
2024-01-30 16:34:17 +00:00
|
|
|
s.Postgres.Model(&DB_Relationship{}).Where("towards_person_id = ?", personId).Find(&dbRelationships)
|
2024-01-29 23:46:22 +00:00
|
|
|
return dbRelationships
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) GetOutgoingRelationships(personId string) []*DB_Relationship {
|
|
|
|
var dbRelationships []*DB_Relationship
|
2024-01-30 16:34:17 +00:00
|
|
|
s.Postgres.Model(&DB_Relationship{}).Where("from_person_id = ?", personId).Find(&dbRelationships)
|
2024-01-29 23:46:22 +00:00
|
|
|
return dbRelationships
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) SaveRelationship(relationship *DB_Relationship) {
|
|
|
|
s.Postgres.Save(relationship)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) DeleteRelationship(relationship *DB_Relationship) {
|
2024-01-30 16:34:17 +00:00
|
|
|
s.Postgres.Delete(relationship)
|
2024-01-29 23:46:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
func (s *PostgresStorage) SaveProfile(profile *DB_Profile) {
|
|
|
|
s.Postgres.Save(profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) DeleteProfile(profileId string) {
|
|
|
|
s.Postgres.Delete(&DB_Profile{}, "id = ?", profileId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) SaveItem(item *DB_Item) {
|
|
|
|
s.Postgres.Save(item)
|
|
|
|
}
|
|
|
|
|
2023-12-17 00:43:17 +00:00
|
|
|
func (s *PostgresStorage) BulkCreateItems(items *[]DB_Item) {
|
|
|
|
s.Postgres.Create(items)
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DeleteItem(itemId string) {
|
|
|
|
s.Postgres.Delete(&DB_Item{}, "id = ?", itemId)
|
|
|
|
}
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
func (s *PostgresStorage) SaveVariant(variant *DB_VariantChannel) {
|
|
|
|
s.Postgres.Save(variant)
|
|
|
|
}
|
|
|
|
|
2024-02-04 01:25:44 +00:00
|
|
|
func (s *PostgresStorage) BulkCreateVariants(variants *[]DB_VariantChannel) {
|
|
|
|
s.Postgres.Create(variants)
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DeleteVariant(variantId string) {
|
|
|
|
s.Postgres.Delete(&DB_VariantChannel{}, "id = ?", variantId)
|
|
|
|
}
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
func (s *PostgresStorage) SaveQuest(quest *DB_Quest) {
|
|
|
|
s.Postgres.Save(quest)
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DeleteQuest(questId string) {
|
|
|
|
s.Postgres.Delete(&DB_Quest{}, "id = ?", questId)
|
|
|
|
}
|
|
|
|
|
2024-02-04 02:05:31 +00:00
|
|
|
func (s *PostgresStorage) SaveLoot(loot *DB_GiftLoot) {
|
2023-11-05 01:58:00 +00:00
|
|
|
s.Postgres.Save(loot)
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DeleteLoot(lootId string) {
|
2024-02-04 02:05:31 +00:00
|
|
|
s.Postgres.Delete(&DB_GiftLoot{}, "id = ?", lootId)
|
2023-11-01 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
func (s *PostgresStorage) SaveGift(gift *DB_Gift) {
|
|
|
|
s.Postgres.Save(gift)
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DeleteGift(giftId string) {
|
|
|
|
s.Postgres.Delete(&DB_Gift{}, "id = ?", giftId)
|
|
|
|
}
|
|
|
|
|
2024-02-04 02:05:31 +00:00
|
|
|
func (s *PostgresStorage) SaveAttribute(attribute *DB_Attribute) {
|
2023-11-05 22:08:53 +00:00
|
|
|
s.Postgres.Save(attribute)
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DeleteAttribute(attributeId string) {
|
2024-02-04 02:05:31 +00:00
|
|
|
s.Postgres.Delete(&DB_Attribute{}, "id = ?", attributeId)
|
2023-11-05 01:58:00 +00:00
|
|
|
}
|
2023-11-20 21:20:26 +00:00
|
|
|
|
|
|
|
func (s *PostgresStorage) SaveLoadout(loadout *DB_Loadout) {
|
|
|
|
s.Postgres.Save(loadout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) DeleteLoadout(loadoutId string) {
|
|
|
|
s.Postgres.Delete(&DB_Loadout{}, "id = ?", loadoutId)
|
2023-12-10 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 15:21:16 +00:00
|
|
|
func (s *PostgresStorage) SavePurchase(purchase *DB_Purchase) {
|
|
|
|
s.Postgres.Save(purchase)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) DeletePurchase(purchaseId string) {
|
|
|
|
s.Postgres.Delete(&DB_Purchase{}, "id = ?", purchaseId)
|
|
|
|
}
|
|
|
|
|
2023-12-10 00:52:59 +00:00
|
|
|
func (s *PostgresStorage) SaveDiscordPerson(discordPerson *DB_DiscordPerson) {
|
|
|
|
s.Postgres.Save(discordPerson)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStorage) DeleteDiscordPerson(discordPersonId string) {
|
|
|
|
s.Postgres.Delete(&DB_DiscordPerson{}, "id = ?", discordPersonId)
|
2023-11-20 21:20:26 +00:00
|
|
|
}
|