public async Task <ForumReviews> Create(ForumReviewCreateViewModel model, ReviewType reviewType, string reviewingUserId) { if (!await userManager.Users.AnyAsync(u => u.Id == reviewingUserId)) { throw new ArgumentException("Не е намерен потребителят с който сте логнати!"); } ForumReviews reviewToBeCreated; //review created by this user for this item which has to be deleted ForumReviews reviewAlreadyCreatedByThisUser; switch (reviewType) { case ReviewType.Post: if (!await PostsManager.Exists(model.ReviewedItemId)) { throw new ArgumentException("Постът, на който искате да направите ревю не съществува!"); } reviewAlreadyCreatedByThisUser = await unitOfWork.ForumReviewsRepository.GetAll() .OfType <PostReviews>() .Where(r => r.UserId == reviewingUserId && r.PostId == model.ReviewedItemId) .FirstOrDefaultAsync(); reviewToBeCreated = Mapper.Map <PostReviews>(model, opts => opts.Items.Add("UserId", reviewingUserId)); break; case ReviewType.Comment: if (!await CommentsManager.Exists(model.ReviewedItemId)) { throw new ArgumentException("Коментарът, на който искате да направите ревю не съществува!"); } reviewAlreadyCreatedByThisUser = await unitOfWork.ForumReviewsRepository.GetAll() .OfType <PostReviews>() .Where(r => r.UserId == reviewingUserId && r.PostId == model.ReviewedItemId) .FirstOrDefaultAsync(); reviewToBeCreated = Mapper.Map <CommentReviews>(model, opts => opts.Items.Add("UserId", reviewingUserId)); break; default: throw new ArgumentException("Не се поддържат Ревюта от избраният от вас тип!"); } if (reviewAlreadyCreatedByThisUser != null) { unitOfWork.ForumReviewsRepository.Delete(reviewAlreadyCreatedByThisUser); } unitOfWork.ForumReviewsRepository.Add(reviewToBeCreated); await unitOfWork.SaveAsync(); return(reviewToBeCreated); }