public async Task <IActionResult> Details(int id) { CakeDetailRatingViewModel ratingModel = new CakeDetailRatingViewModel(); var rating = await _ratingRepository.GetUserRatingByCakeAsync(id); ratingModel.CakeId = id; if (rating != null) { ratingModel.Value = rating.Value; } var cake = await _cakeRepository.GetCakeById(id); ratingModel.cake = cake; return(View(ratingModel)); }
public async Task <IActionResult> Details([Bind("Value,CakeId")] CakeDetailRatingViewModel model) { var existRating = await _ratingRepository.GetUserRatingByCakeAsync(model.CakeId); if (existRating != null) { existRating.Value = model.Value; existRating.Cake = await model.LoadCake(_cakeRepository); _ratingRepository.UpdateRating(existRating); } else { Rating rating = new Rating(); rating.CakeId = model.CakeId; rating.Value = model.Value; rating.UserId = User.FindFirstValue(ClaimTypes.NameIdentifier); await _ratingRepository.AddRatingAsync(rating); } model.cake = await model.LoadCake(_cakeRepository); return(View(model)); }