@@ -0,0 +1,60 @@
|
||||
package sav
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
store Store
|
||||
}
|
||||
|
||||
// NewHandler crée un nouveau Handler.
|
||||
func NewHandler(store Store) *Handler {
|
||||
return &Handler{store: store}
|
||||
}
|
||||
|
||||
type contactSupport struct {
|
||||
Username string `json:"username"`
|
||||
Telegram string `json:"telegram"`
|
||||
Sujet string `json:"sujet"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (h *Handler) CallSupport(c *gin.Context) {
|
||||
var req contactSupport
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
|
||||
contact, err := h.store.ContactSupportByUser(req.Username, req.Telegram, req.Sujet, req.Message)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "erreur serveur"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"success": "message envoyé", "contact": contact})
|
||||
}
|
||||
|
||||
func (h *Handler) GetMessage(c *gin.Context) {
|
||||
p := auth.PrincipalFrom(c)
|
||||
if p == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
||||
return
|
||||
}
|
||||
if p.Role != "admin" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "role non correcte"})
|
||||
return
|
||||
}
|
||||
|
||||
getMessage, err := h.store.GetMessage()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "erreur serveur"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"messages": getMessage})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user