43 lines
786 B
Go
43 lines
786 B
Go
package profile
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
store Store
|
|
}
|
|
|
|
func NewHandler(store Store) *Handler {
|
|
return &Handler{store: store}
|
|
}
|
|
|
|
type newUsername struct {
|
|
Username string `json:"username" binding:"required,min=2,max=120"`
|
|
}
|
|
|
|
func (h *Handler) UpdateUsernameById(c *gin.Context) {
|
|
var u newUsername
|
|
|
|
if err := c.ShouldBindJSON(&u); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
|
return
|
|
}
|
|
|
|
id, ok := c.MustGet("id").(string)
|
|
if !ok {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "id invalide"})
|
|
return
|
|
}
|
|
|
|
user, err := h.store.UpdateUsername(id, u.Username)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "erreur serveur"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, user)
|
|
}
|