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) { func ListCharacters(c *gin.Context) {
logger.Debug("ListCharacters aufgerufen") logger.Debug("ListCharacters aufgerufen")
//var characters []models.Char type AllCharacters struct {
//var listOfChars []models.CharList SelfOwned []models.CharList `json:"self_owned"`
Others []models.CharList `json:"others"`
}
allCharacters := AllCharacters{}
logger.Debug("Lade Charaktere aus der Datenbank...") logger.Debug("Lade Charaktere aus der Datenbank...")
//if err := database.DB.Find(&characters).Error; err != nil { //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)) logger.Debug("Gefundene Charaktere: %d", len(listOfChars))
allCharacters.SelfOwned = listOfChars
/* listPublic, err := models.FindPublicCharList()
for i := range characters {
listOfChars = append(listOfChars, models.CharList{ if err != nil {
BamortBase: models.BamortBase{ logger.Error("Fehler beim Laden der öffentlichen Charaktere: %s", err.Error())
ID: characters[i].ID, respondWithError(c, http.StatusInternalServerError, "Failed to retrieve public characters")
Name: characters[i].Name, return
}, }
Rasse: characters[i].Rasse, allCharacters.Others = listPublic
Typ: characters[i].Typ,
Grad: characters[i].Grad,
Owner: characters[i].User.Username,
})
}
*/
logger.Info("Charakterliste erfolgreich geladen: %d Charaktere", len(listOfChars)) logger.Info("Charakterliste erfolgreich geladen: %d Charaktere", len(listOfChars))
c.JSON(http.StatusOK, listOfChars) c.JSON(http.StatusOK, allCharacters)
} }
func CreateCharacter(c *gin.Context) { func CreateCharacter(c *gin.Context) {
+13
View File
@@ -210,6 +210,19 @@ func (object *Char) FindByUserID(userID uint) ([]Char, error) {
return chars, nil 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) // FindCharListByUserID finds all characters belonging to a specific user for listing (minimal data)
func FindCharListByUserID(userID uint) ([]CharList, error) { func FindCharListByUserID(userID uint) ([]CharList, error) {
var chars []CharList var chars []CharList
+1 -1
View File
@@ -64,7 +64,7 @@ export default {
const response = await API.get('/api/characters', { const response = await API.get('/api/characters', {
headers: { Authorization: `Bearer ${token}` }, headers: { Authorization: `Bearer ${token}` },
}) })
this.characters = response.data this.characters = response.data.self_owned
} catch (error) { } catch (error) {
console.error('Error loading characters:', error) console.error('Error loading characters:', error)
} }