package handler import ( "net/http" "satusehat-rssa/internal/integration" "satusehat-rssa/internal/model" "satusehat-rssa/pkg/common" "github.com/gin-gonic/gin" ) type ServiceRequestHandler struct { repo integration.ServiceRequestInterface } func NewServiceRequestHandler(repo integration.ServiceRequestInterface) *ServiceRequestHandler { return &ServiceRequestHandler{repo: repo} } func (h *ServiceRequestHandler) CreateServiceRequest(c *gin.Context) { var req model.ServiceRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } req.Identifier = append(req.Identifier, common.GetIdentifier("servicerequest")) data, err := h.repo.CreateServiceRequest(req) if err != nil { if data != nil { c.JSON(http.StatusInternalServerError, data) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, data) } func (h *ServiceRequestHandler) GetServiceRequestByPatient(c *gin.Context) { id := c.Query("subject") if id == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "Patient ID is required"}) return } data, err := h.repo.GetServiceRequestByPatient(id) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, data) } func (h *ServiceRequestHandler) UpdateServiceRequest(c *gin.Context) { var req model.ServiceRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } req.Id = c.Param("id") req.Identifier = append(req.Identifier, common.GetIdentifier("servicerequest")) data, err := h.repo.UpdateServiceRequest(req) if err != nil { if data != nil { c.JSON(http.StatusInternalServerError, data) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, data) }