created device config, created new UI elements in device dashboard
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -302,5 +303,147 @@ func (h *DevicesHandler) ListCertsByDevice(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "query failed"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"certs": list})
|
||||
out := make([]dto.DeviceCertDto, 0, len(list))
|
||||
for _, it := range list {
|
||||
out = append(out, dto.MapDeviceCert(it))
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.DeviceCertListDto{Certs: out})
|
||||
}
|
||||
|
||||
// GET /device/:guid/config (admin or assigned user — choose policy; here adminOnly for symmetry with certs)
|
||||
func (h *DevicesHandler) GetDeviceConfig(c *gin.Context) {
|
||||
guid := c.Param("guid")
|
||||
|
||||
// Ensure device exists
|
||||
var d models.Device
|
||||
if err := h.db.Where("guid = ?", guid).First(&d).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "device not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var cfg models.DeviceConfig
|
||||
if err := h.db.Where("device_guid = ?", guid).First(&cfg).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "config not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "query failed"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.MapDeviceConfig(cfg))
|
||||
}
|
||||
|
||||
// POST /device/:guid/config (create)
|
||||
func (h *DevicesHandler) CreateDeviceConfig(c *gin.Context) {
|
||||
guid := c.Param("guid")
|
||||
|
||||
var d models.Device
|
||||
if err := h.db.Where("guid = ?", guid).First(&d).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "device not found"})
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure not exists
|
||||
var exists int64
|
||||
_ = h.db.Model(&models.DeviceConfig{}).Where("device_guid = ?", guid).Count(&exists).Error
|
||||
if exists > 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "config already exists"})
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.CreateDeviceConfigDto
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
cfg := models.DeviceConfig{
|
||||
DeviceGUID: guid,
|
||||
MGuid: guid,
|
||||
MRecordingDuration: req.MRecordingDuration,
|
||||
MBaseURL: req.MBaseURL,
|
||||
MPolling: req.MPolling,
|
||||
MJitter: req.MJitter,
|
||||
}
|
||||
if err := h.db.Create(&cfg).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "create failed"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, dto.MapDeviceConfig(cfg))
|
||||
}
|
||||
|
||||
// PUT /device/:guid/config (partial update)
|
||||
func (h *DevicesHandler) UpdateDeviceConfig(c *gin.Context) {
|
||||
guid := c.Param("guid")
|
||||
|
||||
var req dto.UpdateDeviceConfigDto
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var cfg models.DeviceConfig
|
||||
err := h.db.Where("device_guid = ?", guid).First(&cfg).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// Create-on-update behavior
|
||||
// m_baseUrl is required to create (NOT NULL constraint in model)
|
||||
if req.MBaseURL == nil || strings.TrimSpace(*req.MBaseURL) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "m_baseUrl is required to create config"})
|
||||
return
|
||||
}
|
||||
|
||||
// Defaults
|
||||
recDur := 240
|
||||
if req.MRecordingDuration != nil {
|
||||
recDur = *req.MRecordingDuration
|
||||
}
|
||||
poll := 30
|
||||
if req.MPolling != nil {
|
||||
poll = *req.MPolling
|
||||
}
|
||||
jitter := 10
|
||||
if req.MJitter != nil {
|
||||
jitter = *req.MJitter
|
||||
}
|
||||
|
||||
cfg = models.DeviceConfig{
|
||||
DeviceGUID: guid,
|
||||
MGuid: guid,
|
||||
MRecordingDuration: recDur,
|
||||
MBaseURL: strings.TrimSpace(*req.MBaseURL),
|
||||
MPolling: poll,
|
||||
MJitter: jitter,
|
||||
}
|
||||
if err := h.db.Create(&cfg).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "create failed"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, dto.MapDeviceConfig(cfg))
|
||||
return
|
||||
}
|
||||
// Other DB error
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "query failed"})
|
||||
return
|
||||
}
|
||||
|
||||
// Patch only provided fields
|
||||
if req.MRecordingDuration != nil {
|
||||
cfg.MRecordingDuration = *req.MRecordingDuration
|
||||
}
|
||||
if req.MBaseURL != nil {
|
||||
cfg.MBaseURL = strings.TrimSpace(*req.MBaseURL)
|
||||
}
|
||||
if req.MPolling != nil {
|
||||
cfg.MPolling = *req.MPolling
|
||||
}
|
||||
if req.MJitter != nil {
|
||||
cfg.MJitter = *req.MJitter
|
||||
}
|
||||
|
||||
if err := h.db.Save(&cfg).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "save failed"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.MapDeviceConfig(cfg))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user