Skip to content

Commit

Permalink
modified the models.DishRating struct as well with the appropriate ch…
Browse files Browse the repository at this point in the history
…ange
  • Loading branch information
CommanderStorm committed Mar 13, 2024
1 parent a9959b4 commit ba60932
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
11 changes: 5 additions & 6 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,11 @@ func (s *CampusServer) CreateDishRating(ctx context.Context, input *pb.CreateDis
resPath := imageWrapper(input.Image, "dishes", dishInMensa.Dish)

rating := model.DishRating{
Comment: input.Comment,
CafeteriaID: cafeteriaID,
DishID: dishInMensa.Dish,
Points: input.Points,
Timestamp: time.Now(),
Image: resPath,
Comment: input.Comment,
DishID: dishInMensa.Dish,
Points: input.Points,
Timestamp: time.Now(),
Image: resPath,
}
if err := tx.Create(&rating).Error; err != nil {
log.WithError(err).Error("while creating a new dishInMensa rating.")
Expand Down
19 changes: 10 additions & 9 deletions server/backend/migration/20240311000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@ import (

// migrate20240311000000
// made sure that dishes have the correct indexes
// changed how `dish_ratings` is bound to `dish`
// changed how `dish_rating` is bound to `dish`
func migrate20240311000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20240311000000",
Migrate: func(tx *gorm.DB) error {
// make sure that dish_ratings are FK-bound to dishes
// make sure that dish_rating is FK-bound to dish
if err := tx.Exec(`alter table dish_rating
add constraint dish_rating_dish_dish_fk
foreign key (dishID) references dish (dish)
on delete cascade;`).Error; err != nil {
add constraint dish_rating_dish_dish_fk
foreign key (dishID) references dish (dish)
on update cascade
on delete cascade;`).Error; err != nil {
return err
}
// because dishes already have a cafeteria, storing this again is not necessary
if err := tx.Exec(`alter table dish_rating drop column cafeteriaID`).Error; err != nil {
if err := tx.Exec("alter table dish_rating drop column cafeteriaID").Error; err != nil {
return err
}
// uniqueness
return tx.Exec("create unique index dish_name_cafeteriaID_uindex on dish (name, cafeteriaID)").Error
},
Rollback: func(tx *gorm.DB) error {
// make sure that dish_ratings are FK-bound to dishes
if err := tx.Exec(`alter table dish_rating drop constraint dish_rating_dish_dish_fk`).Error; err != nil {
// make sure that dish_rating is FK-bound to dishes
if err := tx.Exec("alter table dish_rating drop constraint dish_rating_dish_dish_fk").Error; err != nil {
return err
}
// because dishes already have a cafeteria, storing this agiain is not nessesary
if err := tx.Exec(`alter table dish_rating add column cafeteriaID int not null`).Error; err != nil {
if err := tx.Exec("alter table dish_rating add column cafeteriaID int not null").Error; err != nil {
return err
}
// uniqueness
Expand Down
14 changes: 7 additions & 7 deletions server/model/dish_rating.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
)

type DishRating struct {
DishRating int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishRating;type:int;not null;" json:"dishRating"`
Points int32 `gorm:"column:points;type:int;not null;" json:"points"`
CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"`
DishID int64 `gorm:"column:dishID;foreignKey:dish;type:int;not null;" json:"dishID"`
Comment string `gorm:"column:comment;type:text;" json:"comment"`
Timestamp time.Time `gorm:"column:timestamp;type:timestamp;not null;" json:"timestamp"`
Image string `gorm:"column:image;type:text;" json:"image"`
DishRating int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishRating;type:int;not null;" json:"dishRating"`
Points int32 `gorm:"column:points;type:int;not null;" json:"points"`
DishID int64 `gorm:"column:dishID;type:int;not null;" json:"dishID"`
Dish Dish `gorm:"foreignKey:dishID;references:dish;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Comment string `gorm:"column:comment;type:text;" json:"comment"`
Timestamp time.Time `gorm:"column:timestamp;type:timestamp;not null;" json:"timestamp"`
Image string `gorm:"column:image;type:text;" json:"image"`
}

// TableName sets the insert table name for this struct type
Expand Down

0 comments on commit ba60932

Please sign in to comment.