protected void Page_Load(object sender, EventArgs e) { // chưa đăng nhập thì không được vào đây if (Session.Contents[Constant.USER_SESSION] == null) { return; } string IDProduct = Request.Form.Get("idProduct"); string contentComment = Request.Form.Get("comment"); string levelComment = Request.Form.Get("level"); if ("".Equals(IDProduct) || "".Equals(contentComment) || IDProduct == null || contentComment == null) { return; } UserEntity user = (UserEntity)Session.Contents[Constant.USER_SESSION]; // tránh trường hợp user này đang đăng nhập thì bị xóa khỏi hệ thống -> ở btl này có thể ko đụng đến if (userRepository.FindUserByID(user.ID) == null) { return; } // tránh trường hợp client cố tình thay đổi id trong javascript if (productRepository.FindProductByID(Int32.Parse(IDProduct)) == null) { return; } SimpleComment simpleComment = new SimpleComment(); simpleComment.Content = contentComment; simpleComment.TimeComment = DateTime.Now; simpleComment.IDUser = user.ID; string role = user.Role == Constant.ROLE_ADMIN ? "admin" : "user"; // nếu là comment level1 thì thêm mới thôi if (Constant.LEVEL_COMMENT_1.Equals(levelComment)) { CommentLevel1Entity commentMain = new CommentLevel1Entity(); commentMain.CommentMain = simpleComment; commentRepository.SaveCommentLevel1(ref commentMain, Int32.Parse(IDProduct)); Response.Write(user.Fullname + "|" + role + "|" + commentMain.IDCommentMain); } else if (Constant.LEVEL_COMMENT_2.Equals(levelComment)) { int idCmt = Int32.Parse(Request.Form.Get("idCmt")); commentRepository.SaveCommentLevel2(simpleComment, Int32.Parse(IDProduct), idCmt); Response.Write(user.Fullname + "|" + role); // commentRepository.SaveCommentLevel2(simpleComment, Int32.Parse(IDProduct)); } }
public void SaveCommentLevel1(ref CommentLevel1Entity commentLevel1, int IDProduct) { List <CommentOfAPost> listCommentOfAllProduct = FindAllComments(); // nếu file rỗng thì tạo comment cho bài viết luôn if (listCommentOfAllProduct == null) { listCommentOfAllProduct = new List <CommentOfAPost>(); CommentOfAPost commentOfAPost = new CommentOfAPost(); commentOfAPost.IDProduct = IDProduct; // tạo id cho comment đầu tiên của bài viết commentLevel1.IDCommentMain = 1; commentOfAPost.ListComment = new List <CommentLevel1Entity>(); commentOfAPost.ListComment.Add(commentLevel1); listCommentOfAllProduct.Add(commentOfAPost); file.WriteFileJson(Constant.DATA_COMMENT, JsonConvert.SerializeObject(listCommentOfAllProduct)); return; } else { // file json đã có giá trị, đi kiểm tra xem post đã có cmt chưa foreach (CommentOfAPost commentOfAPost in listCommentOfAllProduct) { // nếu mà có rồi thì thêm vào thôi if (commentOfAPost.IDProduct == IDProduct) { commentLevel1.IDCommentMain = commentOfAPost.ListComment[commentOfAPost.ListComment.Count - 1].IDCommentMain + 1; commentOfAPost.ListComment.Add(commentLevel1); listCommentOfAllProduct.Add(commentOfAPost); file.WriteFileJson(Constant.DATA_COMMENT, JsonConvert.SerializeObject(listCommentOfAllProduct)); return; } } // nếu mà post chưa có bình luận commentLevel1.IDCommentMain = 1; CommentOfAPost comment = new CommentOfAPost(); comment.IDProduct = IDProduct; comment.ListComment = new List <CommentLevel1Entity>(); comment.ListComment.Add(commentLevel1); listCommentOfAllProduct.Add(comment); file.WriteFileJson(Constant.DATA_COMMENT, JsonConvert.SerializeObject(listCommentOfAllProduct)); return; } }