public async Task <IActionResult> AllFriends() { var user = await this.userManager.GetUserAsync(this.User); var friendships = this.db.Friendships.Where(u => u.FriendFrom.Email.Equals(user.Email)) .Select(e => new Friendship { FriendFrom = e.FriendFrom, FriendTo = e.FriendTo, }).ToList(); var viewModel = new FriendShipViewModel(); viewModel.FriendshipsModel = friendships; return(this.View(viewModel)); }
public ActionResult CreateFriendShip(Guid requestedToId) { if (Session["Email"] == null) { return(RedirectToAction("Login", "Account")); } if (requestedToId.Equals(Session["UserId"])) { return(RedirectToAction("Details", "Profiles", routeValues: new { id = requestedToId })); } var friendShip = new FriendShipViewModel { RequestedToId = requestedToId }; //JObject jObj = new JObject //{ // ["requestedToId"] = requestedToId //}; var client = GlobalWebApiClient.GetClient(); var response = client.PostAsJsonAsync("api/friendships/createfriendship/", friendShip).Result; if (response.IsSuccessStatusCode) { client = GlobalWebApiClient.GetClient(); var requestedTo = client.GetAsync(@"api/profiles/profile/info/" + requestedToId.ToString()).Result; //var fsResult = JsonConvert.DeserializeObject<IEnumerable<FriendShipViewModel>>(client.GetStringAsync(@"api/friendships/all").Result); if (response.IsSuccessStatusCode) { var pvm = response.Content.ReadAsAsync <ProfileViewModel>().Result; //var result = response.Content.ReadAsStringAsync().Result; return(RedirectToAction("Details", "Profiles", pvm)); } } return(View("Error")); }
//GET: Profiles/Details/5 public ActionResult Details(Guid?id) { ProfileViewModel pvm = null; if (id == null) { if (Session["Email"] == null) { return(RedirectToAction("Login", "Account")); } else { try { var client = GlobalWebApiClient.GetClientRegion(); var response = client.GetAsync(@"api/profiles/profile/" + Session["Email"].ToString().EncodeBase64()).Result; if (response.IsSuccessStatusCode) { pvm = response.Content.ReadAsAsync <ProfileViewModel>().Result; id = pvm.Id; } else { ViewBag.Result = "Server Error. Please contact administrator!"; } } catch (Exception ex) { var result = ex.Message; } } } else { try { var client1 = GlobalWebApiClient.GetClient(); var responseProfile = client1.GetAsync("api/profiles/profile/info/" + id.ToString()).Result; //var fsResult = JsonConvert.DeserializeObject<IEnumerable<FriendShipViewModel>>(client.GetStringAsync(@"api/friendships/all").Result); if (responseProfile.IsSuccessStatusCode) { var profileViewModel = responseProfile.Content.ReadAsAsync <ProfileViewModel>().Result; var userId = Guid.Parse(Session["UserId"].ToString()); if (profileViewModel.Id == userId) { var friends = client1.GetAsync("api/friendships/friends/" + userId.ToString()).Result; ProfileViewModel myFriend = null; bool isMyFriend = false; if (friends.IsSuccessStatusCode) { var myFriendShips = friends.Content.ReadAsAsync <IEnumerable <ProfileViewModel> >().Result; if (myFriendShips.Count() > 0) { myFriend = myFriendShips.FirstOrDefault(p => p.Id.Equals(p.Id)); var friendResult = client1.GetAsync("api/profiles/profile/info/" + myFriend.Id.ToString()).Result; var pvmFriend = friendResult.Content.ReadAsAsync <ProfileViewModel>().Result; isMyFriend = myFriendShips.Any(p => p.Id == pvmFriend.Id); var friendShip = new FriendShipViewModel(); if (isMyFriend) { //ViewBag.IsFriend = isMyFriend; friendShip.Status = StatusEnumViewModel.Accepted; } var friendId = Guid.Parse(myFriend.Id.ToString()); foreach (var myFriendShip in myFriendShips) { if (myFriendShip.Id.Equals(friendId) && !friendShip.Status.Equals(StatusEnumViewModel.Accepted)) { ViewBag.IsFriend = isMyFriend; return(View(profileViewModel)); } else if (myFriendShip.Id.Equals(friendId) && friendShip.Status.Equals(StatusEnumViewModel.Accepted)) { ViewBag.FriendsCount = myFriendShips.ToList().Count; ViewBag.Friends = myFriendShips.ToList(); ViewBag.IsFriend = isMyFriend; return(View(profileViewModel)); } } } ViewBag.FriendsCount = myFriendShips.Count(); ViewBag.Friends = myFriendShips.ToList(); ViewBag.IsFriend = isMyFriend; return(View(profileViewModel)); } } var client2 = GlobalWebApiClient.GetClient(); var friendsResult = client2.GetAsync("api/friendships/friends/" + id.ToString()).Result; if (friendsResult.IsSuccessStatusCode) { var myFriendShips = friendsResult.Content.ReadAsAsync <IEnumerable <ProfileViewModel> >().Result; bool isMyFriend = myFriendShips.Any(p => p.Id == userId); var friendShip = new FriendShipViewModel(); if (isMyFriend) { //ViewBag.IsFriend = isMyFriend; friendShip.Status = StatusEnumViewModel.Accepted; foreach (var myFriendShip in myFriendShips) { if (myFriendShip.Id.Equals(userId) && !friendShip.Status.Equals(StatusEnumViewModel.Accepted)) { ViewBag.IsFriend = isMyFriend; return(View(profileViewModel)); } else if (myFriendShip.Id.Equals(userId) && friendShip.Status.Equals(StatusEnumViewModel.Accepted)) { ViewBag.FriendsCount = myFriendShips.Count(); ViewBag.Friends = myFriendShips.ToList(); ViewBag.IsFriend = isMyFriend; return(View(profileViewModel)); } } } } return(View(profileViewModel)); } else { ViewBag.Result = "Server Error. Please contact administrator!"; } } catch (Exception ex) { var result = ex.Message; } } return(View("Error")); }