34 lines
735 B
Go
34 lines
735 B
Go
package task
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
)
|
|
|
|
func creationHandler(ctx echo.Context, service Service) error {
|
|
var model CreationModel
|
|
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
|
}
|
|
|
|
userId := ctx.Param("userId")
|
|
|
|
entity := model.MapModelToEntity(userId)
|
|
|
|
if err := service.Create(&entity); err != nil {
|
|
return err
|
|
}
|
|
|
|
return ctx.NoContent(http.StatusCreated)
|
|
}
|
|
|
|
func AddHandlers(e *echo.Echo) {
|
|
taskService := NewService()
|
|
|
|
userTasksGroup := e.Group("/users/:userId/tasks")
|
|
|
|
userTasksGroup.POST("", func(ctx echo.Context) error {
|
|
return creationHandler(ctx, taskService)
|
|
})
|
|
}
|