-
Notifications
You must be signed in to change notification settings - Fork 1
/
news.go
126 lines (98 loc) · 3.62 KB
/
news.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
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) handleNewsList(c echo.Context) error {
var newsItems []db.NewsItem
res := h.db.Preload(clause.Associations).Order("\"order\",id").Find(&newsItems)
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, newsItems)
}
func (h *HTTPHandler) handleNewsGet(c echo.Context) error {
newsItem := &db.NewsItem{}
res := h.db.Preload(clause.Associations).First(newsItem, "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, newsItem)
}
func (h *HTTPHandler) handleNewsCreate(c echo.Context) error {
newsItem := &db.NewsItem{}
err := c.Bind(newsItem)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error reading data: %v", err)})
}
newsItem.ID = 0
res := h.db.Create(newsItem)
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, newsItem)
}
func (h *HTTPHandler) handleNewsUpdate(c echo.Context) error {
newsItem := &db.NewsItem{}
err := c.Bind(newsItem)
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"})
}
newsItem.ID = uint(id)
res := h.db.Save(newsItem)
if res.Error != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error saving data: %v", res.Error)})
}
err = h.db.Model(&newsItem).Association("LanguageItems").Replace(&newsItem.LanguageItems)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error saving data: %v", res.Error)})
}
err = h.db.Model(&newsItem).Association("TimeFrames").Replace(&newsItem.TimeFrames)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error saving data: %v", res.Error)})
}
for _, langItem := range newsItem.LanguageItems {
res := h.db.Save(&langItem)
if res.Error != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": fmt.Sprintf("error saving data: %v", res.Error)})
}
}
for _, tf := range newsItem.TimeFrames {
res := h.db.Save(&tf)
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, newsItem)
}
func (h *HTTPHandler) handleNewsDelete(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.NewsItem{
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)})
}
h.broadcastUpdate()
return c.JSON(http.StatusOK, echo.Map{"status": "ok"})
}