59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"satusehat-rssa/internal/integration"
|
|
"satusehat-rssa/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type GoalHandler struct {
|
|
Goal integration.GoalInterface
|
|
}
|
|
|
|
func NewGoalHandler(Goal integration.GoalInterface) *GoalHandler {
|
|
return &GoalHandler{Goal: Goal}
|
|
}
|
|
|
|
func (h GoalHandler) CreateGoal(c *gin.Context) {
|
|
var req model.GoalRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
res, err := h.Goal.CreateGoal(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 GoalHandler) UpdateGoal(c *gin.Context) {
|
|
var req model.GoalRequest
|
|
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.Goal.CreateGoal(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)
|
|
}
|