public async Task <int> SavePostAsync(AuthorPostViewModel model, CancellationToken cancellationToken) { using (var operation = _telemetry.StartOperation <RequestTelemetry>("SavePostAsync")) { _telemetry.TrackTrace("Checking what to do with id " + model.Id); if (_context.EpisodeEntry.Any(x => x.Id == model.Id)) { try { var entity = await _context.EpisodeEntry.Include(e => e.EpisodeEntryPlayer) .Include(e => e.EpisodeEntryStatus).Include(e => e.EpisodeEntryType) .FirstOrDefaultAsync(e => e.Id == model.Id, cancellationToken: cancellationToken); _mapper.Map(model, entity); //entity.UpdatedAt = DateTime.UtcNow; HandleSubmit(entity, model); await _context.SaveChangesAsync(cancellationToken); } catch (Exception ex) { _telemetry.TrackException(ex); } } else { var pId = _userService.GetId(); if (model.Authors.All(x => x.Id != pId)) { model.Authors.Add(new AuthorViewModel(pId, _userService.GetName(), await _userService.GetEmailAsync(cancellationToken))); } var entity = _mapper.Map <EpisodeEntry>(model); // entity.CreatedAt = DateTime.UtcNow; // entity.UpdatedAt = DateTime.UtcNow; HandleSubmit(entity, model); if (entity.EpisodeEntryTypeId == 0) { entity.EpisodeEntryTypeId = _context.EpisodeEntryType.First(x => x.Name == StaticValues.Post).Id; } await _context.EpisodeEntry.AddAsync(entity, cancellationToken); await _context.SaveChangesAsync(cancellationToken); model.Id = entity.Id; } //TODO: Notify authors if this is draft update, or if status is changed. } return(model.Id); }
public async Task <IActionResult> ProcessPostData(AuthorPostViewModel model, CancellationToken token) { if (!ModelState.IsValid) { return(View("Post", model)); } var id = await _authoringService.SavePostAsync(model, token); TempData["Message"] = "Yay it saved"; return(RedirectToAction("Writing", "My")); }
public async Task <AuthorPostViewModel> GetPost(int id, CancellationToken cancellationToken) { var vm = new AuthorPostViewModel(); vm = await _context.EpisodeEntry.ProjectTo <AuthorPostViewModel>(_mapper.ConfigurationProvider).FirstOrDefaultAsync(x => x.Id == id, cancellationToken: cancellationToken); vm.Statuses = await _context.EpisodeEntryStatus.Select(x => new SelectListItem(x.Name, x.Id.ToString(), x.Name == StaticValues.Draft)).ToListAsync(cancellationToken: cancellationToken); vm.TypeId = _context.EpisodeEntryType.First(x => x.Name == StaticValues.Post).Id; vm.PostTypes = await _context.EpisodeEntryType.Select(x => new SelectListItem(x.Name, x.Id.ToString(), x.Id == vm.TypeId)).ToListAsync(cancellationToken: cancellationToken); if (vm.Authors.All(x => x.Name != _userService.GetName())) { vm.Authors.Add(new AuthorViewModel(_userService.GetId(), vm.Id, _userService.GetName(), await _userService.GetEmailAsync(cancellationToken))); } return(vm); }
private void HandleSubmit(EpisodeEntry entity, AuthorPostViewModel model) { var submitValue = model.submitpost; if (string.IsNullOrEmpty(submitValue)) { return; } if (submitValue.Equals("publish")) { entity.PublishedAt = DateTime.UtcNow; entity.EpisodeEntryStatusId = _context.EpisodeEntryStatus.First(x => x.Name == StaticValues.Published).Id; } if (submitValue.Equals("schedule")) { entity.PublishedAt = model.PostAt; entity.EpisodeEntryStatusId = _context.EpisodeEntryStatus.First(x => x.Name == StaticValues.Pending).Id; } }
public async Task <AuthorPostViewModel> NewPost(CancellationToken cancellationToken) { var vm = new AuthorPostViewModel(); // TODO: Find active episode var activeEpisodeId = await _context.Episode.Include(e => e.Status).CountAsync(e => e.Status.Name == StaticValues.Published || e.Status.Name == StaticValues.Archived); vm = new AuthorPostViewModel(activeEpisodeId); //vm.Authors.Add(_userService.GetId()); vm.Statuses = await _context.EpisodeEntryStatus.Select(x => new SelectListItem(x.Name, x.Id.ToString(), x.Name == StaticValues.Draft)).ToListAsync(cancellationToken: cancellationToken); vm.StatusId = int.Parse(vm.Statuses.First(x => x.Text == StaticValues.Draft).Value); vm.TypeId = _context.EpisodeEntryType.First(x => x.Name == StaticValues.Post).Id; vm.PostTypes = await _context.EpisodeEntryType.Select(x => new SelectListItem(x.Name, x.Id.ToString(), x.Id == vm.TypeId)).ToListAsync(cancellationToken: cancellationToken); if (vm.Authors.All(x => x.Name != _userService.GetName())) { vm.Authors.Add(new AuthorViewModel(_userService.GetId(), vm.Id, _userService.GetName(), await _userService.GetEmailAsync(cancellationToken))); } return(vm); }