public PostDE CreatePost(PostDto postDto) { PostDE post = _mapper.Map <PostDE>(postDto); if (!_userRepository.UserExists(postDto.CreatedBy)) { throw new AppException("UserId " + postDto.CreatedBy + " does not exist"); } post.PostId = new Guid(); post.CreatedOn = DateTime.Now; post.isEdited = false; if (postDto.CourseFolderId.Count > 0) { CreatePostFolder(postDto.PostId, postDto.CourseFolderId); } _context.tbl_db_post.Add(post); _context.SaveChanges(); if (postDto.CourseFolderId != null) { CreatePostFolder(post.PostId, postDto.CourseFolderId); } return(post); }
public void UpdatePost(PostDto postDto) { PostDE post = _context.tbl_db_post.Where(x => x.PostId == postDto.PostId).FirstOrDefault(); var postFolder = _context.tbl_db_post_folder.Where(x => x.PostId == postDto.PostId).FirstOrDefault(); if (post == null) { throw new AppException("PostId does not exist"); } if (!string.IsNullOrEmpty(postDto.Title)) { post.Title = postDto.Title; } if (!string.IsNullOrEmpty(postDto.Description)) { post.Description = postDto.Description; } if (postFolder != null && postDto.CourseFolderId[0] != postFolder.CourseFolderId) { postFolder.CourseFolderId = postDto.CourseFolderId[0]; _context.tbl_db_post_folder.Update(postFolder); } post.isEdited = true; _context.tbl_db_post.Update(post); }
public void DeletePost(Guid postId) { PostDE post = _context.tbl_db_post.Where(x => x.PostId == postId).FirstOrDefault(); if (post == null) { throw new AppException("PostId does not exist"); } _context.tbl_db_reply.RemoveRange(GetReplies(post.PostId)); _context.tbl_db_post_folder.RemoveRange(GetPostFolders(post.PostId)); _context.tbl_db_post.Remove(post); _context.SaveChanges(); }