/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <IRequestResult <Story> > Handle(EditStoryCommand request, CancellationToken cancellationToken) { var story = await context.Stories .AsTracking() .Include(entity => entity.Author) .SingleAsync(entity => entity.Slug == request.Slug, cancellationToken); /*var author = await context.Authors * .Where(user => user.Id == 1) * .FirstOrDefaultAsync(cancellationToken);*/ var name = request.User.Identity.Name; if (story.Author.UserName != name) { return(RequestResult.Error <Story>(new ArgumentException("", nameof(name)))); } if (story.Title != request.Title) { story.Title = request.Title; story.Slug = slugGenerator.CreateFrom(request.Title); } story.Content = request.Content; story.Modified = dateTimeProvider.Now; await context.SaveChangesAsync(cancellationToken); var temp = mapper.Map <Story>(story); return(RequestResult.Success(temp)); }
public async Task <IRequestResult <Story> > Handle(CreateStoryCommand request, CancellationToken cancellationToken) { //var name = request.User.Identity.Name; var author = await context.Authors .Where(user => user.Id == 1) .FirstOrDefaultAsync(cancellationToken); var slug = slugGenerator.CreateFrom(request.Title); var story = new Persistence.Models.Story { Title = request.Title, Slug = slug, Content = request.Content, Status = StoryStatus.Draft, IsPublic = request.IsPublic, Created = dateTimeProvider.Now, Author = author }; await context.Stories.AddAsync(story, cancellationToken); await context.SaveChangesAsync(cancellationToken); return(RequestResult.Success(mapper.Map <Story>(story))); }