Beginning to systematically add CRUD database functions.
This commit is contained in:
parent
98a53d6590
commit
20106b1f3f
3 changed files with 291 additions and 33 deletions
|
@ -105,6 +105,7 @@ func CreateOrUpdateUser(context *gin.Context) {
|
|||
AvatarDecoration: currentDiscordUser.Avatar_Decoration_Data.Asset,
|
||||
LoginToken: string(oauthTokenJSON),
|
||||
LoggedIn: true,
|
||||
ApiKey: nil,
|
||||
}
|
||||
if databasecommands.GetDatabaseUserExists(GlobalDatabase, currentDiscordUser.Id) {
|
||||
dbOAuthToken := databasecommands.GetDatabaseUserToken(GlobalDatabase, currentDiscordUser.Id)
|
||||
|
|
|
@ -44,6 +44,14 @@ func CreateDatabaseUser(db *gorm.DB, user databasemodels.User) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabasePerson(db *gorm.DB, person databasemodels.Person) error {
|
||||
result := db.Create(&person)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseGroup(db *gorm.DB, group databasemodels.Group) error {
|
||||
result := db.Create(&group)
|
||||
if result.Error != nil {
|
||||
|
@ -52,8 +60,39 @@ func CreateDatabaseGroup(db *gorm.DB, group databasemodels.Group) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseCharacter(db *gorm.DB, character databasemodels.Character) error {
|
||||
result := db.Create(&character)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseRole(db *gorm.DB, role databasemodels.Role) error {
|
||||
result := db.Create(&role)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseTier(db *gorm.DB, tier databasemodels.Tier) error {
|
||||
result := db.Create(&tier)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseFunctionSet(db *gorm.DB, functionSet databasemodels.FunctionSet) error {
|
||||
result := db.Create(&functionSet)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseFunction(db *gorm.DB, function databasemodels.Function) error {
|
||||
log.Println(function)
|
||||
result := db.Create(&function)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -69,6 +108,46 @@ func CreateDatabaseFunctionTag(db *gorm.DB, functionTag databasemodels.FunctionT
|
|||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseInventorySlot(db *gorm.DB, inventorySlot databasemodels.InventorySlot) error {
|
||||
result := db.Create(&inventorySlot)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseItem(db *gorm.DB, item databasemodels.Item) error {
|
||||
result := db.Create(&item)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseItemTag(db *gorm.DB, itemTag databasemodels.ItemTag) error {
|
||||
result := db.Create(&itemTag)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseCustomization(db *gorm.DB, customization databasemodels.Customization) error {
|
||||
result := db.Create(&customization)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDatabaseSchematic(db *gorm.DB, schematic databasemodels.Schematic) error {
|
||||
result := db.Create(&schematic)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read Functions
|
||||
|
||||
func GetDatabaseUserToken(db *gorm.DB, id string) string {
|
||||
|
@ -103,8 +182,7 @@ func GetDatabaseUserLoggedIn(db *gorm.DB, oauthTokenJSON string) bool {
|
|||
|
||||
func GetDatabaseUserLoggedInFromDiscordId(db *gorm.DB, discordId string) bool {
|
||||
var queryUser databasemodels.User
|
||||
var result *gorm.DB
|
||||
result = db.Where("id = ?", discordId).Take(&queryUser)
|
||||
result := db.Where("id = ?", discordId).Take(&queryUser)
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return false
|
||||
} else {
|
||||
|
@ -112,6 +190,24 @@ func GetDatabaseUserLoggedInFromDiscordId(db *gorm.DB, discordId string) bool {
|
|||
}
|
||||
}
|
||||
|
||||
func GetDatabasePerson(db *gorm.DB, inputPerson string) databasemodels.Person {
|
||||
var outputPerson databasemodels.Person
|
||||
result := db.Model(&databasemodels.Person{}).Where("name = ?", inputPerson).Take(&outputPerson)
|
||||
if result.Error != nil {
|
||||
return databasemodels.Person{}
|
||||
}
|
||||
return outputPerson
|
||||
}
|
||||
|
||||
func GetDatabasePersons(db *gorm.DB) []databasemodels.Person {
|
||||
var outputPersons []databasemodels.Person
|
||||
db.Find(&outputPersons)
|
||||
for index, outputPerson := range outputPersons {
|
||||
db.Model(&outputPerson).Association("Groups").Find(&outputPersons[index].Groups)
|
||||
}
|
||||
return outputPersons
|
||||
}
|
||||
|
||||
func GetDatabaseGroup(db *gorm.DB, inputGroup string) databasemodels.Group {
|
||||
var outputGroup databasemodels.Group
|
||||
result := db.Model(&databasemodels.Group{}).Where("name = ?", inputGroup).Take(&outputGroup)
|
||||
|
@ -135,6 +231,64 @@ func GetDatabaseGroups(db *gorm.DB) []string {
|
|||
}
|
||||
}
|
||||
|
||||
func GetDatabaseCharacter(db *gorm.DB, inputCharacter string) databasemodels.Character {
|
||||
var outputCharacter databasemodels.Character
|
||||
result := db.Model(&databasemodels.Character{}).Where("name = ?", inputCharacter).Take(&outputCharacter)
|
||||
if result.Error != nil {
|
||||
return databasemodels.Character{}
|
||||
}
|
||||
return outputCharacter
|
||||
}
|
||||
|
||||
func GetDatabaseCharacters(db *gorm.DB) []databasemodels.Character {
|
||||
var outputCharacters []databasemodels.Character
|
||||
db.Find(&outputCharacters)
|
||||
for index, outputCharacter := range outputCharacters {
|
||||
db.Model(&outputCharacter).Association("Owners").Find(&outputCharacters[index].Owners)
|
||||
db.Model(&outputCharacter).Association("Roles").Find(&outputCharacters[index].Roles)
|
||||
db.Model(&outputCharacter).Association("FunctionSets").Find(&outputCharacters[index].FunctionSets)
|
||||
db.Model(&outputCharacter).Association("Inventory").Find(&outputCharacters[index].Inventory)
|
||||
}
|
||||
return outputCharacters
|
||||
}
|
||||
|
||||
func GetDatabaseRole(db *gorm.DB, inputRole string) databasemodels.Role {
|
||||
var outputRole databasemodels.Role
|
||||
result := db.Model(&databasemodels.Role{}).Where("name = ?", inputRole).Take(&outputRole)
|
||||
if result.Error != nil {
|
||||
return databasemodels.Role{}
|
||||
}
|
||||
return outputRole
|
||||
}
|
||||
|
||||
func GetDatabaseRoles(db *gorm.DB) []databasemodels.Role {
|
||||
var outputRoles []databasemodels.Role
|
||||
db.Find(&outputRoles)
|
||||
for index, outputRole := range outputRoles {
|
||||
db.Model(&outputRole).Association("Tiers").Find(&outputRoles[index].Tiers)
|
||||
db.Model(&outputRole).Association("Visibility").Find(&outputRoles[index].Visibility)
|
||||
}
|
||||
return outputRoles
|
||||
}
|
||||
|
||||
func GetDatabaseTier(db *gorm.DB, inputTier int) databasemodels.Tier {
|
||||
var outputTier databasemodels.Tier
|
||||
result := db.Model(&databasemodels.Tier{}).Where("id = ?", inputTier).Take(&outputTier)
|
||||
if result.Error != nil {
|
||||
return databasemodels.Tier{}
|
||||
}
|
||||
return outputTier
|
||||
}
|
||||
|
||||
func GetDatabaseTiers(db *gorm.DB) []databasemodels.Tier {
|
||||
var outputTiers []databasemodels.Tier
|
||||
db.Find(&outputTiers)
|
||||
for index, outputTier := range outputTiers {
|
||||
db.Model(&outputTier).Association("FunctionSets").Find(&outputTiers[index].FunctionSets)
|
||||
}
|
||||
return outputTiers
|
||||
}
|
||||
|
||||
func GetDatabaseFunction(db *gorm.DB, inputFunction string) databasemodels.Function {
|
||||
var outputFunction databasemodels.Function
|
||||
result := db.Model(&databasemodels.Function{}).Where("name = ?", inputFunction).Take(&outputFunction)
|
||||
|
@ -147,15 +301,10 @@ func GetDatabaseFunction(db *gorm.DB, inputFunction string) databasemodels.Funct
|
|||
func GetDatabaseFunctions(db *gorm.DB) []databasemodels.Function {
|
||||
var outputFunctions []databasemodels.Function
|
||||
db.Find(&outputFunctions)
|
||||
for outputFunctionId, outputFunction := range outputFunctions {
|
||||
var outputFunctionTags []databasemodels.FunctionTag
|
||||
db.Model(&outputFunction).Association("Tags").Find(&outputFunctionTags)
|
||||
var outputFunctionRequirements []databasemodels.Function
|
||||
db.Model(&outputFunction).Association("Requirements").Find(&outputFunctionRequirements)
|
||||
outputFunctions[outputFunctionId].Tags = outputFunctionTags
|
||||
outputFunctions[outputFunctionId].Requirements = outputFunctionRequirements
|
||||
for index, outputFunction := range outputFunctions {
|
||||
db.Model(&outputFunction).Association("Tags").Find(&outputFunctions[index].Tags)
|
||||
db.Model(&outputFunction).Association("Requirements").Find(&outputFunctions[index].Requirements)
|
||||
}
|
||||
log.Printf("Functions: %v", outputFunctions)
|
||||
return outputFunctions
|
||||
}
|
||||
|
||||
|
@ -196,4 +345,108 @@ func UpdateDatabaseUser(db *gorm.DB, user databasemodels.User) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabasePerson(db *gorm.DB, person databasemodels.Person) error {
|
||||
result := db.Save(&person)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseGroup(db *gorm.DB, group databasemodels.Group) error {
|
||||
result := db.Save(&group)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseCharacter(db *gorm.DB, character databasemodels.Character) error {
|
||||
result := db.Save(&character)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseRole(db *gorm.DB, role databasemodels.Role) error {
|
||||
result := db.Save(&role)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseTier(db *gorm.DB, tier databasemodels.Tier) error {
|
||||
result := db.Save(&tier)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseFunctionSet(db *gorm.DB, functionSet databasemodels.FunctionSet) error {
|
||||
result := db.Save(&functionSet)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseFunction(db *gorm.DB, function databasemodels.Function) error {
|
||||
result := db.Save(&function)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseFunctionTag(db *gorm.DB, functionTag databasemodels.FunctionTag) error {
|
||||
result := db.Save(&functionTag)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseInventorySlot(db *gorm.DB, inventoySlot databasemodels.InventorySlot) error {
|
||||
result := db.Save(&inventoySlot)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseItem(db *gorm.DB, item databasemodels.Item) error {
|
||||
result := db.Save(&item)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseItemTag(db *gorm.DB, itemTag databasemodels.ItemTag) error {
|
||||
result := db.Save(&itemTag)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseCustomization(db *gorm.DB, customization databasemodels.Customization) error {
|
||||
result := db.Save(&customization)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseSchematic(db *gorm.DB, schematic databasemodels.Schematic) error {
|
||||
result := db.Save(&schematic)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete Functions
|
||||
|
|
|
@ -11,89 +11,93 @@ type User struct {
|
|||
AvatarDecoration string `json:"avatar_decoration"`
|
||||
LoginToken string `json:"login_token"`
|
||||
LoggedIn bool `json:"logged_in"`
|
||||
ApiKey []struct {
|
||||
Name string `gorm:"primaryKey uniqueIndex" json:"name"`
|
||||
Key string `json:"key"`
|
||||
} `gorm:"foreignKey:Name" json:"api_key"`
|
||||
}
|
||||
|
||||
type Person struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Name string `gorm:"primaryKey" json:"name"`
|
||||
Groups []Group `gorm:"many2many:person_group_associations" json:"groups"` // Unique
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Name string `gorm:"primaryKey" json:"name"`
|
||||
}
|
||||
|
||||
type Character struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"uniqueIndex" json:"name"`
|
||||
Name string `gorm:"primaryKey; uniqueIndex" json:"name"`
|
||||
Owners []Person `gorm:"many2many:character_owner_associations" json:"owners"` // Unique
|
||||
Roles []Role `gorm:"many2many:character_role_associations" json:"roles"` // Unique
|
||||
FunctionSets []FunctionSet `gorm:"foreignkey:id; references:Name" json:"function_sets"`
|
||||
Inventory []InventorySlot `gorm:"foreignkey:id; references:Name" json:"inventory"`
|
||||
FunctionSets []FunctionSet `gorm:"many2many:character_functionset_associations" json:"function_sets"`
|
||||
Inventory []InventorySlot `gorm:"many2many:character_inventory_associations" json:"inventory"`
|
||||
}
|
||||
|
||||
type Role struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Tiers []Tier `gorm:"foreignkey:id; references:Name" json:"tiers"`
|
||||
Visibility []Group `gorm:"foreignkey:Name" json:"visibility"` // Unique
|
||||
Name string `gorm:"primaryKey" json:"name"`
|
||||
Tiers []Tier `gorm:"many2many:role_tier_associations" json:"tiers"`
|
||||
Visibility []Group `gorm:"many2many:role_visibility_associations" json:"visibility"` // Unique
|
||||
}
|
||||
|
||||
type Tier struct {
|
||||
gorm.Model
|
||||
FunctionSets []FunctionSet `gorm:"foreignkey:id; references:id" json:"function_sets"`
|
||||
FunctionSets []FunctionSet `gorm:"many2many:tier_functionset_associations" json:"function_sets"`
|
||||
}
|
||||
|
||||
type FunctionSet struct {
|
||||
gorm.Model
|
||||
Functions []Function `gorm:"foreignkey:Name" json:"functions"`
|
||||
Functions []Function `gorm:"many2many:functionset_function_associations" json:"functions"`
|
||||
}
|
||||
|
||||
type Function struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"primaryKey" gorm:"uniqueIndex" json:"name"`
|
||||
Name string `gorm:"primaryKey uniqueIndex" json:"name"`
|
||||
Tags []FunctionTag `gorm:"many2many:function_tag_associations" json:"tags"`
|
||||
Requirements []Function `gorm:"many2many:function_requirement_associations" json:"requirements"`
|
||||
}
|
||||
|
||||
type FunctionTag struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"primaryKey" gorm:"uniqueIndex" json:"name"`
|
||||
Name string `gorm:"primaryKey uniqueIndex" json:"name"`
|
||||
}
|
||||
|
||||
type InventorySlot struct {
|
||||
gorm.Model
|
||||
Item Item `gorm:"foreignkey:Name" json:"item"`
|
||||
Item Item `gorm:"foreignKey:Name" json:"item"`
|
||||
Quantity int64 `json:"quantity"` // Positive
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Functions []Function `gorm:"foreignkey:Name" json:"functions"`
|
||||
Name string `gorm:"primaryKey uniqueIndex" json:"name"`
|
||||
Functions []Function `gorm:"many2many:item_function_associations" json:"functions"`
|
||||
FlavorText string `json:"flavor_text"`
|
||||
RulesDescription string `json:"rules_description"`
|
||||
PhysrepRequirements string `json:"physrep_requirements"`
|
||||
Tags []ItemTag `gorm:"foreignkey:Name" json:"tags"` // Unique
|
||||
Customizations []Customization `gorm:"foreignkey:Name" json:"customizations"`
|
||||
Visibility []Group `gorm:"foreignkey:Name" json:"visibility"` // Unique
|
||||
Tags []ItemTag `gorm:"many2many:item_tag_associations" json:"tags"` // Unique
|
||||
Customizations []Customization `gorm:"many2many:item_customization_associations" json:"customizations"`
|
||||
Visibility []Group `gorm:"many2many:item_visibility_associations" json:"visibility"` // Unique
|
||||
}
|
||||
|
||||
type ItemTag struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"primaryKey" json:"name"`
|
||||
Name string `gorm:"primaryKey uniqueIndex" json:"name"`
|
||||
}
|
||||
|
||||
type Customization struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Functions []Function `gorm:"foreignkey:Name" json:"functions"`
|
||||
Functions []Function `gorm:"many2many:customization_function_associations" json:"functions"`
|
||||
FlavorText string `json:"flavor_text"`
|
||||
RulesDescription string `json:"rules_description"`
|
||||
PhysrepRequirements string `json:"physrep_requirements"`
|
||||
Tags []ItemTag `gorm:"foreignkey:Name" json:"tags"` // Unique
|
||||
Visibility []Group `gorm:"foreignkey:Name" json:"visibility"` // Unique
|
||||
Tags []ItemTag `gorm:"many2many:customization_tag_associations" json:"tags"` // Unique
|
||||
Visibility []Group `gorm:"many2many:customization_visibility_associations" json:"visibility"` // Unique
|
||||
}
|
||||
|
||||
type Schematic struct {
|
||||
|
|
Loading…
Reference in a new issue