76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"satusehat-rssa/internal/integration"
|
|
"satusehat-rssa/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ConditionHandler struct {
|
|
// Define fields for ConditionHandler
|
|
Condition integration.ConditionInterface
|
|
}
|
|
|
|
func NewConditionHandler(condition integration.ConditionInterface) *ConditionHandler {
|
|
return &ConditionHandler{Condition: condition}
|
|
}
|
|
func (c *ConditionHandler) CreateCondition(ctx *gin.Context) {
|
|
var bodyParam model.ConditionRequest
|
|
if err := ctx.ShouldBindJSON(&bodyParam); err != nil {
|
|
ctx.JSON(http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
res, err := c.Condition.CreateCondition(bodyParam)
|
|
if err != nil {
|
|
if res != nil {
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *ConditionHandler) GetConditionByPatient(ctx *gin.Context) {
|
|
patientId := ctx.Query("subject")
|
|
if patientId == "" {
|
|
ctx.JSON(http.StatusBadRequest, "patientId is required")
|
|
return
|
|
}
|
|
|
|
res, err := c.Condition.GetConditionByPatient(patientId)
|
|
if err != nil {
|
|
if res != nil {
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *ConditionHandler) UpdateCondition(ctx *gin.Context) {
|
|
var bodyParam model.ConditionRequest
|
|
if err := ctx.ShouldBindJSON(&bodyParam); err != nil {
|
|
ctx.JSON(http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
bodyParam.Id = ctx.Param("id")
|
|
res, err := c.Condition.UpdateCondition(bodyParam)
|
|
if err != nil {
|
|
if res != nil {
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|