-
Notifications
You must be signed in to change notification settings - Fork 1
/
displays.go
98 lines (76 loc) · 2.69 KB
/
displays.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
package v1
import (
"fmt"
"net/http"
"strconv"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/labstack/echo/v4"
"github.com/mect/expino-api/pkg/db"
)
func (h *HTTPHandler) handleDisplayList(c echo.Context) error {
var displays []db.Display
res := h.db.Preload(clause.Associations).Order("id").Find(&displays)
if res.Error != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error reading data: %v", res.Error)})
}
return c.JSON(http.StatusOK, displays)
}
func (h *HTTPHandler) handleDisplayGet(c echo.Context) error {
display := &db.Display{}
res := h.db.Preload(clause.Associations).First(display, "id = ?", c.Param("id"))
if res.Error != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error reading data: %v", res.Error)})
}
return c.JSON(http.StatusOK, display)
}
func (h *HTTPHandler) handleDisplayCreate(c echo.Context) error {
display := &db.Display{}
err := c.Bind(display)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error reading data: %v", err)})
}
display.ID = 0
res := h.db.Create(display)
if res.Error != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error saving data: %v", res.Error)})
}
return c.JSON(http.StatusOK, display)
}
func (h *HTTPHandler) handleDisplayUpdate(c echo.Context) error {
display := &db.Display{}
err := c.Bind(display)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error reading data: %v", err)})
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"error": fmt.Sprintf("error reading ID: %v", err)})
} else if id < 0 {
return c.JSON(http.StatusBadRequest, echo.Map{"error": "ID cannot be negative"})
}
display.ID = uint(id)
res := h.db.Save(display)
if res.Error != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error saving data: %v", res.Error)})
}
h.broadcastUpdate()
return c.JSON(http.StatusOK, display)
}
func (h *HTTPHandler) handleDisplayDelete(c echo.Context) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"error": fmt.Sprintf("error reading ID: %v", err)})
} else if id < 0 {
return c.JSON(http.StatusBadRequest, echo.Map{"error": "ID cannot be negative"})
}
res := h.db.Delete(&db.Display{
Model: gorm.Model{
ID: uint(id),
},
})
if res.Error != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error deleting data: %v", res.Error)})
}
return c.JSON(http.StatusOK, echo.Map{"status": "ok"})
}