/
news.go
119 lines (106 loc) · 3.42 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
package backend
import (
"context"
"errors"
"fmt"
"gorm.io/gorm"
"google.golang.org/protobuf/types/known/timestamppb"
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSourcesRequest) (*pb.ListNewsSourcesReply, error) {
if err := s.checkDevice(ctx); err != nil {
return nil, err
}
var sources []model.NewsSource
if err := s.db.WithContext(ctx).
Joins("File").
Find(&sources).Error; err != nil {
log.WithError(err).Error("could not find newsSources")
return nil, status.Error(codes.Internal, "could not ListNewsSources")
}
var resp []*pb.NewsSource
for _, source := range sources {
resp = append(resp, &pb.NewsSource{
Source: fmt.Sprintf("%d", source.Source),
Title: source.Title,
IconUrl: source.File.FullExternalUrl(),
})
}
return &pb.ListNewsSourcesReply{Sources: resp}, nil
}
func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (*pb.ListNewsReply, error) {
if err := s.checkDevice(ctx); err != nil {
return nil, err
}
var newsEntries []model.News
tx := s.db.WithContext(ctx).
Joins("File").
Joins("NewsSource").
Joins("NewsSource.File")
if req.NewsSource != 0 {
tx = tx.Where("src = ?", req.NewsSource)
}
if req.OldestDateAt.GetSeconds() != 0 || req.OldestDateAt.GetNanos() != 0 {
tx = tx.Where("date > ?", req.OldestDateAt.AsTime())
}
if req.LastNewsId != 0 {
tx = tx.Where("news > ?", req.LastNewsId)
}
if err := tx.Find(&newsEntries).Error; err != nil {
log.WithError(err).Error("could not find news item")
return nil, status.Error(codes.Internal, "could not ListNews")
}
var resp []*pb.News
for _, item := range newsEntries {
imgUrl := ""
if item.File != nil {
imgUrl = item.File.FullExternalUrl()
}
resp = append(resp, &pb.News{
Id: item.News,
Title: item.Title,
Text: item.Description,
Link: item.Link,
ImageUrl: imgUrl,
SourceId: fmt.Sprintf("%d", item.NewsSource.Source),
SourceTitle: item.NewsSource.Title,
SourceIconUrl: item.NewsSource.File.FullExternalUrl(),
Created: timestamppb.New(item.Created),
Date: timestamppb.New(item.Date),
})
}
return &pb.ListNewsReply{News: resp}, nil
}
func (s *CampusServer) ListNewsAlerts(ctx context.Context, req *pb.ListNewsAlertsRequest) (*pb.ListNewsAlertsReply, error) {
if err := s.checkDevice(ctx); err != nil {
return nil, err
}
var res []*model.NewsAlert
tx := s.db.WithContext(ctx).
Joins("File").
Where("news_alert.to >= NOW()")
if req.LastNewsAlertId != 0 {
tx = tx.Where("news_alert.news_alert > ?", req.LastNewsAlertId)
}
if err := tx.Find(&res).Error; errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "no news alerts")
} else if err != nil {
log.WithError(err).Error("could not ListNewsAlerts")
return nil, status.Error(codes.Internal, "could not ListNewsAlerts")
}
var alerts []*pb.NewsAlert
for _, alert := range res {
alerts = append(alerts, &pb.NewsAlert{
ImageUrl: alert.File.FullExternalUrl(),
Link: alert.Link.String,
Created: timestamppb.New(alert.Created),
From: timestamppb.New(alert.From),
To: timestamppb.New(alert.To),
})
}
return &pb.ListNewsAlertsReply{Alerts: alerts}, nil
}