package task import ( "net/http" "strconv" "git.tek.govt.hu/dowerx/szoe-pontok/database/task" "git.tek.govt.hu/dowerx/szoe-pontok/model" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/validator/v10" ) func Add(c *gin.Context) { var tsk model.Task if c.MustBindWith(&tsk, binding.Form) != nil { return } issuer, ok := c.Get("neptun") if !ok { c.JSON(http.StatusBadRequest, gin.H{ "status": http.StatusBadRequest, "error": "not logged in", }) return } tsk.Issuer = issuer.(string) val := validator.New(validator.WithRequiredStructEnabled()) if err := val.Struct(tsk); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "status": http.StatusBadRequest, "error": err.Error(), }) return } if err := task.Add(tsk); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "status": http.StatusBadRequest, "error": err.Error(), }) return } else { c.JSON(http.StatusOK, gin.H{ "status": http.StatusOK, "message": "added", }) } } func Remove(c *gin.Context) { id := c.Param("id") idInt, err := strconv.Atoi(id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{ "status": http.StatusBadRequest, "error": err.Error(), }) return } if err := task.Remove(idInt); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "status": http.StatusBadRequest, "error": err.Error(), }) } else { c.JSON(http.StatusOK, gin.H{ "status": http.StatusOK, "message": "removed", }) } } func List(c *gin.Context) { recipient, ok := c.Get("neptun") if !ok { c.JSON(http.StatusBadRequest, gin.H{ "status": http.StatusBadRequest, "error": "not logged in", }) return } tasks, err := task.List(recipient.(string)) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, "error": err.Error(), }) } else { c.JSON(http.StatusOK, gin.H{ "status": http.StatusOK, "tasks": tasks, }) } } func ListUser(c *gin.Context) { recipient := c.Param("neptun") tasks, err := task.List(recipient) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, "error": err.Error(), }) } else { c.JSON(http.StatusOK, gin.H{ "status": http.StatusOK, "tasks": tasks, }) } }