119 lines
2.9 KiB
Go
119 lines
2.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"gestion/db"
|
|
"gestion/utils"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func SubmitLivreurRating(c *gin.Context) {
|
|
clientUsername := c.GetString("username")
|
|
if clientUsername == "" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Non authentifié"})
|
|
return
|
|
}
|
|
|
|
orderID, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil || orderID <= 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID commande invalide"})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Rating int `json:"rating" binding:"required,min=1,max=5"`
|
|
Comment string `json:"comment"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Note invalide (1 à 5 requis)"})
|
|
return
|
|
}
|
|
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
ownerUsername, livreurUsername, err := database.GetOrderForRating(orderID)
|
|
if err != nil {
|
|
utils.ServerErr(c, "Erreur lecture commande", err)
|
|
return
|
|
}
|
|
if ownerUsername == "" {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Commande introuvable ou non terminée"})
|
|
return
|
|
}
|
|
if ownerUsername != clientUsername {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Commande non autorisée"})
|
|
return
|
|
}
|
|
if livreurUsername == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Aucun livreur assigné à cette commande"})
|
|
return
|
|
}
|
|
|
|
existing, err := database.GetOrderRating(orderID)
|
|
if err != nil {
|
|
utils.ServerErr(c, "Erreur vérification avis", err)
|
|
return
|
|
}
|
|
if existing != nil {
|
|
c.JSON(http.StatusConflict, gin.H{"error": "Vous avez déjà noté ce livreur pour cette commande"})
|
|
return
|
|
}
|
|
|
|
if err := database.SubmitLivreurRating(orderID, livreurUsername, clientUsername, req.Rating, req.Comment); err != nil {
|
|
utils.ServerErr(c, "Erreur enregistrement avis", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
func GetLivreurRatings(c *gin.Context) {
|
|
username := c.Param("username")
|
|
if username == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Username requis"})
|
|
return
|
|
}
|
|
|
|
database := c.MustGet("database").(*db.Database)
|
|
ratings, avg, err := database.GetLivreurRatings(username)
|
|
if err != nil {
|
|
utils.ServerErr(c, "Erreur récupération avis", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"ratings": ratings,
|
|
"average": avg,
|
|
"count": len(ratings),
|
|
})
|
|
}
|
|
|
|
func GetOrderRatingStatus(c *gin.Context) {
|
|
clientUsername := c.GetString("username")
|
|
if clientUsername == "" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Non authentifié"})
|
|
return
|
|
}
|
|
|
|
orderID, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil || orderID <= 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalide"})
|
|
return
|
|
}
|
|
|
|
database := c.MustGet("database").(*db.Database)
|
|
rating, err := database.GetOrderRating(orderID)
|
|
if err != nil {
|
|
utils.ServerErr(c, "Erreur", err)
|
|
return
|
|
}
|
|
|
|
if rating == nil {
|
|
c.JSON(http.StatusOK, gin.H{"rated": false})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"rated": true, "rating": rating.Rating, "comment": rating.Comment})
|
|
}
|