/
news.go
43 lines (34 loc) · 1.03 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
package display
import (
"fmt"
"net/http"
"time"
"gorm.io/gorm/clause"
"github.com/labstack/echo/v4"
"github.com/mect/expino-api/pkg/db"
)
func (h *HTTPHandler) handleNewsList(c echo.Context) error {
display, ok := c.Get("display").(*db.Display)
if !ok {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": "error reading display data from session"})
}
var newsItems []db.NewsItem
res := h.db.Preload(clause.Associations).Where("display_id", display.ID).Where("hidden", false).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)})
}
var displayItems []db.NewsItem
for _, item := range newsItems {
showNow := len(item.TimeFrames) == 0
for _, tf := range item.TimeFrames {
if time.Now().After(tf.From) && time.Now().Before(tf.To) {
showNow = true
break
}
}
if showNow {
displayItems = append(displayItems, item)
}
}
return c.JSON(http.StatusOK, displayItems)
}