public async Task <HttpResponseMessage> SaveReview(ShortReviewDTO review) { try { await _reviewService.SaveReview(review); } catch { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public async Task SaveReview(ShortReviewDTO reviewDto) { var book = await _unitOfWork.BookRepository.Query .Include(b => b.Location) .Include(b => b.Vendor) .Include(b => b.Vendor.Person) .Include(b => b.Vendor.Person.Account) .Include(b => b.Work) .Include(b => b.Work.Subcategory) .Include(b => b.Customer) .Include(b => b.Customer.Person) .Include(b => b.Customer.Person.Account) .Include(b => b.Company) .Include(b => b.Company.Account) .SingleAsync(b => b.Id == reviewDto.BookId); if (!string.IsNullOrEmpty(reviewDto.Text.Trim())) { var review = new Review(); review.BookId = reviewDto.BookId; review.Description = reviewDto.Text; review.Grade = reviewDto.Grade; review.WorkName = book.Work.Name; if (book.IsCompanyTask) { review.Sender = book.Company.Account; } else { review.Sender = book.Customer.Person.Account; } if (reviewDto.PerformerType == "vendor") { review.To = book.Vendor.Person.Name; review.ToAccountId = book.Vendor.Person.Account.Id; } else { review.To = book.Company.Name; review.ToAccountId = book.Company.Account.Id; } review.Date = DateTime.Now; _unitOfWork.ReviewRepository.Create(review); } book.Status = BookStatus.Confirmed; _unitOfWork.BookRepository.Update(book); var rating = new Rating(); rating.Book = book; rating.Grade = reviewDto.Grade; if (book.IsCompanyTask) { rating.Sender = book.Company.Account; } else { rating.Sender = book.Customer.Person.Account; } rating.Reciever = (reviewDto.PerformerType == "vendor" ? book.Vendor.Person.Account : book.Company.Account); _unitOfWork.RatingRepository.Create(rating); await _unitOfWork.SaveAsync(); string senderName; if (book.IsCompanyTask) { senderName = book.Company.Name; } else { senderName = $"{book.Customer.Person.Name} {book.Customer.Person.Surname}"; } var notification = new NotificationDTO() { Title = $"New review", Description = $"{senderName} send review for your work {book.Work.Name}.", SourceItemId = book.Id, Time = DateTime.Now, Type = NotificationType.TaskNotification }; var receiverId = rating.Reciever.Id; await _notificationService.CreateAsync(receiverId, notification); }