added Maintenance to frontend and backend

This commit is contained in:
2025-01-18 20:59:35 +01:00
parent fb7e3967ae
commit b27d511e13
16 changed files with 981 additions and 14 deletions
+133
View File
@@ -0,0 +1,133 @@
package gsmaster
import (
"bamort/database"
"net/http"
"github.com/gin-gonic/gin"
)
func GetMasterData(c *gin.Context) {
type dtaStruct struct {
Skills []Skill `json:"skills"`
Weaponskills []WeaponSkill `json:"weaponskills"`
Spell []Spell `json:"spells"`
Equipment []Equipment `json:"equipment"`
Weapons []Weapon `json:"weapons"`
SkillCategories []string `json:"skillcategories"`
}
var dta dtaStruct
var err error
var ski Skill
if err := database.DB.Find(&dta.Skills).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve Skills"})
return
}
if err := database.DB.Find(&dta.Weaponskills).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve Weaponskills"})
return
}
if err := database.DB.Find(&dta.Spell).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve Spell"})
return
}
if err := database.DB.Find(&dta.Equipment).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve Equipment"})
return
}
if err := database.DB.Find(&dta.Weapons).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve Weapons"})
return
}
dta.SkillCategories, err = ski.GetSkillCategories()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve SkillCategories" + err.Error()})
return
}
c.JSON(http.StatusOK, dta)
}
func GetMDSkills(c *gin.Context) {
type dtaStruct struct {
Skills []Skill `json:"skills"`
Weaponskills []WeaponSkill `json:"weaponskills"`
Spell []Spell `json:"spells"`
Equipment []Equipment `json:"equipment"`
Weapons []Weapon `json:"weapons"`
SkillCategories []string `json:"skillcategories"`
}
var dta dtaStruct
if err := database.DB.Find(&dta.Skills).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve characters"})
return
}
c.JSON(http.StatusOK, dta.Skills)
}
func GetMDSkill(c *gin.Context) {
type dtaStruct struct {
Skills []Skill `json:"skills"`
Weaponskills []WeaponSkill `json:"weaponskills"`
Spell []Spell `json:"spells"`
Equipment []Equipment `json:"equipment"`
Weapons []Weapon `json:"weapons"`
SkillCategories []string `json:"skillcategories"`
}
var dta dtaStruct
if err := database.DB.Find(&dta.Skills).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve characters"})
return
}
c.JSON(http.StatusOK, dta)
}
func UpdateMDSkill(c *gin.Context) {
type dtaStruct struct {
Skills []Skill `json:"skills"`
Weaponskills []WeaponSkill `json:"weaponskills"`
Spell []Spell `json:"spells"`
Equipment []Equipment `json:"equipment"`
Weapons []Weapon `json:"weapons"`
SkillCategories []string `json:"skillcategories"`
}
var dta dtaStruct
if err := database.DB.Find(&dta.Skills).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve characters"})
return
}
c.JSON(http.StatusOK, dta)
}
func AddSkill(c *gin.Context) {
type dtaStruct struct {
Skills []Skill `json:"skills"`
Weaponskills []WeaponSkill `json:"weaponskills"`
Spell []Spell `json:"spells"`
Equipment []Equipment `json:"equipment"`
Weapons []Weapon `json:"weapons"`
SkillCategories []string `json:"skillcategories"`
}
var dta dtaStruct
if err := database.DB.Find(&dta.Skills).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve characters"})
return
}
c.JSON(http.StatusOK, dta)
}
func DeleteMDSkill(c *gin.Context) {
type dtaStruct struct {
Skills []Skill `json:"skills"`
Weaponskills []WeaponSkill `json:"weaponskills"`
Spell []Spell `json:"spells"`
Equipment []Equipment `json:"equipment"`
Weapons []Weapon `json:"weapons"`
SkillCategories []string `json:"skillcategories"`
}
var dta dtaStruct
if err := database.DB.Find(&dta.Skills).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve characters"})
return
}
c.JSON(http.StatusOK, dta)
}
+15
View File
@@ -152,6 +152,21 @@ func (object *Skill) Save() error {
}
return nil
}
func (object *Skill) GetSkillCategories() ([]string, error) {
var categories []string
gameSystem := "midgard"
result := database.DB.Model(&Skill{}).
Where("system = ? and category is not null", gameSystem).
Distinct().
Pluck("category", &categories)
if result.Error != nil {
return nil, result.Error
}
return categories, nil
}
func (object *WeaponSkill) TableName() string {
return dbPrefix + "_" + "weaponskills"
}
+1
View File
@@ -39,6 +39,7 @@ func main() {
protected.PUT("/ausruestung/:ausruestung_id", equipment.UpdateAusruestung)
protected.DELETE("/ausruestung/:ausruestung_id", equipment.DeleteAusruestung)
*/
router.MaintenanceRouterGrp(protected)
protected.POST("/upload", importer.UploadFiles)
protected.GET("/setupcheck", maintenance.SetupCheck)
+12
View File
@@ -2,6 +2,7 @@ package router
import (
"bamort/character"
"bamort/gsmaster"
"bamort/user"
"github.com/gin-contrib/cors"
@@ -42,3 +43,14 @@ func CharRouterGrp(rt *gin.RouterGroup) *gin.RouterGroup {
//rCharGrp.DELETE("/{id}/skills/{id}", character.DeleteSkill) //ADEletedd a skill to a character
return rCharGrp
}
func MaintenanceRouterGrp(rt *gin.RouterGroup) *gin.RouterGroup {
rCharGrp := rt.Group("/maintenance")
rCharGrp.GET("", gsmaster.GetMasterData)
rCharGrp.GET("/skills", gsmaster.GetMDSkills)
rCharGrp.GET("/skills/:id", gsmaster.GetMDSkill)
rCharGrp.PUT("/skills/:id", gsmaster.UpdateMDSkill)
rCharGrp.POST("/skills", gsmaster.AddSkill)
rCharGrp.DELETE("/skills/:id", gsmaster.DeleteMDSkill)
return rCharGrp
}
+206
View File
@@ -0,0 +1,206 @@
package tests
import (
"bamort/character"
"bamort/gsmaster"
"bamort/maintenance"
"bamort/router"
"bamort/user"
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestMaintSetupCheck(t *testing.T) {
//r := gin.Default()
c := gin.Context{}
maintenance.SetupCheck(&c)
assert.Empty(t, nil, "expected NIL to be empty")
/*
SetupTestDB()
TestCreateChar(t)
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
// Character routes
rCharGrp := router.CharRouterGrp(protected)
rCharGrp.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "Test OK"})
})
// Create a test HTTP request
req, _ := http.NewRequest("GET", "/api/characters", nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer ${token}")
// Create a response recorder to capture the handler's response
respRecorder := httptest.NewRecorder()
// Perform the test request
r.ServeHTTP(respRecorder, req)
// Assert the response status code
assert.Equal(t, http.StatusOK, respRecorder.Code)
// Assert the response body
var listOfCharacter []*character.CharList
err := json.Unmarshal(respRecorder.Body.Bytes(), &listOfCharacter)
assert.NoError(t, err)
assert.Equal(t, "Harsk Hammerhuter, Zen", listOfCharacter[0].Name)
assert.Equal(t, "Zwerg", listOfCharacter[0].Rasse)
assert.Equal(t, 1, int(listOfCharacter[0].ID)) // Check the simulated ID
assert.Equal(t, "Krieger", listOfCharacter[0].Typ)
assert.Equal(t, 3, listOfCharacter[0].Grad)
assert.Equal(t, "test", listOfCharacter[0].Owner)
assert.Equal(t, false, listOfCharacter[0].Public)
*/
}
func TestGetMasterData(t *testing.T) {
SetupTestDB(false)
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
// Character routes
rCharGrp := router.MaintenanceRouterGrp(protected)
rCharGrp.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "Test OK"})
})
u := user.User{}
u.FirstId(1)
// Create a test HTTP request
req, _ := http.NewRequest("GET", "/api/maintenance", nil)
req.Header.Set("Content-Type", "application/json")
token := user.GenerateToken(&u)
req.Header.Set("Authorization", "Bearer "+token)
// Create a response recorder to capture the handler's response
respRecorder := httptest.NewRecorder()
// Perform the test request
r.ServeHTTP(respRecorder, req)
// Assert the response status code
assert.Equal(t, http.StatusOK, respRecorder.Code)
// Assert the response body
type dtaStruct struct {
Skills []gsmaster.Skill `json:"skills"`
Weaponskills []gsmaster.WeaponSkill `json:"weaponskills"`
Spell []gsmaster.Spell `json:"spells"`
Equipment []gsmaster.Equipment `json:"equipment"`
Weapons []gsmaster.Weapon `json:"weapons"`
}
var dta dtaStruct
err := json.Unmarshal(respRecorder.Body.Bytes(), &dta)
assert.NoError(t, err)
}
func TestGetMDSkillCategories(t *testing.T) {
SetupTestDB(false)
//gsmaster.MigrateStructure()
ski := gsmaster.Skill{}
categories, err := ski.GetSkillCategories()
assert.NoError(t, err)
assert.LessOrEqual(t, 1, len(categories))
assert.Equal(t, "Allgemein", categories[0])
}
func TestGetMDSkills(t *testing.T) {
SetupTestDB()
//TestCreateChar(t)
//TestRegisterUser(t)
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
// Character routes
rCharGrp := router.CharRouterGrp(protected)
rCharGrp.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "Test OK"})
})
// Create a test HTTP request
req, _ := http.NewRequest("GET", "/api/characters/9", nil)
//req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", "application/json")
//req.Header.Set("Authorization", "Bearer ${token}")
req.Header.Set("Authorization", "Bearer dc7a780.1:bba7f4daabda117f2a2c14263")
// Create a response recorder to capture the handler's response
respRecorder := httptest.NewRecorder()
// Perform the test request
r.ServeHTTP(respRecorder, req)
// Assert the response status code
assert.Equal(t, http.StatusOK, respRecorder.Code)
// Assert the response body
var listOfCharacter *character.Char
err := json.Unmarshal(respRecorder.Body.Bytes(), &listOfCharacter)
assert.NoError(t, err)
assert.Equal(t, "Harsk Hammerhuter, Zen", listOfCharacter.Name)
assert.Equal(t, "Zwerg", listOfCharacter.Rasse)
assert.Equal(t, 1, int(listOfCharacter.ID)) // Check the simulated ID
assert.Equal(t, "Krieger", listOfCharacter.Typ)
assert.Equal(t, 3, listOfCharacter.Grad)
//assert.Equal(t, "test", listOfCharacter.Owner)
//assert.Equal(t, false, listOfCharacter.Public)
}
// TestCreateCharacter tests the POST /characters endpoint
func TestGetMDSkill(t *testing.T) {
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
// Character routes
rCharGrp := router.CharRouterGrp(protected)
rCharGrp.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "Test OK"})
})
// Define the test case input
testCharacter := Character{
Name: "Aragorn",
Race: "Human",
}
jsonData, _ := json.Marshal(testCharacter)
// Create a test HTTP request
req, _ := http.NewRequest("POST", "/characters", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
// Create a response recorder to capture the handler's response
respRecorder := httptest.NewRecorder()
// Perform the test request
r.ServeHTTP(respRecorder, req)
// Assert the response status code
assert.Equal(t, http.StatusCreated, respRecorder.Code)
// Assert the response body
var createdCharacter Character
err := json.Unmarshal(respRecorder.Body.Bytes(), &createdCharacter)
assert.NoError(t, err)
assert.Equal(t, "Aragorn", createdCharacter.Name)
assert.Equal(t, "Human", createdCharacter.Race)
assert.Equal(t, 1, createdCharacter.ID) // Check the simulated ID
}
+24 -14
View File
@@ -9,6 +9,7 @@ import (
"bamort/skills"
"bamort/user"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
@@ -16,22 +17,31 @@ import (
var migrationDone bool
// SetupTestDB creates an in-memory SQLite database for testing
func SetupTestDB() {
func SetupTestDB(opts ...bool) {
useTestDB := true
if len(opts) > 0 {
useTestDB = opts[0]
}
if database.DB == nil {
//*
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
panic("failed to connect to the test database")
var db *gorm.DB
var err error
if useTestDB {
//*
db, err = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
panic("failed to connect to the test database")
}
//*/
} else {
//* //testin with persitant MariaDB
dsn := "bamort:bG4)efozrc@tcp(192.168.0.5:3306)/bamort?charset=utf8mb4&parseTime=True&loc=Local"
//dsn := "root:26Osiris-Mar@tcp(192.168.0.5:3306)/bamort?charset=utf8mb4&parseTime=True&loc=Local"
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect to the test database")
}
//*/
}
//*/
/* //testin with persitant MariaDB
dsn := "bamort:bG4)efozrc@tcp(192.168.0.5:3306)/bamort?charset=utf8mb4&parseTime=True&loc=Local"
//dsn := "root:26Osiris-Mar@tcp(192.168.0.5:3306)/bamort?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect to the test database")
}
//*/
database.DB = db
}
if !migrationDone {