/
tasks.go
76 lines (70 loc) · 1.82 KB
/
tasks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package endpoint
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/infiniteloopsco/guartz/models"
"github.com/jinzhu/gorm"
)
//TaskList serves the route GET /tasks
func TaskList(c *gin.Context) {
var tasks []models.Task
models.Gdb.Find(&tasks)
c.JSON(http.StatusOK, tasks)
}
//TaskShow serves the route GET /tasks/:task_id
func TaskShow(c *gin.Context) {
var task models.Task
models.Gdb.Where("id like ?", c.Param("task_id")).First(&task)
if task.ID == "" {
c.JSON(http.StatusNotFound, "")
} else {
c.JSON(http.StatusOK, task)
}
}
//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
})
}
//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
})
}