// GET employee's and questions for form to display to users to create new stories public async Task<ActionResult> CreateStory() { var questionTemplates = (await _proxy.GetQuestionTemplatesAsync()) .Where(x => x.IsActive) .ToList(); var employees = await DirectoryHelper.GetDirectoryEmployees(); ViewBag.Employees = employees.OrderBy(x => x.MenuName).ToList(); var employee = employees.FirstOrDefault(x => x.Id == GetUserId()); ViewBag.AddStory = true; var story = new EquityStoryContract { ContactName = employee.Name, UserName = employee.Id, ContactEmail = employee.Email, ContactPhone = employee.WorkPhone, Questions = questionTemplates.Select(x => new EquityQuestionContract { QuestionText = x.Question, AnswerText = "" }).ToList() }; return View("Story", story); }
public async Task<ActionResult> EditStory(EquityStoryContract story) { if (!ModelState.IsValid) { ViewBag.AddStory = false; return View("Story", story); } await _proxy.PutExistingStoryAsync(story); return RedirectToAction("Admin"); }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <returns></returns> /// <exception cref="Exception"></exception> public async Task PutStoryAsync(EquityStoryContract model) { var updatedStory = await Context.EquityStories.FindAsync(model.Id); if (updatedStory == null) { throw new CannotFindIdException("story", model.Id); } var editedStory = EquityMapper.EquityContractToEntity(model); Context.Entry(updatedStory).CurrentValues.SetValues(editedStory); Context.Entry(updatedStory).State = EntityState.Modified; if (await Context.SaveChangesAsync() <= 0) { throw new CannotSaveToDatabaseException("Story"); } }
// map entity to contract public static EquityStoryContract EquityStoryEntityToContract(EquityStory entity) { var contract = new EquityStoryContract { Id = entity.Id, Title = entity.Title, Description = entity.Description, IsActive = entity.IsActive, PublishDate = entity.PublishDate, ContactName = entity.ContactName, ContactEmail = entity.ContactEmail, ContactPhone = entity.ContactPhone, UserName = entity.UserName, ImgThumb = entity.ImgThumb, Questions = JsonConvert.DeserializeObject <List <EquityQuestionContract> >(entity.Questions), CreatedBy = entity.CreatedBy, ArticleContent = entity.ArticleContent }; return(contract); }
public async Task<ActionResult> CreateStory(EquityStoryContract story) { if(!ModelState.IsValid) { var questionTemplates = (await _proxy.GetQuestionTemplatesAsync()) .Where(x => x.IsActive) .ToList(); var employees = await DirectoryHelper.GetDirectoryEmployees(); ViewBag.Employees = employees.OrderBy(x => x.MenuName).ToList(); var employee = employees.FirstOrDefault(x => x.Id == GetUserId()); // COMMENT: This ViewBag.AddStory = true is twice (see below model) and I think it should be false anyway ViewBag.AddStory = true; return View("Story", story); } story.IsActive = false; var newStoryId = await _proxy.PostNewStoryAsync(story); if (newStoryId > 0) { var emailService = new EmailService(Config.ApplicationMode); var urlScheme = Request.Url != null ? Request.Url.Scheme : "https"; var urlHost = Request.Url != null ? Request.Url.Host : Config.ApplicationRootUri; var postUrl = Url.Action("View", "EquityStories", new RouteValueDictionary(new { id = newStoryId }), urlScheme, urlHost); var message = new IdentityMessage { Subject = "Equity Success stories - New Story", Body = $"<div>Equity Story {story.Title} has been submitted by {story.ContactName}.To view and edit this post <a href='{postUrl}'>click here.</a></div>", Destination = Config.EquityStoriesAdminEmail }; await emailService.SendAsync(message); return RedirectToAction("Success", new { id = newStoryId }); } return View("Story", story); }
/// <summary> /// post new equity story /// </summary> /// /// <param EquityContract="model"></param> /// <returns></returns> public async Task <int> PostStoryAsync(EquityStoryContract model) { if (Context.EquityStories.Find(model.Id) != null) { throw new EntityConflictException("story", model.Id); } var newStory = EquityMapper.EquityContractToEntity(model); Context.EquityStories.Add(newStory); Context.Entry(newStory).State = EntityState.Added; var saved = await Context.SaveChangesAsync(); if (saved <= 0) { throw new CannotSaveToDatabaseException("story"); } var storyId = newStory.Id; return(storyId); }
// map contract to entity public static EquityStory EquityContractToEntity(EquityStoryContract contract) { var entity = new EquityStory { Id = contract.Id, Title = contract.Title, Description = contract.Description, IsActive = contract.IsActive, PublishDate = contract.PublishDate, ContactName = contract.ContactName, ContactEmail = contract.ContactEmail, ContactPhone = contract.ContactPhone, UserName = contract.UserName, ImgThumb = contract.ImgThumb, Questions = JsonConvert.SerializeObject(contract.Questions), CreatedBy = contract.ContactName, CreatedOn = DateTime.Now, ArticleContent = contract.ArticleContent }; return(entity); }
/// <summary> /// puts edited equity story /// </summary> /// /// <param EquityContract="model"></param> /// <returns></returns> public async Task PutExistingStoryAsync(EquityStoryContract model) { await _client.PutAsync($"{EquityStoriesDiscoveryRoute}", model); }
/// <summary> /// post new equity story /// </summary> /// /// <param EquityContract="model"></param> /// <returns></returns> public async Task <int> PostNewStoryAsync(EquityStoryContract model) { var response = await _client.PostWithResultAsync <int>(EquityStoriesDiscoveryRoute, model); return(response); }
public async Task <IHttpActionResult> PostNewStoryAsync([FromBody] EquityStoryContract model) { var storyId = await _repository.PostStoryAsync(model); return(CreatedAtRoute("StoryById", new { id = storyId }, storyId)); }
public async Task <IHttpActionResult> PutExistingStoryAsync([FromBody] EquityStoryContract model) { await _repository.PutStoryAsync(model); return(new NoContentResponse()); }