Files
satusehat-bridging/internal/handler/specimen_handler.go
2025-11-24 09:13:08 +07:00

78 lines
1.9 KiB
Go

package handler
import (
"net/http"
"satusehat-rssa/internal/integration"
"satusehat-rssa/internal/model"
"satusehat-rssa/pkg/common"
"github.com/gin-gonic/gin"
)
type SpecimenHandler struct {
Specimen integration.SpecimenInterface
}
func NewSpecimenHandler(specimen integration.SpecimenInterface) *SpecimenHandler {
return &SpecimenHandler{Specimen: specimen}
}
func (s *SpecimenHandler) CreateSpecimen(c *gin.Context) {
var req model.SpecimenRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
req.Identifier = append(req.Identifier, common.GetIdentifier("specimen"))
res, err := s.Specimen.CreateSpecimen(req)
if err != nil {
if res != nil {
c.JSON(http.StatusInternalServerError, res)
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, res)
}
func (s *SpecimenHandler) GetSpecimenByPatient(c *gin.Context) {
id := c.Query("subject")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Subject is required"})
return
}
specimen, err := s.Specimen.GetSpecimenByPatient(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if specimen == nil {
c.JSON(http.StatusNotFound, gin.H{"message": "Specimen not found"})
return
}
c.JSON(http.StatusOK, specimen)
}
func (s *SpecimenHandler) UpdateSpecimen(c *gin.Context) {
var req model.SpecimenRequest
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("specimen"))
res, err := s.Specimen.UpdateSpecimen(req)
if err != nil {
if res != nil {
c.JSON(http.StatusInternalServerError, res)
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, res)
}