Starting to update CRUD methods.
This commit is contained in:
parent
767499a9ed
commit
3756e275d3
15 changed files with 241 additions and 222 deletions
|
@ -3,13 +3,13 @@ package main
|
|||
import (
|
||||
api "example.com/api"
|
||||
authdiscord "example.com/auth/discord"
|
||||
databasecommands "example.com/database/commands"
|
||||
database "example.com/database"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
api.GlobalDatabase = databasecommands.InitializeDatabase()
|
||||
api.GlobalDatabase = database.InitializeDatabase()
|
||||
api.GlobalConfig.ParseConfig("../config.toml")
|
||||
api.GlobalOAuth = authdiscord.CreateDiscordOAuthConfig(api.GlobalConfig)
|
||||
app := gin.Default()
|
||||
|
@ -19,18 +19,18 @@ func main() {
|
|||
app.GET("/auth/logout", api.AuthLogoutRedirect)
|
||||
// Create & Update
|
||||
app.POST("/post/user/update", api.CreateOrUpdateUser)
|
||||
app.POST("/post/group", api.CreateDatabaseGroup)
|
||||
app.POST("/post/function", api.CreateDatabaseFunction)
|
||||
app.POST("/post/function-tag", api.CreateDatabaseFunctionTag)
|
||||
app.POST("/post/group", api.CreateGroup)
|
||||
app.POST("/post/function", api.CreateFunction)
|
||||
app.POST("/post/function-tag", api.CreateFunctionTag)
|
||||
// Read
|
||||
app.GET("/get/user/info", api.GetUserInfo)
|
||||
app.GET("/get/user/authorized", api.GetIsUserAuthorized)
|
||||
app.GET("/get/groups", api.GetDatabaseGroups)
|
||||
app.GET("/get/all/groups", api.GetAllDatabaseGroups)
|
||||
app.GET("/get/functions", api.GetDatabaseFunctions)
|
||||
app.GET("/get/all/functions", api.GetAllDatabaseFunctions)
|
||||
app.GET("/get/function-tags", api.GetDatabaseFunctionTags)
|
||||
app.GET("/get/all/function-tags", api.GetAllDatabaseFunctionTags)
|
||||
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)
|
||||
// Delete
|
||||
app.Run(":31337")
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ module gin-cpularp
|
|||
|
||||
go 1.24.2
|
||||
|
||||
replace example.com/database/commands => ./lib/database/commands
|
||||
replace example.com/database => ./lib/database
|
||||
|
||||
replace example.com/database/user => ./lib/database/user
|
||||
|
||||
|
@ -41,7 +41,7 @@ replace example.com/api => ./lib/api
|
|||
require (
|
||||
example.com/api v0.0.0
|
||||
example.com/auth/discord v0.0.0
|
||||
example.com/database/commands v0.0.0
|
||||
example.com/database v0.0.0
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
)
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
authdiscord "example.com/auth/discord"
|
||||
configserver "example.com/config/server"
|
||||
databasecommands "example.com/database/commands"
|
||||
function "example.com/database/function"
|
||||
functiontag "example.com/database/functiontag"
|
||||
group "example.com/database/group"
|
||||
|
@ -50,7 +49,7 @@ func AuthLoginRedirect(context *gin.Context) {
|
|||
func AuthLogoutRedirect(context *gin.Context) {
|
||||
oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
|
||||
if err == nil {
|
||||
databasecommands.LogoutDatabaseUser(GlobalDatabase, oauthTokenJSON)
|
||||
user.Logout(GlobalDatabase, oauthTokenJSON)
|
||||
context.SetCookie("discord-oauthtoken", "", -1, "", GlobalConfig.API.Domain, false, true)
|
||||
} else {
|
||||
log.Println(err)
|
||||
|
@ -58,8 +57,6 @@ func AuthLogoutRedirect(context *gin.Context) {
|
|||
context.Redirect(http.StatusTemporaryRedirect, GlobalConfig.GetFrontendRootDomain())
|
||||
}
|
||||
|
||||
// Create & Update Endpoints (post/, put/, patch)
|
||||
|
||||
func CreateOrUpdateUser(context *gin.Context) {
|
||||
oauthTokenJSON := context.GetString("discord-oauthtoken")
|
||||
err := error(nil)
|
||||
|
@ -110,43 +107,15 @@ func CreateOrUpdateUser(context *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func CreateDatabaseGroup(context *gin.Context) {
|
||||
GetIsUserAuthorized(context)
|
||||
isAuthorized := context.GetBool("is-authorized")
|
||||
if isAuthorized {
|
||||
name := context.Query("name")
|
||||
if name != "" {
|
||||
group.Group{Name: name}.Create(GlobalDatabase)
|
||||
context.Status(http.StatusOK)
|
||||
} else {
|
||||
context.AbortWithStatus(http.StatusBadRequest)
|
||||
}
|
||||
} else {
|
||||
context.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
// Create Endpoints (post/)
|
||||
|
||||
func CreateDatabaseFunction(context *gin.Context) {
|
||||
GetIsUserAuthorized(context)
|
||||
func CreateGroup(context *gin.Context) {
|
||||
GetUserLoggedIn(context)
|
||||
isAuthorized := context.GetBool("is-authorized")
|
||||
if isAuthorized {
|
||||
name, nameOk := context.GetQuery("name")
|
||||
tags, tagsOk := context.GetQueryArray("tags")
|
||||
requirements, requirementsOk := context.GetQueryArray("requirements")
|
||||
if nameOk {
|
||||
var newTags []functiontag.FunctionTag
|
||||
var newRequirements []function.Function
|
||||
if tagsOk {
|
||||
newTags = *functiontag.Get(GlobalDatabase, tags)
|
||||
}
|
||||
if requirementsOk {
|
||||
newRequirements = *function.Get(GlobalDatabase, requirements)
|
||||
}
|
||||
function.Function{
|
||||
Name: name,
|
||||
Tags: newTags,
|
||||
Requirements: newRequirements,
|
||||
}.Create(GlobalDatabase)
|
||||
group.Create(GlobalDatabase, name)
|
||||
context.Status(http.StatusOK)
|
||||
} else {
|
||||
context.AbortWithStatus(http.StatusBadRequest)
|
||||
|
@ -156,15 +125,31 @@ func CreateDatabaseFunction(context *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func CreateDatabaseFunctionTag(context *gin.Context) {
|
||||
GetIsUserAuthorized(context)
|
||||
func CreateFunction(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.Create(GlobalDatabase, name, tags, requirements)
|
||||
context.Status(http.StatusOK)
|
||||
} else {
|
||||
context.AbortWithStatus(http.StatusBadRequest)
|
||||
}
|
||||
} else {
|
||||
context.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
func CreateFunctionTag(context *gin.Context) {
|
||||
GetUserLoggedIn(context)
|
||||
isAuthorized := context.GetBool("is-authorized")
|
||||
if isAuthorized {
|
||||
name := context.Query("name")
|
||||
if name != "" {
|
||||
functiontag.FunctionTag{
|
||||
Name: name,
|
||||
}.Create(GlobalDatabase)
|
||||
functiontag.Create(GlobalDatabase, name)
|
||||
context.Status(http.StatusOK)
|
||||
} else {
|
||||
context.AbortWithStatus(http.StatusBadRequest)
|
||||
|
@ -174,9 +159,11 @@ func CreateDatabaseFunctionTag(context *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
// Update Endpoints (put/)
|
||||
|
||||
// Read Endpoints (get/)
|
||||
|
||||
func GetUserInfo(context *gin.Context) {
|
||||
func GetDiscordUser(context *gin.Context) {
|
||||
oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
|
||||
if err == nil {
|
||||
var oauthToken *oauth2.Token
|
||||
|
@ -206,7 +193,7 @@ func GetUserInfo(context *gin.Context) {
|
|||
context.Redirect(http.StatusTemporaryRedirect, GlobalConfig.GetFrontendRootDomain())
|
||||
}
|
||||
|
||||
func GetIsUserAuthorized(context *gin.Context) {
|
||||
func GetUserLoggedIn(context *gin.Context) {
|
||||
oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
|
||||
if err == nil {
|
||||
var oauthToken *oauth2.Token
|
||||
|
@ -229,66 +216,54 @@ func GetIsUserAuthorized(context *gin.Context) {
|
|||
context.Set("is-authorized", false)
|
||||
}
|
||||
|
||||
func GetDatabaseGroups(context *gin.Context) {
|
||||
func GetGroups(context *gin.Context) {
|
||||
groupNames, ok := context.GetQueryArray("groups")
|
||||
if ok {
|
||||
groups := group.Get(GlobalDatabase, groupNames)
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"groups": groups,
|
||||
"groups": group.Get(GlobalDatabase, groupNames),
|
||||
})
|
||||
} else {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"groups": []group.Group{},
|
||||
})
|
||||
context.Status(http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllDatabaseGroups(context *gin.Context) {
|
||||
groups := group.GetAll(GlobalDatabase)
|
||||
func GetAllGroups(context *gin.Context) {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"groups": groups,
|
||||
"groups": group.GetAll(GlobalDatabase),
|
||||
})
|
||||
}
|
||||
|
||||
func GetDatabaseFunctions(context *gin.Context) {
|
||||
func GetFunctions(context *gin.Context) {
|
||||
functionNames, ok := context.GetQueryArray("functions")
|
||||
if ok {
|
||||
functions := function.Get(GlobalDatabase, functionNames)
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"functions": functions,
|
||||
"functions": function.Get(GlobalDatabase, functionNames),
|
||||
})
|
||||
} else {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"functions": function.Function{},
|
||||
})
|
||||
context.Status(http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllDatabaseFunctions(context *gin.Context) {
|
||||
functions := function.GetAll(GlobalDatabase)
|
||||
func GetAllFunctions(context *gin.Context) {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"functions": functions,
|
||||
"functions": function.GetAll(GlobalDatabase),
|
||||
})
|
||||
}
|
||||
|
||||
func GetDatabaseFunctionTags(context *gin.Context) {
|
||||
func GetFunctionTags(context *gin.Context) {
|
||||
functionTagNames, ok := context.GetQueryArray("functiontags")
|
||||
if ok {
|
||||
functionTags := functiontag.Get(GlobalDatabase, functionTagNames)
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"function_tags": functionTags,
|
||||
"function_tags": functiontag.Get(GlobalDatabase, functionTagNames),
|
||||
})
|
||||
} else {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"function_tags": []functiontag.FunctionTag{},
|
||||
})
|
||||
context.Status(http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllDatabaseFunctionTags(context *gin.Context) {
|
||||
functionTags := functiontag.GetAll(GlobalDatabase)
|
||||
func GetAllFunctionTags(context *gin.Context) {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"function_tags": functionTags,
|
||||
"function_tags": functiontag.GetAll(GlobalDatabase),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module example.com/api
|
||||
|
||||
replace example.com/database/commands => ../database/commands
|
||||
replace example.com/database => ../database
|
||||
|
||||
replace example.com/database/user => ../database/user
|
||||
|
||||
|
@ -39,7 +39,6 @@ go 1.24.2
|
|||
require (
|
||||
example.com/auth/discord v0.0.0
|
||||
example.com/config/server v0.0.0
|
||||
example.com/database/commands v0.0.0
|
||||
example.com/database/function v0.0.0
|
||||
example.com/database/functiontag v0.0.0
|
||||
example.com/database/group v0.0.0
|
||||
|
@ -51,16 +50,6 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
example.com/database/character v0.0.0 // indirect
|
||||
example.com/database/customization v0.0.0 // indirect
|
||||
example.com/database/functionset v0.0.0 // indirect
|
||||
example.com/database/inventoryslot v0.0.0 // indirect
|
||||
example.com/database/item v0.0.0 // indirect
|
||||
example.com/database/itemtag v0.0.0 // indirect
|
||||
example.com/database/person v0.0.0 // indirect
|
||||
example.com/database/role v0.0.0 // indirect
|
||||
example.com/database/schematic v0.0.0 // indirect
|
||||
example.com/database/tier v0.0.0 // indirect
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
|
@ -77,7 +66,6 @@ require (
|
|||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
|
@ -91,5 +79,4 @@ require (
|
|||
golang.org/x/text v0.15.0 // indirect
|
||||
google.golang.org/protobuf v1.34.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gorm.io/driver/sqlite v1.5.7 // indirect
|
||||
)
|
||||
|
|
|
@ -42,8 +42,6 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
|||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
|
@ -93,8 +91,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
|
|||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
|
||||
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
|
||||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
||||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package databasecommands
|
||||
package database
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
@ -45,7 +45,3 @@ func InitializeDatabase() *gorm.DB {
|
|||
)
|
||||
return db
|
||||
}
|
||||
|
||||
func LogoutDatabaseUser(db *gorm.DB, oauthToken string) {
|
||||
db.Model(&user.User{}).Where("login_token = ?", oauthToken).Update("logged_in", false)
|
||||
}
|
|
@ -15,6 +15,14 @@ type Function struct {
|
|||
Requirements []Function `gorm:"many2many:function_requirement_associations" json:"requirements"`
|
||||
}
|
||||
|
||||
func (function Function) Create(db *gorm.DB) error {
|
||||
result := db.Create(&function)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (function *Function) getAssociations(db *gorm.DB) {
|
||||
db.Model(&function).Association("Tags").Find(&function.Tags)
|
||||
db.Model(&function).Association("Requirements").Find(&function.Requirements)
|
||||
|
@ -33,12 +41,12 @@ func (function Function) Update(db *gorm.DB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (function Function) Create(db *gorm.DB) error {
|
||||
result := db.Create(&function)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
func Create(db *gorm.DB, name string, tags []string, requirements []string) error {
|
||||
return Function{
|
||||
Name: name,
|
||||
Tags: *functiontag.Get(db, tags),
|
||||
Requirements: *Get(db, requirements),
|
||||
}.Create(db)
|
||||
}
|
||||
|
||||
func Get(db *gorm.DB, inputFunctions []string) *[]Function {
|
||||
|
@ -52,12 +60,18 @@ func Get(db *gorm.DB, inputFunctions []string) *[]Function {
|
|||
}
|
||||
|
||||
func GetAll(db *gorm.DB) *[]Function {
|
||||
var outputFunctions []Function
|
||||
var outputFunctionNames []string
|
||||
result := db.Model(&Function{}).Select("name").Find(&outputFunctionNames)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
}
|
||||
outputFunctions = *Get(db, outputFunctionNames)
|
||||
return &outputFunctions
|
||||
return Get(db, outputFunctionNames)
|
||||
}
|
||||
|
||||
func Update(db *gorm.DB, name string, tags []string, requirements []string) error {
|
||||
return Function{
|
||||
Name: name,
|
||||
Tags: *functiontag.Get(db, tags),
|
||||
Requirements: *Get(db, requirements),
|
||||
}.Update(db)
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ type FunctionSet struct {
|
|||
Functions []function.Function `gorm:"many2many:functionset_function_associations" json:"functions"`
|
||||
}
|
||||
|
||||
func CreateDatabaseFunctionSet(db *gorm.DB, functionSet FunctionSet) error {
|
||||
func (functionSet FunctionSet) Create(db *gorm.DB) error {
|
||||
result := db.Create(&functionSet)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -21,41 +21,52 @@ func CreateDatabaseFunctionSet(db *gorm.DB, functionSet FunctionSet) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseFunctionSet(db *gorm.DB, inputFunctionSet int) FunctionSet {
|
||||
var outputFunctionSet FunctionSet
|
||||
result := db.Model(&FunctionSet{}).Where("id = ?", inputFunctionSet).Take(&outputFunctionSet)
|
||||
if result.Error != nil {
|
||||
return FunctionSet{}
|
||||
}
|
||||
db.Model(&outputFunctionSet).Association("Functions").Find(&outputFunctionSet.Functions)
|
||||
return outputFunctionSet
|
||||
func (functionSet *FunctionSet) getAssociations(db *gorm.DB) {
|
||||
db.Model(&functionSet).Association("Functions").Find(&functionSet.Functions)
|
||||
}
|
||||
|
||||
func GetDatabaseFunctionSets(db *gorm.DB, inputFunctionSets []int) []FunctionSet {
|
||||
var outputFunctionSets []FunctionSet
|
||||
db.Find(&outputFunctionSets)
|
||||
for _, inputFunctionSet := range inputFunctionSets {
|
||||
outputFunctionSets = append(outputFunctionSets, GetDatabaseFunctionSet(db, inputFunctionSet))
|
||||
}
|
||||
return outputFunctionSets
|
||||
func (functionSet *FunctionSet) Get(db *gorm.DB, inputFunctionSet int) {
|
||||
db.Where("id = ?", inputFunctionSet).Take(&functionSet)
|
||||
functionSet.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabaseFunctionSets(db *gorm.DB) []FunctionSet {
|
||||
var outputFunctionSets []FunctionSet
|
||||
result := db.Find(&outputFunctionSets)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
}
|
||||
for index, outputFunctionSet := range outputFunctionSets {
|
||||
outputFunctionSets[index] = GetDatabaseFunctionSet(db, int(outputFunctionSet.ID))
|
||||
}
|
||||
return outputFunctionSets
|
||||
}
|
||||
|
||||
func UpdateDatabaseFunctionSet(db *gorm.DB, functionSet FunctionSet) error {
|
||||
func (functionSet FunctionSet) Update(db *gorm.DB) error {
|
||||
result := db.Save(&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),
|
||||
}.Create(db)
|
||||
}
|
||||
|
||||
func Get(db *gorm.DB, inputFunctionSets []int) *[]FunctionSet {
|
||||
var outputFunctionSets []FunctionSet
|
||||
for _, inputFunctionSet := range inputFunctionSets {
|
||||
var outputFunctionSet FunctionSet
|
||||
outputFunctionSet.Get(db, inputFunctionSet)
|
||||
outputFunctionSets = append(outputFunctionSets, outputFunctionSet)
|
||||
}
|
||||
return &outputFunctionSets
|
||||
}
|
||||
|
||||
func GetAll(db *gorm.DB) *[]FunctionSet {
|
||||
var outputFunctionSetIDs []int
|
||||
result := db.Model(&FunctionSet{}).Select("id").Find(&outputFunctionSetIDs)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
}
|
||||
return Get(db, outputFunctionSetIDs)
|
||||
}
|
||||
|
||||
func Update(db *gorm.DB, id int, functions []string) error {
|
||||
outputFunctionSet := FunctionSet{
|
||||
Functions: *function.Get(db, functions),
|
||||
}
|
||||
outputFunctionSet.ID = uint(id)
|
||||
return outputFunctionSet.Update(db)
|
||||
}
|
||||
|
|
|
@ -11,11 +11,11 @@ type FunctionTag struct {
|
|||
Name string `gorm:"primaryKey uniqueIndex" json:"name"`
|
||||
}
|
||||
|
||||
func (functionTag *FunctionTag) Get(db *gorm.DB, inputFunctionTag string) {
|
||||
func (functionTag *FunctionTag) get(db *gorm.DB, inputFunctionTag string) {
|
||||
db.Where("name = ?", inputFunctionTag).Take(&functionTag)
|
||||
}
|
||||
|
||||
func (functionTag FunctionTag) Update(db *gorm.DB) error {
|
||||
func (functionTag FunctionTag) update(db *gorm.DB) error {
|
||||
result := db.Save(&functionTag)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -23,7 +23,7 @@ func (functionTag FunctionTag) Update(db *gorm.DB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (functionTag FunctionTag) Create(db *gorm.DB) error {
|
||||
func (functionTag FunctionTag) create(db *gorm.DB) error {
|
||||
result := db.Create(&functionTag)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -31,11 +31,17 @@ func (functionTag FunctionTag) Create(db *gorm.DB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func Create(db *gorm.DB, name string) error {
|
||||
return FunctionTag{
|
||||
Name: name,
|
||||
}.create(db)
|
||||
}
|
||||
|
||||
func Get(db *gorm.DB, inputFunctionTags []string) *[]FunctionTag {
|
||||
var outputFunctionTags []FunctionTag
|
||||
for _, inputFunctionTag := range inputFunctionTags {
|
||||
var outputFunctionTag FunctionTag
|
||||
outputFunctionTag.Get(db, inputFunctionTag)
|
||||
outputFunctionTag.get(db, inputFunctionTag)
|
||||
outputFunctionTags = append(outputFunctionTags, outputFunctionTag)
|
||||
}
|
||||
return &outputFunctionTags
|
||||
|
@ -51,3 +57,9 @@ func GetAll(db *gorm.DB) *[]FunctionTag {
|
|||
outputFunctionTags = *Get(db, outputFunctionTagNames)
|
||||
return &outputFunctionTags
|
||||
}
|
||||
|
||||
func Update(db *gorm.DB, name string) error {
|
||||
return FunctionTag{
|
||||
Name: name,
|
||||
}.update(db)
|
||||
}
|
||||
|
|
|
@ -1,34 +1,34 @@
|
|||
module example.com/database/commands
|
||||
module example.com/database
|
||||
|
||||
go 1.24.2
|
||||
|
||||
replace example.com/database/user => ../user
|
||||
replace example.com/database/user => ./user
|
||||
|
||||
replace example.com/database/character => ../character
|
||||
replace example.com/database/character => ./character
|
||||
|
||||
replace example.com/database/functionset => ../functionset
|
||||
replace example.com/database/functionset => ./functionset
|
||||
|
||||
replace example.com/database/inventoryslot => ../inventoryslot
|
||||
replace example.com/database/inventoryslot => ./inventoryslot
|
||||
|
||||
replace example.com/database/customization => ../customization
|
||||
replace example.com/database/customization => ./customization
|
||||
|
||||
replace example.com/database/item => ../item
|
||||
replace example.com/database/item => ./item
|
||||
|
||||
replace example.com/database/itemtag => ../itemtag
|
||||
replace example.com/database/itemtag => ./itemtag
|
||||
|
||||
replace example.com/database/person => ../person
|
||||
replace example.com/database/person => ./person
|
||||
|
||||
replace example.com/database/role => ../role
|
||||
replace example.com/database/role => ./role
|
||||
|
||||
replace example.com/database/schematic => ../schematic
|
||||
replace example.com/database/schematic => ./schematic
|
||||
|
||||
replace example.com/database/tier => ../tier
|
||||
replace example.com/database/tier => ./tier
|
||||
|
||||
replace example.com/database/group => ../group
|
||||
replace example.com/database/group => ./group
|
||||
|
||||
replace example.com/database/function => ../function
|
||||
replace example.com/database/function => ./function
|
||||
|
||||
replace example.com/database/functiontag => ../functiontag
|
||||
replace example.com/database/functiontag => ./functiontag
|
||||
|
||||
require (
|
||||
example.com/database/character v0.0.0
|
|
@ -31,6 +31,12 @@ func (group Group) Create(db *gorm.DB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func Create(db *gorm.DB, name string) error {
|
||||
return Group{
|
||||
Name: name,
|
||||
}.Create(db)
|
||||
}
|
||||
|
||||
func Get(db *gorm.DB, inputGroups []string) *[]Group {
|
||||
var outputGroups []Group
|
||||
for _, inputGroup := range inputGroups {
|
||||
|
|
|
@ -11,7 +11,7 @@ type ItemTag struct {
|
|||
Name string `gorm:"primaryKey uniqueIndex" json:"name"`
|
||||
}
|
||||
|
||||
func CreateDatabaseItemTag(db *gorm.DB, itemTag ItemTag) error {
|
||||
func (itemTag ItemTag) Create(db *gorm.DB) error {
|
||||
result := db.Create(&itemTag)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -19,39 +19,45 @@ func CreateDatabaseItemTag(db *gorm.DB, itemTag ItemTag) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseItemTag(db *gorm.DB, inputItemTag string) ItemTag {
|
||||
var outputItemTag ItemTag
|
||||
result := db.Model(&ItemTag{}).Where("name = ?", outputItemTag).Take(&outputItemTag)
|
||||
if result.Error != nil {
|
||||
return ItemTag{}
|
||||
}
|
||||
return outputItemTag
|
||||
func (itemTag *ItemTag) Get(db *gorm.DB, inputItemTag string) {
|
||||
db.Where("name = ?", inputItemTag).Take(&itemTag)
|
||||
}
|
||||
|
||||
func GetDatabaseItemTags(db *gorm.DB, inputItemTags []string) []ItemTag {
|
||||
var outputItemTags []ItemTag
|
||||
for _, inputItemTag := range inputItemTags {
|
||||
outputItemTags = append(outputItemTags, GetDatabaseItemTag(db, inputItemTag))
|
||||
}
|
||||
return outputItemTags
|
||||
}
|
||||
|
||||
func GetAllDatabaseItemTags(db *gorm.DB) []ItemTag {
|
||||
var outputItemTags []ItemTag
|
||||
result := db.Find(&outputItemTags)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
}
|
||||
for index, outputItemTag := range outputItemTags {
|
||||
outputItemTags[index] = GetDatabaseItemTag(db, outputItemTag.Name)
|
||||
}
|
||||
return outputItemTags
|
||||
}
|
||||
|
||||
func UpdateDatabaseItemTag(db *gorm.DB, itemTag ItemTag) error {
|
||||
func (itemTag ItemTag) Update(db *gorm.DB) error {
|
||||
result := db.Save(&itemTag)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Create(db *gorm.DB, name string) error {
|
||||
return ItemTag{
|
||||
Name: name,
|
||||
}.Create(db)
|
||||
}
|
||||
|
||||
func Get(db *gorm.DB, inputItemTags []string) *[]ItemTag {
|
||||
var outputItemTags []ItemTag
|
||||
for _, inputItemTag := range inputItemTags {
|
||||
var outputItemTag ItemTag
|
||||
outputItemTag.Get(db, inputItemTag)
|
||||
outputItemTags = append(outputItemTags, outputItemTag)
|
||||
}
|
||||
return &outputItemTags
|
||||
}
|
||||
|
||||
func GetAll(db *gorm.DB) *[]ItemTag {
|
||||
var outputItemTagNames []string
|
||||
result := db.Model(&ItemTag{}).Select("name").Find(&outputItemTagNames)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
}
|
||||
return Get(db, outputItemTagNames)
|
||||
}
|
||||
|
||||
func Update(db *gorm.DB, name string) error {
|
||||
return ItemTag{
|
||||
Name: name,
|
||||
}.Update(db)
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ type Tier struct {
|
|||
FunctionSets []functionset.FunctionSet `gorm:"many2many:tier_functionset_associations" json:"function_sets"`
|
||||
}
|
||||
|
||||
func CreateDatabaseTier(db *gorm.DB, tier Tier) error {
|
||||
func (tier Tier) Create(db *gorm.DB) error {
|
||||
result := db.Create(&tier)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
@ -21,40 +21,52 @@ func CreateDatabaseTier(db *gorm.DB, tier Tier) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDatabaseTier(db *gorm.DB, inputTier int) Tier {
|
||||
var outputTier Tier
|
||||
result := db.Model(&Tier{}).Where("id = ?", inputTier).Take(&outputTier)
|
||||
if result.Error != nil {
|
||||
return Tier{}
|
||||
}
|
||||
db.Model(&outputTier).Association("FunctionSets").Find(&outputTier.FunctionSets)
|
||||
return outputTier
|
||||
func (tier *Tier) getAssociations(db *gorm.DB) {
|
||||
db.Model(&tier).Association("FunctionSets").Find(&tier.FunctionSets)
|
||||
}
|
||||
|
||||
func GetDatabaseTiers(db *gorm.DB, inputTiers []int) []Tier {
|
||||
var outputTiers []Tier
|
||||
for _, inputTier := range inputTiers {
|
||||
outputTiers = append(outputTiers, GetDatabaseTier(db, inputTier))
|
||||
}
|
||||
return outputTiers
|
||||
func (tier *Tier) Get(db *gorm.DB, inputTier int) {
|
||||
db.Where("id = ?", inputTier).Take(&tier)
|
||||
tier.getAssociations(db)
|
||||
}
|
||||
|
||||
func GetAllDatabaseTiers(db *gorm.DB) []Tier {
|
||||
var outputTiers []Tier
|
||||
result := db.Find(&outputTiers)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
}
|
||||
for index, outputTier := range outputTiers {
|
||||
outputTiers[index] = GetDatabaseTier(db, int(outputTier.ID))
|
||||
}
|
||||
return outputTiers
|
||||
}
|
||||
|
||||
func UpdateDatabaseTier(db *gorm.DB, tier Tier) error {
|
||||
func (tier Tier) Update(db *gorm.DB) error {
|
||||
result := db.Save(&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),
|
||||
}.Create(db)
|
||||
}
|
||||
|
||||
func Get(db *gorm.DB, inputTiers []int) *[]Tier {
|
||||
var outputTiers []Tier
|
||||
for _, inputTier := range inputTiers {
|
||||
var outputTier Tier
|
||||
outputTier.Get(db, inputTier)
|
||||
outputTiers = append(outputTiers, outputTier)
|
||||
}
|
||||
return &outputTiers
|
||||
}
|
||||
|
||||
func GetAll(db *gorm.DB) *[]Tier {
|
||||
var outputTierIDs []int
|
||||
result := db.Find(&outputTierIDs)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
}
|
||||
return Get(db, outputTierIDs)
|
||||
}
|
||||
|
||||
func Update(db *gorm.DB, id int, functionSets []int) error {
|
||||
outputTier := Tier{
|
||||
FunctionSets: *functionset.Get(db, functionSets),
|
||||
}
|
||||
outputTier.ID = uint(id)
|
||||
return outputTier.Update(db)
|
||||
}
|
||||
|
|
|
@ -41,6 +41,10 @@ func (user User) Create(db *gorm.DB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func Logout(db *gorm.DB, oauthToken string) {
|
||||
db.Model(&User{}).Where("login_token = ?", oauthToken).Update("logged_in", false)
|
||||
}
|
||||
|
||||
func Exists(db *gorm.DB, id string) bool {
|
||||
var queryUser User
|
||||
result := db.Where("id = ?", id).Take(&queryUser)
|
||||
|
|
Loading…
Reference in a new issue