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

feat: Backend v1 shutdown Tasks #214

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/local/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func canteenHeadCount(c pb.CampusClient, ctx context.Context) {
func canteenRatingTools(c pb.CampusClient, ctx context.Context) {

currentCanteen := "MENSA_GARCHING"
currentDish := "Vegane rote Grütze mit Soja-Vanillesauce" //must be in the dish table
currentDish := "Vegane rote Grütze mit Soja-Vanillesauce" //must be in the dishes table
generateDishRating(c, ctx, currentCanteen, currentDish, 3)
generateCanteenRating(c, ctx, currentCanteen, 2)
queryCanteen(currentCanteen, c, ctx, true)
Expand Down
36 changes: 18 additions & 18 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *CampusServer) GetCafeteriaRatings(ctx context.Context, input *pb.ListCa
// 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 Down Expand Up @@ -276,25 +276,25 @@ func queryTags(cafeteriaID int32, dishID int32, ratingType ModelType, tx *gorm.D
var results []queryRatingTag
var err error
if ratingType == DISH {
err = tx.Table("dish_rating_tag_option options").
err = tx.Table("dish_rating_tag_options options").
Joins("JOIN dish_rating_tag_average 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 == CAFETERIA {
err = tx.Table("cafeteria_rating_tag_option options").
err = tx.Table("cafeteria_rating_tag_options options").
Joins("JOIN cafeteria_rating_tag_average 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_average 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 @@ -325,13 +325,13 @@ func queryTagRatingsOverviewForRating(dishID int64, ratingType ModelType, tx *go
var results []*pb.RatingTagNewRequest
var err error
if ratingType == 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 @@ -355,7 +355,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 @@ -501,10 +501,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 @@ -528,7 +528,7 @@ func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType ModelTy
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 @@ -578,13 +578,13 @@ func getModelStoreTag(tagType ModelType, tx *gorm.DB) *gorm.DB {
if tagType == 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 @@ -646,7 +646,7 @@ func (s *CampusServer) ListNameTags(ctx context.Context, _ *pb.ListNameTagsReque
func (s *CampusServer) GetAvailableCafeteriaTags(ctx context.Context, _ *pb.ListAvailableCanteenTagsRequest) (*pb.ListAvailableCanteenTagsReply, error) {
var result []*pb.TagsOverview
var requestStatus error = nil
err := s.db.WithContext(ctx).Model(&model.CafeteriaRatingTagOption{}).Select("DE as de, EN as en, cafeteriaRatingsTagOption as TagId").Find(&result).Error
err := s.db.WithContext(ctx).Model(&model.CanteenRatingTagOption{}).Select("DE as de, EN as en, cafeteriaRatingsTagOption as TagId").Find(&result).Error
if err != nil {
log.WithError(err).Error("while loading Cafeterias from database.")
requestStatus = status.Error(codes.Internal, "Available cafeteria tags could not be loaded from the database.")
Expand All @@ -662,7 +662,7 @@ func (s *CampusServer) GetAvailableCafeteriaTags(ctx context.Context, _ *pb.List
func (s *CampusServer) GetCafeterias(ctx context.Context, _ *pb.ListCanteensRequest) (*pb.ListCanteensReply, error) {
var result []*pb.Canteen
var requestStatus error = nil
if err := s.db.WithContext(ctx).Model(&model.Cafeteria{}).Select("cafeteria as id,address,latitude,longitude").Scan(&result).Error; err != nil {
if err := s.db.WithContext(ctx).Model(&model.Canteen{}).Select("cafeteria as id,address,latitude,longitude").Scan(&result).Error; err != nil {
log.WithError(err).Error("while loading Cafeterias from database.")
requestStatus = status.Error(codes.Internal, "Cafeterias could not be loaded from the database.")
}
Expand All @@ -685,7 +685,7 @@ func (s *CampusServer) ListDishes(ctx context.Context, request *pb.ListDishesReq

var requestStatus error = nil
var results []string
err := s.db.WithContext(ctx).Table("dishes_of_the_week weekly").
err := s.db.WithContext(ctx).Table("dishes_of_the_weeks weekly").
Where("weekly.day = ? AND weekly.week = ? and weekly.year = ?", request.Day, request.Week, request.Year).
Select("weekly.dishID").
Joins("JOIN dish d ON d.dish = weekly.dishID").
Expand Down
14 changes: 7 additions & 7 deletions server/backend/cron/average_rating_computation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (c *CronService) averageRatingComputation() error {
func computeAverageNameTags(c *CronService) {
var results []model.DishNameTagAverage
err := c.db.Raw("SELECT mr.cafeteriaID as cafeteriaID, mnt.tagnameID as tagID, AVG(mnt.points) as average, MAX(mnt.points) as max, MIN(mnt.points) as min, STD(mnt.points) as std" +
" FROM dish_rating mr" +
" JOIN dish_name_tag mnt ON mr.dishRating = mnt.correspondingRating" +
" FROM dish_ratings mr" +
" JOIN dish_name_tags mnt ON mr.dishRating = mnt.correspondingRating" +
" GROUP BY mr.cafeteriaID, mnt.tagnameID").Scan(&results).Error

if err != nil {
Expand All @@ -40,8 +40,8 @@ func computeAverageNameTags(c *CronService) {
func computeAverageForDishesInCafeteriasTags(c *CronService) {
var results []model.DishRatingTagAverage //todo namen im select anpassen
err := c.db.Raw("SELECT mr.dishID as dishID, mr.cafeteriaID as cafeteriaID, mrt.tagID as tagID, AVG(mrt.points) as average, MAX(mrt.points) as max, MIN(mrt.points) as min, STD(mrt.points) as std" +
" FROM dish_rating mr" +
" JOIN dish_rating_tag mrt ON mr.dishRating = mrt.parentRating" +
" FROM dish_ratings mr" +
" JOIN dish_rating_tags mrt ON mr.dishRating = mrt.parentRating" +
" GROUP BY mr.cafeteriaID, mrt.tagID, mr.dishID").Scan(&results).Error

if err != nil {
Expand All @@ -61,8 +61,8 @@ func computeAverageForDishesInCafeteriasTags(c *CronService) {
func computeAverageCafeteriaTags(c *CronService) {
var results []model.CafeteriaRatingTagAverage
err := c.db.Raw("SELECT cr.cafeteriaID as cafeteriaID, crt.tagID as tagID, AVG(crt.points) as average, MAX(crt.points) as max, MIN(crt.points) as min, STD(crt.points) as std" +
" FROM cafeteria_rating cr" +
" JOIN cafeteria_rating_tag crt ON cr.cafeteriaRating = crt.correspondingRating" +
" FROM cafeteria_ratings cr" +
" JOIN cafeteria_rating_tags crt ON cr.cafeteriaRating = crt.correspondingRating" +
" GROUP BY cr.cafeteriaID, crt.tagID").Scan(&results).Error

if err != nil {
Expand Down Expand Up @@ -98,7 +98,7 @@ func computeAverageForDishesInCafeterias(c *CronService) {

func computeAverageForCafeteria(c *CronService) {
var results []model.CafeteriaRatingAverage
err := c.db.Model(&model.CafeteriaRating{}).
err := c.db.Model(&model.CanteenRating{}).
Select("cafeteriaID, AVG(points) as average, MAX(points) as max, MIN(points) as min, STD(points) as std").
Group("cafeteriaID").Find(&results).Error

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 result []CafeteriaWithID
if err := c.db.Model(&model.Cafeteria{}).Select("name,cafeteria").Scan(&result).Error; err != nil {
if err := c.db.Model(&model.Canteen{}).Select("name,cafeteria").Scan(&result).Error; err != nil {
log.WithError(err).Error("Error while querying all cafeteria names from the database.")
}

Expand Down Expand Up @@ -137,13 +137,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 @@ -162,15 +162,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 @@ -49,7 +49,7 @@ func (c *CronService) movieCron() error {
for _, item := range channel.Items {
logFields := log.Fields{"link": item.Link, "title": item.Title, "date": item.PubDate, "location": item.Location, "url": item.Enclosure.Url}
var exists bool
if err := c.db.Model(model.Kino{}).Select("count(*) > 0").Find(&exists, "link = ?", item.Link).Error; err != nil {
if err := c.db.Model(model.Movie{}).Select("count(*) > 0").Find(&exists, "link = ?", item.Link).Error; err != nil {
log.WithError(err).WithFields(logFields).Error("Cound lot check if movie already exists")
continue
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (c *CronService) movieCron() error {
}

// save the result of the previous steps (🎉)
movie := model.Kino{
movie := model.Movie{
Date: date,
Title: item.Title,
Year: omdbMovie.ReleaseYear,
Expand Down
31 changes: 0 additions & 31 deletions server/backend/migration/20230825000000.go

This file was deleted.

79 changes: 79 additions & 0 deletions server/backend/migration/20230912000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package migration

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

// migrate20230912000000
// Removes ticketsales from the db
func migrate20230912000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20230912000000",
Migrate: func(tx *gorm.DB) error {
// order intentional to avoid foreign key constraint errors
for _, tbl := range []string{"ticket_history", "ticket_type", "ticket_payment", "ticket_admin2group", "ticket_admin", "event", "ticket_group"} {
if err := tx.Migrator().DropTable(tbl); err != nil {
return err
}
}
return nil
},

Rollback: func(tx *gorm.DB) error {
//ticket_group
if err := tx.Exec(`create table ticket_group (ticket_group int auto_increment primary key,description text not null);`).Error; err != nil {
return err
}
//event
if err := tx.Exec("create table event(event int auto_increment primary key, news int null, kino int null, file int null, title varchar(100) not null, description text not null, locality varchar(200) not null, link varchar(200) null, start datetime null, end datetime null, ticket_group int default 1 null, constraint fkEventFile foreign key (file) references files (file) on update cascade on delete set null, constraint fkEventGroup foreign key (ticket_group) references ticket_group (ticket_group), constraint fkKino foreign key (kino) references kino (kino) on update cascade on delete set null, constraint fkNews foreign key (news) references news (news) on update cascade on delete set null);").Error; err != nil {
return err
}
if err := tx.Exec("create index file on event (file);").Error; err != nil {
return err
}
if err := tx.Exec("create fulltext index searchTitle on event (title);").Error; err != nil {
return err
}
//ticket_admin
if err := tx.Exec("create table ticket_admin(ticket_admin int auto_increment primary key, `key` text not null, created timestamp default current_timestamp() not null, active tinyint(1) default 0 not null, comment text null);").Error; err != nil {
return err
}
//ticket_admin2group
if err := tx.Exec("create table ticket_admin2group( ticket_admin2group int auto_increment primary key, ticket_admin int not null, ticket_group int not null, constraint fkTicketAdmin foreign key (ticket_admin) references ticket_admin (ticket_admin) on update cascade on delete cascade, constraint fkTicketGroup foreign key (ticket_group) references ticket_group (ticket_group) on update cascade on delete cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_admin on ticket_admin2group (ticket_admin);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_group on ticket_admin2group (ticket_group);").Error; err != nil {
return err
}
//ticket_payment
if err := tx.Exec("create table ticket_payment( ticket_payment int auto_increment primary key, name varchar(50) not null, min_amount int null, max_amount int null, config text not null);").Error; err != nil {
return err
}
//ticket_type
if err := tx.Exec("create table ticket_type( ticket_type int auto_increment primary key, event int not null, ticket_payment int not null, price double not null, contingent int not null, description varchar(100) not null, constraint fkEvent foreign key (event) references event (event) on update cascade on delete cascade, constraint fkPayment foreign key (ticket_payment) references ticket_payment (ticket_payment) on update cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index event on ticket_type (event);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_payment on ticket_type (ticket_payment);").Error; err != nil {
return err
}
//ticket_history
if err := tx.Exec("create table ticket_history( ticket_history int auto_increment primary key, member int not null, ticket_payment int null, ticket_type int not null, purchase datetime null, redemption datetime null, created timestamp default current_timestamp() not null, code char(128) not null, constraint fkMember foreign key (member) references member (member) on update cascade on delete cascade, constraint fkTicketPayment foreign key (ticket_payment) references ticket_payment (ticket_payment) on update cascade, constraint fkTicketType foreign key (ticket_type) references ticket_type (ticket_type) on update cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index member on ticket_history (member);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_payment on ticket_history (ticket_payment);").Error; err != nil {
return err
}
return tx.Exec("create index ticket_type on ticket_history (ticket_type);").Error
},
}
}
2 changes: 1 addition & 1 deletion server/backend/migration/20231003000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func addNotIncluded(parentId int64, db *gorm.DB, v nameTag) {
Expression: expression,
NameTagID: parentId}).Error
if err != nil {
log.WithError(err).WithFields(fields).Error("Unable to create new DishNameTagOptionExcluded")
log.WithError(err).WithFields(fields).Error("Unable to create new ExcludedDishNameTagOption")
}
}
}
Expand Down
Loading
Loading