API Endpoint ListCharacters
This commit is contained in:
@@ -2,6 +2,7 @@ package character
|
||||
|
||||
import (
|
||||
"bamort/database"
|
||||
"bamort/models"
|
||||
"bamort/skills"
|
||||
|
||||
"encoding/json"
|
||||
@@ -22,11 +23,24 @@ Add CRUD operations for characters:
|
||||
|
||||
func ListCharacters(c *gin.Context) {
|
||||
var characters []Char
|
||||
var listOfChars []CharList
|
||||
if err := database.DB.Find(&characters).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve characters"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, characters)
|
||||
for i := range characters {
|
||||
listOfChars = append(listOfChars, CharList{
|
||||
BamortBase: models.BamortBase{
|
||||
ID: characters[i].ID,
|
||||
Name: characters[i].Name,
|
||||
},
|
||||
Rasse: characters[i].Rasse,
|
||||
Typ: characters[i].Typ,
|
||||
Grad: characters[i].Grad,
|
||||
Owner: "test",
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, listOfChars)
|
||||
}
|
||||
|
||||
func CreateCharacter(c *gin.Context) {
|
||||
|
||||
@@ -97,6 +97,14 @@ type Char struct {
|
||||
Ausruestung []equipment.Ausruestung `gorm:"foreignKey:CharacterID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"ausruestung"`
|
||||
Image string `json:"image,omitempty"`
|
||||
}
|
||||
type CharList struct {
|
||||
models.BamortBase
|
||||
Rasse string `json:"rasse"`
|
||||
Typ string `json:"typ"`
|
||||
Grad int `json:"grad"`
|
||||
Owner string `json:"owner"`
|
||||
Public bool `json:"public"`
|
||||
}
|
||||
|
||||
func (object *Char) TableName() string {
|
||||
return dbPrefix + "_" + "chars"
|
||||
|
||||
@@ -64,12 +64,16 @@ func TestListCharacters(t *testing.T) {
|
||||
assert.Equal(t, http.StatusOK, respRecorder.Code)
|
||||
|
||||
// Assert the response body
|
||||
var listOfCharacter []*character.Char
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user