@@ -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})
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package sav
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
)
|
||||
|
||||
type MemContactStore struct {
|
||||
mu sync.RWMutex
|
||||
items map[string]Contact
|
||||
}
|
||||
|
||||
func NewMemContactStore() *MemContactStore {
|
||||
return &MemContactStore{
|
||||
items: make(map[string]Contact),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MemContactStore) ContactSupportByUser(username, telegram, sujet, message string) (Contact, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
contact := Contact{
|
||||
ID: uuid.NewString(),
|
||||
Username: auth.NormalizeUsername(username),
|
||||
Telegram: auth.NormalizeUsername(telegram),
|
||||
Sujet: sujet,
|
||||
Message: message,
|
||||
}
|
||||
|
||||
m.items[contact.ID] = contact
|
||||
return contact, nil
|
||||
}
|
||||
|
||||
func (m *MemContactStore) GetMessage() ([]Contact, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
contacts := make([]Contact, 0, len(m.items))
|
||||
for _, c := range m.items {
|
||||
contacts = append(contacts, c)
|
||||
}
|
||||
sort.Slice(contacts, func(i, j int) bool {
|
||||
return contacts[i].CreatedAt.After(contacts[j].CreatedAt)
|
||||
})
|
||||
return contacts, nil
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package sav
|
||||
|
||||
import "time"
|
||||
|
||||
type Contact struct {
|
||||
ID string `gorm:"type:uuid;primaryKey" json:"id"`
|
||||
Username string `gorm:"size:64;not null" json:"username"`
|
||||
Telegram string `gorm:"size:64;not null" json:"telegram"`
|
||||
Sujet string `gorm:"size:64;not null" json:"sujet"`
|
||||
Message string `gorm:"size:64;not null" json:"message"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package sav
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
ContactSupportByUser(username, telegram, sujet, message string) (Contact, error)
|
||||
GetMessage() ([]Contact, error)
|
||||
}
|
||||
|
||||
type GormStore struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewGormStore(db *gorm.DB) *GormStore {
|
||||
return &GormStore{db: db}
|
||||
}
|
||||
|
||||
func (s *GormStore) ContactSupportByUser(username, telegram, sujet, message string) (Contact, error) {
|
||||
contact := Contact{
|
||||
ID: uuid.NewString(),
|
||||
Username: auth.NormalizeUsername(username),
|
||||
Telegram: auth.NormalizeUsername(telegram),
|
||||
Sujet: sujet,
|
||||
Message: message,
|
||||
}
|
||||
|
||||
if err := s.db.Create(&contact).Error; err != nil {
|
||||
return Contact{}, err
|
||||
}
|
||||
return contact, nil
|
||||
}
|
||||
|
||||
func (s *GormStore) GetMessage() ([]Contact, error) {
|
||||
var contacts []Contact
|
||||
if err := s.db.Find(&contacts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return contacts, nil
|
||||
}
|
||||
Reference in New Issue
Block a user