Skip to content

Commit

Permalink
squashed the changes from #214 for simpler review
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Aug 23, 2024
1 parent 1bfce50 commit 6d3d6fb
Show file tree
Hide file tree
Showing 34 changed files with 117 additions and 158 deletions.
39 changes: 20 additions & 19 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 @@ -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 @@ -484,7 +485,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 Down Expand Up @@ -534,13 +535,13 @@ 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{})
return tx.Model(&model.CanteenRatingTag{})
}
}

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 +610,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 +629,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 +658,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
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
58 changes: 58 additions & 0 deletions server/backend/migration/20240824000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package migration

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

type wrongTableName struct {
Original string
New string
}

var wrongTableNames = []wrongTableName{
{"crontab", "crontabs"},
{"kino", "movies"},
{"cafeteria", "canteens"},
{"canteen_head_count", "canteen_head_counts"},
{"cafeteria_rating", "canteen_ratings"},
{"cafeteria_rating_tag", "canteen_rating_tags"},
{"cafeteria_rating_tag_option", "canteen_rating_tag_options"},
{"dish", "dishes"},
{"dish_rating", "dish_ratings"},
{"dish_rating_tag", "dish_rating_tags"},
{"dish_rating_tag_option", "dish_rating_tag_options"},
{"dish_name_tag", "dish_name_tags"},
{"dish_name_tag_option", "dish_name_tag_options"},
{"dish_name_tag_option_excluded", "excluded_dish_name_tag_options"},
{"dish_name_tag_option_included", "included_dish_name_tag_option"},
{"dish_to_dish_name_tag", "dish_to_dish_name_tags"},
{"dishes_of_the_week", "dishes_of_the_weeks"},
{"update_note", "update_notes"},
{"newsSource", "news_sources"},
{"news_alert", "news_alerts"},
}

// migrate20240824000000
// - replaces all instances of misleadingly named tables with the correct ones
func migrate20240824000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20240824000000",
Migrate: func(tx *gorm.DB) error {
for _, table := range wrongTableNames {
if err := tx.Migrator().RenameTable(table.Original, table.New); err != nil {
return err
}
}
return nil
},
Rollback: func(tx *gorm.DB) error {
for _, table := range wrongTableNames {
if err := tx.Migrator().RenameTable(table.New, table.Original); err != nil {
return err
}
}
return nil
},
}
}
15 changes: 8 additions & 7 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ import (

func autoMigrate(db *gorm.DB) error {
err := db.AutoMigrate(
&model.Cafeteria{},
&model.CafeteriaRating{},
&model.CafeteriaRatingTag{},
&model.CafeteriaRatingTagOption{},
&model.Canteen{},
&model.CanteenRating{},
&model.CanteenRatingTag{},
&model.CanteenRatingTagOption{},
&model.CanteenHeadCount{},
&model.Crontab{},
&model.Device{},
&model.Dish{},
&model.DishNameTag{},
&model.DishNameTagOption{},
&model.DishNameTagOptionExcluded{},
&model.DishNameTagOptionIncluded{},
&model.ExcludedDishNameTagOption{},
&model.IncludedDishNameTagOption{},
&model.DishRating{},
&model.DishRatingTag{},
&model.DishRatingTagOption{},
&model.DishToDishNameTag{},
&model.DishesOfTheWeek{},
&model.Feedback{},
&model.File{},
&model.Kino{},
&model.Movie{},
&model.NewExamResultsSubscriber{},
&model.News{},
&model.NewsAlert{},
Expand Down Expand Up @@ -86,6 +86,7 @@ func manualMigrate(db *gorm.DB) error {
migrate20240511000000(),
migrate20240512000000(),
migrate20240706000000(),
migrate20240824000000(),
}
return gormigrate.New(db, gormigrateOptions, migrations).Migrate()
}
Expand Down
2 changes: 1 addition & 1 deletion server/backend/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (s *CampusServer) ListMovies(ctx context.Context, req *pb.ListMoviesRequest) (*pb.ListMoviesReply, error) {
var movies []model.Kino
var movies []model.Movie
tx := s.db.WithContext(ctx).
Joins("File").
Order("date ASC")
Expand Down
2 changes: 1 addition & 1 deletion server/backend/movie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var (
}
)

const ListMoviesQuery = "SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`kino`.`location`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `kino` LEFT JOIN `files` `File` ON `kino`.`cover` = `File`.`file` WHERE kino > ? ORDER BY date ASC"
const ListMoviesQuery = "SELECT `movies`.`kino`,`movies`.`date`,`movies`.`created`,`movies`.`title`,`movies`.`year`,`movies`.`runtime`,`movies`.`genre`,`movies`.`director`,`movies`.`actors`,`movies`.`rating`,`movies`.`description`,`movies`.`trailer`,`movies`.`cover`,`movies`.`link`,`movies`.`location`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `movies` LEFT JOIN `files` `File` ON `movies`.`cover` = `File`.`file` WHERE kino > ? ORDER BY date ASC"

func (s *MovieSuite) Test_ListMoviesAll() {
server := CampusServer{db: s.DB}
Expand Down
6 changes: 3 additions & 3 deletions server/backend/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSource
if err := s.db.WithContext(ctx).
Joins("File").
Find(&sources).Error; err != nil {
log.WithError(err).Error("could not find newsSources")
log.WithError(err).Error("could not find news_sources")
return nil, status.Error(codes.Internal, "could not ListNewsSources")
}

Expand Down Expand Up @@ -94,9 +94,9 @@ func (s *CampusServer) ListNewsAlerts(ctx context.Context, req *pb.ListNewsAlert
var res []*model.NewsAlert
tx := s.db.WithContext(ctx).
Joins("File").
Where("news_alert.to >= NOW()")
Where("news_alerts.to >= NOW()")
if req.LastNewsAlertId != 0 {
tx = tx.Where("news_alert.news_alert > ?", req.LastNewsAlertId)
tx = tx.Where("news_alerts.news_alert > ?", req.LastNewsAlertId)
}
if err := tx.Find(&res).Error; errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "no news alerts")
Expand Down
Loading

0 comments on commit 6d3d6fb

Please sign in to comment.