82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"satusehat-rssa/internal/integration"
|
|
"satusehat-rssa/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type EncounterHandler struct {
|
|
// Define any dependencies needed for the handler
|
|
Encounter integration.EncounterInterface
|
|
}
|
|
|
|
func NewEncounterHandler(encounter integration.EncounterInterface) *EncounterHandler {
|
|
return &EncounterHandler{Encounter: encounter}
|
|
}
|
|
|
|
// Add methods for handling encounter-related requests here
|
|
func (e *EncounterHandler) CreateEncounter(c *gin.Context) {
|
|
var req model.EncounterRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
res, err := e.Encounter.CreateEncounter(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 (e *EncounterHandler) GetEncounterByPatient(c *gin.Context) {
|
|
id := c.Query("subject")
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Subject is required"})
|
|
return
|
|
}
|
|
|
|
encounter, err := e.Encounter.GetEncounterByPatient(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if encounter == nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"message": "Encounter not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, encounter)
|
|
}
|
|
|
|
func (e *EncounterHandler) UpdateEncounter(c *gin.Context) {
|
|
var req model.EncounterUpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// set id for param
|
|
id := c.Param("id")
|
|
req.ID = id
|
|
|
|
res, err := e.Encounter.UpdateEncounter(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)
|
|
}
|