-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_views.go
93 lines (78 loc) · 1.95 KB
/
api_views.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package neocortex
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (api *API) registerViewsAPI(r *gin.RouterGroup) {
r.POST("/view", func(c *gin.Context) {
view := new(View)
if err := c.BindJSON(view); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if err := api.repository.SaveView(view); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data": view})
})
r.PUT("/view/:id", func(c *gin.Context) {
view := new(View)
if err := c.BindJSON(view); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
err := api.repository.UpdateView(view)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": view,
})
})
r.DELETE("/view/:id", func(c *gin.Context) {
id := c.Param("id")
view, err := api.repository.DeleteView(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": view,
})
})
r.GET("/view/:id", func(c *gin.Context) {
id := c.Param("id")
view, err := api.repository.GetViewByID(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": view,
})
})
r.GET("/views/*name", func(c *gin.Context) {
name := c.Param("name")
if name == "" || name == "/" {
views, err := api.repository.AllViews()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": views,
})
return
}
views, err := api.repository.FindViewByName(name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": views,
})
})
}