public async Task <IFormResult <Post> > UpdateItem(Post item) { var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); var existingPost = await _dbContext.Posts.AsNoTracking().FirstOrDefaultAsync(p => p.Id == item.Id); item.BlogId = item.Blog.Id; item.Blog = null; item.CategoryId = item.Category.Id; item.Category = null; item.CreatedBy = user.Id; item.CreatedOn = DateTime.Now; item.Status = existingPost.Status; var entity = _blogMapper.Map <Models.Post>(item); _dbContext.UpdateGraph(entity, mapping => mapping.AssociatedCollection(p => p.Tags)); await _dbContext.SaveChangesAsync(); var post = await GetItem(item.Id.ToString()); var result = new FormResult <Post>(post) { IsSucceeded = true, SuccessMessage = $"Post has been updated" }; return(await Task.FromResult(result)); }
public async Task <IFormResult <Post> > CreateItem(Post item) { var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); item.Id = Guid.NewGuid(); item.BlogId = item.Blog.Id; item.Blog = null; item.CategoryId = item.Category.Id; item.Category = null; item.CreatedBy = user.Id; item.CreatedOn = DateTime.Now; item.Status = PostStatus.Draft.ToString(); //var dbTags = await _dbContext.Tags.ToDictionaryAsync(t => t.Id, t => t); //var tags = item.Tags as List<Tag>; //for (int i = 0; i < tags.Count; i++) //{ // tags[i] = _blogMapper.Map<Tag>(dbTags[tags[i].Id]); //} var entity = _blogMapper.Map <Models.Post>(item); var dbTags = await _dbContext.Tags.ToDictionaryAsync(t => t.Id, t => t); var tags = entity.Tags as List <Models.Tag>; for (int i = 0; i < tags.Count; i++) { tags[i] = dbTags[tags[i].Id]; } var queryableData = await _dbContext.Posts.AddAsync(entity); await _dbContext.SaveChangesAsync(); if (queryableData.Entity == null) { return new FormResult <Post>() { IsSucceeded = false, ErrorMessage = $"Unable to create the Post" } } ; var newPost = await GetItem(item.Id.ToString()); var result = new FormResult <Post>(newPost) { IsSucceeded = true, SuccessMessage = $"Post has been created" }; return(await Task.FromResult(result)); }
public async Task <IFormResult <Post> > PublishPost(Post post) { var existingPost = await _dbContext.Posts.AsNoTracking().FirstOrDefaultAsync(p => p.Id == post.Id); existingPost.Status = PostStatus.Published.ToString(); var result = new FormResult <Post>(_blogMapper.Map <Post>(existingPost)) { IsSucceeded = true, SuccessMessage = $"Post has been published" }; return(await Task.FromResult(result)); }