selektion von Chars nach UserID and public

This commit is contained in:
2025-08-29 07:07:08 +02:00
parent 471cc7a7b2
commit 7eead94951
3 changed files with 29 additions and 18 deletions
+15 -17
View File
@@ -32,8 +32,11 @@ func respondWithError(c *gin.Context, status int, message string) {
func ListCharacters(c *gin.Context) {
logger.Debug("ListCharacters aufgerufen")
//var characters []models.Char
//var listOfChars []models.CharList
type AllCharacters struct {
SelfOwned []models.CharList `json:"self_owned"`
Others []models.CharList `json:"others"`
}
allCharacters := AllCharacters{}
logger.Debug("Lade Charaktere aus der Datenbank...")
//if err := database.DB.Find(&characters).Error; err != nil {
@@ -46,24 +49,19 @@ func ListCharacters(c *gin.Context) {
}
logger.Debug("Gefundene Charaktere: %d", len(listOfChars))
allCharacters.SelfOwned = listOfChars
/*
for i := range characters {
listOfChars = append(listOfChars, models.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: characters[i].User.Username,
})
}
*/
listPublic, err := models.FindPublicCharList()
if err != nil {
logger.Error("Fehler beim Laden der öffentlichen Charaktere: %s", err.Error())
respondWithError(c, http.StatusInternalServerError, "Failed to retrieve public characters")
return
}
allCharacters.Others = listPublic
logger.Info("Charakterliste erfolgreich geladen: %d Charaktere", len(listOfChars))
c.JSON(http.StatusOK, listOfChars)
c.JSON(http.StatusOK, allCharacters)
}
func CreateCharacter(c *gin.Context) {
+13
View File
@@ -210,6 +210,19 @@ func (object *Char) FindByUserID(userID uint) ([]Char, error) {
return chars, nil
}
func FindPublicCharList() ([]CharList, error) {
var chars []CharList
err := database.DB.Table("char_chars").
Select("char_chars.id, char_chars.name, char_chars.user_id, char_chars.rasse, char_chars.typ, char_chars.grad, char_chars.public, users.username as owner").
Joins("LEFT JOIN users ON char_chars.user_id = users.user_id").
Where("char_chars.public = ?", true).
Find(&chars).Error
if err != nil {
return nil, err
}
return chars, nil
}
// FindCharListByUserID finds all characters belonging to a specific user for listing (minimal data)
func FindCharListByUserID(userID uint) ([]CharList, error) {
var chars []CharList