/// <summary> /// 添加评论 /// </summary> /// <param name="comment"></param> /// <returns></returns> public ResponseModel AddComment(AddComment comment) { var news = _newsService.GetOneNews(comment.NewsId); if (news.code == 0) { return(new ResponseModel { code = 0, result = "新闻不存在" }); } var com = new NewsComment { NewsId = comment.NewsId, AddTime = DateTime.Now, Contents = comment.Contents }; _db.NewsComment.Add(com); int i = _db.SaveChanges(); if (i > 0) { return(new ResponseModel { code = 200, result = "新闻评论添加成功", data = new { contents = comment.Contents, floor = "#" + Convert.ToInt32(news.data.CommentCount) + 1, addTime = DateTime.Now } }); } { return(new ResponseModel { code = 0, result = "新闻评论添加失败" }); } }
/// <summary> /// 添加评论 /// </summary> /// <returns></returns> public ResponseModel AddComment(AddNewsComment comment) { var news = _newsService.GetOneNews(comment.NewsId); if (news == null) { return new ResponseModel { code = 0, result = "News不存在" } } ; var com = new NewsComment { NewsId = comment.NewsId, Contents = comment.Contents, AddTime = DateTime.Now }; _db.NewsComment.Add(com); int i = _db.SaveChanges(); if (i > 0) { return(new ResponseModel { code = 200, result = "NewsComment添加成功", ////第一种写法 //data = new NewsCommentModel //{ // Contents = comment.Contents, // Floor = "#" + news.data.CommentCount + 1,//楼层 // AddTime = DateTime.Now //} //第二种写法 data = new { contents = comment.Contents, floor = "#" + news.data.CommentCount + 1,//楼层 addTime = DateTime.Now } }); } return(new ResponseModel { code = 0, result = "NewsComment添加失败" }); }
/// <summary> /// Add comment /// </summary> /// <param name="comment"></param> /// <returns></returns> public ResponseModel AddComment(AddComment comment) { var news = _newsService.GetOneNews(comment.NewsId); if (news.code == 0) { return new ResponseModel { code = 0, result = "News does not existed." } } ; var com = new NewsComment { AddTime = DateTime.Now, NewsId = comment.NewsId, Contents = comment.Contents, }; _db.NewsComment.Add(com); int i = _db.SaveChanges(); if (i > 0) { return(new ResponseModel { code = 200, result = "News comment add succ", data = new { contents = comment.Contents, floor = "#" + (news.data.CommentCount + 1), addTime = DateTime.Now, } }); } return(new ResponseModel { code = 0, result = "News comment add fail" }); }
public ResponseModel AddComment(AddComment comment) { var news = _newsService.GetOneNews(comment.NewsId); if (news.code == 0) { return(new ResponseModel { code = 0, result = "News inexistence!" }); } var com = new NewsComment { AddTime = DateTime.Now.ToString(), NewsId = comment.NewsId, Contents = comment.Contents }; _db.NewsComment.Add(com); int i = _db.SaveChanges(); if (i > 0) { return(new ResponseModel { code = 200, result = "Comment add success!", data = new //便于前端调用 { contents = comment.Contents, floor = "#" + news.data.CommentCount + 1, addTime = DateTime.Now.ToString() } }); } return(new ResponseModel { code = 0, result = "Comment add failed!" }); }
/// <summary> /// 添加评论 /// </summary> public ResponseModel AddComment(AddComment comment) { var news = _newsService.GetOneNews(comment.NewsId); if (news.code == 0) { return new ResponseModel { code = 0, result = "新闻不存在" } } ; var com = new NewsComment { AddTime = DateTime.Now, NewsId = comment.NewsId, Contents = comment.Contents }; _db.NewsComment.Add(com); int i = _db.SaveChanges(); if (i > 0) { return(new ResponseModel { code = 200, result = "评论添加成功", data = new { contents = comment.Contents, floor = "#" + (news.data.CommentCount + 1), addTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") } }); } return(new ResponseModel { code = 0, result = "评论添加失败" }); }