Skip to content

Commit

Permalink
made sure all places use the same methodology to construct response a…
Browse files Browse the repository at this point in the history
…rrays
  • Loading branch information
CommanderStorm committed Oct 31, 2023
1 parent 4870d02 commit b7f3402
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
36 changes: 16 additions & 20 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,17 @@ func queryLastCafeteriaRatingsWithLimit(input *pb.ListCanteenRatingsRequest, caf
log.WithError(err).Error("while querying last cafeteria ratings.")
return make([]*pb.SingleRatingReply, 0)
}
ratingResults := make([]*pb.SingleRatingReply, len(ratings))

for i, v := range ratings {

tagRatings := queryTagRatingsOverviewForRating(v.CafeteriaRating, CAFETERIA, tx)
ratingResults[i] = &pb.SingleRatingReply{
var resp []*pb.SingleRatingReply
for _, v := range ratings {
resp = append(resp, &pb.SingleRatingReply{
Points: v.Points,
Comment: v.Comment,
Image: getImageToBytes(v.Image),
Visited: timestamppb.New(v.Timestamp),
RatingTags: tagRatings,
}
RatingTags: queryTagRatingsOverviewForRating(v.CafeteriaRating, CAFETERIA, tx),
})
}
return ratingResults
return resp
} else {
return make([]*pb.SingleRatingReply, 0)
}
Expand Down Expand Up @@ -217,18 +214,17 @@ func queryLastDishRatingsWithLimit(input *pb.GetDishRatingsRequest, cafeteriaID
log.WithError(err).Error("while querying last dish ratings from Database.")
return make([]*pb.SingleRatingReply, 0)
}
ratingResults := make([]*pb.SingleRatingReply, len(ratings))

for i, v := range ratings {
ratingResults[i] = &pb.SingleRatingReply{
var resp []*pb.SingleRatingReply
for _, v := range ratings {
resp = append(resp, &pb.SingleRatingReply{
Points: v.Points,
Comment: v.Comment,
RatingTags: queryTagRatingsOverviewForRating(v.DishRating, DISH, tx),
Image: getImageToBytes(v.Image),
Visited: timestamppb.New(v.Timestamp),
}
})
}
return ratingResults
return resp
} else {
return make([]*pb.SingleRatingReply, 0)
}
Expand Down Expand Up @@ -305,18 +301,18 @@ func queryTags(cafeteriaID int32, dishID int32, ratingType ModelType, tx *gorm.D
}

//needed since the gRPC element does not specify column names - cannot be directly queried into the grpc message object.
elements := make([]*pb.RatingTagResult, len(results))
for i, v := range results {
elements[i] = &pb.RatingTagResult{
var resp []*pb.RatingTagResult
for _, v := range results {
resp = append(resp, &pb.RatingTagResult{
TagId: v.TagId,
Avg: v.Average,
Std: v.Std,
Min: v.Min,
Max: v.Max,
}
})
}

return elements
return resp
}

// queryTagRatingOverviewForRating
Expand Down
10 changes: 4 additions & 6 deletions server/backend/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSource

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,
Expand Down Expand Up @@ -60,14 +59,13 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (*
return nil, status.Error(codes.Internal, "could not ListNews")
}

resp := make([]*pb.News, len(newsEntries))
for i, item := range newsEntries {
log.WithField("title", item.Title).Trace("sending news")
var resp []*pb.News
for _, item := range newsEntries {
imgUrl := ""
if item.File != nil {
imgUrl = item.File.FullExternalUrl()
}
resp[i] = &pb.News{
resp = append(resp, &pb.News{
Id: item.News,
Title: item.Title,
Text: item.Description,
Expand All @@ -78,7 +76,7 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (*
SourceIconUrl: item.NewsSource.File.FullExternalUrl(),
Created: timestamppb.New(item.Created),
Date: timestamppb.New(item.Date),
}
})
}
return &pb.ListNewsReply{News: resp}, nil
}
Expand Down
10 changes: 2 additions & 8 deletions server/backend/news_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,7 @@ func (s *NewsSuite) Test_ListNewsNone_withFilters() {
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.ListNews(meta, &pb.ListNewsRequest{NewsSource: 1, LastNewsId: 2})
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsReply{
News: []*pb.News{},
}
require.Equal(s.T(), expectedResp, response)
require.Equal(s.T(), &pb.ListNewsReply{News: nil}, response)
}
func (s *NewsSuite) Test_ListNewsNone() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedListNewsQuery)).
Expand All @@ -165,10 +162,7 @@ func (s *NewsSuite) Test_ListNewsNone() {
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.ListNews(meta, &pb.ListNewsRequest{})
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsReply{
News: []*pb.News{},
}
require.Equal(s.T(), expectedResp, response)
require.Equal(s.T(), &pb.ListNewsReply{News: nil}, response)
}
func (s *NewsSuite) Test_ListNewsMultiple() {
n1 := news1()
Expand Down

0 comments on commit b7f3402

Please sign in to comment.