59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"satusehat-rssa/internal/integration"
|
|
"satusehat-rssa/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type MedicationDispenseHandler struct {
|
|
MedicationDispense integration.MedicationDispenseInterface
|
|
}
|
|
|
|
func NewMedicationDispenseHandler(MedicationDispense integration.MedicationDispenseInterface) *MedicationDispenseHandler {
|
|
return &MedicationDispenseHandler{MedicationDispense: MedicationDispense}
|
|
}
|
|
|
|
func (h MedicationDispenseHandler) CreateMedicationDispense(c *gin.Context) {
|
|
var req model.MedicationDispenseRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
res, err := h.MedicationDispense.CreateMedicationDispense(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 (h MedicationDispenseHandler) UpdateMedicationDispense(c *gin.Context) {
|
|
var req model.MedicationDispenseRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
req.Id = c.Param("id")
|
|
res, err := h.MedicationDispense.CreateMedicationDispense(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)
|
|
}
|