Exemple #1
0
        public IActionResult Friends()
        {
            User      current = GetUser();
            FriendsVM vm      = new FriendsVM
            {
                Friends = new List <FriendVM>(),
            };

            foreach (User u in lists.GetUsers())
            {
                if (u.ID == current.ID)
                {
                    // Can't be friends with yourself
                    continue;
                }

                vm.Friends.Add(new FriendVM
                {
                    UserId   = u.ID,
                    UserName = u.Name,
                    IsFriend = current.Friends.Any(uf => uf.FriendID == u.ID),
                });
            }

            return(View(vm));
        }
Exemple #2
0
        public ActionResult ShowAll(FriendsVM model)
        {
            if (Session["loggedUser"] == null)
            {
                return(RedirectToAction("Login", "Home"));
            }

            model.Page = (model.Page == 0 ? 1 : model.Page);

            Service1Client service = new Service1Client();

            UserDTO current = service.GetUserById(model.Id);

            List <FriendDTO> Friends = service.GetFriendsByUserId(current.Id).ToList();

            if (!String.IsNullOrEmpty(model.SearchName))
            {
                Friends = Friends.Where(u => u.Friend.Username.Contains(model.SearchName)).ToList();
            }

            model.Friends = Friends;

            model.PageCount = (int)Math.Ceiling(model.Friends.Count() / (double)5);

            model.Friends = model.Friends.Skip((model.Page - 1) * 5).Take(5).ToList();

            model.User = current;

            return(View(model));
        }
Exemple #3
0
        public static FriendsVM LoadViewModelAsync()
        {
            UserService _service = new UserService();

            List <User> usersOnline = _service.friendsGetOnline() as List <User>;

            FriendsOnlineVM onlineFriends = new FriendsOnlineVM()
            {
                Name = "Online"
            };

            foreach (User user in usersOnline)
            {
                onlineFriends.Friends.Add(user);
            }

            List <User> users = _service.friendsGet() as List <User>;

            FriendsOnlineVM friends = new FriendsOnlineVM()
            {
                Name = "Friends"
            };

            foreach (User user in users)
            {
                friends.Friends.Add(user);
            }

            FriendsVM viewModel = new FriendsVM();

            viewModel.Pages.Add(onlineFriends);
            viewModel.Pages.Add(friends);

            return(viewModel);
        }
        public FriendsView()
        {
            var vm = new FriendsVM();

            this.BindingContext = vm;

            InitializeComponent();
        }
Exemple #5
0
 public void Test_ViewModel_Load()
 {
     //Arrange
     _controller.Login("*****@*****.**", "Dhjnvytyjub1");
     //Act
     _viewModel = _controller.LoadUsersOnlineViewModel();
     //Assert
     Assert.NotNull(_viewModel);
 }
        public void Refresh_Friends_Friend()
        {
            var friendVM = new FriendsVM();

            App.User.Username = "******";

            friendVM.RefreshCommand.Execute(null);

            Assert.IsInstanceOfType(typeof(ObservableCollection <User>), friendVM.Friends);
        }
        public void Refresh_AppFriends_notEmpty()
        {
            var friendsVM = new FriendsVM();

            App.User.Username = "******";

            friendsVM.RefreshCommand.Execute(null);

            Assert.IsNull(App.User.Friends);
        }
        public void Refresh_Friends_Name()
        {
            var friendsVM = new FriendsVM();

            App.User.Username = "******";

            friendsVM.RefreshCommand.Execute(null);

            int    index = friendsVM.Friends.Count;
            string name;

            if (index != 0)
            {
                name = friendsVM.Friends[index].Username;
            }
            else
            {
                name = "lol";
            }
            Assert.IsNotNull(name);
        }
Exemple #9
0
        public async Task <IActionResult> Friends(FriendsVM vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            foreach (FriendVM friendVM in vm.Friends)
            {
                User u = lists.GetUser(friendVM.UserId);
                if (u == null)
                {
                    ModelState.AddModelError(nameof(friendVM.UserId), "UserID isn't valid");
                    continue;
                }
            }
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            User current = GetUser();

            foreach (FriendVM friendVM in vm.Friends)
            {
                User u = lists.GetUser(friendVM.UserId);
                await lists.ManageFriend(current, u, friendVM.IsFriend);
            }

            if (current.Friends.Count > 0)
            {
                Message("You are now friends with {0}", string.Join(", ", current.Friends.Select(uf => uf.Friend.Name)));
            }
            else
            {
                Message("You have no friends");
            }

            return(RedirectToRoute(Names.UserIndex, "user-controls"));
        }
        public async System.Threading.Tasks.Task<ActionResult> Index(string sn,string se,string sr, string sf)
        {
            if (sn != null)
            {
                ViewData["snVal"] = sn;
            }
            else
            {
                ViewData["snVal"] = "";
            }
            if (se != null)
            {
                ViewData["seVal"] = se;
            }
            else
            {
                ViewData["seVal"] = "";
            }
            if (sr != null)
            {
                ViewData["srVal"] = sr.ToString();
            }
            else
            {
                ViewData["srVal"] = "";
            }
            if (sf != null)
            {
                ViewData["sfVal"] = sf.ToString();
            }
            else
            {
                ViewData["sfVal"] = "";
            }
            //test if user is still logged in and authorized
            HttpCookie jwtCookie = HttpContext.Request.Cookies.Get("jwt");
            //check if user is logged in or not
            UserDTO current_user = null;
            if (jwtCookie == null)
            {
                ViewData["loggedin"] = false;
            }
            else
            {
                ViewData["loggedin"] = true;
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://localhost:44368/api/users/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtCookie.Value);

                    var content = JsonConvert.SerializeObject(jwtCookie);
                    var buffer = System.Text.Encoding.UTF8.GetBytes(content);
                    var byteContent = new ByteArrayContent(buffer);
                    byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    HttpResponseMessage response = await client.PostAsync("userfromtoken", byteContent);

                    string jsonString = await response.Content.ReadAsStringAsync();
                    current_user = JsonConvert.DeserializeObject<UserDTO>(jsonString);
                }
                ViewData["DisplayName"] = current_user.displayName;
            }
            //get all friendships from db
            List<FriendshipDTO> friendships;
            using (var client = new HttpClient())
            {
                client.BaseAddress = url;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtCookie.Value);
                HttpResponseMessage response = null;
                if (sf == null || sf == "")
                {
                    response = await client.GetAsync("all");
                }
                else {
                    response = await client.GetAsync("all/"+sf);
                }
                if (response == null) {
                    return RedirectToAction("Index", "Home");
                }
                string jsonString = await response.Content.ReadAsStringAsync();
                friendships = JsonConvert.DeserializeObject<List<FriendshipDTO>>(jsonString);
            }
            if (current_user == null)
            {
                return RedirectToAction("Login","Users");
            }
            //get friends ids
            List<int> friendsIDs = new List<int>();
            List<FriendshipDTO> myfriendships = new List<FriendshipDTO>();
            foreach (FriendshipDTO fs in friendships) {
                if (fs.user1_id == current_user.Id)
                {
                    friendsIDs.Add(fs.user2_id);
                    myfriendships.Add(fs);
                }
                else if(fs.user2_id == current_user.Id)
                {
                    friendsIDs.Add(fs.user1_id);
                    myfriendships.Add(fs);
                }
            }
            //get friends info
            List<FriendsVM> myFriends = new List<FriendsVM>();
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://localhost:44368/api/users/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtCookie.Value);

                foreach (int f_id in friendsIDs) {
                    HttpResponseMessage response = await client.GetAsync("getbyid/"+f_id);
                    string jsonString = await response.Content.ReadAsStringAsync();
                    FriendsVM friend = JsonConvert.DeserializeObject<FriendsVM>(jsonString);
                    for (int i = 0; i < myfriendships.Count; i++)
                    {
                        if (myfriendships[i].pending) {
                            if (myfriendships[i].user2_id == f_id)
                            {
                                friend.sentpending = true;
                                break;
                            }
                            else if (myfriendships[i].user1_id == f_id)
                            {
                                friend.recievedpending = true;
                                break;
                            }
                        }
                    }
                    //apply search
                    int rat = -1;
                    var isNumberic = int.TryParse(sr, out rat);
                    if (!isNumberic)
                    {
                        sr = "";
                    }
                    bool add = false;
                    if ((sn == "" || sn == null) && (sr == "" || sr == null) && (se == "" || se == null))
                    {
                        add = true;
                    }
                    else {
                        bool okName = false;
                        bool okRating = false;
                        bool okEmail = false;
                        if (sn == "" || sn == null) {
                            okName = true;
                        }
                        if (sr == "" || sr == null)
                        {
                            okRating = true;
                        }
                        if (se == "" || se == null)
                        {
                            okEmail = true;
                        }
                        if (!okName && friend.displayName.Contains(sn)) {
                            okName = true;
                        }
                        if (!okRating && friend.rating == rat)
                        {
                            okRating = true;
                        }
                        if (!okEmail && friend.email.Contains(se))
                        {
                            okEmail = true;
                        }
                        if (okName && okEmail && okRating)
                        {
                            add = true;
                        }
                        else {
                            add = false;
                        }
                    }
                    if (add) {
                        //set proper friendship tier
                        for (int i = 0; i < myfriendships.Count; i++)
                        {
                            if (friend.Id == myfriendships[i].user1_id || friend.Id == myfriendships[i].user2_id) {
                                friend.friendshipTier = myfriendships[i].friendshipTier;
                            }
                        }
                        myFriends.Add(friend);
                    }
                }
            }
            return View(myFriends);
        }