chore: update
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"gestion/db"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetMyReferralBalance — GET /api/v1/referral/balance (client)
|
||||
func GetMyReferralBalance(c *gin.Context) {
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
|
||||
username, exists := c.Get("username")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Utilisateur non authentifié"})
|
||||
return
|
||||
}
|
||||
|
||||
settings, _ := database.GetSettings()
|
||||
if !settings.ReferralEnabled {
|
||||
c.JSON(http.StatusOK, gin.H{"balance": 0, "referral_enabled": false})
|
||||
return
|
||||
}
|
||||
|
||||
balance, err := database.GetClientReferralBalance(username.(string))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"balance": balance, "referral_enabled": true})
|
||||
}
|
||||
|
||||
// CreditClientReferralAdmin — POST /api/v2/admin/protected/client/:username/referral/credit (admin)
|
||||
func CreditClientReferralAdmin(c *gin.Context) {
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
targetUsername := c.Param("username")
|
||||
|
||||
var req struct {
|
||||
Amount float64 `json:"amount" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.Amount <= 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Montant invalide"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := database.CreditClientReferral(targetUsername, req.Amount); err != nil {
|
||||
log.Printf("❌ [REFERRAL] Crédit échoué pour %s: %v", targetUsername, err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
balance, _ := database.GetClientReferralBalance(targetUsername)
|
||||
log.Printf("✅ [REFERRAL] +%.2f€ crédité à %s, nouveau solde: %.2f€", req.Amount, targetUsername, balance)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Solde parrainage crédité",
|
||||
"balance": balance,
|
||||
})
|
||||
}
|
||||
|
||||
// GetClientReferralAdmin — GET /api/v2/admin/protected/client/:username/referral (admin)
|
||||
func GetClientReferralAdmin(c *gin.Context) {
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
targetUsername := c.Param("username")
|
||||
|
||||
balance, err := database.GetClientReferralBalance(targetUsername)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"username": targetUsername,
|
||||
"balance": balance,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user