cleaning_template_controller.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package controllers
  2. import (
  3. "ems-backend/models"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. )
  7. // GetCleaningTemplates lists all cleaning templates
  8. func GetCleaningTemplates(c *gin.Context) {
  9. var templates []models.EquipmentCleaningFormulaTemplate
  10. if err := models.DB.Order("created_at desc").Find(&templates).Error; err != nil {
  11. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  12. return
  13. }
  14. c.JSON(http.StatusOK, gin.H{"data": templates})
  15. }
  16. // CreateCleaningTemplate creates a new cleaning template
  17. func CreateCleaningTemplate(c *gin.Context) {
  18. var template models.EquipmentCleaningFormulaTemplate
  19. if err := c.ShouldBindJSON(&template); err != nil {
  20. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  21. return
  22. }
  23. if err := models.DB.Create(&template).Error; err != nil {
  24. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  25. return
  26. }
  27. c.JSON(http.StatusOK, gin.H{"data": template})
  28. }
  29. // UpdateCleaningTemplate updates an existing cleaning template
  30. func UpdateCleaningTemplate(c *gin.Context) {
  31. id := c.Param("id")
  32. var template models.EquipmentCleaningFormulaTemplate
  33. if err := models.DB.First(&template, "id = ?", id).Error; err != nil {
  34. c.JSON(http.StatusNotFound, gin.H{"error": "Template not found"})
  35. return
  36. }
  37. if err := c.ShouldBindJSON(&template); err != nil {
  38. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  39. return
  40. }
  41. if err := models.DB.Save(&template).Error; err != nil {
  42. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  43. return
  44. }
  45. c.JSON(http.StatusOK, gin.H{"data": template})
  46. }
  47. // DeleteCleaningTemplate deletes a cleaning template
  48. func DeleteCleaningTemplate(c *gin.Context) {
  49. id := c.Param("id")
  50. if err := models.DB.Delete(&models.EquipmentCleaningFormulaTemplate{}, "id = ?", id).Error; err != nil {
  51. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  52. return
  53. }
  54. c.JSON(http.StatusOK, gin.H{"message": "Template deleted"})
  55. }