72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"satusehat-rssa/internal/integration"
|
|
"satusehat-rssa/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ProcedureHandler struct {
|
|
Procedure integration.ProcedureInterface
|
|
}
|
|
|
|
func NewProcedureHandler(procedure integration.ProcedureInterface) *ProcedureHandler {
|
|
return &ProcedureHandler{
|
|
Procedure: procedure,
|
|
}
|
|
}
|
|
|
|
func (p *ProcedureHandler) CreateProcedure(c *gin.Context) {
|
|
var req model.ProcedureRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := p.Procedure.CreateProcedure(req)
|
|
if err != nil {
|
|
if result != nil {
|
|
c.JSON(http.StatusInternalServerError, result)
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
func (p *ProcedureHandler) GetProcedure(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
result, err := p.Procedure.GetProcedure(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
func (p *ProcedureHandler) UpdateProcedure(c *gin.Context) {
|
|
var req model.ProcedureRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := p.Procedure.UpdateProcedure(req)
|
|
if err != nil {
|
|
if result != nil {
|
|
c.JSON(http.StatusInternalServerError, result)
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|