public async Task <HttpResponseMessage> Order(BookOrderDTO book) { try { await _bookService.Create(book); } catch { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public async Task Create(BookOrderDTO book) { Work work = await _unitOfWork.WorkRepository.GetByIdAsync(book.WorkId); Customer customer = await _unitOfWork.CustomerRepository.GetByIdAsync(book.CustomerId); Location location = null; work.Orders = work.Orders + 1; _unitOfWork.WorkRepository.Update(work); if (book.Location.Id == -1) { location = new Location() { IsDeleted = false, City = book.Location.City, Adress = book.Location.Adress, Latitude = book.Location.Latitude, Longitude = book.Location.Longitude, PostIndex = book.Location.PostIndex }; } else { location = await _unitOfWork.LocationRepository.GetByIdAsync(book.Location.Id); } Company company = null; Vendor vendor = null; if (book.Profile.ToLower() == "company") { company = await _unitOfWork.CompanyRepository.GetByIdAsync(book.ProfileId); } else { vendor = await _unitOfWork.VendorRepository.Query .Include(v => v.Person) .Include(v => v.Person.Account) .SingleAsync(v => v.Id == book.ProfileId); } Book _book = new Book() { IsDeleted = false, Company = company, Customer = customer, CustomerPhone = book.CustomerPhone, Date = book.Date, EndDate = book.EndDate, Description = book.Description, Location = location, Status = BookStatus.Pending, Vendor = vendor, Work = work }; _unitOfWork.BookRepository.Create(_book); await _unitOfWork.SaveAsync(); /* Send Notification */ var notification = new NotificationDTO() { Title = $"New order for {_book.Work.Name}", Description = $"{_book.Customer.Person.Name} {_book.Customer.Person.Surname} booked {_book.Work?.Name}. Check your dashboard to find out details.", SourceItemId = _book.Id, Time = DateTime.Now, Type = NotificationType.TaskNotification }; var receiverId = vendor != null ? vendor.Person.Account.Id : company.Account.Id; await _notificationService.CreateAsync(receiverId, notification); /* Send Message */ string msg = EmailTemplate.NewOrderTemplate(_book.Customer.Person.Name, _book.Customer.Person.Surname, _book.Work?.Name, book.CustomerId); string receiverEmail = vendor != null ? vendor.Person.Account.Email : company.Account.Email; _mailService.Send(new EmailMessage { ReceiverEmail = receiverEmail, Subject = "You have a new order", Body = msg, IsHtml = true }); }