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

Endpoint ListDishes #326

Merged
merged 12 commits into from
Mar 11, 2024
203 changes: 102 additions & 101 deletions server/api/tumdev/campus_backend.pb.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions server/api/tumdev/campus_backend.pb.gw.go

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

9 changes: 6 additions & 3 deletions server/api/tumdev/campus_backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ service Campus {
};
}

// Returns all dishes for a specific cafeteria, year, week and day
rpc ListDishes(ListDishesRequest) returns (ListDishesReply) {
option (google.api.http) = {
get: "/dish/allDishes",
get: "/dishes",
response_body: "dish"
};
}
Expand Down Expand Up @@ -389,9 +390,11 @@ message ListDishesRequest {
string canteen_id = 1;
// >=2022 until the current year
int32 year = 2;
// range 1 - 53
// Week of the dish.
// Must be in the range 1 - 52
int32 week = 3;
// range 0 (Monday) - 4 (Friday)
// Day of the week
// Days must be in the range 0 (Monday) - 4 (Friday)
int32 day = 4;
}

Expand Down
115 changes: 58 additions & 57 deletions server/api/tumdev/campus_backend.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,63 +315,6 @@
]
}
},
"/dish/allDishes": {
"get": {
"operationId": "Campus_ListDishes",
"responses": {
"200": {
"description": "",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "canteenId",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "year",
"description": "\u003e=2022 until the current year",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
},
{
"name": "week",
"description": "range 1 - 53",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
},
{
"name": "day",
"description": "range 0 (Monday) - 4 (Friday)",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
}
],
"tags": [
"Campus"
]
}
},
"/dish/rating/allDishTags": {
"get": {
"operationId": "Campus_ListNameTags",
Expand Down Expand Up @@ -489,6 +432,64 @@
]
}
},
"/dishes": {
"get": {
"summary": "Returns all dishes for a specific cafeteria, year, week and day",
"operationId": "Campus_ListDishes",
"responses": {
"200": {
"description": "",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "canteenId",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "year",
"description": "\u003e=2022 until the current year",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
},
{
"name": "week",
"description": "Week of the dish.\nMust be in the range 1 - 52",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
},
{
"name": "day",
"description": "Day of the week\nDays must be in the range 0 (Monday) - 4 (Friday)",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
}
],
"tags": [
"Campus"
]
}
},
"/feedback": {
"post": {
"operationId": "Campus_CreateFeedback",
Expand Down
2 changes: 2 additions & 0 deletions server/api/tumdev/campus_backend_grpc.pb.go

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

11 changes: 7 additions & 4 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,21 +642,24 @@ func (s *CampusServer) ListDishes(ctx context.Context, req *pb.ListDishesRequest
if req.Year < 2022 {
return &pb.ListDishesReply{}, status.Error(codes.Internal, "Years must be larger or equal to 2022 ") // currently, no previous values have been added
}
if req.Week < 1 || req.Week > 53 {
return &pb.ListDishesReply{}, status.Error(codes.Internal, "Weeks must be in the range 1 - 53")
if req.Week < 1 || req.Week > 52 {
return &pb.ListDishesReply{}, status.Error(codes.Internal, "Weeks must be in the range 1 - 52")
}
if req.Day < 0 || req.Day > 4 {
return &pb.ListDishesReply{}, status.Error(codes.Internal, "Days must be in the range 1 (Monday) - 4 (Friday)")
return &pb.ListDishesReply{}, status.Error(codes.Internal, "Days must be in the range 0 (Monday) - 4 (Friday)")
}

var requestStatus error = nil
var results []string
// the eat api has two types of ids, the enum ids (uppercase, with `_`) and the ids (lowercase, with `-`)
cafeteriaName := strings.ReplaceAll(strings.ToUpper(req.CanteenId), "-", "_")

err := s.db.WithContext(ctx).Table("dishes_of_the_week weekly").
Where("weekly.day = ? AND weekly.week = ? and weekly.year = ?", req.Day, req.Week, req.Year).
Select("weekly.dishID").
Joins("JOIN dish d ON d.dish = weekly.dishID").
Joins("JOIN cafeteria c ON c.cafeteria = d.cafeteriaID").
Where("c.name LIKE ?", req.CanteenId).
Where("c.name LIKE ?", cafeteriaName).
Select("d.name").
Find(&results).Error

Expand Down
2 changes: 1 addition & 1 deletion server/backend/cron/dish_name_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func downloadDailyDishes(c *CronService) {
for _, v := range result {
cafeteriaName := strings.Replace(strings.ToLower(v.Name), "_", "-", 10)

req := fmt.Sprintf("https://tum-dev.github.io/eat-api/%s/%d/%d.json", cafeteriaName, year, week)
req := fmt.Sprintf("https://tum-dev.github.io/eat-api/%s/%d/%02d.json", cafeteriaName, year, week)
log.WithField("req", req).Debug("Fetching menu")
var resp, err = http.Get(req)
if err != nil {
Expand Down
Loading