feat: add profile route and ui
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package profile
|
||||
|
||||
import (
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormStore : Store adossé à PostgreSQL via GORM.
|
||||
type GormStore struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewGormStore(db *gorm.DB) *GormStore {
|
||||
return &GormStore{db: db}
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
UpdateUsername(id, newUsername string) (auth.User, error)
|
||||
}
|
||||
|
||||
func (s *GormStore) UpdateUsername(id, newUsername string) (auth.User, error) {
|
||||
result := s.db.Model(&auth.User{}).
|
||||
Where("id = ?", id).
|
||||
Update("username", newUsername)
|
||||
|
||||
if result.Error != nil {
|
||||
return auth.User{}, result.Error
|
||||
}
|
||||
|
||||
var user auth.User
|
||||
if err := s.db.First(&user, "id = ?", id).Error; err != nil {
|
||||
return auth.User{}, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package profile
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
)
|
||||
|
||||
type MemStore struct {
|
||||
mu sync.RWMutex
|
||||
items map[string]auth.User
|
||||
}
|
||||
|
||||
func NewMemStore() *MemStore {
|
||||
return &MemStore{items: make(map[string]auth.User)}
|
||||
}
|
||||
Reference in New Issue
Block a user