Skip to content

Commit

Permalink
refactored dishratingtags
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Aug 23, 2024
1 parent 7b77345 commit d8cbdca
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,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 @@ -422,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 Down Expand Up @@ -474,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 @@ -498,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 @@ -528,15 +535,6 @@ 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.CanteenRatingTag{})
}
}

func getIDForCafeteriaName(name string, tx *gorm.DB) int32 {
Expand Down

0 comments on commit d8cbdca

Please sign in to comment.