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-11-20 23:20:42 +00:00
|
|
|
l := logger.Default.LogMode(logger.Silent)
|
|
|
|
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")
|
|
|
|
s.Migrate(&DB_Profile{}, "Profiles")
|
|
|
|
s.Migrate(&DB_Item{}, "Items")
|
|
|
|
s.Migrate(&DB_Gift{}, "Gifts")
|
|
|
|
s.Migrate(&DB_Quest{}, "Quests")
|
2023-11-20 23:20:42 +00:00
|
|
|
s.Migrate(&DB_Loadout{}, "Loadouts")
|
2023-11-09 18:53:19 +00:00
|
|
|
s.Migrate(&DB_Loot{}, "Loot")
|
|
|
|
s.Migrate(&DB_VariantChannel{}, "Variants")
|
|
|
|
s.Migrate(&DB_PAttribute{}, "Attributes")
|
|
|
|
}
|
|
|
|
|
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;`)
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:40:14 +00:00
|
|
|
func (s *PostgresStorage) GetPerson(personId string) *DB_Person {
|
|
|
|
var dbPerson DB_Person
|
|
|
|
s.Postgres.
|
|
|
|
Preload("Profiles").
|
2023-11-20 23:20:42 +00:00
|
|
|
Preload("Profiles.Loadouts").
|
2023-10-31 22:40:14 +00:00
|
|
|
Preload("Profiles.Items.Variants").
|
|
|
|
Preload("Profiles.Gifts.Loot").
|
|
|
|
Preload("Profiles.Attributes").
|
|
|
|
Preload("Profiles.Items").
|
|
|
|
Preload("Profiles.Gifts").
|
|
|
|
Preload("Profiles.Quests").
|
2023-11-20 21:20:26 +00:00
|
|
|
Where("id = ?", personId).
|
2023-10-31 22:40:14 +00:00
|
|
|
Find(&dbPerson)
|
|
|
|
|
|
|
|
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
|
|
|
|
s.Postgres.
|
2023-11-20 23:20:42 +00:00
|
|
|
Preload("Profiles").
|
|
|
|
Preload("Profiles.Loadouts").
|
|
|
|
Preload("Profiles.Items.Variants").
|
|
|
|
Preload("Profiles.Gifts.Loot").
|
|
|
|
Preload("Profiles.Attributes").
|
|
|
|
Preload("Profiles.Items").
|
|
|
|
Preload("Profiles.Gifts").
|
|
|
|
Preload("Profiles.Quests").
|
2023-11-03 23:48:50 +00:00
|
|
|
Where("display_name = ?", displayName).
|
|
|
|
Find(&dbPerson)
|
|
|
|
|
|
|
|
if dbPerson.ID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dbPerson
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:40:14 +00:00
|
|
|
func (s *PostgresStorage) GetAllPersons() []*DB_Person {
|
|
|
|
var dbPersons []*DB_Person
|
|
|
|
|
|
|
|
s.Postgres.
|
|
|
|
Preload("Profiles").
|
2023-11-20 23:20:42 +00:00
|
|
|
Preload("Profiles.Loadouts").
|
2023-10-31 22:40:14 +00:00
|
|
|
Preload("Profiles.Items.Variants").
|
|
|
|
Preload("Profiles.Gifts.Loot").
|
|
|
|
Preload("Profiles.Attributes").
|
|
|
|
Preload("Profiles.Items").
|
|
|
|
Preload("Profiles.Gifts").
|
|
|
|
Preload("Profiles.Quests").
|
|
|
|
Find(&dbPersons)
|
|
|
|
|
|
|
|
return dbPersons
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
s.Postgres.Delete(&DB_Person{}, "id = ?", personId)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
func (s *PostgresStorage) SaveLoot(loot *DB_Loot) {
|
|
|
|
s.Postgres.Save(loot)
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DeleteLoot(lootId string) {
|
|
|
|
s.Postgres.Delete(&DB_Loot{}, "id = ?", lootId)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-11-05 22:08:53 +00:00
|
|
|
func (s *PostgresStorage) SaveAttribute(attribute *DB_PAttribute) {
|
|
|
|
s.Postgres.Save(attribute)
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func (s *PostgresStorage) DeleteAttribute(attributeId string) {
|
|
|
|
s.Postgres.Delete(&DB_PAttribute{}, "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)
|
|
|
|
}
|