コード例 #1
0
        public ActionResult Index(string option, string username)
        {
            _viewModel = new HeaderViewModel();

            if (option != null)
            {
                switch (option.ToLower())
                {
                case "newsfeed":
                {
                    CreateLayoutView("Trang cá nhân");
                    break;
                }

                case "about":
                {
                    CreateLayoutView("Thông tin");
                    break;
                }

                case "friend":
                {
                    CreateLayoutView("Bạn bè");
                    break;
                }

                default:
                {
                    CreateLayoutView("Trang cá nhân");
                    break;
                }
                }
            }
            else
            {
                CreateLayoutView("Trang cá nhân");
            }
            if (username == null || username == "false")
            {
                username = _viewModel.User.UserName;
            }
            var user = _service.GetUserByUserName(username);

            FieldHelper.CopyNotNullValue(HeaderViewModel, user);
            Friend friend = _friendService.FindRelationship(User.Identity.GetUserId(), user.Id);

            if (friend != null)
            {
                HeaderViewModel.CodeRelationshipId = friend.CodeRelationshipId;
                HeaderViewModel.Id_User_Request    = friend.Id_User;
            }
            return(View(_viewModel));
        }
コード例 #2
0
        public JsonResult Friend_action(FriendActionViewModel data)
        {
            if (data.Id_User == User.Identity.GetUserId() && data.Id_Friend != null)
            {
                Friend          friend = _friendService.FindRelationship(data.Id_User, data.Id_Friend);
                ApplicationUser user1  = _service.GetUserById(data.Id_User);
                ApplicationUser user2  = _service.GetUserById(data.Id_Friend);

                if (friend != null)
                {
                    if (data.CodeRelationshipId == 0)
                    {
                        _friendService.Delete(friend);
                        if (friend.CodeRelationshipId == 1)
                        {
                            if (user1.Friend > 1)
                            {
                                user1.Friend--;
                            }
                            if (user2.Friend > 1)
                            {
                                user2.Friend--;
                            }
                        }
                    }
                    else
                    {
                        friend.CodeRelationshipId = data.CodeRelationshipId;
                        _friendService.Update(friend);
                        user1.Friend++;
                        user2.Friend++;
                    }
                    _applicationUserService.Update(user1);
                    _applicationUserService.Update(user2);
                }
                else
                {
                    friend = new Friend();
                    FieldHelper.CopyNotNullValue(friend, data);
                    _friendService.Add(friend);
                }
                try
                {
                    _friendService.Save();
                    _applicationUserService.Save();
                    return(Json(new
                    {
                        result = "success",
                        id = friend.Id
                    }));
                }
                catch (Exception e)
                {
                    return(Json(new
                    {
                        result = "failed"
                    }));
                }
            }
            return(Json(new
            {
                result = "failed",
            }));
        }