Пример #1
0
//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
	})
}
Пример #2
0
//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
	})
}