Add ID to all database tables + add deleting from storage!

This commit is contained in:
eccentric 2023-11-01 21:33:25 +00:00
parent 8e3cd016d9
commit 226241588c
10 changed files with 108 additions and 21 deletions

13
main.go
View File

@ -45,6 +45,10 @@ func init() {
if DROP_TABLES {
user := person.NewPerson()
{
user.CommonCoreProfile.Attributes.AddAttribute(person.NewAttribute("xp", 1030))
user.CommonCoreProfile.Attributes.AddAttribute(person.NewAttribute("level", 100))
user.CommonCoreProfile.Attributes.AddAttribute(person.NewAttribute("quest_manager", aid.JSON{}))
user.CommonCoreProfile.Items.AddItem(person.NewItem("Currency:MtxPurchased", 100))
user.CommonCoreProfile.Items.AddItem(person.NewItem("Token:CampaignAccess", 1))
@ -79,13 +83,18 @@ func init() {
user.CommonCoreProfile.Items.AddItem(person.NewItem("Token:ReceiveMtxCurrency", 1))
}
user.CommonCoreProfile.Diff(snapshot)
aid.PrintJSON(user.CommonCoreProfile.Changes)
user.Save()
}
go storage.Cache.CacheKiller()
}
func main() {
users := person.AllFromDatabase()
for _, user := range users {
aid.PrintJSON(user.Snapshot())
}
// aid.WaitForExit()
}

View File

@ -2,21 +2,25 @@ package person
import (
"encoding/json"
"reflect"
"github.com/ectrc/snow/storage"
"github.com/google/uuid"
)
type Attribute struct {
ID string
Key string
Value interface{}
Type string
}
func NewAttribute(key string, value interface{}, attributeType string) *Attribute {
func NewAttribute(key string, value interface{}) *Attribute {
return &Attribute{
ID: uuid.New().String(),
Key: key,
Value: value,
Type: attributeType,
Type: reflect.TypeOf(value).String(),
}
}
@ -28,6 +32,7 @@ func FromDatabaseAttribute(db *storage.DB_PAttribute) *Attribute {
}
return &Attribute{
ID: db.ID,
Key: db.Key,
Value: value,
Type: db.Type,
@ -41,9 +46,14 @@ func (a *Attribute) ToDatabase(profileId string) *storage.DB_PAttribute {
}
return &storage.DB_PAttribute{
ID: a.ID,
ProfileID: profileId,
Key: a.Key,
Key: a.Key,
ValueJSON: string(value),
Type: a.Type,
Type: a.Type,
}
}
func (a *Attribute) Delete() {
storage.Repo.DeleteAttribute(a.ID)
}

View File

@ -57,9 +57,11 @@ func (g *Gift) FillLoot(loot []*Item) {
}
func (g *Gift) Delete() {
g.Quantity = 0
g.Loot = []*Item{}
//storage.Repo.DeleteGift(g.ID)
for _, item := range g.Loot {
item.DeleteLoot()
}
storage.Repo.DeleteGift(g.ID)
}
func (g *Gift) ToDatabase(profileId string) *storage.DB_Gift {

View File

@ -105,8 +105,11 @@ func (i *Item) GetAttribute(attribute string) interface{} {
}
func (i *Item) Delete() {
//storage.Repo.DeleteItem(i.ID)
i.Quantity = 0
storage.Repo.DeleteItem(i.ID)
}
func (i *Item) DeleteLoot() {
storage.Repo.DeleteLoot(i.ID)
}
func (i *Item) NewChannel(channel string, owned []string, active string) *VariantChannel {
@ -204,6 +207,7 @@ func (i *Item) Snapshot() ItemSnapshot {
}
type VariantChannel struct {
ID string
ItemID string
Channel string
Owned []string
@ -212,6 +216,7 @@ type VariantChannel struct {
func FromDatabaseVariant(variant *storage.DB_VariantChannel) *VariantChannel {
return &VariantChannel{
ID: variant.ID,
ItemID: variant.ItemID,
Channel: variant.Channel,
Owned: variant.Owned,
@ -221,6 +226,7 @@ func FromDatabaseVariant(variant *storage.DB_VariantChannel) *VariantChannel {
func (v *VariantChannel) ToDatabase() *storage.DB_VariantChannel {
return &storage.DB_VariantChannel{
ID: v.ID,
ItemID: v.ItemID,
Channel: v.Channel,
Owned: v.Owned,

View File

@ -3,7 +3,6 @@ package person
import (
"fmt"
"github.com/ectrc/snow/aid"
"github.com/ectrc/snow/storage"
"github.com/google/uuid"
"github.com/r3labs/diff/v3"
@ -119,7 +118,7 @@ func (p *Profile) Diff(snapshot *ProfileSnapshot) []diff.Change {
return nil
}
aid.PrintJSON(changes)
// aid.PrintJSON(changes)
for _, change := range changes {
switch change.Path[0] {

View File

@ -37,9 +37,7 @@ func FromDatabaseQuest(quest *storage.DB_Quest, profileType *string) *Quest {
}
func (q *Quest) Delete() {
//storage.Repo.DeleteQuest(q.ID)
q.ObjectiveCounts = []int64{}
q.Objectives = []string{}
storage.Repo.DeleteQuest(q.ID)
}
func (q *Quest) AddObjective(objective string, count int64) {

View File

@ -22,6 +22,12 @@ func (m *ItemMutex) AddItem(item *Item) {
}
func (m *ItemMutex) DeleteItem(id string) {
item := m.GetItem(id)
if item == nil {
return
}
item.Delete()
m.Delete(id)
// storage.Repo.DeleteItem(id)
}

View File

@ -25,6 +25,10 @@ func (s *PostgresStorage) Migrate(table interface{}, tableName string) {
s.Postgres.Table(tableName).AutoMigrate(table)
}
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;`)
}
func (s *PostgresStorage) GetPerson(personId string) *DB_Person {
var dbPerson DB_Person
s.Postgres.
@ -64,6 +68,26 @@ func (s *PostgresStorage) SavePerson(person *DB_Person) {
s.Postgres.Save(person)
}
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;`)
func (s *PostgresStorage) DeleteItem(itemId string) {
s.Postgres.Delete(&DB_Item{}, "id = ?", itemId)
}
func (s *PostgresStorage) DeleteVariant(variantId string) {
s.Postgres.Delete(&DB_VariantChannel{}, "id = ?", variantId)
}
func (s *PostgresStorage) DeleteQuest(questId string) {
s.Postgres.Delete(&DB_Quest{}, "id = ?", questId)
}
func (s *PostgresStorage) DeleteLoot(lootId string) {
s.Postgres.Delete(&DB_Loot{}, "id = ?", lootId)
}
func (s *PostgresStorage) DeleteGift(giftId string) {
s.Postgres.Delete(&DB_Gift{}, "id = ?", giftId)
}
func (s *PostgresStorage) DeleteAttribute(attributeId string) {
s.Postgres.Delete(&DB_PAttribute{}, "id = ?", attributeId)
}

View File

@ -11,6 +11,13 @@ type Storage interface {
GetPerson(personId string) *DB_Person
GetAllPersons() []*DB_Person
SavePerson(person *DB_Person)
DeleteItem(itemId string)
DeleteVariant(variantId string)
DeleteQuest(questId string)
DeleteLoot(lootId string)
DeleteGift(giftId string)
DeleteAttribute(attributeId string)
}
type Repository struct {
@ -46,3 +53,27 @@ func (r *Repository) SavePerson(person *DB_Person) {
Cache.SavePerson(person)
r.Storage.SavePerson(person)
}
func (r *Repository) DeleteItem(itemId string) {
r.Storage.DeleteItem(itemId)
}
func (r *Repository) DeleteVariant(variantId string) {
r.Storage.DeleteVariant(variantId)
}
func (r *Repository) DeleteQuest(questId string) {
r.Storage.DeleteQuest(questId)
}
func (r *Repository) DeleteLoot(lootId string) {
r.Storage.DeleteLoot(lootId)
}
func (r *Repository) DeleteGift(giftId string) {
r.Storage.DeleteGift(giftId)
}
func (r *Repository) DeleteAttribute(attributeId string) {
r.Storage.DeleteAttribute(attributeId)
}

View File

@ -53,8 +53,9 @@ func (DB_Profile) TableName() string {
}
type DB_PAttribute struct {
Key string `gorm:"primary_key"`
ID string `gorm:"primary_key"`
ProfileID string
Key string
ValueJSON string
Type string
}
@ -78,10 +79,11 @@ func (DB_Item) TableName() string {
}
type DB_VariantChannel struct {
Channel string `gorm:"primary_key"`
ID string `gorm:"primary_key"`
ItemID string
Channel string
Owned pq.StringArray `gorm:"type:text[]"`
Active string
ItemID string
}
func (DB_VariantChannel) TableName() string {