public async Task <ActionResult> SubmitQuestion(SubmitMarketItemQuestionModel model) { if (!ModelState.IsValid) { return(RedirectToAction("MarketItem", new { marketItemId = model.MarketItemId })); } var request = new CreateMarketQuestionModel { MarketItemId = model.MarketItemId, Question = model.Question }; var result = await MarketplaceWriter.CreateQuestion(User.Identity.GetUserId(), request); if (!result.Success) { return(ViewMessage(new ViewMessageModel(ViewMessageType.Danger, Resources.Market.itemActionErrorMessageTitle, Resources.Market.itemQuestionPostErrorMessage))); } var user = await UserManager.FindByIdAsync(request.MarketItemUserId.ToString()); if (user != null) { var callbackUrl = Url.Action("MarketItem", "MarketPlace", new { marketItemId = model.MarketItemId }, protocol: Request.Url.Scheme); await SendEmailAsync(EmailTemplateType.MarketPlaceNewQuestion, model.MarketItemId, user.Email, user.Id, user.UserName, model.Question, callbackUrl); } return(RedirectToAction("MarketItem", new { marketItemId = model.MarketItemId })); }
public async Task <IWriterResult> CreateQuestion(string userId, CreateMarketQuestionModel model) { try { var currentUser = new Guid(userId); using (var context = ExchangeDataContextFactory.CreateContext()) { var user = await context.Users.FirstOrDefaultNoLockAsync(x => x.Id == currentUser); if (user == null) { return(new WriterResult(false, "Unauthorized")); } var marketItem = await context.MarketItem .Include(m => m.Questions) .Where(x => x.Id == model.MarketItemId) .FirstOrDefaultNoLockAsync().ConfigureAwait(false); if (marketItem == null) { return(new WriterResult(false, $"Market item #{model.MarketItemId} not found.")); } marketItem.Questions.Add(new Entity.MarketItemQuestion { Answer = "", Question = model.Question, Timestamp = DateTime.UtcNow, UserId = currentUser }); await context.SaveChangesAsync().ConfigureAwait(false); model.MarketItemUserId = marketItem.UserId; return(new WriterResult(true)); } } catch (Exception) { throw; } }