forked from yvesago/gin-model-template
/
agent.go
192 lines (154 loc) · 4.51 KB
/
agent.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package models
import (
"fmt"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/go-gorp/gorp"
)
/**
Search for XXX to fix fields mapping in Update handler, mandatory fields
or remove sqlite tricks
vim search and replace cmd to customize struct, handler and instances
:%s/Agent/NewStruct/g
:%s/agent/newinst/g
**/
// XXX custom struct name and fields
// Agent db and json type
type Agent struct {
Id int64 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
IP string `db:"ip" json:"ip"`
FileSurvey string `db:"filesurvey" json:"filesurvey"`
Role string `db:"role" json:"role"`
Status string `db:"status" json:"status"`
Created time.Time `db:"created" json:"created"` // or int64
Updated time.Time `db:"updated" json:"updated"`
}
// Hooks : PreInsert and PreUpdate
// PreInsert set created an updated time before insert in db
func (a *Agent) PreInsert(s gorp.SqlExecutor) error {
a.Created = time.Now() // or time.Now().UnixNano()
a.Updated = a.Created
return nil
}
// PreUpdate set updated time before insert in db
func (a *Agent) PreUpdate(s gorp.SqlExecutor) error {
a.Updated = time.Now()
return nil
}
// REST handlers
// GetAgents return all agents filtered by URL query
func GetAgents(c *gin.Context) {
dbmap := c.MustGet("DBmap").(*gorp.DbMap)
verbose := c.MustGet("Verbose").(bool)
query := "SELECT * FROM agent"
// Parse query string
q := c.Request.URL.Query()
query = query + ParseQuery(q)
if verbose == true {
fmt.Println(q)
fmt.Println("query: " + query)
}
var agents []Agent
_, err := dbmap.Select(&agents, query)
if err == nil {
c.Header("X-Total-Count", strconv.Itoa(len(agents)))
c.JSON(200, agents)
} else {
c.JSON(404, gin.H{"error": "no agent(s) into the table"})
}
// curl -i http://localhost:8080/api/v1/agents
}
// GetAgent return one agent by id
func GetAgent(c *gin.Context) {
dbmap := c.MustGet("DBmap").(*gorp.DbMap)
id := c.Params.ByName("id")
var agent Agent
err := dbmap.SelectOne(&agent, "SELECT * FROM agent WHERE id=? LIMIT 1", id)
if err == nil {
c.JSON(200, agent)
} else {
c.JSON(404, gin.H{"error": "agent not found"})
}
// curl -i http://localhost:8080/api/v1/agents/1
}
// PostAgent create and return agent
func PostAgent(c *gin.Context) {
dbmap := c.MustGet("DBmap").(*gorp.DbMap)
verbose := c.MustGet("Verbose").(bool)
var agent Agent
c.Bind(&agent)
if verbose == true {
fmt.Println(agent)
}
if agent.Name != "" && agent.IP != "" { // XXX Check mandatory fields
err := dbmap.Insert(&agent)
if err == nil {
c.JSON(201, agent)
} else {
checkErr(err, "Insert failed")
}
} else {
c.JSON(400, gin.H{"error": "Mandatory fields are empty"})
}
// curl -i -X POST -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Queen\" }" http://localhost:8080/api/v1/agents
}
// UpdateAgent by id
func UpdateAgent(c *gin.Context) {
dbmap := c.MustGet("DBmap").(*gorp.DbMap)
verbose := c.MustGet("Verbose").(bool)
id := c.Params.ByName("id")
var agent Agent
err := dbmap.SelectOne(&agent, "SELECT * FROM agent WHERE id=?", id)
if err == nil {
var json Agent
c.Bind(&json)
if verbose == true {
fmt.Println(json)
}
agentId, _ := strconv.ParseInt(id, 0, 64)
//TODO : find fields via reflections
//XXX custom fields mapping
agent := Agent{
Id: agentId,
IP: json.IP,
Name: json.Name,
Role: json.Role,
FileSurvey: json.FileSurvey,
Status: json.Status,
Created: agent.Created, //agent read from previous select
}
if agent.Name != "" && agent.IP != "" { // XXX Check mandatory fields
_, err = dbmap.Update(&agent)
if err == nil {
c.JSON(200, agent)
} else {
checkErr(err, "Updated failed")
}
} else {
c.JSON(400, gin.H{"error": "mandatory fields are empty"})
}
} else {
c.JSON(404, gin.H{"error": "agent not found"})
}
// curl -i -X PUT -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Merlyn\" }" http://localhost:8080/api/v1/agents/1
}
// DeleteAgent by id
func DeleteAgent(c *gin.Context) {
dbmap := c.MustGet("DBmap").(*gorp.DbMap)
id := c.Params.ByName("id")
var agent Agent
err := dbmap.SelectOne(&agent, "SELECT * FROM agent WHERE id=?", id)
if err == nil {
_, err = dbmap.Delete(&agent)
if err == nil {
c.JSON(200, gin.H{"id #" + id: "deleted"})
} else {
checkErr(err, "Delete failed")
}
} else {
c.JSON(404, gin.H{"error": "agent not found"})
}
// curl -i -X DELETE http://localhost:8080/api/v1/agents/1
}