public static MemoryRepository<Comment> GetCommentsRepository() { var commentsRepository = new MemoryRepository<Comment>(); var users = GetUsersRepository(); var posts = GetPostsRepository(); for (int i = 0; i < 20; i++) { var user = users.GetById("id" + i % 10); var post = posts.GetById(i); var comment = new Comment { Id = i, CommentDate = new DateTime(2015, 11, i + 1), Content = "content" + i, PostId = post.Id, Post = post, UserId = user.Id, User = user, }; commentsRepository.Add(comment); } return commentsRepository; }
public IHttpActionResult Create(int id, CommentProjection commentProjection) { if (!this.ExistsArticle(id)) { return this.BadRequest(string.Format("Article with id={0} does not exists!", id)); } if (string.IsNullOrEmpty(commentProjection.Content)) { return this.BadRequest("Invalid comment content!"); } var userId = this.GetUserId(); var comment = new Comment() { Content = commentProjection.Content, DateCreated = DateTime.Now, ArticleID = id, AuthorID = userId }; this.ForumSystemData.Comments.Add(comment); this.ForumSystemData.SaveChanges(); commentProjection.ID = comment.ID; commentProjection.ArticleId = id; commentProjection.AuthorName = this.GetUsername(); return this.Ok(commentProjection); }
public IHttpActionResult Create(int postId, CommentDataModel model) { var userID = this.User.Identity.GetUserId(); var comment = new Comment { PostId = postId, UserId = userID, Content = model.Content, CommentDate = DateTime.Now }; this.data.Comments.Add(comment); try { this.data.SaveChanges(); } catch { return this.BadRequest(); } return Ok(model); }
private IList<Comment> GenerateValidTestComments(int count) { List<Comment> comments = new List<Comment>(); for (int i = 0; i < count; i++) { var comment = new Comment { PostId = i, Content = "The Content #" + i, CommentDate = DateTime.Now, User = new User() }; comments.Add(comment); } return comments; }