120 lines
3.4 KiB
Go
120 lines
3.4 KiB
Go
package projects
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/omnex/control-plane/api/internal/auth"
|
|
)
|
|
|
|
// RequestHandler : routes des demandes d'achat de site vitrine. Les routes
|
|
// client agissent toujours sur le compte authentifié, jamais sur un
|
|
// identifiant fourni par la requête.
|
|
type RequestHandler struct {
|
|
svc *RequestService
|
|
h *Handler // traduction des erreurs en codes HTTP
|
|
}
|
|
|
|
func NewRequestHandler(svc *RequestService, h *Handler) *RequestHandler {
|
|
return &RequestHandler{svc: svc, h: h}
|
|
}
|
|
|
|
type requestBody struct {
|
|
SiteName string `json:"site_name" binding:"required,min=1,max=64"`
|
|
Months int `json:"months" binding:"required,min=1,max=12"`
|
|
Message string `json:"message" binding:"omitempty,max=500"`
|
|
}
|
|
|
|
type approveBody struct {
|
|
// Months : durée validée ; 0 ou absent = celle demandée par le client.
|
|
Months int `json:"months" binding:"omitempty,min=1,max=12"`
|
|
AdminUsername string `json:"admin_username" binding:"required,min=3,max=64,alphanum"`
|
|
AdminPassword string `json:"admin_password" binding:"required,min=12"`
|
|
AdminNumber int `json:"admin_number" binding:"omitempty,min=1,max=20"`
|
|
}
|
|
|
|
type rejectBody struct {
|
|
Note string `json:"note" binding:"omitempty,max=500"`
|
|
}
|
|
|
|
// Create : POST /project-requests — le client demande un site (201).
|
|
func (r *RequestHandler) Create(c *gin.Context) {
|
|
p := auth.PrincipalFrom(c)
|
|
if p == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
|
return
|
|
}
|
|
var body requestBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
req, err := r.svc.Create(p.Username, RequestInput{SiteName: body.SiteName, Months: body.Months, Message: body.Message})
|
|
if err != nil {
|
|
r.h.writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, req)
|
|
}
|
|
|
|
// ListMine : GET /project-requests/mine — demandes du client authentifié.
|
|
func (r *RequestHandler) ListMine(c *gin.Context) {
|
|
p := auth.PrincipalFrom(c)
|
|
if p == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
|
return
|
|
}
|
|
items, err := r.svc.ListMine(p.Username)
|
|
if err != nil {
|
|
r.h.writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"items": items})
|
|
}
|
|
|
|
// List : GET /project-requests — toutes les demandes (admin).
|
|
func (r *RequestHandler) List(c *gin.Context) {
|
|
items, err := r.svc.List()
|
|
if err != nil {
|
|
r.h.writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"items": items})
|
|
}
|
|
|
|
// Approve : POST /project-requests/:id/approve — déploie le site (202).
|
|
func (r *RequestHandler) Approve(c *gin.Context) {
|
|
var body approveBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
req, project, err := r.svc.Approve(c.Param("id"), ApproveInput{
|
|
Months: body.Months,
|
|
AdminUsername: body.AdminUsername,
|
|
AdminPassword: body.AdminPassword,
|
|
AdminNumber: body.AdminNumber,
|
|
})
|
|
if err != nil {
|
|
r.h.writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusAccepted, gin.H{"request": req, "project": project})
|
|
}
|
|
|
|
// Reject : POST /project-requests/:id/reject {"note": "..."}.
|
|
func (r *RequestHandler) Reject(c *gin.Context) {
|
|
var body rejectBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
req, err := r.svc.Reject(c.Param("id"), body.Note)
|
|
if err != nil {
|
|
r.h.writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, req)
|
|
}
|