Skip to content

Commit

Permalink
Merge branch 'main' into unique-dishes
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm authored Mar 12, 2024
2 parents 76dad8f + be68c6f commit 9c3a373
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 220 deletions.
204 changes: 102 additions & 102 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.

8 changes: 5 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,10 @@ message ListDishesRequest {
string canteen_id = 1;
// >=2022 until the current year
int32 year = 2;
// range 1 - 53
// 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": "Must 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 @@ -68,7 +68,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
4 changes: 2 additions & 2 deletions server/backend/cron/email_templates/feedback_body.gohtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1>Feedback via TumCampusApp:</h1>
{{ if .Feedback.Valid -}}
{{ if .Feedback -}}
<blockquote>
{{- .Feedback.String -}}
{{- .Feedback -}}
</blockquote>
{{- else -}}
<i>no feedback provided</i>
Expand Down
4 changes: 2 additions & 2 deletions server/backend/cron/email_templates/feedback_body.txt.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Feedback via TumCampusApp:

{{ if .Feedback.Valid }}
{{- .Feedback.String -}}
{{ if .Feedback }}
{{- .Feedback -}}
{{ else -}}
no feedback provided
{{- end }}
Expand Down
6 changes: 3 additions & 3 deletions server/backend/cron/feedback_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func messageWithHeaders(feedback *model.Feedback) *gomail.Message {
// From
m.SetAddressHeader("From", os.Getenv("SMTP_USERNAME"), "TUM Campus App")
// To
if feedback.Recipient.Valid {
m.SetHeader("To", feedback.Recipient.String)
if feedback.Recipient != "" {
m.SetHeader("To", feedback.Recipient)
} else {
m.SetHeader("To", "[email protected]")
m.SetHeader("To", "[email protected]") // should not ever happen as checked in the api
}
// ReplyTo
if feedback.ReplyTo.Valid {
Expand Down
14 changes: 7 additions & 7 deletions server/backend/cron/feedback_email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func TestIterate(t *testing.T) {

func fullFeedback() *model.Feedback {
return &model.Feedback{
EmailId: null.StringFrom("magic-id"),
Recipient: null.StringFrom("tca"),
EmailId: "magic-id",
Recipient: "tca",
ReplyTo: null.StringFrom("[email protected]"),
Feedback: null.StringFrom("This is a Test"),
Feedback: "This is a Test",
ImageCount: 1,
Latitude: null.FloatFrom(0),
Longitude: null.FloatFrom(0),
Expand All @@ -38,10 +38,10 @@ func fullFeedback() *model.Feedback {

func emptyFeedback() *model.Feedback {
return &model.Feedback{
EmailId: null.String{},
Recipient: null.String{},
EmailId: "",
Recipient: "",
ReplyTo: null.String{},
Feedback: null.String{},
Feedback: "",
ImageCount: 0,
Latitude: null.Float{},
Longitude: null.Float{},
Expand All @@ -56,7 +56,7 @@ func TestHeaderInstantiationWithFullFeedback(t *testing.T) {
fb := fullFeedback()
m := messageWithHeaders(fb)
assert.Equal(t, []string{`"TUM Campus App" <[email protected]>`}, m.GetHeader("From"))
assert.Equal(t, []string{fb.Recipient.String}, m.GetHeader("To"))
assert.Equal(t, []string{fb.Recipient}, m.GetHeader("To"))
assert.Equal(t, []string{"[email protected]"}, m.GetHeader("Reply-To"))
assert.Equal(t, []string{fb.Timestamp.Time.Format(time.RFC1123Z)}, m.GetHeader("Date"))
assert.Equal(t, []string{"Feedback via Tum Campus App"}, m.GetHeader("Subject"))
Expand Down
18 changes: 11 additions & 7 deletions server/backend/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func (s *CampusServer) CreateFeedback(stream pb.Campus_CreateFeedbackServer) err
log.WithError(err).Error("Error generating uuid")
return status.Error(codes.Internal, "Error starting feedback submission")
}
feedback := &model.Feedback{EmailId: null.StringFrom(id.String())}
feedback := &model.Feedback{EmailId: id.String(), Recipient: "[email protected]"}

// download images
dbPath := path.Join("feedback", feedback.EmailId.String)
dbPath := path.Join("feedback", feedback.EmailId)
var uploadedFilenames []*string
for {
req, err := stream.Recv()
Expand All @@ -40,7 +40,7 @@ func (s *CampusServer) CreateFeedback(stream pb.Campus_CreateFeedbackServer) err
}
if err != nil {
log.WithError(err).Error("Error receiving feedback")
deleteUploaded(feedback.EmailId.String)
deleteUploaded(feedback.EmailId)
return status.Error(codes.Internal, "Error receiving feedback")
}
mergeFeedback(feedback, req)
Expand All @@ -53,6 +53,10 @@ func (s *CampusServer) CreateFeedback(stream pb.Campus_CreateFeedbackServer) err
}
}
feedback.ImageCount = int32(len(uploadedFilenames))
// validate feedback
if feedback.Feedback == "" && feedback.ImageCount == 0 {
return status.Error(codes.InvalidArgument, "Please attach an image or feedback for us")
}
// save feedback to db
if err := s.db.WithContext(stream.Context()).Transaction(func(tx *gorm.DB) error {
for _, filename := range uploadedFilenames {
Expand All @@ -68,7 +72,7 @@ func (s *CampusServer) CreateFeedback(stream pb.Campus_CreateFeedbackServer) err
return tx.Create(feedback).Error
}); err != nil {
log.WithError(err).Error("Error creating feedback")
deleteUploaded(feedback.EmailId.String)
deleteUploaded(feedback.EmailId)
return status.Error(codes.Internal, "Error creating feedback")
}

Expand Down Expand Up @@ -130,20 +134,20 @@ func inferFileName(mime *mimetype.MIME, dbPath string, counter int) (*string, *s

func mergeFeedback(feedback *model.Feedback, req *pb.CreateFeedbackRequest) {
if req.Recipient.Enum() != nil {
feedback.Recipient = null.StringFrom(receiverFromTopic(req.Recipient))
feedback.Recipient = receiverFromTopic(req.Recipient)
}
if req.OsVersion != "" {
feedback.OsVersion = null.StringFrom(req.OsVersion)
}
if req.AppVersion != "" {
feedback.AppVersion = null.StringFrom(req.AppVersion)
}
if req.Location != nil {
if req.Location != nil && req.Location.Longitude != 0 && req.Location.Latitude != 0 {
feedback.Longitude = null.FloatFrom(req.Location.Longitude)
feedback.Latitude = null.FloatFrom(req.Location.Latitude)
}
if req.Message != "" {
feedback.Feedback = null.StringFrom(req.Message)
feedback.Feedback = req.Message
}
if req.FromEmail != "" {
feedback.ReplyTo = null.StringFrom(req.FromEmail)
Expand Down
Loading

0 comments on commit 9c3a373

Please sign in to comment.