Skip to content

Commit

Permalink
Table name cleanup (#420)
Browse files Browse the repository at this point in the history
* squashed the changes from #214 for simpler review

* reverted changing Crontab table name

* fixed a typo in one table

* tested a different implementation of canteen rating tags

* tested different rating tag configuration

* refactored dishratingtags

* different test

* tested different rating tags

* tested different rating tags

* finally figured out how rating tags should work

* tested simplifications to the Feedback model

* tested simplifications to the Feedback model

* made sure that `autoIncrement` is spelled as in the docs not a second version that works too

* fixed formatting

* fixed formatting
  • Loading branch information
CommanderStorm authored Aug 23, 2024
1 parent 13eb331 commit a928e68
Show file tree
Hide file tree
Showing 51 changed files with 259 additions and 293 deletions.
87 changes: 43 additions & 44 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *CampusServer) ListCanteenRatings(ctx context.Context, input *pb.ListCan
// queryLastCafeteriaRatingsWithLimit
// Queries the actual ratings for a cafeteria and attaches the tag ratings which belong to the ratings
func queryLastCafeteriaRatingsWithLimit(input *pb.ListCanteenRatingsRequest, cafeteriaID int32, tx *gorm.DB) []*pb.SingleRatingReply {
var ratings []model.CafeteriaRating
var ratings []model.CanteenRating
var err error

var limit = int(input.Limit)
Expand All @@ -81,7 +81,8 @@ func queryLastCafeteriaRatingsWithLimit(input *pb.ListCanteenRatingsRequest, caf
} else {
to = input.To.AsTime()
}
err = tx.Order("timestamp desc, cafeteriaRating desc").
err = tx.
Order("timestamp desc, cafeteriaRating desc").
Limit(limit).
Find(&ratings, "cafeteriaID = ? AND timestamp < ? AND timestamp > ?", cafeteriaID, to, from).Error
} else {
Expand Down Expand Up @@ -233,25 +234,25 @@ func queryTags(cafeteriaID int32, dishID int32, ratingType model.ModelType, tx *
var results []queryRatingTag
var err error
if ratingType == model.DISH {
err = tx.Table("dish_rating_tag_option options").
err = tx.Table("dish_rating_tag_options options").
Joins("JOIN dish_rating_tag_statistics results ON options.dishRatingTagOption = results.tagID").
Select("options.dishRatingTagOption as tagId, results.average as avg, "+
"results.min as min, results.max as max, results.std as std").
Where("results.cafeteriaID = ? AND results.dishID = ?", cafeteriaID, dishID).
Scan(&results).Error
} else if ratingType == model.CAFETERIA {
err = tx.Table("cafeteria_rating_tag_option options").
err = tx.Table("cafeteria_rating_tag_options options").
Joins("JOIN cafeteria_rating_tag_statistics results ON options.cafeteriaRatingTagOption = results.tagID").
Select("options.cafeteriaRatingTagOption as tagId, results.average as avg, "+
"results.min as min, results.max as max, results.std as std").
Where("results.cafeteriaID = ?", cafeteriaID).
Scan(&results).Error
} else { //Query for name tags
err = tx.Table("dish_to_dish_name_tag mapping").
err = tx.Table("dish_to_dish_name_tags mapping").
Where("mapping.dishID = ?", dishID).
Select("mapping.nameTagID as tag").
Joins("JOIN dish_name_tag_statistics results ON mapping.nameTagID = results.tagID").
Joins("JOIN dish_name_tag_option options ON mapping.nameTagID = options.dishNameTagOption").
Joins("JOIN dish_name_tag_options options ON mapping.nameTagID = options.dishNameTagOption").
Select("mapping.nameTagID as tagId, results.average as avg, " +
"results.min as min, results.max as max, results.std as std").
Scan(&results).Error
Expand Down Expand Up @@ -282,13 +283,13 @@ func queryTagRatingsOverviewForRating(dishID int64, ratingType model.ModelType,
var results []*pb.RatingTagNewRequest
var err error
if ratingType == model.DISH {
err = tx.Table("dish_rating_tag_option options").
Joins("JOIN dish_rating_tag rating ON options.dishRatingTagOption = rating.tagID").
err = tx.Table("dish_rating_tag_options options").
Joins("JOIN dish_rating_tags rating ON options.dishRatingTagOption = rating.tagID").
Select("dishRatingTagOption as tagId, points, parentRating").
Find(&results, "parentRating = ?", dishID).Error
} else {
err = tx.Table("cafeteria_rating_tag_option options").
Joins("JOIN cafeteria_rating_tag rating ON options.cafeteriaRatingTagOption = rating.tagID").
err = tx.Table("cafeteria_rating_tag_options options").
Joins("JOIN cafeteria_rating_tags rating ON options.cafeteriaRatingTagOption = rating.tagID").
Select("cafeteriaRatingTagOption as tagId, points, correspondingRating").
Find(&results, "correspondingRating = ?", dishID).Error
}
Expand All @@ -312,7 +313,7 @@ func (s *CampusServer) CreateCanteenRating(ctx context.Context, input *pb.Create
}

resPath := imageWrapper(input.Image, "cafeterias", cafeteriaID)
rating := model.CafeteriaRating{
rating := model.CanteenRating{
Comment: input.Comment,
Points: input.Points,
CafeteriaID: cafeteriaID,
Expand Down Expand Up @@ -411,7 +412,7 @@ func (s *CampusServer) CreateDishRating(ctx context.Context, input *pb.CreateDis
return nil, status.Error(codes.Internal, "Error while creating the new rating in the database. Rating has not been saved.")
}

assignDishNameTag(rating, dishInCafeteria.Dish, tx)
assignDishNameTag(&rating, dishInCafeteria.Dish, tx)

if err := storeRatingTags(rating.DishRating, input.RatingTags, model.DISH, tx); err != nil {
return &pb.CreateDishRatingReply{}, err
Expand All @@ -421,7 +422,7 @@ func (s *CampusServer) CreateDishRating(ctx context.Context, input *pb.CreateDis

// assignDishNameTag
// Query all name tags for this specific dish and generate the DishNameTag Ratings ffor each name tag
func assignDishNameTag(rating model.DishRating, dishID int64, tx *gorm.DB) {
func assignDishNameTag(rating *model.DishRating, dishID int64, tx *gorm.DB) {
var nameTagIDs []int64
err := tx.Model(&model.DishToDishNameTag{}).
Where("dishID = ? ", dishID).
Expand All @@ -432,9 +433,9 @@ func assignDishNameTag(rating model.DishRating, dishID int64, tx *gorm.DB) {
} else {
for _, tagID := range nameTagIDs {
if err := tx.Create(&model.DishNameTag{
CorrespondingRating: rating.DishRating,
Points: rating.Points,
TagNameID: tagID,
RatingID: rating.DishRating,
Points: rating.Points,
TagNameID: tagID,
}).Error; err != nil {
log.WithError(err).Error("while creating a new dish name rating.")
}
Expand All @@ -457,10 +458,10 @@ func inputSanitizationForNewRatingElements(rating int32, comment string, cafeter
return -1, status.Error(codes.InvalidArgument, "Comments must not contain @ symbols in order to prevent misuse. Rating has not been saved.")
}

var result *model.Cafeteria
var result *model.Canteen
if res := tx.First(&result, "name LIKE ?", cafeteriaName); errors.Is(res.Error, gorm.ErrRecordNotFound) || res.RowsAffected == 0 {
log.WithError(res.Error).Error("Error while querying the cafeteria id by name: ", cafeteriaName)
return -1, status.Error(codes.InvalidArgument, "Cafeteria does not exist. Rating has not been saved.")
return -1, status.Error(codes.InvalidArgument, "Canteen does not exist. Rating has not been saved.")
}

return result.Cafeteria, nil
Expand All @@ -473,8 +474,7 @@ func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType model.M
var errorOccurred = ""
var warningOccurred = ""
if len(tags) > 0 {
usedTagIds := make(map[int]int)
insertModel := getModelStoreTag(tagType, tx)
usedTagIds := make(map[int64]bool)
for _, currentTag := range tags {
var err error
var count int64
Expand All @@ -484,7 +484,7 @@ func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType model.M
Where("dishRatingTagOption LIKE ?", currentTag.TagId).
Count(&count).Error
} else {
err = tx.Model(&model.CafeteriaRatingTagOption{}).
err = tx.Model(&model.CanteenRatingTagOption{}).
Where("cafeteriaRatingTagOption LIKE ?", currentTag.TagId).
Count(&count).Error
}
Expand All @@ -497,17 +497,25 @@ func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType model.M
log.WithFields(fields).Info("tag does not exist")
errorOccurred = fmt.Sprintf("%s, %d", errorOccurred, currentTag.TagId)
} else {
if usedTagIds[int(currentTag.TagId)] == 0 {
err := insertModel.
Create(&model.DishRatingTag{
CorrespondingRating: parentRatingID,
Points: int32(currentTag.Points),
TagID: currentTag.TagId,
}).Error
if err != nil {
log.WithError(err).Error("while Creating a currentTag rating for a new rating.")
if !usedTagIds[currentTag.TagId] {
if tagType == model.DISH {
if err := tx.Create(&model.DishRatingTag{
RatingID: parentRatingID,
Points: int32(currentTag.Points),
TagID: currentTag.TagId,
}).Error; err != nil {
log.WithError(err).Error("while Creating a currentTag rating for a new rating.")
}
} else {
if err := tx.Create(&model.CanteenRatingTag{
RatingID: parentRatingID,
Points: int32(currentTag.Points),
TagID: currentTag.TagId,
}).Error; err != nil {
log.WithError(err).Error("while Creating a currentTag rating for a new rating.")
}
}
usedTagIds[int(currentTag.TagId)] = 1
usedTagIds[currentTag.TagId] = true

} else {
warningOccurred = fmt.Sprintf("%s, %d", warningOccurred, currentTag.TagId)
Expand All @@ -527,20 +535,11 @@ func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType model.M
} else {
return nil
}

}

func getModelStoreTag(tagType model.ModelType, tx *gorm.DB) *gorm.DB {
if tagType == model.DISH {
return tx.Model(&model.DishRatingTag{})
} else {
return tx.Model(&model.CafeteriaRatingTag{})
}
}

func getIDForCafeteriaName(name string, tx *gorm.DB) int32 {
var result int32 = -1
err := tx.Model(&model.Cafeteria{}).
err := tx.Model(&model.Canteen{}).
Where("name LIKE ?", name).
Select("cafeteria").
Scan(&result).Error
Expand Down Expand Up @@ -609,7 +608,7 @@ func (s *CampusServer) GetAvailableCafeteriaTags(ctx context.Context, _ *pb.List
var result []*pb.TagsOverview
var requestStatus error = nil
err := s.db.WithContext(ctx).
Model(&model.CafeteriaRatingTagOption{}).
Model(&model.CanteenRatingTagOption{}).
Select("DE as de, EN as en, cafeteriaRatingsTagOption as TagId").
Find(&result).Error
if err != nil {
Expand All @@ -628,7 +627,7 @@ func (s *CampusServer) GetCafeterias(ctx context.Context, _ *pb.ListCanteensRequ
var result []*pb.Canteen
var requestStatus error = nil
if err := s.db.WithContext(ctx).
Model(&model.Cafeteria{}).
Model(&model.Canteen{}).
Select("cafeteria as id,address,latitude,longitude").
Scan(&result).Error; err != nil {
log.WithError(err).Error("while loading Cafeterias from database.")
Expand Down Expand Up @@ -657,7 +656,7 @@ func (s *CampusServer) ListDishes(ctx context.Context, req *pb.ListDishesRequest
cafeteriaName := strings.ReplaceAll(strings.ToUpper(req.CanteenId), "-", "_")

err := s.db.WithContext(ctx).
Table("dishes_of_the_week weekly").
Table("dishes_of_the_weeks 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").
Expand Down
10 changes: 5 additions & 5 deletions server/backend/cron/dish_name_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *CronService) dishNameDownloadCron() error {
}

func downloadDailyDishes(c *CronService) {
var results []model.Cafeteria
var results []model.Canteen
if err := c.db.Find(&results).Error; err != nil {
log.WithError(err).Error("Error while querying all cafeteria names from the database.")
return
Expand Down Expand Up @@ -145,13 +145,13 @@ func downloadCanteenNames(c *CronService) {
}

for _, cafeteriaName := range cafeteriaNames {
mensa := model.Cafeteria{
mensa := model.Canteen{
Name: cafeteriaName.Name,
Address: cafeteriaName.Location.Address,
Latitude: cafeteriaName.Location.Latitude,
Longitude: cafeteriaName.Location.Longitude,
}
var cafeteriaResult model.Cafeteria
var cafeteriaResult model.Canteen
if err := c.db.First(&cafeteriaResult, "name = ?", cafeteriaName.Name).Error; err != nil {
if err := c.db.Create(&mensa).Error; err != nil {
log.WithError(err).Error("Error while creating the db entry for the cafeteria ", cafeteriaName.Name)
Expand All @@ -170,15 +170,15 @@ func downloadCanteenNames(c *CronService) {
func addDishTagsToMapping(dishID int64, dishName string, db *gorm.DB) {
lowercaseDish := strings.ToLower(dishName)
var includedTags []int64
if err := db.Model(&model.DishNameTagOptionIncluded{}).
if err := db.Model(&model.IncludedDishNameTagOption{}).
Where("? LIKE CONCAT('%', expression ,'%')", lowercaseDish).
Select("nameTagID").
Scan(&includedTags).Error; err != nil {
log.WithError(err).Error("Error while querying all included expressions for the CanteenDish: ", lowercaseDish)
}

var excludedTags []int64
if err := db.Model(&model.DishNameTagOptionExcluded{}).
if err := db.Model(&model.ExcludedDishNameTagOption{}).
Where("? LIKE CONCAT('%', expression ,'%')", lowercaseDish).
Select("nameTagID").
Scan(&excludedTags).Error; err != nil {
Expand Down
6 changes: 1 addition & 5 deletions server/backend/cron/feedback_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ func messageWithHeaders(feedback *model.Feedback) *gomail.Message {
m.SetHeader("Reply-To", feedback.ReplyToEmail.String)
}
// Timestamp
if feedback.Timestamp.Valid {
m.SetDateHeader("Date", feedback.Timestamp.Time)
} else {
m.SetDateHeader("Date", time.Now())
}
m.SetDateHeader("Date", feedback.Timestamp)
// Subject
if feedback.Recipient == "[email protected]" {
versionString := "TCA"
Expand Down
6 changes: 3 additions & 3 deletions server/backend/cron/feedback_email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func fullFeedback() *model.Feedback {
Longitude: null.FloatFrom(0),
AppVersion: null.StringFrom("TCA 10.2"),
OsVersion: null.StringFrom("Android 10.0"),
Timestamp: null.TimeFrom(time.Now()),
Timestamp: time.Now(),
}
}

Expand All @@ -49,7 +49,7 @@ func emptyFeedback() *model.Feedback {
Longitude: null.Float{},
AppVersion: null.String{},
OsVersion: null.String{},
Timestamp: null.Time{},
Timestamp: time.Now(),
}
}

Expand All @@ -61,7 +61,7 @@ func TestHeaderInstantiationWithFullFeedback(t *testing.T) {
assert.Equal(t, []string{`"TUM Campus App" <[email protected]>`}, m.GetHeader("From"))
assert.Equal(t, []string{fb.Recipient}, m.GetHeader("To"))
assert.Equal(t, []string{"\"Erika Mustermann\" <[email protected]>"}, m.GetHeader("Reply-To"))
assert.Equal(t, []string{fb.Timestamp.Time.Format(time.RFC1123Z)}, m.GetHeader("Date"))
assert.Equal(t, []string{fb.Timestamp.Format(time.RFC1123Z)}, m.GetHeader("Date"))
assert.Equal(t, []string{"Feedback via the TUM Campus App"}, m.GetHeader("Subject"))
}

Expand Down
4 changes: 2 additions & 2 deletions server/backend/cron/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *CronService) movieCron() error {
}
log.Trace("parsing upcoming feed")
var allMovieLinks []string
if err := c.db.Model(&model.Kino{}).
if err := c.db.Model(&model.Movie{}).
Distinct().
Pluck("Link", &allMovieLinks).Error; err != nil {
return err
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *CronService) movieCron() error {
log.WithFields(logFields).WithError(err).Error("error while finding imdb id")
continue
}
movie := model.Kino{
movie := model.Movie{
Date: date,
Title: item.Title,
Location: null.StringFrom(item.Location),
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20210709193000.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type File struct {
File int64 `gorm:"primary_key;AUTO_INCREMENT;column:file;type:int;" json:"file"`
File int64 `gorm:"primary_key;autoIncrement;column:file;type:int;" json:"file"`
Name string `gorm:"column:name;type:text;size:16777215;" json:"name"`
Path string `gorm:"column:path;type:text;size:16777215;" json:"path"`
Downloads int32 `gorm:"column:downloads;type:int;default:0;" json:"downloads"`
Expand Down
Loading

0 comments on commit a928e68

Please sign in to comment.