forked from fwtpe/owl-backend
/
dashboard_screen_controller.go
153 lines (143 loc) · 3.57 KB
/
dashboard_screen_controller.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package dashboardScreenOWl
import (
"fmt"
"strconv"
h "github.com/fwtpe/owl-backend/modules/f2e-api/app/helper"
d "github.com/fwtpe/owl-backend/modules/f2e-api/app/model/dashboard"
"github.com/gin-gonic/gin"
)
type APICreateScreenInput struct {
Name string `json:"name" binding:"required"`
}
func CreateScreen(c *gin.Context) {
var inputs APICreateScreenInput
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, badstatus, err)
return
}
screenName := inputs.Name
if screenName == "" {
h.JSONR(c, badstatus, "name is empty, please check it")
return
}
screen := d.DashboardScreen{
Name: screenName,
}
tx := db.Dashboard.Begin()
if dt := tx.Save(&screen); dt.Error != nil {
h.JSONR(c, expecstatus, dt.Error)
tx.Rollback()
return
}
tx.Commit()
h.JSONR(c, fmt.Sprintf("screen is created, id: %v", screen.ID))
return
}
type APIGetScreenListOuput struct {
Screen d.DashboardScreen `json:"screen"`
GraphTitles []string `json:"graph_names"`
GraphIds []int64 `json:"graph_ids"`
}
func GetScreenList(c *gin.Context) {
screens := []d.DashboardScreen{}
db.Dashboard.Model(&screens).Scan(&screens)
result := []APIGetScreenListOuput{}
for _, s := range screens {
tmp := APIGetScreenListOuput{}
tmp.Screen = s
graphs := s.Graphs()
graphTitles := []string{}
graphIds := []int64{}
for _, g := range graphs {
graphTitles = append(graphTitles, g.Title)
graphIds = append(graphIds, g.ID)
}
tmp.GraphTitles = graphTitles
tmp.GraphIds = graphIds
result = append(result, tmp)
}
h.JSONR(c, result)
return
}
type APIUpdateScreenInput struct {
ID int64 `json:"id" binding:"required"`
Name string `json:"name" binding:"required"`
}
func UpdateScreen(c *gin.Context) {
var inputs APIUpdateScreenInput
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, badstatus, err.Error())
return
}
tx := db.Dashboard.Begin()
screen := d.DashboardScreen{
ID: inputs.ID,
Name: inputs.Name,
}
if dt := tx.Model(&screen).Where("id = ?", screen.ID).Update(&screen); dt.Error != nil {
dt.Rollback()
h.JSONR(c, badstatus, dt.Error)
return
}
tx.Commit()
h.JSONR(c, fmt.Sprintf("screen: %d has been updated.", inputs.ID))
return
}
func GetScreen(c *gin.Context) {
sid := c.Params.ByName("sid")
if sid == "" {
h.JSONR(c, badstatus, "screen id is missing")
return
}
sidi, err := strconv.Atoi(sid)
if err != nil {
h.JSONR(c, badstatus, err.Error())
return
}
screen := d.DashboardScreen{}
if dt := db.Dashboard.Model(&screen).Where("id = ?", sidi).Scan(&screen); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
var output APIGetScreenListOuput
output.Screen = screen
graphs := screen.Graphs()
graphTitles := []string{}
graphIds := []int64{}
for _, g := range graphs {
graphTitles = append(graphTitles, g.Title)
graphIds = append(graphIds, g.ID)
}
output.GraphTitles = graphTitles
output.GraphIds = graphIds
h.JSONR(c, output)
return
}
func DeleteScreen(c *gin.Context) {
sid := c.Params.ByName("sid")
if sid == "" {
h.JSONR(c, badstatus, "screen id is missing")
return
}
sidi, err := strconv.Atoi(sid)
if err != nil {
h.JSONR(c, badstatus, err.Error())
return
}
screen := d.DashboardScreen{ID: int64(sidi)}
tx := db.Dashboard.Begin()
if dt := tx.Delete(&screen); dt.Error != nil {
tx.Rollback()
h.JSONR(c, badstatus, dt.Error)
return
}
graph := d.DashboardGraph{}
if dt := tx.Where("screen_id = ?", sidi).Delete(&graph); dt.Error != nil {
tx.Rollback()
h.JSONR(c, badstatus, dt.Error)
return
}
tx.Commit()
h.JSONR(c, fmt.Sprintf("screen: %d and related graph is removed.", sidi))
return
}