public void SavePost(PostDto postDto, List <PostCategoryDto> postsCategoriesDto) { var post = _mapper.Map <Posts>(postDto); post.PostDate = DateTime.Now; var postCategories = _mapper.Map <IEnumerable <PostsCategories> >(postsCategoriesDto); _context.AddRange(postCategories); _context.Add(post); _context.SaveChanges(); }
public void SavePost(PostDto postDto) { var postsCategoriesDto = new List <PostCategoryDto>(); postDto.Id = Guid.NewGuid(); foreach (var categoryDto in postDto.Categories) { postsCategoriesDto.Add(new PostCategoryDto { Id = Guid.NewGuid(), CategoryId = categoryDto.Id, PostId = postDto.Id }); } var post = _mapper.Map <Posts>(postDto); post.PostDate = DateTime.Now; var postCategories = _mapper.Map <IEnumerable <PostsCategories> >(postsCategoriesDto); _context.AddRange(postCategories); _context.Add(post); _context.SaveChanges(); }