diff --git a/internal/handler/handlers.go b/internal/handler/handlers.go index ed7d61f..5ca7fd1 100644 --- a/internal/handler/handlers.go +++ b/internal/handler/handlers.go @@ -33,7 +33,7 @@ func (h *Handler) Health(c *gin.Context) { // Hello returns a simple greeting func (h *Handler) Hello(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"message": "hello from lambda-iot core"}) + c.JSON(http.StatusOK, gin.H{"message": "hello from lambdaiot"}) } // Login issues a JWT token for demo purposes @@ -64,6 +64,35 @@ func (h *Handler) Login(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"token": signed}) } +func (h *Handler) CreateSensor(c *gin.Context) { + var req struct { + DeviceID string `json:"device_id" binding:"required,uuid"` + Name string `json:"name" binding:"required"` + Type string `json:"type" binding:"required"` + Data_Type string `json:"data_type" binding:"required"` + } + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + deviceUUID, err := uuid.Parse(req.DeviceID) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid device id"}) + return + } + + // Create sensor in database + sensorID := uuid.New() + _, err = h.DB.Exec("INSERT INTO sensors (id, device_id, name, type, data_type) VALUES (UUID_TO_BIN(?), UUID_TO_BIN(?), ?, ?, ?)", sensorID.String(), deviceUUID.String(), req.Name, req.Type, req.Data_Type) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create sensor"}) + return + } + + c.JSON(http.StatusCreated, gin.H{"id": sensorID.String()}) +} + // CreateDevice creates a new device func (h *Handler) CreateDevice(c *gin.Context) { var req struct {