263 lines
8.6 KiB
Go
263 lines
8.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gitea.d3m0k1d.ru/d3m0k1d/rostpoliplast/backend/internal/storage"
|
|
)
|
|
|
|
type LineHandlers struct {
|
|
Repo storage.RepositoryInterface
|
|
}
|
|
|
|
func (h *LineHandlers) RegisterRoutes(g *gin.RouterGroup) {
|
|
line := g.Group("/line")
|
|
{
|
|
line.GET("/stages", h.GetLineStages)
|
|
line.GET("/stages/:id", h.GetLineStageByID)
|
|
line.POST("/stages", h.CreateLineStage)
|
|
line.PUT("/stages/:id", h.UpdateLineStage)
|
|
line.DELETE("/stages/:id", h.DeleteLineStage)
|
|
}
|
|
|
|
sensors := g.Group("/sensors")
|
|
{
|
|
sensors.POST("/readings", h.CreateSensorReading)
|
|
sensors.GET("/readings/stage/:id", h.GetSensorReadingsByStage)
|
|
}
|
|
|
|
events := g.Group("/events")
|
|
{
|
|
events.POST("", h.CreateProductionEvent)
|
|
events.GET("/stage/:id", h.GetProductionEventsByStage)
|
|
}
|
|
|
|
g.GET("/line/stats", h.GetLineStats)
|
|
}
|
|
|
|
// GetLineStages Получить все этапы линии
|
|
// @Summary Получить все этапы линии
|
|
// @Description Возвращает список всех этапов производственной линии
|
|
// @Tags line
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {array} storage.LineStage
|
|
// @Router /line/stages [get]
|
|
func (h *LineHandlers) GetLineStages(c *gin.Context) {
|
|
stages, err := h.Repo.GetLineStages(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, stages)
|
|
}
|
|
|
|
// GetLineStageByID Получить этап линии по ID
|
|
// @Summary Получить этап линии по ID
|
|
// @Description Возвращает этап линии по ID
|
|
// @Tags line
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "ID этапа"
|
|
// @Success 200 {object} storage.LineStage
|
|
// @Router /line/stages/{id} [get]
|
|
func (h *LineHandlers) GetLineStageByID(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
stage, err := h.Repo.GetLineStageByID(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, stage)
|
|
}
|
|
|
|
// CreateLineStage Создать этап линии
|
|
// @Summary Создать этап линии
|
|
// @Description Создаёт новый этап производственной линии
|
|
// @Tags line
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param stage body storage.LineStage true "Данные этапа"
|
|
// @Success 201 {object} storage.LineStage
|
|
// @Router /line/stages [post]
|
|
func (h *LineHandlers) CreateLineStage(c *gin.Context) {
|
|
var input storage.LineStage
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
stage, err := h.Repo.CreateLineStage(c.Request.Context(), input)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, stage)
|
|
}
|
|
|
|
// UpdateLineStage Обновить этап линии
|
|
// @Summary Обновить этап линии
|
|
// @Description Обновляет данные этапа линии по ID
|
|
// @Tags line
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "ID этапа"
|
|
// @Param stage body storage.LineStage true "Данные этапа"
|
|
// @Success 200 {object} storage.LineStage
|
|
// @Router /line/stages/{id} [put]
|
|
func (h *LineHandlers) UpdateLineStage(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
var input storage.LineStage
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
stage, err := h.Repo.UpdateLineStage(c.Request.Context(), id, input)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, stage)
|
|
}
|
|
|
|
// DeleteLineStage Удалить этап линии
|
|
// @Summary Удалить этап линии
|
|
// @Description Удаляет этап линии по ID
|
|
// @Tags line
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "ID этапа"
|
|
// @Success 200 {object} map[string]bool
|
|
// @Router /line/stages/{id} [delete]
|
|
func (h *LineHandlers) DeleteLineStage(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
if err := h.Repo.DeleteLineStage(c.Request.Context(), id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"deleted": true})
|
|
}
|
|
|
|
// CreateSensorReading Создать показание датчика
|
|
// @Summary Создать показание датчика
|
|
// @Description Создаёт новое показание датчика для этапа линии
|
|
// @Tags sensors
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param reading body storage.SensorReading true "Данные показания"
|
|
// @Success 201 {object} storage.SensorReading
|
|
// @Router /sensors/readings [post]
|
|
func (h *LineHandlers) CreateSensorReading(c *gin.Context) {
|
|
var input storage.SensorReading
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
reading, err := h.Repo.CreateSensorReading(c.Request.Context(), input)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, reading)
|
|
}
|
|
|
|
// GetSensorReadingsByStage Получить показания датчиков по этапу
|
|
// @Summary Получить показания датчиков по этапу
|
|
// @Description Возвращает последние 100 показаний датчиков для указанного этапа
|
|
// @Tags sensors
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "ID этапа"
|
|
// @Success 200 {array} storage.SensorReading
|
|
// @Router /sensors/readings/stage/{id} [get]
|
|
func (h *LineHandlers) GetSensorReadingsByStage(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
readings, err := h.Repo.GetSensorReadingsByStage(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, readings)
|
|
}
|
|
|
|
// CreateProductionEvent Создать событие на линии
|
|
// @Summary Создать событие на линии
|
|
// @Description Создаёт новое событие на этапе производственной линии
|
|
// @Tags events
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param event body storage.ProductionEvent true "Данные события"
|
|
// @Success 201 {object} storage.ProductionEvent
|
|
// @Router /events [post]
|
|
func (h *LineHandlers) CreateProductionEvent(c *gin.Context) {
|
|
var input storage.ProductionEvent
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
event, err := h.Repo.CreateProductionEvent(c.Request.Context(), input)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, event)
|
|
}
|
|
|
|
// GetProductionEventsByStage Получить события по этапу
|
|
// @Summary Получить события по этапу
|
|
// @Description Возвращает последние 100 событий для указанного этапа
|
|
// @Tags events
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "ID этапа"
|
|
// @Success 200 {array} storage.ProductionEvent
|
|
// @Router /events/stage/{id} [get]
|
|
func (h *LineHandlers) GetProductionEventsByStage(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
events, err := h.Repo.GetProductionEventsByStage(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, events)
|
|
}
|
|
|
|
// GetLineStats Получить статистику линии
|
|
// @Summary Получить статистику линии
|
|
// @Description Возвращает сводную статистику работы производственной линии
|
|
// @Tags line
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} storage.LineStats
|
|
// @Router /line/stats [get]
|
|
func (h *LineHandlers) GetLineStats(c *gin.Context) {
|
|
stats, err := h.Repo.GetLineStats(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, stats)
|
|
}
|