/
routes_indexers.go
96 lines (76 loc) · 2.02 KB
/
routes_indexers.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
package app
import (
"net/http"
"sort"
"github.com/labstack/echo/v4"
)
func (a *Application) IndexersIndex(c echo.Context, page int, limit int) error {
indexers, count, err := a.DB.IndexerList(page, limit)
if err != nil {
return err
}
sort.Slice(indexers, func(i, j int) bool {
return indexers[i].Name < indexers[j].Name
})
return c.JSON(http.StatusOK, H{"count": count, "results": indexers})
}
func (a *Application) IndexersCreate(c echo.Context) error {
indexer := &Indexer{}
if err := c.Bind(indexer); err != nil {
return err
}
if err := a.DB.Indexer.Save(indexer); err != nil {
return err
}
return c.JSON(http.StatusOK, H{"error": false, "result": indexer})
}
func (a *Application) IndexersShow(c echo.Context, id string) error {
subject, err := a.DB.Indexer.Get(id, &Indexer{})
if err != nil {
return err
}
return c.JSON(http.StatusOK, H{"error": false, "result": subject})
}
func (a *Application) IndexersUpdate(c echo.Context, id string) error {
subject := &Indexer{}
if err := c.Bind(subject); err != nil {
return err
}
if err := a.DB.Indexer.Save(subject); err != nil {
return err
}
return c.JSON(http.StatusOK, H{"error": false, "result": subject})
}
type Setting struct {
Key string `json:"setting"`
Value bool `json:"value"`
}
func (a *Application) IndexersSettings(c echo.Context, id string) error {
subject, err := a.DB.IndexerGet(id)
if err != nil {
return err
}
setting := &Setting{}
if err := c.Bind(setting); err != nil {
return err
}
switch setting.Key {
case "active":
subject.Active = setting.Value
}
if err := a.DB.Indexer.Save(subject); err != nil {
return err
}
return c.JSON(http.StatusOK, H{"error": false})
}
func (a *Application) IndexersDelete(c echo.Context, id string) error {
// asssuming this is a CRUD route, get the subject from the database
subject, err := a.DB.Indexer.Get(id, &Indexer{})
if err != nil {
return err
}
if err := a.DB.Indexer.Delete(subject); err != nil {
return err
}
return c.JSON(http.StatusOK, H{"error": false})
}