Skip to content
This repository was archived by the owner on Sep 17, 2019. It is now read-only.

Commit b31fc20

Browse files
committed
All REST done
1 parent 1094c1c commit b31fc20

24 files changed

Lines changed: 2480 additions & 65 deletions

‎conf/app.conf‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ enablexsrf = false
1313
xsrfkey = 61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o
1414
xsrfexpire = 3600
1515

16-
pidfile = "logs/moduleab_server.pid"
16+
pidFile = "logs/moduleab_server.pid"
1717

1818
[database]
1919
mysqluser = "ModulesAB"

‎controllers/backup_sets.go‎

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ func (a *BackupSetsController) Put() {
167167
if name != "" {
168168
backupSet := &models.BackupSets{
169169
Name: name,
170-
Oas: new(models.Oas),
171-
Oss: new(models.Oss),
170+
Oas: new(models.Oas),
171+
Oss: new(models.Oss),
172172
}
173173
backupSets, err := models.GetBackupSets(backupSet, 0, 0)
174174
if err != nil {
@@ -218,3 +218,119 @@ func (a *BackupSetsController) Put() {
218218
return
219219
}
220220
}
221+
222+
/*************************************/
223+
224+
// @router /:name/hosts [post]
225+
func (h *BackupSetsController) AddBackupSetsHosts() {
226+
name := h.GetString(":name")
227+
beego.Debug("[C] Got name:", name)
228+
if name != "" {
229+
backupSet := &models.BackupSets{
230+
Name: name,
231+
}
232+
backupSets, err := models.GetBackupSets(backupSet, 0, 0)
233+
if err != nil {
234+
h.Data["json"] = map[string]string{
235+
"message": fmt.Sprint("Failed to get with name:", name),
236+
"error": err.Error(),
237+
}
238+
beego.Warn("[C] Got error:", err)
239+
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
240+
h.ServeJSON()
241+
return
242+
}
243+
if len(backupSets) == 0 {
244+
beego.Debug("[C] Got nothing with name:", name)
245+
h.Ctx.Output.SetStatus(http.StatusNotFound)
246+
h.ServeJSON()
247+
return
248+
}
249+
250+
hosts := make([]*models.Hosts, 0)
251+
err = json.Unmarshal(h.Ctx.Input.RequestBody, hosts)
252+
if err != nil {
253+
beego.Warn("[C] Got error:", err)
254+
h.Data["json"] = map[string]string{
255+
"message": "Bad request",
256+
"error": err.Error(),
257+
}
258+
h.Ctx.Output.SetStatus(http.StatusBadRequest)
259+
h.ServeJSON()
260+
return
261+
}
262+
beego.Debug("[C] Got data:", hosts)
263+
err = models.AddBackupSetsHosts(backupSets[0], hosts)
264+
if err != nil {
265+
beego.Warn("[C] Got error:", err)
266+
h.Data["json"] = map[string]string{
267+
"message": "Failed to add new path",
268+
"error": err.Error(),
269+
}
270+
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
271+
h.ServeJSON()
272+
return
273+
}
274+
275+
h.Ctx.Output.SetStatus(http.StatusNoContent)
276+
h.ServeJSON()
277+
return
278+
}
279+
}
280+
281+
// @router /:name/hosts [delete]
282+
func (h *BackupSetsController) DeleteBackupSetsHosts() {
283+
name := h.GetString(":name")
284+
beego.Debug("[C] Got name:", name)
285+
if name != "" {
286+
backupSet := &models.BackupSets{
287+
Name: name,
288+
}
289+
backupSets, err := models.GetBackupSets(backupSet, 0, 0)
290+
if err != nil {
291+
h.Data["json"] = map[string]string{
292+
"message": fmt.Sprint("Failed to get with name:", name),
293+
"error": err.Error(),
294+
}
295+
beego.Warn("[C] Got error:", err)
296+
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
297+
h.ServeJSON()
298+
return
299+
}
300+
if len(backupSets) == 0 {
301+
beego.Debug("[C] Got nothing with name:", name)
302+
h.Ctx.Output.SetStatus(http.StatusNotFound)
303+
h.ServeJSON()
304+
return
305+
}
306+
307+
hosts := make([]*models.Hosts, 0)
308+
err = json.Unmarshal(h.Ctx.Input.RequestBody, hosts)
309+
if err != nil {
310+
beego.Warn("[C] Got error:", err)
311+
h.Data["json"] = map[string]string{
312+
"message": "Bad request",
313+
"error": err.Error(),
314+
}
315+
h.Ctx.Output.SetStatus(http.StatusBadRequest)
316+
h.ServeJSON()
317+
return
318+
}
319+
beego.Debug("[C] Got data:", hosts)
320+
err = models.DeleteBackupSetsHosts(backupSets[0], hosts)
321+
if err != nil {
322+
beego.Warn("[C] Got error:", err)
323+
h.Data["json"] = map[string]string{
324+
"message": "Failed to delete path",
325+
"error": err.Error(),
326+
}
327+
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
328+
h.ServeJSON()
329+
return
330+
}
331+
332+
h.Ctx.Output.SetStatus(http.StatusNoContent)
333+
h.ServeJSON()
334+
return
335+
}
336+
}

‎controllers/client.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,5 @@ func (c *ClientController) DeleteSignal() {
216216
c.Ctx.Output.SetStatus(http.StatusNotFound)
217217
c.ServeJSON()
218218
return
219-
220219
}
221220
}

‎controllers/client_jobs.go‎

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package controllers
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"moduleab_server/models"
7+
"net/http"
8+
9+
"github.com/astaxie/beego"
10+
)
11+
12+
type ClientJobsController struct {
13+
beego.Controller
14+
}
15+
16+
// @Title createClientJob
17+
// @router / [post]
18+
func (a *ClientJobsController) Post() {
19+
clientJob := new(models.ClientJobs)
20+
err := json.Unmarshal(a.Ctx.Input.RequestBody, clientJob)
21+
if err != nil {
22+
beego.Warn("[C] Got error:", err)
23+
a.Data["json"] = map[string]string{
24+
"message": "Bad request",
25+
"error": err.Error(),
26+
}
27+
a.Ctx.Output.SetStatus(http.StatusBadRequest)
28+
a.ServeJSON()
29+
return
30+
}
31+
beego.Debug("[C] Got data:", clientJob)
32+
id, err := models.AddClientJob(clientJob)
33+
if err != nil {
34+
beego.Warn("[C] Got error:", err)
35+
a.Data["json"] = map[string]string{
36+
"message": "Failed to add New host",
37+
"error": err.Error(),
38+
}
39+
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
40+
a.ServeJSON()
41+
return
42+
}
43+
44+
beego.Debug("[C] Got id:", id)
45+
a.Data["json"] = map[string]string{
46+
"id": id,
47+
}
48+
a.Ctx.Output.SetStatus(http.StatusCreated)
49+
a.ServeJSON()
50+
return
51+
}
52+
53+
// @Title getClientJob
54+
// @router /:id [get]
55+
func (a *ClientJobsController) Get() {
56+
id := a.GetString(":id")
57+
beego.Debug("[C] Got id:", id)
58+
if id != "" {
59+
clientJob := &models.ClientJobs{
60+
Id: id,
61+
}
62+
clientJobs, err := models.GetClientJobs(clientJob, 0, 0)
63+
if err != nil {
64+
a.Data["json"] = map[string]string{
65+
"message": fmt.Sprint("Failed to get with id:", id),
66+
"error": err.Error(),
67+
}
68+
beego.Warn("[C] Got error:", err)
69+
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
70+
a.ServeJSON()
71+
return
72+
}
73+
a.Data["json"] = clientJobs
74+
if len(clientJobs) == 0 {
75+
beego.Debug("[C] Got nothing with id:", id)
76+
a.Ctx.Output.SetStatus(http.StatusNotFound)
77+
a.ServeJSON()
78+
return
79+
} else {
80+
a.Ctx.Output.SetStatus(http.StatusOK)
81+
a.ServeJSON()
82+
return
83+
}
84+
}
85+
}
86+
87+
// @Title listClientJobs
88+
// @router / [get]
89+
func (a *ClientJobsController) GetAll() {
90+
limit, _ := a.GetInt("limit", 0)
91+
index, _ := a.GetInt("index", 0)
92+
93+
clientJob := &models.ClientJobs{}
94+
clientJobs, err := models.GetClientJobs(clientJob, limit, index)
95+
if err != nil {
96+
a.Data["json"] = map[string]string{
97+
"message": fmt.Sprint("Failed to get"),
98+
"error": err.Error(),
99+
}
100+
beego.Warn("[C] Got error:", err)
101+
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
102+
a.ServeJSON()
103+
return
104+
}
105+
a.Data["json"] = clientJobs
106+
if len(clientJobs) == 0 {
107+
beego.Debug("[C] Got nothing")
108+
a.Ctx.Output.SetStatus(http.StatusNotFound)
109+
a.ServeJSON()
110+
return
111+
} else {
112+
a.Ctx.Output.SetStatus(http.StatusOK)
113+
a.ServeJSON()
114+
return
115+
}
116+
}
117+
118+
// @Title deleteClientJob
119+
// @router /:id [delete]
120+
func (a *ClientJobsController) Delete() {
121+
id := a.GetString(":id")
122+
beego.Debug("[C] Got id:", id)
123+
if id != "" {
124+
clientJob := &models.ClientJobs{
125+
Id: id,
126+
}
127+
clientJobs, err := models.GetClientJobs(clientJob, 0, 0)
128+
if err != nil {
129+
a.Data["json"] = map[string]string{
130+
"message": fmt.Sprint("Failed to get with id:", id),
131+
"error": err.Error(),
132+
}
133+
beego.Warn("[C] Got error:", err)
134+
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
135+
a.ServeJSON()
136+
return
137+
}
138+
if len(clientJobs) == 0 {
139+
beego.Debug("[C] Got nothing with id:", id)
140+
a.Ctx.Output.SetStatus(http.StatusNotFound)
141+
a.ServeJSON()
142+
return
143+
}
144+
err = models.DeleteClientJob(clientJobs[0])
145+
if err != nil {
146+
a.Data["json"] = map[string]string{
147+
"message": fmt.Sprint("Failed to delete with id:", id),
148+
"error": err.Error(),
149+
}
150+
beego.Warn("[C] Got error:", err)
151+
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
152+
a.ServeJSON()
153+
return
154+
155+
}
156+
a.Ctx.Output.SetStatus(http.StatusNoContent)
157+
a.ServeJSON()
158+
return
159+
}
160+
}
161+
162+
// @Title updateClientJob
163+
// @router /:id [put]
164+
func (a *ClientJobsController) Put() {
165+
id := a.GetString(":id")
166+
beego.Debug("[C] Got clientJob id:", id)
167+
if id != "" {
168+
clientJob := &models.ClientJobs{
169+
Id: id,
170+
}
171+
clientJobs, err := models.GetClientJobs(clientJob, 0, 0)
172+
if err != nil {
173+
a.Data["json"] = map[string]string{
174+
"message": fmt.Sprint("Failed to get with id:", id),
175+
"error": err.Error(),
176+
}
177+
beego.Warn("[C] Got error:", err)
178+
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
179+
a.ServeJSON()
180+
return
181+
}
182+
if len(clientJobs) == 0 {
183+
beego.Debug("[C] Got nothing with id:", id)
184+
a.Ctx.Output.SetStatus(http.StatusNotFound)
185+
a.ServeJSON()
186+
return
187+
}
188+
189+
err = json.Unmarshal(a.Ctx.Input.RequestBody, clientJob)
190+
clientJob.Id = clientJobs[0].Id
191+
if err != nil {
192+
beego.Warn("[C] Got error:", err)
193+
a.Data["json"] = map[string]string{
194+
"message": "Bad request",
195+
"error": err.Error(),
196+
}
197+
a.Ctx.Output.SetStatus(http.StatusBadRequest)
198+
a.ServeJSON()
199+
return
200+
}
201+
beego.Debug("[C] Got clientJob data:", clientJob)
202+
err = models.UpdateClientJob(clientJob)
203+
if err != nil {
204+
a.Data["json"] = map[string]string{
205+
"message": fmt.Sprint("Failed to update with id:", id),
206+
"error": err.Error(),
207+
}
208+
beego.Warn("[C] Got error:", err)
209+
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
210+
a.ServeJSON()
211+
return
212+
213+
}
214+
a.Ctx.Output.SetStatus(http.StatusAccepted)
215+
a.ServeJSON()
216+
return
217+
}
218+
}

‎controllers/error.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ func (e *ErrorController) Error404() {
1717
}
1818
e.ServeJSON()
1919
}
20+
21+
func (e *ErrorController) Error500() {
22+
e.Ctx.Output.SetStatus(http.StatusInternalServerError)
23+
e.Data["json"] = map[string]string{
24+
"message": "Got serieve error.",
25+
}
26+
e.ServeJSON()
27+
}

0 commit comments

Comments
 (0)