public ResponseData <object> AddLikeCount([FromBody] LikePostData postData) { ResponseData <object> responseData; if (SessionHelper.IsExist(postData.Guid)) { // 文章点赞数增加 ArticleTb article = db.ArticleTb.Where(a => a.articleId == postData.ArticleId).FirstOrDefault(); article.likeCount++; // 查询记录是否存在 LikeTb oldLikeItem = db.LikeTb .Where(l => l.articleId == postData.ArticleId && l.userId == postData.UserId) .FirstOrDefault(); if (oldLikeItem == null) { // 添加到点赞列表 LikeTb likeItem = new LikeTb() { userId = postData.UserId, articleId = postData.ArticleId }; try { db.LikeTb.Add(likeItem); db.Entry(article).State = System.Data.Entity.EntityState.Modified; if (db.SaveChanges() > 0) { responseData = ResponseHelper <object> .SendSuccessResponse(new List <object> { article.likeCount }); } else { responseData = ResponseHelper <object> .SendErrorResponse("点赞失败"); } } catch (Exception ex) { responseData = ResponseHelper <object> .SendErrorResponse(ex.Message); } } else { responseData = ResponseHelper <object> .SendErrorResponse("已经点过一次赞了"); } } else { responseData = ResponseHelper <object> .SendErrorResponse("未登录", Models.StatusCode.OPERATION_ERROR); } return(responseData); }
public ResponseData <object> ShowArticleById(int articleId) { ResponseData <object> responseData; try { ArticleTb newArticle = db.ArticleTb.Where(a => a.isDel == false && a.articleId == articleId).FirstOrDefault(); if (newArticle != null) { newArticle.viewCount++; db.Entry(newArticle).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } var article = (from a in db.ArticleTb where a.isDel == false where a.articleId == articleId from u in db.RoleTb where u.roleId == a.authorId select new { a.articleId, a.title, a.content, a.likeCount, a.viewCount, a.publishTime, u.nickName }).First(); var commentIdList = from c in db.CommentTb where article.articleId == c.articleId select c.commentId; if (article != null) { List <object> res = new List <object>() { new { article, commentIdList } }; responseData = ResponseHelper <object> .SendSuccessResponse(res); } else { responseData = ResponseHelper <object> .SendErrorResponse("暂无文章数据"); } } catch (Exception ex) { responseData = ResponseHelper <object> .SendErrorResponse(ex.Message); } return(responseData); }
public ResponseData <object> DeleteLikeCount([FromBody] LikePostData postData) { ResponseData <object> responseData; if (SessionHelper.IsExist(postData.Guid)) { // 文章点赞数减少 ArticleTb article = db.ArticleTb.Where(a => a.articleId == postData.ArticleId).FirstOrDefault(); article.likeCount--; try { // 获取点赞列表 LikeTb likeItem = db.LikeTb .Where(l => l.articleId == postData.ArticleId && l.userId == postData.UserId) .FirstOrDefault(); if (likeItem != null) { db.LikeTb.Remove(likeItem); if (db.SaveChanges() > 0) { responseData = ResponseHelper <object> .SendSuccessResponse(new List <object> { article.likeCount }); } else { responseData = ResponseHelper <object> .SendErrorResponse("取消点赞失败"); } } else { responseData = ResponseHelper <object> .SendErrorResponse("未点赞,不能取消", Models.StatusCode.OPERATION_ERROR); } } catch (Exception ex) { responseData = ResponseHelper <object> .SendErrorResponse(ex.Message); } } else { responseData = ResponseHelper <object> .SendErrorResponse("未登录", Models.StatusCode.OPERATION_ERROR); } return(responseData); }
public ResponseData <object> UpdateAritcle([FromBody] ArticlePostData postData) { ResponseData <object> responseData; if (SessionHelper.IsExist(postData.Guid)) { ArticleTb article = db.ArticleTb .Where(a => a.isDel == false && a.articleId == postData.ArticleId) .First(); if (article != null) { article.title = postData.Title ?? article.title; article.content = postData.Content ?? article.content; try { db.Entry(article).State = System.Data.Entity.EntityState.Modified; if (db.SaveChanges() > 0) { List <object> res = new List <object>() { new { article.title, article.content } }; responseData = ResponseHelper <object> .SendSuccessResponse(res); } else { responseData = ResponseHelper <object> .SendErrorResponse("修改失败"); } } catch (Exception ex) { responseData = ResponseHelper <object> .SendErrorResponse(ex.Message); } } else { responseData = ResponseHelper <object> .SendErrorResponse("无此文章数据"); } } else { responseData = ResponseHelper <object> .SendErrorResponse("未登录", Models.StatusCode.OPERATION_ERROR); } return(responseData); }
public ResponseData <ArticleTb> PublishNewArticle([FromBody] ArticlePostData postData) { ResponseData <ArticleTb> responseData; if (SessionHelper.IsExist(postData.Guid)) { ArticleTb article = new ArticleTb() { title = postData.Title, content = postData.Content, publishTime = DateTime.Now, authorId = postData.AuthorId }; try { db.ArticleTb.Add(article); if (db.SaveChanges() > 0) { responseData = ResponseHelper <ArticleTb> .SendSuccessResponse(); } else { responseData = ResponseHelper <ArticleTb> .SendErrorResponse("发布失败"); } } catch (Exception ex) { responseData = ResponseHelper <ArticleTb> .SendErrorResponse(ex.Message); } } else { responseData = ResponseHelper <ArticleTb> .SendErrorResponse("未登录", Models.StatusCode.OPERATION_ERROR); } return(responseData); }