public HttpResponseMessage EditComment(string sessionKey, int id, CommentModel comment) { User user = null; try { user = this.usersRepository.GetBySessionKey(sessionKey); } catch (Exception) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid user!"); } if (id != comment.ID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } Comment updatedComment = null; try { updatedComment = CommentsMapper.ToCommentEntity(comment, usersRepository, newsArticlesRepository); } catch (Exception) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid comment model provided!"); } this.commentsRepository.Update(id, updatedComment); return Request.CreateResponse(HttpStatusCode.OK); }
public HttpResponseMessage AddComment(string sessionKey, CommentModel comment) { User user = null; try { user = this.usersRepository.GetBySessionKey(sessionKey); } catch (Exception) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid user!"); } comment.Author = user.Nickname; Comment commentEntity = null; try { commentEntity = CommentsMapper.ToCommentEntity(comment, usersRepository, newsArticlesRepository); } catch (Exception) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid news article model provided!"); } comment.ID = commentEntity.ID; this.commentsRepository.Add(commentEntity); return Request.CreateResponse(HttpStatusCode.Created, comment); }
public static CommentModel ToCommentModel(Comment commentEntity) { CommentModel commentModel = new CommentModel() { ID = commentEntity.ID, Author = commentEntity.Author.Nickname, Content = commentEntity.Content, Date = commentEntity.Date }; return commentModel; }
public static Comment CreateOrLoadComment( CommentModel comment, IRepository<Comment> commentRepository, DbUsersRepository usersRepository) { Comment commentEntity = commentRepository.Get(comment.ID); if (commentEntity != null) { return commentEntity; } Comment newComment = CommentsMapper.ToCommentEntity(comment, usersRepository); return newComment; }
public static Comment ToCommentEntity( CommentModel commentModel, DbUsersRepository usersRepository, IRepository<NewsArticle> newsArticlesRepository) { Comment commentEntity = new Comment() { Content = commentModel.Content, Date = commentModel.Date, Author = usersRepository.GetByNickname(commentModel.Author) }; NewsArticle newsArticle = newsArticlesRepository.Get(commentModel.ArticleID); newsArticle.Comments.Add(commentEntity); return commentEntity; }
public static CommentModel ToCommentModel(Comment commentEntity) { CommentModel commentModel = new CommentModel() { ID = commentEntity.ID, Author = commentEntity.Author.Nickname, Content = commentEntity.Content, Date = commentEntity.Date }; foreach (Comment subComment in commentEntity.SubComments) { commentModel.SubComments.Add(new CommentDetails() { ID = subComment.ID, Author = subComment.Author.Nickname, Content = subComment.Content, Date = subComment.Date }); } return commentModel; }
public static Comment ToCommentEntity(CommentModel commentModel, DbUsersRepository usersRepository) { Comment commentEntity = new Comment() { ID = commentModel.ID, Content = commentModel.Content, Date = commentModel.Date, Author = usersRepository.GetByNickname(commentModel.Author) }; foreach (CommentDetails subComment in commentModel.SubComments) { commentEntity.SubComments.Add(new Comment() { ID = subComment.ID, Content = subComment.Content, Date = subComment.Date, Author = usersRepository.GetByNickname(subComment.Author) }); } return commentEntity; }