Skip to content

Commit

Permalink
fixed the eazy cases where connection handling can be added
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 19, 2023
1 parent 4e17351 commit b29fbb4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 73 deletions.
134 changes: 68 additions & 66 deletions server/backend/cafeteriaService.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions server/backend/canteenHeadCount.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

// GetCanteenHeadCount RPC Endpoint
func (s *CampusServer) GetCanteenHeadCount(_ context.Context, input *pb.GetCanteenHeadCountRequest) (*pb.GetCanteenHeadCountReply, error) {
func (s *CampusServer) GetCanteenHeadCount(ctx context.Context, input *pb.GetCanteenHeadCountRequest) (*pb.GetCanteenHeadCountReply, error) {
data := model.CanteenHeadCount{Count: 0, MaxCount: 0, Percent: -1} // Initialize with an empty (not found) value
err := s.db.Model(&model.CanteenHeadCount{}).Where(model.CanteenHeadCount{CanteenId: input.CanteenId}).FirstOrInit(&data).Error
err := s.db.WithContext(ctx).Model(&model.CanteenHeadCount{}).Where(model.CanteenHeadCount{CanteenId: input.CanteenId}).FirstOrInit(&data).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.WithError(err).Error("while querying the canteen head count for: ", input.CanteenId)
return nil, errors.New("failed to query head count")
Expand Down
4 changes: 2 additions & 2 deletions server/backend/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *CampusServer) GetNewsSources(ctx context.Context, _ *pb.GetNewsSourcesR
}

var sources []model.NewsSource
if err := s.db.Joins("Files").Find(&sources).Error; err != nil {
if err := s.db.WithContext(ctx).Joins("Files").Find(&sources).Error; err != nil {
log.WithError(err).Error("could not find newsSources")
return nil, status.Error(codes.Internal, "could not GetNewsSources")
}
Expand All @@ -42,7 +42,7 @@ func (s *CampusServer) GetNews(ctx context.Context, req *pb.GetNewsRequest) (*pb
}

var newsEntries []model.News
tx := s.db.Joins("Files")
tx := s.db.WithContext(ctx).Joins("Files")
if req.NewsSource != 0 {
tx = tx.Where("src = ?", req.NewsSource)
}
Expand Down
2 changes: 1 addition & 1 deletion server/backend/newsAlerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *CampusServer) GetTopNews(ctx context.Context, _ *pb.GetTopNewsRequest)
}

var res *model.NewsAlert
err := s.db.Joins("Files").Where("NOW() between `from` and `to`").First(&res).Error
err := s.db.WithContext(ctx).Joins("Files").Where("NOW() between `from` and `to`").First(&res).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "no current active top news")
} else if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *CampusServer) SearchRooms(ctx context.Context, req *pb.SearchRoomsReque
Campus string
Name string
}
err := s.db.Raw("SELECT r.*, a.campus, a.name "+
err := s.db.WithContext(ctx).Raw("SELECT r.*, a.campus, a.name "+
"FROM roomfinder_rooms r "+
"LEFT JOIN roomfinder_building2area a ON a.building_nr = r.building_nr "+
"WHERE MATCH(room_code, info, address) AGAINST(?)", req.Query).Scan(&res).Error
Expand Down
2 changes: 1 addition & 1 deletion server/backend/updateNote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *CampusServer) GetUpdateNote(ctx context.Context, req *pb.GetUpdateNoteR
}

res := model.UpdateNote{VersionCode: req.Version}
if err := s.db.First(&res).Error; errors.Is(err, gorm.ErrRecordNotFound) {
if err := s.db.WithContext(ctx).First(&res).Error; errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "No update note found")
} else if err != nil {
log.WithField("VersionCode", req.Version).WithError(err).Error("Failed to get update note")
Expand Down

0 comments on commit b29fbb4

Please sign in to comment.