//ExecutionCreate serves the route POST /tasks/:task_id/executions func ExecutionCreate(c *gin.Context) { models.InTx(func(txn *gorm.DB) bool { var task models.Task if txn.Where("id like ? ", c.Param("task_id")).First(&task); task.ID == "" { c.JSON(http.StatusNotFound, "") return false } var execution models.Execution if err := c.BindJSON(&execution); err != nil { c.JSON(http.StatusBadRequest, err) return false } execution.TaskID = task.ID if valid, errMap := models.ValidStruct(&execution); !valid { c.JSON(http.StatusConflict, errMap) return false } if txn.Create(&execution).Error != nil { c.JSON(http.StatusBadRequest, "Execution can't be saved") return false } c.JSON(http.StatusOK, execution) return true }) }
//TaskCreate serves the route POST /tasks func TaskCreate(c *gin.Context) { models.InTx(func(txn *gorm.DB) bool { var task models.Task if err := c.BindJSON(&task); err != nil { c.JSON(http.StatusBadRequest, err) return false } if valid, errMap := models.ValidStruct(&task); !valid { c.JSON(http.StatusConflict, errMap) return false } var taskExistent models.Task models.Gdb.Where("id like ?", task.ID).First(&taskExistent) var err error if task.ID != "" && taskExistent.ID != "" { taskExistent.Periodicity = task.Periodicity taskExistent.Command = task.Command err = txn.Save(&taskExistent).Error } else { err = txn.Create(&task).Error } if err != nil { c.JSON(http.StatusBadRequest, "Couldn't create the task") return false } c.JSON(http.StatusOK, task) return true }) }
func createExecution(task models.Task) models.Execution { execution := models.Execution{TaskID: task.ID} models.InTx(func(txn *gorm.DB) bool { if txn.Create(&execution).Error != nil { panic("error creating the execution") } return true }) return execution }
func createTask() models.Task { task := models.Task{ Periodicity: "@every 10s", Command: "curl -X POST --data payload={\"channel\":\"#general\",\"text\":\"EOOO\"} https://hooks.slack.com/services/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", } models.InTx(func(txn *gorm.DB) bool { if txn.Create(&task).Error != nil { panic("error creating the task") } return true }) return task }
//TaskDelete serves the route DELETE /tasks/:task_id func TaskDelete(c *gin.Context) { models.InTx(func(txn *gorm.DB) bool { var task models.Task models.Gdb.Where("id like ?", c.Param("task_id")).First(&task) if task.ID == "" { c.JSON(http.StatusNotFound, "") return false } if err := txn.Delete(&task).Error; err != nil { c.JSON(http.StatusBadRequest, "Could not delete the task") return false } c.JSON(http.StatusOK, task) return true }) }