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.GlobalConfig.ParseConfig("../config.toml")
|
||||
api.GlobalOAuth = authdiscord.CreateDiscordOAuthConfig(api.GlobalConfig)
|
||||
app := gin.Default()
|
||||
router := gin.Default()
|
||||
// Authentication Workflow
|
||||
app.GET("/auth/callback", api.AuthCallback)
|
||||
app.GET("/auth/login", api.AuthLoginRedirect)
|
||||
app.GET("/auth/logout", api.AuthLogoutRedirect)
|
||||
// Create & Update
|
||||
app.POST("/post/user/update", api.CreateOrUpdateUser)
|
||||
app.POST("/post/group", api.CreateGroup)
|
||||
app.POST("/post/function", api.CreateFunction)
|
||||
app.POST("/post/function-tag", api.CreateFunctionTag)
|
||||
router.GET("/auth/callback", api.AuthCallback)
|
||||
router.GET("/auth/login", api.AuthLoginRedirect)
|
||||
router.GET("/auth/logout", api.AuthLogoutRedirect)
|
||||
// Create
|
||||
router.POST("/user/update", api.CreateOrUpdateUser)
|
||||
router.POST("/group", api.CreateGroup)
|
||||
router.POST("/function", api.CreateFunction)
|
||||
router.POST("/function-tag", api.CreateFunctionTag)
|
||||
// Update
|
||||
router.PUT("/function", api.UpdateFunction)
|
||||
// Read
|
||||
app.GET("/get/user/info", api.GetDiscordUser)
|
||||
app.GET("/get/user/authorized", api.GetUserLoggedIn)
|
||||
app.GET("/get/groups", api.GetGroups)
|
||||
app.GET("/get/all/groups", api.GetAllGroups)
|
||||
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)
|
||||
router.GET("/user/info", api.GetDiscordUser)
|
||||
router.GET("/user/authorized", api.GetUserLoggedIn)
|
||||
router.GET("/:object", api.GetObjects)
|
||||
router.GET("/all/:object", api.GetAllObjects)
|
||||
// Delete
|
||||
app.Run(":31337")
|
||||
router.DELETE("/function", api.DeleteFunction)
|
||||
router.Run(":31337")
|
||||
}
|
||||
|
|
|
@ -5,12 +5,23 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
authdiscord "example.com/auth/discord"
|
||||
configserver "example.com/config/server"
|
||||
character "example.com/database/character"
|
||||
customization "example.com/database/customization"
|
||||
function "example.com/database/function"
|
||||
functionset "example.com/database/functionset"
|
||||
functiontag "example.com/database/functiontag"
|
||||
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"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -25,6 +36,19 @@ var GlobalOAuth *oauth2.Config
|
|||
|
||||
// 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
|
||||
|
||||
func AuthCallback(context *gin.Context) {
|
||||
|
@ -161,6 +185,24 @@ func CreateFunctionTag(context *gin.Context) {
|
|||
|
||||
// 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/)
|
||||
|
||||
func GetDiscordUser(context *gin.Context) {
|
||||
|
@ -216,55 +258,143 @@ func GetUserLoggedIn(context *gin.Context) {
|
|||
context.Set("is-authorized", false)
|
||||
}
|
||||
|
||||
func GetGroups(context *gin.Context) {
|
||||
groupNames, ok := context.GetQueryArray("groups")
|
||||
if ok {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"groups": group.Get(GlobalDatabase, groupNames),
|
||||
})
|
||||
func GetObjects(context *gin.Context) {
|
||||
objectIDs, idOk := context.GetQueryArray("id")
|
||||
if idOk {
|
||||
switch objectType := context.Param("object"); objectType {
|
||||
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 {
|
||||
context.Status(http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllGroups(context *gin.Context) {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"groups": group.GetAll(GlobalDatabase),
|
||||
})
|
||||
}
|
||||
|
||||
func GetFunctions(context *gin.Context) {
|
||||
functionNames, ok := context.GetQueryArray("functions")
|
||||
if ok {
|
||||
func GetAllObjects(context *gin.Context) {
|
||||
switch objectType := context.Param("object"); objectType {
|
||||
case "persons":
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"functions": function.Get(GlobalDatabase, functionNames),
|
||||
"persons": person.GetAll(GlobalDatabase),
|
||||
})
|
||||
} else {
|
||||
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 {
|
||||
case "groups":
|
||||
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/)
|
||||
|
||||
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
|
||||
|
||||
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/config/server 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"`
|
||||
}
|
||||
|
||||
func CreateDatabaseCharacter(db *gorm.DB, character Character) error {
|
||||
func (character Character) Create(db *gorm.DB) error {
|
||||
result := db.Create(&character)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -28,43 +28,90 @@ func CreateDatabaseCharacter(db *gorm.DB, character Character) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseCharacter(db *gorm.DB, inputCharacter string) Character {
|
||||
var outputCharacter Character
|
||||
result := db.Model(&Character{}).Where("name = ?", inputCharacter).Take(&outputCharacter)
|
||||
if result.Error != nil {
|
||||
return Character{}
|
||||
}
|
||||
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 (character Character) getAssociations(db *gorm.DB) {
|
||||
db.Model(&character).Association("Owners").Find(&character.Owners)
|
||||
db.Model(&character).Association("Roles").Find(&character.Roles)
|
||||
db.Model(&character).Association("FunctionSets").Find(&character.FunctionSets)
|
||||
db.Model(&character).Association("Inventory").Find(&character.Inventory)
|
||||
}
|
||||
|
||||
func GetDatabaseCharacters(db *gorm.DB, inputCharacters []string) []Character {
|
||||
var outputCharacters []Character
|
||||
for _, inputCharacter := range inputCharacters {
|
||||
outputCharacters = append(outputCharacters, GetDatabaseCharacter(db, inputCharacter))
|
||||
}
|
||||
return outputCharacters
|
||||
func (character *Character) Get(db *gorm.DB, inputCharacter string) {
|
||||
db.Where("name = ?", inputCharacter).Take(&character)
|
||||
character.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabaseCharacters(db *gorm.DB) []Character {
|
||||
var outputCharacters []Character
|
||||
result := db.Find(&outputCharacters)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
func (character Character) Update(db *gorm.DB) error {
|
||||
var originalCharacter Character
|
||||
originalCharacter.Get(db, character.Name)
|
||||
ownersError := db.Model(&originalCharacter).Association("Owners").Replace(&character.Owners)
|
||||
if ownersError != nil {
|
||||
return ownersError
|
||||
}
|
||||
for index, outputCharacter := range outputCharacters {
|
||||
outputCharacters[index] = GetDatabaseCharacter(db, outputCharacter.Name)
|
||||
rolesError := db.Model(&originalCharacter).Association("Roles").Replace(&character.Roles)
|
||||
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 {
|
||||
result := db.Save(&character)
|
||||
func (character Character) Delete(db *gorm.DB) error {
|
||||
result := db.Delete(&character)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func CreateDatabaseCustomization(db *gorm.DB, customization Customization) error {
|
||||
func (customization Customization) Create(db *gorm.DB) error {
|
||||
result := db.Create(&customization)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -29,42 +29,93 @@ func CreateDatabaseCustomization(db *gorm.DB, customization Customization) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseCustomization(db *gorm.DB, inputCustomization string) Customization {
|
||||
var outputCustomization Customization
|
||||
result := db.Model(&Customization{}).Where("name = ?", inputCustomization).Take(&outputCustomization)
|
||||
if result.Error != nil {
|
||||
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 (customization *Customization) getAssociations(db *gorm.DB) {
|
||||
db.Model(&customization).Association("Functions").Find(&customization.Functions)
|
||||
db.Model(&customization).Association("Tags").Find(&customization.Tags)
|
||||
db.Model(&customization).Association("Visibility").Find(&customization.Visibility)
|
||||
}
|
||||
|
||||
func GetDatabaseCustomizations(db *gorm.DB, inputCustomizations []string) []Customization {
|
||||
var outputCustomizations []Customization
|
||||
for _, inputCustomization := range inputCustomizations {
|
||||
outputCustomizations = append(outputCustomizations, GetDatabaseCustomization(db, inputCustomization))
|
||||
}
|
||||
return outputCustomizations
|
||||
func (customization *Customization) Get(db *gorm.DB, inputCustomization string) {
|
||||
db.Where("name = ?", inputCustomization).Take(&customization)
|
||||
customization.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabaseCustomizations(db *gorm.DB) []Customization {
|
||||
var outputCustomizations []Customization
|
||||
result := db.Find(&outputCustomizations)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
func (customization Customization) Update(db *gorm.DB) error {
|
||||
var originalCustomization Customization
|
||||
db.Updates(&Customization{
|
||||
FlavorText: customization.FlavorText,
|
||||
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 {
|
||||
outputCustomizations[index] = GetDatabaseCustomization(db, outputCustomization.Name)
|
||||
tagsError := db.Model(&originalCustomization).Association("Tags").Replace(&customization.Tags)
|
||||
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 {
|
||||
result := db.Save(&customization)
|
||||
func (customization Customization) Delete(db *gorm.DB) error {
|
||||
result := db.Delete(&customization)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
log.Println(*functiontag.Get(db, tags))
|
||||
log.Println(*Get(db, requirements))
|
||||
return Function{
|
||||
Name: name,
|
||||
Tags: *functiontag.Get(db, tags),
|
||||
Requirements: *Get(db, requirements),
|
||||
}.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
|
||||
}
|
||||
|
||||
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 {
|
||||
return FunctionSet{
|
||||
Functions: *function.Get(db, functions),
|
||||
|
@ -70,3 +78,10 @@ func Update(db *gorm.DB, id int, functions []string) error {
|
|||
outputFunctionSet.ID = uint(id)
|
||||
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
|
||||
}
|
||||
|
||||
func CreateDatabaseInventorySlot(db *gorm.DB, inventorySlot InventorySlot) error {
|
||||
func (inventorySlot InventorySlot) Create(db *gorm.DB) error {
|
||||
result := db.Create(&inventorySlot)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -22,40 +22,73 @@ func CreateDatabaseInventorySlot(db *gorm.DB, inventorySlot InventorySlot) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseInventorySlot(db *gorm.DB, inputInventorySlot int) InventorySlot {
|
||||
var outputInventorySlot InventorySlot
|
||||
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 (inventorySlot *InventorySlot) getAssociations(db *gorm.DB) {
|
||||
db.Model(&inventorySlot).Association("Item").Find(&inventorySlot.Item)
|
||||
|
||||
}
|
||||
|
||||
func GetDatabaseInventorySlots(db *gorm.DB, inputInventorySlots []int) []InventorySlot {
|
||||
var outputInventorySlots []InventorySlot
|
||||
for _, inputInventorySlot := range inputInventorySlots {
|
||||
outputInventorySlots = append(outputInventorySlots, GetDatabaseInventorySlot(db, inputInventorySlot))
|
||||
}
|
||||
return outputInventorySlots
|
||||
func (inventorySlot *InventorySlot) Get(db *gorm.DB, inputInventorySlot int) {
|
||||
db.Where("id = ?", inputInventorySlot).Take(&inventorySlot)
|
||||
inventorySlot.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabaseInventorySlots(db *gorm.DB) []InventorySlot {
|
||||
var outputInventorySlots []InventorySlot
|
||||
result := db.Find(&outputInventorySlots)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
func (inventorySlot InventorySlot) Update(db *gorm.DB) error {
|
||||
var originalIventorySlot InventorySlot
|
||||
originalIventorySlot.Get(db, int(inventorySlot.ID))
|
||||
itemsError := db.Model(&originalIventorySlot).Association("Item").Replace(&inventorySlot.Item)
|
||||
if itemsError != nil {
|
||||
return itemsError
|
||||
}
|
||||
for index, outputInventorySlot := range outputInventorySlots {
|
||||
outputInventorySlots[index] = GetDatabaseInventorySlot(db, int(outputInventorySlot.ID))
|
||||
}
|
||||
return outputInventorySlots
|
||||
originalIventorySlot.Quantity = inventorySlot.Quantity
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabaseInventorySlot(db *gorm.DB, inventoySlot InventorySlot) error {
|
||||
result := db.Save(&inventoySlot)
|
||||
func (inventorySlot InventorySlot) Delete(db *gorm.DB) error {
|
||||
result := db.Delete(&inventorySlot)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func CreateDatabaseItem(db *gorm.DB, item Item) error {
|
||||
func (item Item) Create(db *gorm.DB) error {
|
||||
result := db.Create(&item)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -31,43 +31,96 @@ func CreateDatabaseItem(db *gorm.DB, item Item) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseItem(db *gorm.DB, inputItem string) Item {
|
||||
var outputItem Item
|
||||
result := db.Model(&Item{}).Where("name = ?", inputItem).Take(&outputItem)
|
||||
if result.Error != nil {
|
||||
return Item{}
|
||||
}
|
||||
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 (item Item) getAssociations(db *gorm.DB) {
|
||||
db.Model(&item).Association("Functions").Find(&item.Functions)
|
||||
db.Model(&item).Association("Tags").Find(&item.Tags)
|
||||
db.Model(&item).Association("Customizations").Find(&item.Customizations)
|
||||
db.Model(&item).Association("Visibility").Find(&item.Visibility)
|
||||
}
|
||||
|
||||
func GetDatabaseItems(db *gorm.DB, inputItems []string) []Item {
|
||||
var outputItems []Item
|
||||
for _, inputItem := range inputItems {
|
||||
outputItems = append(outputItems, GetDatabaseItem(db, inputItem))
|
||||
}
|
||||
return outputItems
|
||||
func (item Item) Get(db *gorm.DB, inputItem string) {
|
||||
db.Where("name = ?", inputItem).Take(&item)
|
||||
item.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabaseItems(db *gorm.DB) []Item {
|
||||
var outputItems []Item
|
||||
result := db.Find(&outputItems)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
func (item Item) Update(db *gorm.DB) error {
|
||||
var originalItem Item
|
||||
originalItem.Get(db, item.Name)
|
||||
functionsError := db.Model(&originalItem).Association("Functions").Find(&originalItem.Functions)
|
||||
if functionsError != nil {
|
||||
return functionsError
|
||||
}
|
||||
for index, outputItem := range outputItems {
|
||||
outputItems[index] = GetDatabaseItem(db, outputItem.Name)
|
||||
tagsError := db.Model(&originalItem).Association("Tags").Find(&originalItem.Tags)
|
||||
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 {
|
||||
result := db.Save(&item)
|
||||
func (item Item) Delete(db *gorm.DB) error {
|
||||
result := db.Delete(&item)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
return ItemTag{
|
||||
Name: name,
|
||||
|
@ -61,3 +69,10 @@ func Update(db *gorm.DB, name string) error {
|
|||
Name: name,
|
||||
}.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
|
||||
}
|
||||
|
||||
func CreateDatabasePerson(db *gorm.DB, person Person) error {
|
||||
func (person Person) Create(db *gorm.DB) error {
|
||||
result := db.Create(&person)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -22,40 +22,69 @@ func CreateDatabasePerson(db *gorm.DB, person Person) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabasePerson(db *gorm.DB, inputPerson string) Person {
|
||||
var outputPerson Person
|
||||
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 (person *Person) getAssociations(db *gorm.DB) {
|
||||
db.Model(&person).Association("Groups").Find(&person.Groups)
|
||||
}
|
||||
|
||||
func GetDatabasePersons(db *gorm.DB, inputPersons []string) []Person {
|
||||
var outputPersons []Person
|
||||
for _, inputPerson := range inputPersons {
|
||||
outputPersons = append(outputPersons, GetDatabasePerson(db, inputPerson))
|
||||
}
|
||||
return outputPersons
|
||||
func (person *Person) Get(db *gorm.DB, inputPerson string) {
|
||||
db.Where("name = ?", inputPerson).Take(&person)
|
||||
person.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabasePersons(db *gorm.DB) []Person {
|
||||
var outputPersons []Person
|
||||
result := db.Find(&outputPersons)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
func (person Person) Update(db *gorm.DB) error {
|
||||
var originalPerson Person
|
||||
originalPerson.Get(db, person.Name)
|
||||
groupsError := db.Model(&originalPerson).Association("Groups").Replace(&person.Groups)
|
||||
if groupsError != nil {
|
||||
return groupsError
|
||||
}
|
||||
for index, outputPerson := range outputPersons {
|
||||
outputPersons[index] = GetDatabasePerson(db, outputPerson.Name)
|
||||
}
|
||||
return outputPersons
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabasePerson(db *gorm.DB, person Person) error {
|
||||
result := db.Save(&person)
|
||||
func (person Person) Delete(db *gorm.DB) error {
|
||||
result := db.Delete(&person)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func CreateDatabaseRole(db *gorm.DB, role Role) error {
|
||||
func (role Role) Create(db *gorm.DB) error {
|
||||
result := db.Create(&role)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -24,42 +24,71 @@ func CreateDatabaseRole(db *gorm.DB, role Role) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseRole(db *gorm.DB, inputRole string) Role {
|
||||
var outputRole Role
|
||||
result := db.Model(&Role{}).Where("name = ?", inputRole).Take(&outputRole)
|
||||
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 (role *Role) getAssociations(db *gorm.DB) {
|
||||
db.Model(&role).Association("Tiers").Find(&role.Tiers)
|
||||
db.Model(&role).Association("Visibility").Find(&role.Visibility)
|
||||
//
|
||||
}
|
||||
|
||||
func GetDatabaseRoles(db *gorm.DB) []Role {
|
||||
var outputRoles []Role
|
||||
db.Find(&outputRoles)
|
||||
for _, outputRole := range outputRoles {
|
||||
outputRoles = append(outputRoles, GetDatabaseRole(db, outputRole.Name))
|
||||
}
|
||||
return outputRoles
|
||||
func (role *Role) Get(db *gorm.DB, inputRole string) {
|
||||
db.Where("name = ?", inputRole).Take(&role)
|
||||
role.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabaseRoles(db *gorm.DB) []Role {
|
||||
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 {
|
||||
func (role Role) Update(db *gorm.DB) error {
|
||||
result := db.Save(&role)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func CreateDatabaseSchematic(db *gorm.DB, schematic Schematic) error {
|
||||
func (schematic Schematic) Create(db *gorm.DB) error {
|
||||
result := db.Create(&schematic)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -28,44 +28,95 @@ func CreateDatabaseSchematic(db *gorm.DB, schematic Schematic) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseSchematic(db *gorm.DB, inputSchematic int) Schematic {
|
||||
var outputSchematic Schematic
|
||||
result := db.Model(&Schematic{}).Where("id = ?", inputSchematic).Take(&outputSchematic)
|
||||
if result.Error != nil {
|
||||
return Schematic{}
|
||||
}
|
||||
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 (schematic *Schematic) getAssociations(db *gorm.DB) {
|
||||
db.Model(&schematic).Association("Material").Find(&schematic.Material)
|
||||
db.Model(&schematic).Association("Tools").Find(&schematic.Tools)
|
||||
db.Model(&schematic).Association("Requirements").Find(&schematic.Requirements)
|
||||
db.Model(&schematic).Association("Result").Find(&schematic.Result)
|
||||
db.Model(&schematic).Association("Visibility").Find(&schematic.Visibility)
|
||||
}
|
||||
|
||||
func GetDatabaseSchematics(db *gorm.DB, inputSchematics []int) []Schematic {
|
||||
var outputSchematics []Schematic
|
||||
for _, inputSchematic := range inputSchematics {
|
||||
outputSchematics = append(outputSchematics, GetDatabaseSchematic(db, inputSchematic))
|
||||
}
|
||||
return outputSchematics
|
||||
func (schematic *Schematic) Get(db *gorm.DB, inputSchematic int) {
|
||||
db.Model(&Schematic{}).Where("id = ?", inputSchematic).Take(&schematic)
|
||||
schematic.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabaseSchematics(db *gorm.DB) []Schematic {
|
||||
var outputSchematics []Schematic
|
||||
result := db.Find(&outputSchematics)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
func (schematic Schematic) Update(db *gorm.DB) error {
|
||||
materialError := db.Model(&schematic).Association("Material").Replace(&schematic.Material)
|
||||
if materialError != nil {
|
||||
return materialError
|
||||
}
|
||||
for index, outputSchematic := range outputSchematics {
|
||||
outputSchematics[index] = GetDatabaseSchematic(db, int(outputSchematic.ID))
|
||||
toolsError := db.Model(&schematic).Association("Tools").Replace(&schematic.Tools)
|
||||
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 {
|
||||
result := db.Save(&schematic)
|
||||
func (schematic Schematic) Delete(db *gorm.DB) error {
|
||||
result := db.Delete(&schematic)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
return Tier{
|
||||
FunctionSets: *functionset.Get(db, functionSets),
|
||||
|
@ -70,3 +78,10 @@ func Update(db *gorm.DB, id int, functionSets []int) error {
|
|||
outputTier.ID = uint(id)
|
||||
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