Biiiiig updates to the API and methods.
This commit is contained in:
parent
3756e275d3
commit
dcbe844c41
14 changed files with 752 additions and 252 deletions
|
@ -12,25 +12,24 @@ func main() {
|
||||||
api.GlobalDatabase = database.InitializeDatabase()
|
api.GlobalDatabase = database.InitializeDatabase()
|
||||||
api.GlobalConfig.ParseConfig("../config.toml")
|
api.GlobalConfig.ParseConfig("../config.toml")
|
||||||
api.GlobalOAuth = authdiscord.CreateDiscordOAuthConfig(api.GlobalConfig)
|
api.GlobalOAuth = authdiscord.CreateDiscordOAuthConfig(api.GlobalConfig)
|
||||||
app := gin.Default()
|
router := gin.Default()
|
||||||
// Authentication Workflow
|
// Authentication Workflow
|
||||||
app.GET("/auth/callback", api.AuthCallback)
|
router.GET("/auth/callback", api.AuthCallback)
|
||||||
app.GET("/auth/login", api.AuthLoginRedirect)
|
router.GET("/auth/login", api.AuthLoginRedirect)
|
||||||
app.GET("/auth/logout", api.AuthLogoutRedirect)
|
router.GET("/auth/logout", api.AuthLogoutRedirect)
|
||||||
// Create & Update
|
// Create
|
||||||
app.POST("/post/user/update", api.CreateOrUpdateUser)
|
router.POST("/user/update", api.CreateOrUpdateUser)
|
||||||
app.POST("/post/group", api.CreateGroup)
|
router.POST("/group", api.CreateGroup)
|
||||||
app.POST("/post/function", api.CreateFunction)
|
router.POST("/function", api.CreateFunction)
|
||||||
app.POST("/post/function-tag", api.CreateFunctionTag)
|
router.POST("/function-tag", api.CreateFunctionTag)
|
||||||
|
// Update
|
||||||
|
router.PUT("/function", api.UpdateFunction)
|
||||||
// Read
|
// Read
|
||||||
app.GET("/get/user/info", api.GetDiscordUser)
|
router.GET("/user/info", api.GetDiscordUser)
|
||||||
app.GET("/get/user/authorized", api.GetUserLoggedIn)
|
router.GET("/user/authorized", api.GetUserLoggedIn)
|
||||||
app.GET("/get/groups", api.GetGroups)
|
router.GET("/:object", api.GetObjects)
|
||||||
app.GET("/get/all/groups", api.GetAllGroups)
|
router.GET("/all/:object", api.GetAllObjects)
|
||||||
app.GET("/get/functions", api.GetFunctions)
|
|
||||||
app.GET("/get/all/functions", api.GetAllFunctions)
|
|
||||||
app.GET("/get/function-tags", api.GetFunctionTags)
|
|
||||||
app.GET("/get/all/function-tags", api.GetAllFunctionTags)
|
|
||||||
// Delete
|
// Delete
|
||||||
app.Run(":31337")
|
router.DELETE("/function", api.DeleteFunction)
|
||||||
|
router.Run(":31337")
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,23 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
authdiscord "example.com/auth/discord"
|
authdiscord "example.com/auth/discord"
|
||||||
configserver "example.com/config/server"
|
configserver "example.com/config/server"
|
||||||
|
character "example.com/database/character"
|
||||||
|
customization "example.com/database/customization"
|
||||||
function "example.com/database/function"
|
function "example.com/database/function"
|
||||||
|
functionset "example.com/database/functionset"
|
||||||
functiontag "example.com/database/functiontag"
|
functiontag "example.com/database/functiontag"
|
||||||
group "example.com/database/group"
|
group "example.com/database/group"
|
||||||
|
inventoryslot "example.com/database/inventoryslot"
|
||||||
|
item "example.com/database/item"
|
||||||
|
itemtag "example.com/database/itemtag"
|
||||||
|
person "example.com/database/person"
|
||||||
|
role "example.com/database/role"
|
||||||
|
schematic "example.com/database/schematic"
|
||||||
|
tier "example.com/database/tier"
|
||||||
user "example.com/database/user"
|
user "example.com/database/user"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -25,6 +36,19 @@ var GlobalOAuth *oauth2.Config
|
||||||
|
|
||||||
// Private Functions
|
// Private Functions
|
||||||
|
|
||||||
|
func objectIDStringsToInts(context *gin.Context, objectIDs []string) *[]int {
|
||||||
|
var objectIDInts []int
|
||||||
|
for _, objectID := range objectIDs {
|
||||||
|
objectIDInt, err := strconv.Atoi(objectID)
|
||||||
|
if err != nil {
|
||||||
|
objectIDInts = append(objectIDInts, objectIDInt)
|
||||||
|
} else {
|
||||||
|
context.AbortWithStatus(http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &objectIDInts
|
||||||
|
}
|
||||||
|
|
||||||
// Authentication Workflow
|
// Authentication Workflow
|
||||||
|
|
||||||
func AuthCallback(context *gin.Context) {
|
func AuthCallback(context *gin.Context) {
|
||||||
|
@ -161,6 +185,24 @@ func CreateFunctionTag(context *gin.Context) {
|
||||||
|
|
||||||
// Update Endpoints (put/)
|
// Update Endpoints (put/)
|
||||||
|
|
||||||
|
func UpdateFunction(context *gin.Context) {
|
||||||
|
GetUserLoggedIn(context)
|
||||||
|
isAuthorized := context.GetBool("is-authorized")
|
||||||
|
if isAuthorized {
|
||||||
|
name, nameOk := context.GetQuery("name")
|
||||||
|
tags := context.QueryArray("tags")
|
||||||
|
requirements := context.QueryArray("requirements")
|
||||||
|
if nameOk {
|
||||||
|
function.Update(GlobalDatabase, name, tags, requirements)
|
||||||
|
context.Status(http.StatusOK)
|
||||||
|
} else {
|
||||||
|
context.AbortWithStatus(http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
context.AbortWithStatus(http.StatusUnauthorized)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Read Endpoints (get/)
|
// Read Endpoints (get/)
|
||||||
|
|
||||||
func GetDiscordUser(context *gin.Context) {
|
func GetDiscordUser(context *gin.Context) {
|
||||||
|
@ -216,55 +258,143 @@ func GetUserLoggedIn(context *gin.Context) {
|
||||||
context.Set("is-authorized", false)
|
context.Set("is-authorized", false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGroups(context *gin.Context) {
|
func GetObjects(context *gin.Context) {
|
||||||
groupNames, ok := context.GetQueryArray("groups")
|
objectIDs, idOk := context.GetQueryArray("id")
|
||||||
if ok {
|
if idOk {
|
||||||
context.JSON(http.StatusOK, gin.H{
|
switch objectType := context.Param("object"); objectType {
|
||||||
"groups": group.Get(GlobalDatabase, groupNames),
|
case "persons":
|
||||||
})
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"persons": person.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "groups":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"groups": group.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "characters":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"characters": character.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "roles":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"roles": role.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "tiers":
|
||||||
|
objectIDInts := objectIDStringsToInts(context, objectIDs)
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"tiers": tier.Get(GlobalDatabase, *objectIDInts),
|
||||||
|
})
|
||||||
|
case "function-sets":
|
||||||
|
objectIDInts := objectIDStringsToInts(context, objectIDs)
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"function_sets": functionset.Get(GlobalDatabase, *objectIDInts),
|
||||||
|
})
|
||||||
|
case "functions":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"functions": function.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "function-tags":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"function_tags": functiontag.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "inventory-slot":
|
||||||
|
objectIDInts := objectIDStringsToInts(context, objectIDs)
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"inventory_slot": inventoryslot.Get(GlobalDatabase, *objectIDInts),
|
||||||
|
})
|
||||||
|
case "items":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"items": item.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "item-tags":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"item_tags": itemtag.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "customizations":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"customizations": customization.Get(GlobalDatabase, objectIDs),
|
||||||
|
})
|
||||||
|
case "schematics":
|
||||||
|
objectIDInts := objectIDStringsToInts(context, objectIDs)
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"schematics": schematic.Get(GlobalDatabase, *objectIDInts),
|
||||||
|
})
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
context.Status(http.StatusBadRequest)
|
context.Status(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllGroups(context *gin.Context) {
|
func GetAllObjects(context *gin.Context) {
|
||||||
context.JSON(http.StatusOK, gin.H{
|
switch objectType := context.Param("object"); objectType {
|
||||||
"groups": group.GetAll(GlobalDatabase),
|
case "persons":
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetFunctions(context *gin.Context) {
|
|
||||||
functionNames, ok := context.GetQueryArray("functions")
|
|
||||||
if ok {
|
|
||||||
context.JSON(http.StatusOK, gin.H{
|
context.JSON(http.StatusOK, gin.H{
|
||||||
"functions": function.Get(GlobalDatabase, functionNames),
|
"persons": person.GetAll(GlobalDatabase),
|
||||||
})
|
})
|
||||||
} else {
|
case "groups":
|
||||||
context.Status(http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAllFunctions(context *gin.Context) {
|
|
||||||
context.JSON(http.StatusOK, gin.H{
|
|
||||||
"functions": function.GetAll(GlobalDatabase),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetFunctionTags(context *gin.Context) {
|
|
||||||
functionTagNames, ok := context.GetQueryArray("functiontags")
|
|
||||||
if ok {
|
|
||||||
context.JSON(http.StatusOK, gin.H{
|
context.JSON(http.StatusOK, gin.H{
|
||||||
"function_tags": functiontag.Get(GlobalDatabase, functionTagNames),
|
"groups": group.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "characters":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"characters": character.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "roles":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"roles": role.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "tiers":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"tiers": tier.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "function-sets":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"function_sets": functionset.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "functions":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"functions": function.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "function-tags":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"function_tags": functiontag.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "inventory-slot":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"inventory_slot": inventoryslot.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "items":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"items": item.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "item-tags":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"item_tags": itemtag.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "customizations":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"customizations": customization.GetAll(GlobalDatabase),
|
||||||
|
})
|
||||||
|
case "schematics":
|
||||||
|
context.JSON(http.StatusOK, gin.H{
|
||||||
|
"schematics": schematic.GetAll(GlobalDatabase),
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
context.Status(http.StatusBadRequest)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllFunctionTags(context *gin.Context) {
|
|
||||||
context.JSON(http.StatusOK, gin.H{
|
|
||||||
"function_tags": functiontag.GetAll(GlobalDatabase),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete Endpoints (delete/)
|
// Delete Endpoints (delete/)
|
||||||
|
|
||||||
|
func DeleteFunction(context *gin.Context) {
|
||||||
|
GetUserLoggedIn(context)
|
||||||
|
isAuthorized := context.GetBool("is-authorized")
|
||||||
|
if isAuthorized {
|
||||||
|
functionNames, ok := context.GetQueryArray("name")
|
||||||
|
if ok {
|
||||||
|
function.Delete(GlobalDatabase, functionNames)
|
||||||
|
context.Status(http.StatusOK)
|
||||||
|
} else {
|
||||||
|
context.AbortWithStatus(http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
context.AbortWithStatus(http.StatusUnauthorized)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -37,6 +37,16 @@ replace example.com/config/server => ../config/server
|
||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
example.com/database/character v0.0.0
|
||||||
|
example.com/database/customization v0.0.0
|
||||||
|
example.com/database/functionset v0.0.0
|
||||||
|
example.com/database/inventoryslot v0.0.0
|
||||||
|
example.com/database/item v0.0.0
|
||||||
|
example.com/database/itemtag v0.0.0
|
||||||
|
example.com/database/person v0.0.0
|
||||||
|
example.com/database/role v0.0.0
|
||||||
|
example.com/database/schematic v0.0.0
|
||||||
|
example.com/database/tier v0.0.0
|
||||||
example.com/auth/discord v0.0.0
|
example.com/auth/discord v0.0.0
|
||||||
example.com/config/server v0.0.0
|
example.com/config/server v0.0.0
|
||||||
example.com/database/function v0.0.0
|
example.com/database/function v0.0.0
|
||||||
|
|
|
@ -20,7 +20,7 @@ type Character struct {
|
||||||
Inventory []inventoryslot.InventorySlot `gorm:"many2many:character_inventory_associations" json:"inventory"`
|
Inventory []inventoryslot.InventorySlot `gorm:"many2many:character_inventory_associations" json:"inventory"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDatabaseCharacter(db *gorm.DB, character Character) error {
|
func (character Character) Create(db *gorm.DB) error {
|
||||||
result := db.Create(&character)
|
result := db.Create(&character)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
|
@ -28,43 +28,90 @@ func CreateDatabaseCharacter(db *gorm.DB, character Character) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseCharacter(db *gorm.DB, inputCharacter string) Character {
|
func (character Character) getAssociations(db *gorm.DB) {
|
||||||
var outputCharacter Character
|
db.Model(&character).Association("Owners").Find(&character.Owners)
|
||||||
result := db.Model(&Character{}).Where("name = ?", inputCharacter).Take(&outputCharacter)
|
db.Model(&character).Association("Roles").Find(&character.Roles)
|
||||||
if result.Error != nil {
|
db.Model(&character).Association("FunctionSets").Find(&character.FunctionSets)
|
||||||
return Character{}
|
db.Model(&character).Association("Inventory").Find(&character.Inventory)
|
||||||
}
|
|
||||||
db.Model(&outputCharacter).Association("Owners").Find(&outputCharacter.Owners)
|
|
||||||
db.Model(&outputCharacter).Association("Roles").Find(&outputCharacter.Roles)
|
|
||||||
db.Model(&outputCharacter).Association("FunctionSets").Find(&outputCharacter.FunctionSets)
|
|
||||||
db.Model(&outputCharacter).Association("Inventory").Find(&outputCharacter.Inventory)
|
|
||||||
return outputCharacter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseCharacters(db *gorm.DB, inputCharacters []string) []Character {
|
func (character *Character) Get(db *gorm.DB, inputCharacter string) {
|
||||||
var outputCharacters []Character
|
db.Where("name = ?", inputCharacter).Take(&character)
|
||||||
for _, inputCharacter := range inputCharacters {
|
character.getAssociations(db)
|
||||||
outputCharacters = append(outputCharacters, GetDatabaseCharacter(db, inputCharacter))
|
|
||||||
}
|
|
||||||
return outputCharacters
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllDatabaseCharacters(db *gorm.DB) []Character {
|
func (character Character) Update(db *gorm.DB) error {
|
||||||
var outputCharacters []Character
|
var originalCharacter Character
|
||||||
result := db.Find(&outputCharacters)
|
originalCharacter.Get(db, character.Name)
|
||||||
if result.Error != nil {
|
ownersError := db.Model(&originalCharacter).Association("Owners").Replace(&character.Owners)
|
||||||
log.Println(result.Error)
|
if ownersError != nil {
|
||||||
|
return ownersError
|
||||||
}
|
}
|
||||||
for index, outputCharacter := range outputCharacters {
|
rolesError := db.Model(&originalCharacter).Association("Roles").Replace(&character.Roles)
|
||||||
outputCharacters[index] = GetDatabaseCharacter(db, outputCharacter.Name)
|
if rolesError != nil {
|
||||||
|
return rolesError
|
||||||
}
|
}
|
||||||
return outputCharacters
|
functionSetsError := db.Model(&originalCharacter).Association("FunctionSets").Replace(&character.FunctionSets)
|
||||||
|
if functionSetsError != nil {
|
||||||
|
return functionSetsError
|
||||||
|
}
|
||||||
|
inventoryError := db.Model(&originalCharacter).Association("Inventory").Replace(&character.Inventory)
|
||||||
|
if inventoryError != nil {
|
||||||
|
return inventoryError
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateDatabaseCharacter(db *gorm.DB, character Character) error {
|
func (character Character) Delete(db *gorm.DB) error {
|
||||||
result := db.Save(&character)
|
result := db.Delete(&character)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Create(db *gorm.DB, name string, owners []string, roles []string, functionsets []int, inventory []int) error {
|
||||||
|
return Character{
|
||||||
|
Name: name,
|
||||||
|
Owners: *person.Get(db, owners),
|
||||||
|
Roles: *role.Get(db, roles),
|
||||||
|
FunctionSets: *functionset.Get(db, functionsets),
|
||||||
|
Inventory: *inventoryslot.Get(db, inventory),
|
||||||
|
}.Create(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(db *gorm.DB, inputCharacters []string) *[]Character {
|
||||||
|
var outputCharacters []Character
|
||||||
|
for _, inputCharacter := range inputCharacters {
|
||||||
|
var outputCharacter Character
|
||||||
|
outputCharacter.Get(db, inputCharacter)
|
||||||
|
outputCharacters = append(outputCharacters, outputCharacter)
|
||||||
|
}
|
||||||
|
return &outputCharacters
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAll(db *gorm.DB) *[]Character {
|
||||||
|
var outputCharacterNames []string
|
||||||
|
result := db.Model(&Character{}).Select("name").Find(&outputCharacterNames)
|
||||||
|
if result.Error != nil {
|
||||||
|
log.Println(result.Error)
|
||||||
|
}
|
||||||
|
return Get(db, outputCharacterNames)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(db *gorm.DB, name string, owners []string, roles []string, functionsets []int, inventory []int) error {
|
||||||
|
return Character{
|
||||||
|
Name: name,
|
||||||
|
Owners: *person.Get(db, owners),
|
||||||
|
Roles: *role.Get(db, roles),
|
||||||
|
FunctionSets: *functionset.Get(db, functionsets),
|
||||||
|
Inventory: *inventoryslot.Get(db, inventory),
|
||||||
|
}.Update(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputCharacters []string) {
|
||||||
|
characters := Get(db, inputCharacters)
|
||||||
|
for _, character := range *characters {
|
||||||
|
character.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ type Customization struct {
|
||||||
Visibility []group.Group `gorm:"many2many:customization_visibility_associations" json:"visibility"` // Unique
|
Visibility []group.Group `gorm:"many2many:customization_visibility_associations" json:"visibility"` // Unique
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDatabaseCustomization(db *gorm.DB, customization Customization) error {
|
func (customization Customization) Create(db *gorm.DB) error {
|
||||||
result := db.Create(&customization)
|
result := db.Create(&customization)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
|
@ -29,42 +29,93 @@ func CreateDatabaseCustomization(db *gorm.DB, customization Customization) error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseCustomization(db *gorm.DB, inputCustomization string) Customization {
|
func (customization *Customization) getAssociations(db *gorm.DB) {
|
||||||
var outputCustomization Customization
|
db.Model(&customization).Association("Functions").Find(&customization.Functions)
|
||||||
result := db.Model(&Customization{}).Where("name = ?", inputCustomization).Take(&outputCustomization)
|
db.Model(&customization).Association("Tags").Find(&customization.Tags)
|
||||||
if result.Error != nil {
|
db.Model(&customization).Association("Visibility").Find(&customization.Visibility)
|
||||||
return Customization{}
|
|
||||||
}
|
|
||||||
db.Model(&outputCustomization).Association("Functions").Find(&outputCustomization.Functions)
|
|
||||||
db.Model(&outputCustomization).Association("Tags").Find(&outputCustomization.Tags)
|
|
||||||
db.Model(&outputCustomization).Association("Visibility").Find(&outputCustomization.Visibility)
|
|
||||||
return outputCustomization
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseCustomizations(db *gorm.DB, inputCustomizations []string) []Customization {
|
func (customization *Customization) Get(db *gorm.DB, inputCustomization string) {
|
||||||
var outputCustomizations []Customization
|
db.Where("name = ?", inputCustomization).Take(&customization)
|
||||||
for _, inputCustomization := range inputCustomizations {
|
customization.getAssociations(db)
|
||||||
outputCustomizations = append(outputCustomizations, GetDatabaseCustomization(db, inputCustomization))
|
|
||||||
}
|
|
||||||
return outputCustomizations
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllDatabaseCustomizations(db *gorm.DB) []Customization {
|
func (customization Customization) Update(db *gorm.DB) error {
|
||||||
var outputCustomizations []Customization
|
var originalCustomization Customization
|
||||||
result := db.Find(&outputCustomizations)
|
db.Updates(&Customization{
|
||||||
if result.Error != nil {
|
FlavorText: customization.FlavorText,
|
||||||
log.Println(result.Error)
|
RulesDescription: customization.RulesDescription,
|
||||||
|
PhysrepRequirements: customization.PhysrepRequirements,
|
||||||
|
})
|
||||||
|
functionsError := db.Model(&originalCustomization).Association("Functions").Replace(&customization.Functions)
|
||||||
|
if functionsError != nil {
|
||||||
|
return functionsError
|
||||||
}
|
}
|
||||||
for index, outputCustomization := range outputCustomizations {
|
tagsError := db.Model(&originalCustomization).Association("Tags").Replace(&customization.Tags)
|
||||||
outputCustomizations[index] = GetDatabaseCustomization(db, outputCustomization.Name)
|
if tagsError != nil {
|
||||||
|
return tagsError
|
||||||
}
|
}
|
||||||
return outputCustomizations
|
visibilityError := db.Model(&originalCustomization).Association("Visibility").Replace(&customization.Visibility)
|
||||||
|
if visibilityError != nil {
|
||||||
|
return visibilityError
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateDatabaseCustomization(db *gorm.DB, customization Customization) error {
|
func (customization Customization) Delete(db *gorm.DB) error {
|
||||||
result := db.Save(&customization)
|
result := db.Delete(&customization)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Create(db *gorm.DB, name string, functions []string, flavorText string, rulesDescription string, physrepRequesrements string, itemTags []string, visibility []string) error {
|
||||||
|
return Customization{
|
||||||
|
Name: name,
|
||||||
|
Functions: *function.Get(db, functions),
|
||||||
|
FlavorText: flavorText,
|
||||||
|
RulesDescription: rulesDescription,
|
||||||
|
PhysrepRequirements: physrepRequesrements,
|
||||||
|
Tags: *itemtag.Get(db, itemTags),
|
||||||
|
Visibility: *group.Get(db, visibility),
|
||||||
|
}.Create(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(db *gorm.DB, inputCustomizations []string) *[]Customization {
|
||||||
|
var outputCustomizations []Customization
|
||||||
|
for _, inputCustomization := range inputCustomizations {
|
||||||
|
var outputCustomization Customization
|
||||||
|
outputCustomization.Get(db, inputCustomization)
|
||||||
|
outputCustomizations = append(outputCustomizations, outputCustomization)
|
||||||
|
}
|
||||||
|
return &outputCustomizations
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAll(db *gorm.DB) *[]Customization {
|
||||||
|
var outputCustomizationNames []string
|
||||||
|
result := db.Model(&Customization{}).Select("name").Find(&outputCustomizationNames)
|
||||||
|
if result.Error != nil {
|
||||||
|
log.Println(result.Error)
|
||||||
|
}
|
||||||
|
return Get(db, outputCustomizationNames)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(db *gorm.DB, name string, functions []string, flavorText string, rulesDescription string, physrepRequesrements string, itemTags []string, visibility []string) error {
|
||||||
|
return Customization{
|
||||||
|
Name: name,
|
||||||
|
Functions: *function.Get(db, functions),
|
||||||
|
FlavorText: flavorText,
|
||||||
|
RulesDescription: rulesDescription,
|
||||||
|
PhysrepRequirements: physrepRequesrements,
|
||||||
|
Tags: *itemtag.Get(db, itemTags),
|
||||||
|
Visibility: *group.Get(db, visibility),
|
||||||
|
}.Update(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputCustomizations []string) {
|
||||||
|
customizations := Get(db, inputCustomizations)
|
||||||
|
for _, customization := range *customizations {
|
||||||
|
customization.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -34,7 +34,21 @@ func (function *Function) Get(db *gorm.DB, inputFunction string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (function Function) Update(db *gorm.DB) error {
|
func (function Function) Update(db *gorm.DB) error {
|
||||||
result := db.Save(&function)
|
var originalFunction Function
|
||||||
|
originalFunction.Get(db, function.Name)
|
||||||
|
tagsError := db.Model(&originalFunction).Association("Tags").Replace(&function.Tags)
|
||||||
|
if tagsError != nil {
|
||||||
|
return tagsError
|
||||||
|
}
|
||||||
|
requirementsError := db.Model(&originalFunction).Association("Requirements").Replace(&function.Requirements)
|
||||||
|
if requirementsError != nil {
|
||||||
|
return requirementsError
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (function Function) Delete(db *gorm.DB) error {
|
||||||
|
result := db.Delete(&function)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
|
@ -69,9 +83,18 @@ func GetAll(db *gorm.DB) *[]Function {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Update(db *gorm.DB, name string, tags []string, requirements []string) error {
|
func Update(db *gorm.DB, name string, tags []string, requirements []string) error {
|
||||||
|
log.Println(*functiontag.Get(db, tags))
|
||||||
|
log.Println(*Get(db, requirements))
|
||||||
return Function{
|
return Function{
|
||||||
Name: name,
|
Name: name,
|
||||||
Tags: *functiontag.Get(db, tags),
|
Tags: *functiontag.Get(db, tags),
|
||||||
Requirements: *Get(db, requirements),
|
Requirements: *Get(db, requirements),
|
||||||
}.Update(db)
|
}.Update(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputFunctions []string) {
|
||||||
|
functions := Get(db, inputFunctions)
|
||||||
|
for _, function := range *functions {
|
||||||
|
function.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -38,6 +38,14 @@ func (functionSet FunctionSet) Update(db *gorm.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (functionSet FunctionSet) Delete(db *gorm.DB) error {
|
||||||
|
result := db.Delete(&functionSet)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Create(db *gorm.DB, functions []string) error {
|
func Create(db *gorm.DB, functions []string) error {
|
||||||
return FunctionSet{
|
return FunctionSet{
|
||||||
Functions: *function.Get(db, functions),
|
Functions: *function.Get(db, functions),
|
||||||
|
@ -70,3 +78,10 @@ func Update(db *gorm.DB, id int, functions []string) error {
|
||||||
outputFunctionSet.ID = uint(id)
|
outputFunctionSet.ID = uint(id)
|
||||||
return outputFunctionSet.Update(db)
|
return outputFunctionSet.Update(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputFunctionSets []int) {
|
||||||
|
functionSets := Get(db, inputFunctionSets)
|
||||||
|
for _, functionSet := range *functionSets {
|
||||||
|
functionSet.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ type InventorySlot struct {
|
||||||
Quantity int64 `json:"quantity"` // Positive
|
Quantity int64 `json:"quantity"` // Positive
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDatabaseInventorySlot(db *gorm.DB, inventorySlot InventorySlot) error {
|
func (inventorySlot InventorySlot) Create(db *gorm.DB) error {
|
||||||
result := db.Create(&inventorySlot)
|
result := db.Create(&inventorySlot)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
|
@ -22,40 +22,73 @@ func CreateDatabaseInventorySlot(db *gorm.DB, inventorySlot InventorySlot) error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseInventorySlot(db *gorm.DB, inputInventorySlot int) InventorySlot {
|
func (inventorySlot *InventorySlot) getAssociations(db *gorm.DB) {
|
||||||
var outputInventorySlot InventorySlot
|
db.Model(&inventorySlot).Association("Item").Find(&inventorySlot.Item)
|
||||||
result := db.Model(&InventorySlot{}).Where("id = ?", inputInventorySlot).Take(&outputInventorySlot)
|
|
||||||
if result.Error != nil {
|
|
||||||
return InventorySlot{}
|
|
||||||
}
|
|
||||||
db.Model(&outputInventorySlot).Association("Item").Find(&outputInventorySlot.Item)
|
|
||||||
return outputInventorySlot
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseInventorySlots(db *gorm.DB, inputInventorySlots []int) []InventorySlot {
|
func (inventorySlot *InventorySlot) Get(db *gorm.DB, inputInventorySlot int) {
|
||||||
var outputInventorySlots []InventorySlot
|
db.Where("id = ?", inputInventorySlot).Take(&inventorySlot)
|
||||||
for _, inputInventorySlot := range inputInventorySlots {
|
inventorySlot.getAssociations(db)
|
||||||
outputInventorySlots = append(outputInventorySlots, GetDatabaseInventorySlot(db, inputInventorySlot))
|
|
||||||
}
|
|
||||||
return outputInventorySlots
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllDatabaseInventorySlots(db *gorm.DB) []InventorySlot {
|
func (inventorySlot InventorySlot) Update(db *gorm.DB) error {
|
||||||
var outputInventorySlots []InventorySlot
|
var originalIventorySlot InventorySlot
|
||||||
result := db.Find(&outputInventorySlots)
|
originalIventorySlot.Get(db, int(inventorySlot.ID))
|
||||||
if result.Error != nil {
|
itemsError := db.Model(&originalIventorySlot).Association("Item").Replace(&inventorySlot.Item)
|
||||||
log.Println(result.Error)
|
if itemsError != nil {
|
||||||
|
return itemsError
|
||||||
}
|
}
|
||||||
for index, outputInventorySlot := range outputInventorySlots {
|
originalIventorySlot.Quantity = inventorySlot.Quantity
|
||||||
outputInventorySlots[index] = GetDatabaseInventorySlot(db, int(outputInventorySlot.ID))
|
return nil
|
||||||
}
|
|
||||||
return outputInventorySlots
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateDatabaseInventorySlot(db *gorm.DB, inventoySlot InventorySlot) error {
|
func (inventorySlot InventorySlot) Delete(db *gorm.DB) error {
|
||||||
result := db.Save(&inventoySlot)
|
result := db.Delete(&inventorySlot)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Create(db *gorm.DB, itemName string, quantity int64) error {
|
||||||
|
return InventorySlot{
|
||||||
|
Item: (*item.Get(db, []string{itemName}))[0],
|
||||||
|
Quantity: quantity,
|
||||||
|
}.Create(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(db *gorm.DB, inputInventorySlots []int) *[]InventorySlot {
|
||||||
|
var outputInventorySlots []InventorySlot
|
||||||
|
for _, inputInventorySlot := range inputInventorySlots {
|
||||||
|
var outputInventorySlot InventorySlot
|
||||||
|
outputInventorySlot.Get(db, inputInventorySlot)
|
||||||
|
outputInventorySlots = append(outputInventorySlots, outputInventorySlot)
|
||||||
|
}
|
||||||
|
return &outputInventorySlots
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAll(db *gorm.DB) *[]InventorySlot {
|
||||||
|
var outputInventorySlotIDs []int
|
||||||
|
result := db.Model(&InventorySlot{}).Select("id").Find(&outputInventorySlotIDs)
|
||||||
|
if result.Error != nil {
|
||||||
|
log.Println(result.Error)
|
||||||
|
}
|
||||||
|
return Get(db, outputInventorySlotIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(db *gorm.DB, itemID int, itemName string, quantity int64) error {
|
||||||
|
inventorySlot := InventorySlot{
|
||||||
|
Item: (*item.Get(db, []string{itemName}))[0],
|
||||||
|
Quantity: quantity,
|
||||||
|
}
|
||||||
|
inventorySlot.ID = uint(itemID)
|
||||||
|
return inventorySlot.Update(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputInventorySlotIDs []int) {
|
||||||
|
inventorySlots := Get(db, inputInventorySlotIDs)
|
||||||
|
for _, inventorySlot := range *inventorySlots {
|
||||||
|
inventorySlot.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ type Item struct {
|
||||||
Visibility []group.Group `gorm:"many2many:item_visibility_associations" json:"visibility"` // Unique
|
Visibility []group.Group `gorm:"many2many:item_visibility_associations" json:"visibility"` // Unique
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDatabaseItem(db *gorm.DB, item Item) error {
|
func (item Item) Create(db *gorm.DB) error {
|
||||||
result := db.Create(&item)
|
result := db.Create(&item)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
|
@ -31,43 +31,96 @@ func CreateDatabaseItem(db *gorm.DB, item Item) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseItem(db *gorm.DB, inputItem string) Item {
|
func (item Item) getAssociations(db *gorm.DB) {
|
||||||
var outputItem Item
|
db.Model(&item).Association("Functions").Find(&item.Functions)
|
||||||
result := db.Model(&Item{}).Where("name = ?", inputItem).Take(&outputItem)
|
db.Model(&item).Association("Tags").Find(&item.Tags)
|
||||||
if result.Error != nil {
|
db.Model(&item).Association("Customizations").Find(&item.Customizations)
|
||||||
return Item{}
|
db.Model(&item).Association("Visibility").Find(&item.Visibility)
|
||||||
}
|
|
||||||
db.Model(&outputItem).Association("Functions").Find(&outputItem.Functions)
|
|
||||||
db.Model(&outputItem).Association("Tags").Find(&outputItem.Tags)
|
|
||||||
db.Model(&outputItem).Association("Customizations").Find(&outputItem.Customizations)
|
|
||||||
db.Model(&outputItem).Association("Visibility").Find(&outputItem.Visibility)
|
|
||||||
return outputItem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseItems(db *gorm.DB, inputItems []string) []Item {
|
func (item Item) Get(db *gorm.DB, inputItem string) {
|
||||||
var outputItems []Item
|
db.Where("name = ?", inputItem).Take(&item)
|
||||||
for _, inputItem := range inputItems {
|
item.getAssociations(db)
|
||||||
outputItems = append(outputItems, GetDatabaseItem(db, inputItem))
|
|
||||||
}
|
|
||||||
return outputItems
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllDatabaseItems(db *gorm.DB) []Item {
|
func (item Item) Update(db *gorm.DB) error {
|
||||||
var outputItems []Item
|
var originalItem Item
|
||||||
result := db.Find(&outputItems)
|
originalItem.Get(db, item.Name)
|
||||||
if result.Error != nil {
|
functionsError := db.Model(&originalItem).Association("Functions").Find(&originalItem.Functions)
|
||||||
log.Println(result.Error)
|
if functionsError != nil {
|
||||||
|
return functionsError
|
||||||
}
|
}
|
||||||
for index, outputItem := range outputItems {
|
tagsError := db.Model(&originalItem).Association("Tags").Find(&originalItem.Tags)
|
||||||
outputItems[index] = GetDatabaseItem(db, outputItem.Name)
|
if tagsError != nil {
|
||||||
|
return tagsError
|
||||||
}
|
}
|
||||||
return outputItems
|
customizationsError := db.Model(&originalItem).Association("Customizations").Find(&originalItem.Customizations)
|
||||||
|
if customizationsError != nil {
|
||||||
|
return customizationsError
|
||||||
|
}
|
||||||
|
visibilityError := db.Model(&originalItem).Association("Visibility").Find(&originalItem.Visibility)
|
||||||
|
if visibilityError != nil {
|
||||||
|
return visibilityError
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateDatabaseItem(db *gorm.DB, item Item) error {
|
func (item Item) Delete(db *gorm.DB) error {
|
||||||
result := db.Save(&item)
|
result := db.Delete(&item)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Create(db *gorm.DB, name string, functions []string, flavorText string, rulesDescription string, physrepRequirements string, tags []string, customizations []string, visibility []string) error {
|
||||||
|
return Item{
|
||||||
|
Name: name,
|
||||||
|
Functions: *function.Get(db, functions),
|
||||||
|
FlavorText: flavorText,
|
||||||
|
RulesDescription: rulesDescription,
|
||||||
|
PhysrepRequirements: physrepRequirements,
|
||||||
|
Tags: *itemtag.Get(db, tags),
|
||||||
|
Customizations: *customization.Get(db, customizations),
|
||||||
|
Visibility: *group.Get(db, visibility),
|
||||||
|
}.Create(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(db *gorm.DB, inputItems []string) *[]Item {
|
||||||
|
var outputItems []Item
|
||||||
|
for _, inputItem := range inputItems {
|
||||||
|
var outputItem Item
|
||||||
|
outputItem.Get(db, inputItem)
|
||||||
|
outputItems = append(outputItems, outputItem)
|
||||||
|
}
|
||||||
|
return &outputItems
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAll(db *gorm.DB) *[]Item {
|
||||||
|
var outputItemNames []string
|
||||||
|
result := db.Model(&Item{}).Select("name").Find(&outputItemNames)
|
||||||
|
if result.Error != nil {
|
||||||
|
log.Println(result.Error)
|
||||||
|
}
|
||||||
|
return Get(db, outputItemNames)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(db *gorm.DB, name string, functions []string, flavorText string, rulesDescription string, physrepRequirements string, tags []string, customizations []string, visibility []string) error {
|
||||||
|
return Item{
|
||||||
|
Name: name,
|
||||||
|
Functions: *function.Get(db, functions),
|
||||||
|
FlavorText: flavorText,
|
||||||
|
RulesDescription: rulesDescription,
|
||||||
|
PhysrepRequirements: physrepRequirements,
|
||||||
|
Tags: *itemtag.Get(db, tags),
|
||||||
|
Customizations: *customization.Get(db, customizations),
|
||||||
|
Visibility: *group.Get(db, visibility),
|
||||||
|
}.Update(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputItem []string) {
|
||||||
|
items := Get(db, inputItem)
|
||||||
|
for _, item := range *items {
|
||||||
|
item.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -31,6 +31,14 @@ func (itemTag ItemTag) Update(db *gorm.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (itemTag ItemTag) Delete(db *gorm.DB) error {
|
||||||
|
result := db.Delete(&itemTag)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Create(db *gorm.DB, name string) error {
|
func Create(db *gorm.DB, name string) error {
|
||||||
return ItemTag{
|
return ItemTag{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
@ -61,3 +69,10 @@ func Update(db *gorm.DB, name string) error {
|
||||||
Name: name,
|
Name: name,
|
||||||
}.Update(db)
|
}.Update(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputItemTags []string) {
|
||||||
|
itemtags := Get(db, inputItemTags)
|
||||||
|
for _, itemtag := range *itemtags {
|
||||||
|
itemtag.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ type Person struct {
|
||||||
Groups []group.Group `gorm:"many2many:person_group_associations" json:"groups"` // Unique
|
Groups []group.Group `gorm:"many2many:person_group_associations" json:"groups"` // Unique
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDatabasePerson(db *gorm.DB, person Person) error {
|
func (person Person) Create(db *gorm.DB) error {
|
||||||
result := db.Create(&person)
|
result := db.Create(&person)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
|
@ -22,40 +22,69 @@ func CreateDatabasePerson(db *gorm.DB, person Person) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabasePerson(db *gorm.DB, inputPerson string) Person {
|
func (person *Person) getAssociations(db *gorm.DB) {
|
||||||
var outputPerson Person
|
db.Model(&person).Association("Groups").Find(&person.Groups)
|
||||||
result := db.Model(&Person{}).Where("name = ?", inputPerson).Take(&outputPerson)
|
|
||||||
if result.Error != nil {
|
|
||||||
return Person{}
|
|
||||||
}
|
|
||||||
db.Model(&outputPerson).Association("Groups").Find(&outputPerson.Groups)
|
|
||||||
return outputPerson
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabasePersons(db *gorm.DB, inputPersons []string) []Person {
|
func (person *Person) Get(db *gorm.DB, inputPerson string) {
|
||||||
var outputPersons []Person
|
db.Where("name = ?", inputPerson).Take(&person)
|
||||||
for _, inputPerson := range inputPersons {
|
person.getAssociations(db)
|
||||||
outputPersons = append(outputPersons, GetDatabasePerson(db, inputPerson))
|
|
||||||
}
|
|
||||||
return outputPersons
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllDatabasePersons(db *gorm.DB) []Person {
|
func (person Person) Update(db *gorm.DB) error {
|
||||||
var outputPersons []Person
|
var originalPerson Person
|
||||||
result := db.Find(&outputPersons)
|
originalPerson.Get(db, person.Name)
|
||||||
if result.Error != nil {
|
groupsError := db.Model(&originalPerson).Association("Groups").Replace(&person.Groups)
|
||||||
log.Println(result.Error)
|
if groupsError != nil {
|
||||||
|
return groupsError
|
||||||
}
|
}
|
||||||
for index, outputPerson := range outputPersons {
|
return nil
|
||||||
outputPersons[index] = GetDatabasePerson(db, outputPerson.Name)
|
|
||||||
}
|
|
||||||
return outputPersons
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateDatabasePerson(db *gorm.DB, person Person) error {
|
func (person Person) Delete(db *gorm.DB) error {
|
||||||
result := db.Save(&person)
|
result := db.Delete(&person)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Create(db *gorm.DB, name string, groups []string) error {
|
||||||
|
return Person{
|
||||||
|
Name: name,
|
||||||
|
Groups: *group.Get(db, groups),
|
||||||
|
}.Create(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(db *gorm.DB, inputPersons []string) *[]Person {
|
||||||
|
var outputPersons []Person
|
||||||
|
for _, inputPerson := range inputPersons {
|
||||||
|
var outputPerson Person
|
||||||
|
outputPerson.Get(db, inputPerson)
|
||||||
|
outputPersons = append(outputPersons, outputPerson)
|
||||||
|
}
|
||||||
|
return &outputPersons
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAll(db *gorm.DB) *[]Person {
|
||||||
|
var outputPersonNames []string
|
||||||
|
result := db.Model(&Person{}).Select("name").Find(&outputPersonNames)
|
||||||
|
if result.Error != nil {
|
||||||
|
log.Println(result.Error)
|
||||||
|
}
|
||||||
|
return Get(db, outputPersonNames)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(db *gorm.DB, name string, groups []string) error {
|
||||||
|
return Person{
|
||||||
|
Name: name,
|
||||||
|
Groups: *group.Get(db, groups),
|
||||||
|
}.Update(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputPersons []string) {
|
||||||
|
persons := Get(db, inputPersons)
|
||||||
|
for _, person := range *persons {
|
||||||
|
person.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ type Role struct {
|
||||||
Visibility []group.Group `gorm:"many2many:role_visibility_associations" json:"visibility"` // Unique
|
Visibility []group.Group `gorm:"many2many:role_visibility_associations" json:"visibility"` // Unique
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDatabaseRole(db *gorm.DB, role Role) error {
|
func (role Role) Create(db *gorm.DB) error {
|
||||||
result := db.Create(&role)
|
result := db.Create(&role)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
|
@ -24,42 +24,71 @@ func CreateDatabaseRole(db *gorm.DB, role Role) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseRole(db *gorm.DB, inputRole string) Role {
|
func (role *Role) getAssociations(db *gorm.DB) {
|
||||||
var outputRole Role
|
db.Model(&role).Association("Tiers").Find(&role.Tiers)
|
||||||
result := db.Model(&Role{}).Where("name = ?", inputRole).Take(&outputRole)
|
db.Model(&role).Association("Visibility").Find(&role.Visibility)
|
||||||
if result.Error != nil {
|
//
|
||||||
return Role{}
|
|
||||||
}
|
|
||||||
db.Model(&outputRole).Association("Tiers").Find(&outputRole.Tiers)
|
|
||||||
db.Model(&outputRole).Association("Visibility").Find(&outputRole.Visibility)
|
|
||||||
return outputRole
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseRoles(db *gorm.DB) []Role {
|
func (role *Role) Get(db *gorm.DB, inputRole string) {
|
||||||
var outputRoles []Role
|
db.Where("name = ?", inputRole).Take(&role)
|
||||||
db.Find(&outputRoles)
|
role.getAssociations(db)
|
||||||
for _, outputRole := range outputRoles {
|
|
||||||
outputRoles = append(outputRoles, GetDatabaseRole(db, outputRole.Name))
|
|
||||||
}
|
|
||||||
return outputRoles
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllDatabaseRoles(db *gorm.DB) []Role {
|
func (role Role) Update(db *gorm.DB) error {
|
||||||
var outputRoles []Role
|
|
||||||
result := db.Find(&outputRoles)
|
|
||||||
if result.Error != nil {
|
|
||||||
log.Println(result.Error)
|
|
||||||
}
|
|
||||||
for index, outputRole := range outputRoles {
|
|
||||||
outputRoles[index] = GetDatabaseRole(db, outputRole.Name)
|
|
||||||
}
|
|
||||||
return outputRoles
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateDatabaseRole(db *gorm.DB, role Role) error {
|
|
||||||
result := db.Save(&role)
|
result := db.Save(&role)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (role Role) Delete(db *gorm.DB) error {
|
||||||
|
result := db.Delete(&role)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Create(db *gorm.DB, name string, tiers []int, visibility []string) error {
|
||||||
|
return Role{
|
||||||
|
Name: name,
|
||||||
|
Tiers: *tier.Get(db, tiers),
|
||||||
|
Visibility: *group.Get(db, visibility),
|
||||||
|
}.Create(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(db *gorm.DB, inputRoles []string) *[]Role {
|
||||||
|
var outputRoles []Role
|
||||||
|
for _, inputRole := range inputRoles {
|
||||||
|
var outputRole Role
|
||||||
|
outputRole.Get(db, inputRole)
|
||||||
|
outputRoles = append(outputRoles, outputRole)
|
||||||
|
}
|
||||||
|
return &outputRoles
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAll(db *gorm.DB) *[]Role {
|
||||||
|
var outputRoleNames []string
|
||||||
|
result := db.Model(&Role{}).Select("name").Find(&outputRoleNames)
|
||||||
|
if result.Error != nil {
|
||||||
|
log.Println(result.Error)
|
||||||
|
}
|
||||||
|
return Get(db, outputRoleNames)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(db *gorm.DB, name string, tiers []int, visibility []string) error {
|
||||||
|
return Role{
|
||||||
|
Name: name,
|
||||||
|
Tiers: *tier.Get(db, tiers),
|
||||||
|
Visibility: *group.Get(db, visibility),
|
||||||
|
}.Update(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputRoles []string) {
|
||||||
|
roles := Get(db, inputRoles)
|
||||||
|
for _, role := range *roles {
|
||||||
|
role.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ type Schematic struct {
|
||||||
Visibility []group.Group `gorm:"many2many:schematic_group_associations" json:"visibility"` // Unique
|
Visibility []group.Group `gorm:"many2many:schematic_group_associations" json:"visibility"` // Unique
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDatabaseSchematic(db *gorm.DB, schematic Schematic) error {
|
func (schematic Schematic) Create(db *gorm.DB) error {
|
||||||
result := db.Create(&schematic)
|
result := db.Create(&schematic)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
|
@ -28,44 +28,95 @@ func CreateDatabaseSchematic(db *gorm.DB, schematic Schematic) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseSchematic(db *gorm.DB, inputSchematic int) Schematic {
|
func (schematic *Schematic) getAssociations(db *gorm.DB) {
|
||||||
var outputSchematic Schematic
|
db.Model(&schematic).Association("Material").Find(&schematic.Material)
|
||||||
result := db.Model(&Schematic{}).Where("id = ?", inputSchematic).Take(&outputSchematic)
|
db.Model(&schematic).Association("Tools").Find(&schematic.Tools)
|
||||||
if result.Error != nil {
|
db.Model(&schematic).Association("Requirements").Find(&schematic.Requirements)
|
||||||
return Schematic{}
|
db.Model(&schematic).Association("Result").Find(&schematic.Result)
|
||||||
}
|
db.Model(&schematic).Association("Visibility").Find(&schematic.Visibility)
|
||||||
db.Model(&outputSchematic).Association("Material").Find(&outputSchematic.Material)
|
|
||||||
db.Model(&outputSchematic).Association("Tools").Find(&outputSchematic.Tools)
|
|
||||||
db.Model(&outputSchematic).Association("Requirements").Find(&outputSchematic.Requirements)
|
|
||||||
db.Model(&outputSchematic).Association("Result").Find(&outputSchematic.Result)
|
|
||||||
db.Model(&outputSchematic).Association("Visibility").Find(&outputSchematic.Visibility)
|
|
||||||
return outputSchematic
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDatabaseSchematics(db *gorm.DB, inputSchematics []int) []Schematic {
|
func (schematic *Schematic) Get(db *gorm.DB, inputSchematic int) {
|
||||||
var outputSchematics []Schematic
|
db.Model(&Schematic{}).Where("id = ?", inputSchematic).Take(&schematic)
|
||||||
for _, inputSchematic := range inputSchematics {
|
schematic.getAssociations(db)
|
||||||
outputSchematics = append(outputSchematics, GetDatabaseSchematic(db, inputSchematic))
|
|
||||||
}
|
|
||||||
return outputSchematics
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllDatabaseSchematics(db *gorm.DB) []Schematic {
|
func (schematic Schematic) Update(db *gorm.DB) error {
|
||||||
var outputSchematics []Schematic
|
materialError := db.Model(&schematic).Association("Material").Replace(&schematic.Material)
|
||||||
result := db.Find(&outputSchematics)
|
if materialError != nil {
|
||||||
if result.Error != nil {
|
return materialError
|
||||||
log.Println(result.Error)
|
|
||||||
}
|
}
|
||||||
for index, outputSchematic := range outputSchematics {
|
toolsError := db.Model(&schematic).Association("Tools").Replace(&schematic.Tools)
|
||||||
outputSchematics[index] = GetDatabaseSchematic(db, int(outputSchematic.ID))
|
if toolsError != nil {
|
||||||
|
return toolsError
|
||||||
}
|
}
|
||||||
return outputSchematics
|
requirementsError := db.Model(&schematic).Association("Requirements").Replace(&schematic.Requirements)
|
||||||
|
if requirementsError != nil {
|
||||||
|
return requirementsError
|
||||||
|
}
|
||||||
|
resultError := db.Model(&schematic).Association("Result").Replace(&schematic.Result)
|
||||||
|
if resultError != nil {
|
||||||
|
return resultError
|
||||||
|
}
|
||||||
|
visibilityError := db.Model(&schematic).Association("Visibility").Replace(&schematic.Visibility)
|
||||||
|
if visibilityError != nil {
|
||||||
|
return visibilityError
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateDatabaseSchematic(db *gorm.DB, schematic Schematic) error {
|
func (schematic Schematic) Delete(db *gorm.DB) error {
|
||||||
result := db.Save(&schematic)
|
result := db.Delete(&schematic)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Create(db *gorm.DB, material []int, tools []int, requirements []string, timeUnits int64, result int, visibility []string) error {
|
||||||
|
return Schematic{
|
||||||
|
Material: *inventoryslot.Get(db, material),
|
||||||
|
Tools: *inventoryslot.Get(db, tools),
|
||||||
|
Requirements: *function.Get(db, requirements),
|
||||||
|
TimeUnits: timeUnits,
|
||||||
|
Result: (*inventoryslot.Get(db, []int{result}))[0],
|
||||||
|
Visibility: *group.Get(db, visibility),
|
||||||
|
}.Create(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(db *gorm.DB, inputSchematics []int) *[]Schematic {
|
||||||
|
var outputSchematics []Schematic
|
||||||
|
for _, inputSchematic := range inputSchematics {
|
||||||
|
var outputSchematic Schematic
|
||||||
|
outputSchematic.Get(db, inputSchematic)
|
||||||
|
outputSchematics = append(outputSchematics, outputSchematic)
|
||||||
|
}
|
||||||
|
return &outputSchematics
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAll(db *gorm.DB) *[]Schematic {
|
||||||
|
var outputSchematics []int
|
||||||
|
result := db.Model(&Schematic{}).Select("id").Find(&outputSchematics)
|
||||||
|
if result.Error != nil {
|
||||||
|
log.Println(result.Error)
|
||||||
|
}
|
||||||
|
return Get(db, outputSchematics)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(db *gorm.DB, material []int, tools []int, requirements []string, timeUnits int64, result int, visibility []string) error {
|
||||||
|
return Schematic{
|
||||||
|
Material: *inventoryslot.Get(db, material),
|
||||||
|
Tools: *inventoryslot.Get(db, tools),
|
||||||
|
Requirements: *function.Get(db, requirements),
|
||||||
|
TimeUnits: timeUnits,
|
||||||
|
Result: (*inventoryslot.Get(db, []int{result}))[0],
|
||||||
|
Visibility: *group.Get(db, visibility),
|
||||||
|
}.Update(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputSchematics []int) {
|
||||||
|
schematics := Get(db, inputSchematics)
|
||||||
|
for _, schematic := range *schematics {
|
||||||
|
schematic.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -38,6 +38,14 @@ func (tier Tier) Update(db *gorm.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tier Tier) Delete(db *gorm.DB) error {
|
||||||
|
result := db.Delete(&tier)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Create(db *gorm.DB, functionSets []int) error {
|
func Create(db *gorm.DB, functionSets []int) error {
|
||||||
return Tier{
|
return Tier{
|
||||||
FunctionSets: *functionset.Get(db, functionSets),
|
FunctionSets: *functionset.Get(db, functionSets),
|
||||||
|
@ -70,3 +78,10 @@ func Update(db *gorm.DB, id int, functionSets []int) error {
|
||||||
outputTier.ID = uint(id)
|
outputTier.ID = uint(id)
|
||||||
return outputTier.Update(db)
|
return outputTier.Update(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Delete(db *gorm.DB, inputTiers []int) {
|
||||||
|
tiers := Get(db, inputTiers)
|
||||||
|
for _, tier := range *tiers {
|
||||||
|
tier.Delete(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue