public ActionResult Create(PostViewModel postViewModel, int id) { var id_doctor = Convert.ToInt32(Session["Id"]); // Add post Post post = new Post() { PostName = postViewModel.PostName, PostContent = postViewModel.PostContent, CreateDate = DateTime.Now, StatusBlock = postViewModel.StatusBlock, Doctor_ID = id_doctor, Topic_ID = id, }; db.Posts.Add(post); // Add likePost LikePost likePosts = new LikePost() { StatusLike = false, Post_ID = post.Id, Doctor_ID = id_doctor }; db.LikePosts.Add(likePosts); db.SaveChanges(); return(RedirectToAction("DetailPost/" + post.Id, "Post")); }
private int SaveLike(int newstId, int commentId) { var news = _db.News.Where(x => x.Id == newstId).FirstOrDefault(); var comment = news.Comments.Where(x => x.CommentId == commentId).FirstOrDefault(); LikePost liked = null; if (comment != null) { liked = new LikePost { CommentId = comment.CommentId, NewsId = news.Id, Like = true, User = HttpContext.Current.User.Identity.GetUserId() }; } var like = comment.PostLikes.FirstOrDefault(e => e.User == liked.User); if (like == null) { comment.PostLikes.Add(liked); } else { like.Like = !like.Like; } comment.LikeCount = comment.PostLikes.Count(e => e.Like); _db.SaveChanges(); return(comment.PostLikes.Count(e => e.Like)); }
public void SwitchLikePost(int userId, int postId) { UserModel user = _appContext.UserModels.FirstOrDefault(u => u.Id == userId); PostModel targetPost = _appContext.PostModels.FirstOrDefault(p => p.Id == postId); if (user != null && targetPost != null) { if (_appContext.LikesPosts.FirstOrDefault(lp => lp.RatingPersonId == user.Id && lp.PostId == targetPost.Id) == null) { LikePost sub = new LikePost() { RatingPerson = user, RatingPersonId = userId, PostModel = targetPost, PostId = postId }; _appContext.LikesPosts.Add(sub); _appContext.PostModels.FirstOrDefault(u => u.Id == postId).Rating = targetPost.Rating + 1; _appContext.SaveChanges(); } else { _appContext.LikesPosts.Remove(_appContext.LikesPosts.FirstOrDefault(lp => lp.PostId == postId && lp.RatingPersonId == userId)); _appContext.PostModels.FirstOrDefault(u => u.Id == postId).Rating = targetPost.Rating - 1; _appContext.SaveChanges(); } } }
public IActionResult LikePost(LikePost likePost) { string queryString = $"SELECT * FROM polubienie_obiekt WHERE nazwa_uzytkownika = '{likePost.Username}' AND obiekt_id = '{likePost.Id}';"; using (NpgsqlConnection con = new NpgsqlConnection(_connectionString)) { NpgsqlCommand cmd = new NpgsqlCommand(queryString); cmd.Connection = con; con.Open(); NpgsqlDataReader reader = cmd.ExecuteReader(); if (!reader.HasRows) { queryString = $"INSERT INTO polubienie_obiekt(nazwa_uzytkownika, obiekt_id) VALUES ('{likePost.Username}', '{likePost.Id}');"; } else { queryString = $"DELETE FROM polubienie_obiekt WHERE nazwa_uzytkownika = '{likePost.Username}' AND obiekt_id = '{likePost.Id}';"; } } using (NpgsqlConnection con = new NpgsqlConnection(_connectionString)) { NpgsqlCommand cmd = new NpgsqlCommand(queryString); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); } return(Ok()); }
public void AddLikePost(LikePost likepost) { Post post1 = _context.Posts.FirstOrDefault(x => x.ID == likepost.PostID); _context.LikePosts.Add(likepost); post1.LikePosts.Add(likepost); _context.SaveChanges(); }
public IHttpActionResult Like([FromBody] LikePost likeBody) { try { voteService = new VoteService(); return(Ok(voteService.Like(likeBody.tid))); } catch (Exception ex) { log.Error(ex); return(InternalServerError(ex)); } }
//+++ public IResult Add(LikePostCreateDto likePost) { var lpost = new LikePost { PostId = likePost.PostId, UserId = likePost.UserId, LikeStatus = likePost.LikeStatus }; _likePostDal.Add(lpost); return(new SuccessResult(Messages.LikePostAdded)); }
public bool LikePost(LikePost postToLike) { var entity = new LikePost() { OwnerId = _userId, CreatedUtc = DateTimeOffset.Now }; using (var ctx = new ApplicationDbContext()) { ctx.LikePost.Add(entity); return(ctx.SaveChanges() == 1); } }
public IHttpActionResult Post(LikePost postToLike) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateLikePostService(); if (!service.LikePost(postToLike)) { return(InternalServerError()); } return(Ok()); }
public object AddLikePost(LikePost lp) { if (null == lp) { throw new UserException("点赞帖子数据为空"); } string sql = "usp_addlikepost"; SqlParameter[] spm = SqlHelper.GetSqpParameters(new string[] { "@postid", "@userlikeid", "@looked", "@dateline", "@useroperationid" }, new object[] { lp.PostId, lp.UserLikeId, lp.Looked, lp.DateLine, DBNull.Value }, new SqlDbType[] { SqlDbType.Int, SqlDbType.Int, SqlDbType.Int, SqlDbType.DateTime, SqlDbType.Int, }); spm[4].Direction = ParameterDirection.Output; SqlHelper.ExecuteNonquery(sql, CommandType.StoredProcedure, spm); return(spm[4].Value); }
public IActionResult Like() { long id_user = context.User.FirstOrDefault(u => u.login == HttpContext.Session.GetString("Username")).id; long id_post = Convert.ToInt64(Request.Headers["id_post"]); if (context.LikePost.FirstOrDefault(l => (l.id_user == id_user && l.id_post == id_post)) != null) { context.LikePost.Remove(context.LikePost.FirstOrDefault(l => (l.id_user == id_user && l.id_post == id_post))); } else { LikePost like = new LikePost(id_user, id_post, true); context.LikePost.Add(like); } context.SaveChanges(); return(Ok()); }
public IHttpActionResult LikePosts(int postId) { var userId = User.Identity.GetUserId(); var post = this.Data.Posts.All().FirstOrDefault(p => p.Id == postId); if (post.Likes.Any(l => l.OwnerId == userId)) { return(BadRequest("Cannot like post more than one times")); } var like = new LikePost { OwnerId = userId }; post.Likes.Add(like); this.Data.Posts.SaveChanges(); return(Ok(post.Likes.Count())); }
public void Like(int userid, int postid) { Post post = _context.Posts.Find(postid); LikePost like = _context.LikePosts.Where(x => x.UserId == userid && x.PostId == postid).FirstOrDefault(); if (like == null) { LikePost like1 = new LikePost(); like1.UserId = userid; like1.PostId = postid; post.LikeCount += 1; _context.LikePosts.Add(like1); } else { _context.LikePosts.Remove(like); post.LikeCount -= 1; _context.SaveChanges(); } _context.SaveChanges(); }
public int DislikeMessage(int castMessageId, int userId) { var castMessage = _unitOfWork.Repository <StickMan_Users_Cast_AudioData_UploadInformation>().GetSingle(m => m.Id == castMessageId); var like = _unitOfWork.Repository <LikePost>().GetQuery(l => l.PostID == castMessageId && l.UserID == userId).FirstOrDefault(); if (like == null) { var newLike = new LikePost { PostID = castMessage.Id, UserID = userId, IsLike = false }; _unitOfWork.Repository <LikePost>().Insert(newLike); _unitOfWork.Save(); } else { like.IsLike = false; _unitOfWork.Repository <LikePost>().Update(like); _unitOfWork.Save(); } var likes = _unitOfWork.Repository <LikePost>().GetQuery(m => m.PostID == castMessageId).ToList(); var percentage = ((double)likes.Count(l => !l.IsLike) / likes.Count()) * 100; if (percentage >= 70) { RecurringJob.AddOrUpdate("delete-post", () => DeletePost(castMessageId), Cron.Daily); } else { RecurringJob.RemoveIfExists("delete-post"); } return(castMessage.LikePosts.Where(l => !l.IsLike).Count()); }
//依赖注入 public void AddLikePost(int postid, int userid, string username) { if (string.IsNullOrEmpty(username)) { throw new UserException("用户名为空"); } LikePost lp = new LikePost(); lp.PostId = postid; lp.UserLikeId = userid; lp.DateLine = DateTime.Now; lp.Looked = UserLooked.UnLooked; object obj = DbSession.LikePostDal.AddLikePost(lp); if (null == obj) { throw new UserException("添加点赞帖子数据失败"); } int useroperationid = Convert.ToInt32(obj); //记录好友操作 AddFriendOperation.Add(DbSession, userid, useroperationid); }
public ActionResult DetailPost(PostViewModel postView, CommentViewModel commentViewModel, string btnlikePost) { var idSess = Convert.ToInt32(Session["Id"]); // Add like and unlike if (!string.IsNullOrEmpty(btnlikePost)) { var likePost = db.LikePosts.SingleOrDefault(x => x.Post_ID == postView.Id && x.Doctor_ID == idSess); if (likePost != null) { if (idSess == likePost.Doctor_ID) { if (likePost.StatusLike == false) { likePost.StatusLike = true; } else { likePost.StatusLike = false; } } } else { LikePost like = new LikePost() { StatusLike = true, Post_ID = postView.Id, Doctor_ID = idSess, }; db.LikePosts.Add(like); } } db.SaveChanges(); return(RedirectToAction("DetailPost/" + postView.Id)); }
//like a post public ActionResult LikeUserPost(int id) { if (Session["email"] != null) { using (BitBookContext db = new BitBookContext()) { string userEmail = ""; userEmail = Session["email"].ToString(); var user = db.Users.FirstOrDefault(x => x.Email.Equals(userEmail)); var likeStatus = db.LikePosts.FirstOrDefault(a => a.UserId.Equals(user.Id) && a.PostId.Equals(id)); if (likeStatus == null) { LikePost aLikePost = new LikePost(); aLikePost.PostId = id; aLikePost.UserId = user.Id; aLikePost.CurrretTime = DateTime.Now; db.LikePosts.Add(aLikePost); db.SaveChanges(); return(RedirectToAction("Home", "Registration")); } else { Response.Write("<script> alert('You already like ')</script>"); } } return(RedirectToAction("Home", "Registration")); } else { return(RedirectToAction("Login", "Registration")); } }
public void Post([FromBody] LikePost value) { _ils.AddLikePost(value); }
private void btnLuu_Click(object sender, EventArgs e) { // cập nhật thông tin bài viết tblPost p = new tblPost(); p.IDPost = idpost; p.TimePost = DateTime.Parse(timepost); p.CountComment = tongsocomment; p.CountLike = int.Parse(tongsoluonglike); if (danhgia == "Tích cực") { p.Status = "Tích cực"; } else { p.Status = "Tiêu cực"; } Post.Sua(p); // kết thúc cập nhật thông tin bài viết //thêm tài khoản người dùng for (int i = 0; i < lsIDTichCuc.Count; i++) { tblUserFB newu = new tblUserFB(); tblLikePost newlp = new tblLikePost(); newu.IDUser = lsIDTichCuc[i].ToString(); newu.Name = lsNameTichCuc[i].ToString(); UserFB.Them(newu); newlp.IDPost = idpost; newlp.IDUserFB = lsIDTichCuc[i].ToString(); newlp.Status = 1; LikePost.Them(newlp); } for (int i = 0; i < lsIDTieuCuc.Count; i++) { tblUserFB newu = new tblUserFB(); tblLikePost newlp = new tblLikePost(); newu.IDUser = lsIDTieuCuc[i].ToString(); newu.Name = lsNameTieuCuc[i].ToString(); UserFB.Them(newu); newlp.IDPost = idpost; newlp.IDUserFB = lsIDTieuCuc[i].ToString(); newlp.Status = -1; LikePost.Them(newlp); } //thêm từ những người bình luận for (int i = 0; i < LsNguoiDungBL.Items.Count; i++) { tblUserFB newu = new tblUserFB(); newu.IDUser = lsIDUserBinhLuan[i]; newu.Name = LsNguoiDungBL.Items[i].Text; UserFB.Them(newu); //lưu người dùng bình luận vào sql tblCommentPost newcommentpost = new tblCommentPost(); newcommentpost.IDPost = idpost; newcommentpost.IDComment = lsIDBinhLuan[i].ToString(); newcommentpost.IDUser = lsIDUserBinhLuan[i]; newcommentpost.Description = lsNoiDungBL[i]; newcommentpost.Status = 0; CommentPost.Them(newcommentpost); //kết thúc lưu người dùng bình luận } //kết thúc thêm tài khoản MessageBox.Show("Đã lưu tất cả thông tin!"); }
public int LikePost(int postID, LikePost Like) { //using (var ctx = new ApplicationDbContext()) //Other stuff return(1); }