Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:news testing #216

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions server/api/CampusService.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/api/CampusService.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/api/CampusService.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ service Campus {
};
}

rpc GetNewsSources (google.protobuf.Empty) returns (NewsSourceArray) {
rpc GetNewsSources (google.protobuf.Empty) returns (NewsSourceReply) {
option (google.api.http) = {
get: "/news/sources",
response_body: "sources"
Expand Down Expand Up @@ -376,7 +376,7 @@ message Room {
string name = 9;
}

message NewsSourceArray {
message NewsSourceReply {
repeated NewsSource sources = 1;
}

Expand Down
10 changes: 5 additions & 5 deletions server/api/CampusService_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/api/gen/openapiv2/CampusService.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@
}
}
},
"apiNewsSourceArray": {
"apiNewsSourceReply": {
"type": "object",
"properties": {
"sources": {
Expand Down
9 changes: 9 additions & 0 deletions server/backend/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ type deviceBuffer struct {
interval time.Duration // flush interval
}

func newDeviceBuffer() *deviceBuffer {
return &deviceBuffer{
lock: sync.Mutex{},
devices: make(map[string]*model.Devices),
interval: time.Minute,
}

}

func (s *CampusServer) RunDeviceFlusher() error {
for {
time.Sleep(s.deviceBuf.interval)
Expand Down
61 changes: 61 additions & 0 deletions server/backend/news.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package backend

import (
"context"
"errors"
"fmt"
pb "github.com/TUM-Dev/Campus-Backend/server/api"
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/gorm"
)

func (s *CampusServer) GetNewsSources(ctx context.Context, _ *emptypb.Empty) (newsSources *pb.NewsSourceReply, err error) {
if err = s.checkDevice(ctx); err != nil {
return
}

var sources []model.NewsSource
if err := s.db.Joins("Files").Find(&sources).Error; err != nil {
log.WithError(err).Error("could not find newsSources")
return nil, status.Error(codes.Internal, "could not GetNewsSources")
}

var resp []*pb.NewsSource
for _, source := range sources {
log.WithField("title", source.Title).Trace("sending news source")
resp = append(resp, &pb.NewsSource{
Source: fmt.Sprintf("%d", source.Source),
Title: source.Title,
Icon: source.Files.URL.String,
})
}
return &pb.NewsSourceReply{Sources: resp}, nil
}

func (s *CampusServer) GetTopNews(ctx context.Context, _ *emptypb.Empty) (*pb.GetTopNewsReply, error) {
if err := s.checkDevice(ctx); err != nil {
return nil, err
}

var res *model.NewsAlert
err := s.db.Joins("Files").Where("NOW() between `from` and `to`").First(&res).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "no currenty active top news")
} else if err != nil {
log.WithError(err).Error("could not GetTopNews")
return nil, status.Error(codes.Internal, "could not GetTopNews")
}

return &pb.GetTopNewsReply{
ImageUrl: res.Files.URL.String,
Link: res.Link.String,
Created: timestamppb.New(res.Created),
From: timestamppb.New(res.From),
To: timestamppb.New(res.To),
}, nil
}
Loading