Files
bamort/backend/equipment/handlers.go
T

202 lines
5.0 KiB
Go
Raw Normal View History

2024-12-30 16:23:05 +01:00
package equipment
import (
"bamort/database"
2025-07-28 22:08:19 +02:00
"bamort/models"
2024-12-30 16:23:05 +01:00
"net/http"
"github.com/gin-gonic/gin"
)
/*
Endpoints for Managing Ausruestung
1. Create Ausruestung
Allows users to add new equipment items for a specific character.
*/
2025-07-24 07:39:43 +02:00
func respondWithError(c *gin.Context, status int, message string) {
c.JSON(status, gin.H{"error": message})
}
2026-02-01 22:48:07 +01:00
// checkEquipmentOwnership verifies that the logged-in user owns the equipment's character
func checkEquipmentOwnership(c *gin.Context, characterID uint) bool {
userID := c.GetUint("userID")
var character models.Char
if err := database.DB.Select("id", "user_id").First(&character, characterID).Error; err != nil {
respondWithError(c, http.StatusNotFound, "Character not found")
return false
}
if character.UserID != userID {
respondWithError(c, http.StatusForbidden, "You are not authorized to modify this character's equipment")
return false
}
return true
}
2024-12-30 16:23:05 +01:00
func CreateAusruestung(c *gin.Context) {
2025-07-28 22:08:19 +02:00
var ausruestung models.EqAusruestung
2024-12-30 16:23:05 +01:00
if err := c.ShouldBindJSON(&ausruestung); err != nil {
2025-07-24 07:39:43 +02:00
respondWithError(c, http.StatusBadRequest, err.Error())
2024-12-30 16:23:05 +01:00
return
}
2026-02-01 22:48:07 +01:00
// Check ownership
if !checkEquipmentOwnership(c, ausruestung.CharacterID) {
return
}
2024-12-30 16:23:05 +01:00
if err := database.DB.Create(&ausruestung).Error; err != nil {
2025-07-24 07:39:43 +02:00
respondWithError(c, http.StatusInternalServerError, "Failed to create Ausruestung")
2024-12-30 16:23:05 +01:00
return
}
c.JSON(http.StatusCreated, ausruestung)
}
2025-01-03 00:03:31 +01:00
func ListAusruestung(c *gin.Context) {
2024-12-30 16:23:05 +01:00
characterID := c.Param("character_id")
2025-07-28 22:08:19 +02:00
var ausruestung []models.EqAusruestung
2024-12-30 16:23:05 +01:00
if err := database.DB.Where("character_id = ?", characterID).Find(&ausruestung).Error; err != nil {
2025-07-24 07:39:43 +02:00
respondWithError(c, http.StatusInternalServerError, "Failed to retrieve Ausruestung")
2024-12-30 16:23:05 +01:00
return
}
c.JSON(http.StatusOK, ausruestung)
}
func UpdateAusruestung(c *gin.Context) {
ausruestungID := c.Param("ausruestung_id")
2025-07-28 22:08:19 +02:00
var ausruestung models.EqAusruestung
2024-12-30 16:23:05 +01:00
if err := database.DB.First(&ausruestung, ausruestungID).Error; err != nil {
2025-07-24 07:39:43 +02:00
respondWithError(c, http.StatusNotFound, "Ausruestung not found")
2024-12-30 16:23:05 +01:00
return
}
2026-02-01 22:48:07 +01:00
// Check ownership
if !checkEquipmentOwnership(c, ausruestung.CharacterID) {
return
}
2024-12-30 16:23:05 +01:00
if err := c.ShouldBindJSON(&ausruestung); err != nil {
2025-07-24 07:39:43 +02:00
respondWithError(c, http.StatusBadRequest, err.Error())
2024-12-30 16:23:05 +01:00
return
}
if err := database.DB.Save(&ausruestung).Error; err != nil {
2025-07-24 07:39:43 +02:00
respondWithError(c, http.StatusInternalServerError, "Failed to update Ausruestung")
2024-12-30 16:23:05 +01:00
return
}
c.JSON(http.StatusOK, ausruestung)
}
func DeleteAusruestung(c *gin.Context) {
ausruestungID := c.Param("ausruestung_id")
2026-02-01 22:48:07 +01:00
var ausruestung models.EqAusruestung
if err := database.DB.First(&ausruestung, ausruestungID).Error; err != nil {
respondWithError(c, http.StatusNotFound, "Ausruestung not found")
return
}
// Check ownership
if !checkEquipmentOwnership(c, ausruestung.CharacterID) {
return
}
if err := database.DB.Delete(&ausruestung).Error; err != nil {
2025-07-24 07:39:43 +02:00
respondWithError(c, http.StatusInternalServerError, "Failed to delete Ausruestung")
2024-12-30 16:23:05 +01:00
return
}
c.JSON(http.StatusOK, gin.H{"message": "Ausruestung deleted successfully"})
}
2025-12-28 14:48:59 +01:00
/*
Endpoints for Managing Weapons (Waffen)
*/
func CreateWaffe(c *gin.Context) {
var waffe models.EqWaffe
if err := c.ShouldBindJSON(&waffe); err != nil {
respondWithError(c, http.StatusBadRequest, err.Error())
return
}
2026-02-01 22:48:07 +01:00
// Check ownership
if !checkEquipmentOwnership(c, waffe.CharacterID) {
return
}
2025-12-28 14:48:59 +01:00
if err := database.DB.Create(&waffe).Error; err != nil {
respondWithError(c, http.StatusInternalServerError, "Failed to create Waffe")
return
}
c.JSON(http.StatusCreated, waffe)
}
func ListWaffen(c *gin.Context) {
characterID := c.Param("character_id")
var waffen []models.EqWaffe
if err := database.DB.Where("character_id = ?", characterID).Find(&waffen).Error; err != nil {
respondWithError(c, http.StatusInternalServerError, "Failed to retrieve Waffen")
return
}
c.JSON(http.StatusOK, waffen)
}
func UpdateWaffe(c *gin.Context) {
waffeID := c.Param("waffe_id")
var waffe models.EqWaffe
if err := database.DB.First(&waffe, waffeID).Error; err != nil {
respondWithError(c, http.StatusNotFound, "Waffe not found")
return
}
2026-02-01 22:48:07 +01:00
// Check ownership
if !checkEquipmentOwnership(c, waffe.CharacterID) {
return
}
2025-12-28 14:48:59 +01:00
if err := c.ShouldBindJSON(&waffe); err != nil {
respondWithError(c, http.StatusBadRequest, err.Error())
return
}
if err := database.DB.Save(&waffe).Error; err != nil {
respondWithError(c, http.StatusInternalServerError, "Failed to update Waffe")
return
}
c.JSON(http.StatusOK, waffe)
}
func DeleteWaffe(c *gin.Context) {
waffeID := c.Param("waffe_id")
2026-02-01 22:48:07 +01:00
var waffe models.EqWaffe
if err := database.DB.First(&waffe, waffeID).Error; err != nil {
respondWithError(c, http.StatusNotFound, "Waffe not found")
return
}
// Check ownership
if !checkEquipmentOwnership(c, waffe.CharacterID) {
return
}
if err := database.DB.Delete(&waffe).Error; err != nil {
2025-12-28 14:48:59 +01:00
respondWithError(c, http.StatusInternalServerError, "Failed to delete Waffe")
return
}
c.JSON(http.StatusOK, gin.H{"message": "Waffe deleted successfully"})
}