update frontend ui, i18n, filters, and deps
This commit is contained in:
@@ -27,6 +27,9 @@ type categorieRequest struct {
|
||||
func (h *Handler) ListCategories(c *gin.Context) {
|
||||
limit, offset, page := parsePagination(c.Query("page"), c.Query("limit"))
|
||||
query := h.client.Categorie.Query()
|
||||
if nom := c.Query("nom"); nom != "" {
|
||||
query = query.Where(categorie.NomContainsFold(nom))
|
||||
}
|
||||
total, err := query.Count(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"erreur": "impossible de compter les categories"})
|
||||
|
||||
109
backend/internal/handlers/config.go
Normal file
109
backend/internal/handlers/config.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type configPayload struct {
|
||||
Backend map[string]any `json:"backend"`
|
||||
Frontend map[string]any `json:"frontend"`
|
||||
Timezone string `json:"timezone"`
|
||||
}
|
||||
|
||||
// @Summary Lire la configuration
|
||||
// @Tags Config
|
||||
// @Produce json
|
||||
// @Success 200 {object} configPayload
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /config [get]
|
||||
func (h *Handler) GetConfig(c *gin.Context) {
|
||||
config, err := readConfig()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"erreur": "impossible de lire la configuration"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, config)
|
||||
}
|
||||
|
||||
// @Summary Mettre a jour la configuration
|
||||
// @Tags Config
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body configPayload true "Configuration"
|
||||
// @Success 200 {object} configPayload
|
||||
// @Failure 400 {object} map[string]string
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /config [put]
|
||||
func (h *Handler) UpdateConfig(c *gin.Context) {
|
||||
var payload configPayload
|
||||
if err := c.ShouldBindJSON(&payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"erreur": "donnees invalides"})
|
||||
return
|
||||
}
|
||||
if payload.Timezone == "" {
|
||||
payload.Timezone = "Europe/Paris"
|
||||
}
|
||||
|
||||
if err := writeConfig(payload); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"erreur": "impossible de sauvegarder la configuration"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, payload)
|
||||
}
|
||||
|
||||
func configPath() string {
|
||||
value := os.Getenv("CONFIG_PATH")
|
||||
if value == "" {
|
||||
return "./data/config.json"
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func defaultConfig() configPayload {
|
||||
return configPayload{
|
||||
Backend: map[string]any{},
|
||||
Frontend: map[string]any{},
|
||||
Timezone: "Europe/Paris",
|
||||
}
|
||||
}
|
||||
|
||||
func readConfig() (configPayload, error) {
|
||||
path := configPath()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
cfg := defaultConfig()
|
||||
if err := writeConfig(cfg); err != nil {
|
||||
return configPayload{}, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
return configPayload{}, err
|
||||
}
|
||||
|
||||
var cfg configPayload
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return configPayload{}, err
|
||||
}
|
||||
if cfg.Timezone == "" {
|
||||
cfg.Timezone = "Europe/Paris"
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func writeConfig(cfg configPayload) error {
|
||||
path := configPath()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0o644)
|
||||
}
|
||||
52
backend/internal/handlers/debug.go
Normal file
52
backend/internal/handlers/debug.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type debugLogsResponse struct {
|
||||
Logs string `json:"logs"`
|
||||
}
|
||||
|
||||
// @Summary Lire les logs backend
|
||||
// @Tags Debug
|
||||
// @Produce json
|
||||
// @Success 200 {object} debugLogsResponse
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /debug/logs [get]
|
||||
func (h *Handler) GetDebugLogs(c *gin.Context) {
|
||||
logs, err := readLogTail()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"erreur": "impossible de lire les logs"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, debugLogsResponse{Logs: logs})
|
||||
}
|
||||
|
||||
func debugLogPath() string {
|
||||
value := os.Getenv("DEBUG_LOG_PATH")
|
||||
if value == "" {
|
||||
return "./data/logs/backend.log"
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func readLogTail() (string, error) {
|
||||
path := debugLogPath()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
const maxBytes = 20000
|
||||
if len(data) > maxBytes {
|
||||
data = data[len(data)-maxBytes:]
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
@@ -30,6 +30,9 @@ type emplacementRequest struct {
|
||||
func (h *Handler) ListEmplacements(c *gin.Context) {
|
||||
limit, offset, page := parsePagination(c.Query("page"), c.Query("limit"))
|
||||
query := h.client.Emplacement.Query()
|
||||
if nom := c.Query("nom"); nom != "" {
|
||||
query = query.Where(emplacement.NomContainsFold(nom))
|
||||
}
|
||||
total, err := query.Count(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"erreur": "impossible de compter les emplacements"})
|
||||
|
||||
@@ -28,6 +28,9 @@ func RegisterRoutes(r *gin.Engine, client *ent.Client) {
|
||||
v1.POST("/objets/:id/liens_emplacements", h.CreateLienEmplacement)
|
||||
v1.PUT("/liens_emplacements/:id", h.UpdateLienEmplacement)
|
||||
v1.DELETE("/liens_emplacements/:id", h.DeleteLienEmplacement)
|
||||
v1.GET("/config", h.GetConfig)
|
||||
v1.PUT("/config", h.UpdateConfig)
|
||||
v1.GET("/debug/logs", h.GetDebugLogs)
|
||||
|
||||
v1.GET("/categories", h.ListCategories)
|
||||
v1.POST("/categories", h.CreateCategorie)
|
||||
|
||||
Reference in New Issue
Block a user