public ActionResult Index()
        {
            IEnumerable <GalleryViewModel> galleries;
            var client   = GlobalWebApiClient.GetClient();
            var response = client.GetAsync("api/galleries/all").Result;

            try
            {
                if (response.IsSuccessStatusCode)
                {
                    galleries = response.Content.ReadAsAsync <IEnumerable <GalleryViewModel> >().Result;
                    return(View(galleries.ToList()));
                }
                else
                {
                    ViewBag.Error = "Server Error. Please contact administrator!";
                }
            }
            catch (Exception ex)
            {
                var result = ex.Message;
            }
            return(View());
        }
        //GET: Profiles/Home
        public ActionResult Home()
        {
            var client = GlobalWebApiClient.GetClient();
            //var response = client.GetAsync("api/Account/LoggedUser").Result;

            var response = client.GetAsync(@"api/profiles/profile/" + Session["Email"].ToString().EncodeBase64()).Result;

            if (response.IsSuccessStatusCode)
            {
                var userLogged = response.Content.ReadAsAsync <ProfileViewModel>().Result;

                var loggedUserId = Guid.Parse(userLogged.Id.ToString());
                var userId       = Guid.Parse(Session["UserId"].ToString());
                if ((Session["Email"] == null) || !(loggedUserId.Equals(userId)))
                {
                    return(RedirectToAction("Login", "Account"));
                }
                else
                {
                    return(View(userLogged));
                }
            }
            return(RedirectToAction("Create", "Profiles"));
        }
        public ActionResult Edit(Guid?id)
        {
            var client   = GlobalWebApiClient.GetClient();
            var response = client.GetAsync("api/galleries/gallery/info/" + id.ToString()).Result;

            try
            {
                if (response.IsSuccessStatusCode)
                {
                    var gallery = response.Content.ReadAsAsync <GalleryViewModel>().Result;
                    //gallery.ProfileId = Guid.Parse(Session["ProfileId"].ToString());
                    return(View(gallery));
                }
                else
                {
                    return(View());
                }
            }
            catch (Exception ex)
            {
                var result = ex.Message;;
            }
            return(View());
        }
Beispiel #4
0
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                const String URI_ADDRESS = "api/Accounts/Login";

                try
                {
                    var client = GlobalWebApiClient.GetClient();

                    var content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair <string, string>("grant_type", "password"),
                        new KeyValuePair <string, string>("email", model.Email),
                        new KeyValuePair <string, string>("password", model.Password)
                    });

                    var response = await client.PostAsync(URI_ADDRESS, content);

                    if (response.IsSuccessStatusCode)
                    {
                        var tokenResponse = await response.Content.ReadAsStringAsync();

                        var vmTokenResponse = new TokenResponseViewModel
                        {
                            AccessToken = tokenResponse
                        };
                        GlobalWebApiClient.StoreToken(vmTokenResponse);

                        if (vmTokenResponse != null)
                        {
                            Session["Email"] = vmTokenResponse.Username.ToString();

                            var clientUser = GlobalWebApiClient.GetClient();

                            var resultLoggedUser  = clientUser.GetAsync("api/Accounts/LoggedUser").Result;
                            var resultLoggedEmail = clientUser.GetAsync(@"api/profiles/profile/" + Session["Email"].ToString().EncodeBase64()).Result;

                            if (!resultLoggedEmail.IsSuccessStatusCode)
                            {
                                if (resultLoggedUser.IsSuccessStatusCode)
                                {
                                    if (resultLoggedUser != null)
                                    {
                                        var vmUser = await resultLoggedUser.Content.ReadAsAsync <ProfileViewModel>();

                                        Session["UserId"] = vmUser.ProfileVmId;
                                        return(RedirectToAction("Index", "Home"));
                                    }
                                }
                            }
                            else
                            {
                                var vmUser = await resultLoggedEmail.Content.ReadAsAsync <ProfileViewModel>();

                                Session["UserId"] = vmUser.ProfileVmId;
                                return(RedirectToAction("Index", "Home"));
                            }
                        }
                        return(View(model));
                    }

                    return(View(model));
                }
                catch (Exception ex)
                {
                    var result = ex.Message;
                }
            }

            return(View(model));
        }
        //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"));
        }