Esempio n. 1
0
        public JsonResult GetProductCategories()
        {
            var categoryDictionary = new Dictionary <string, string>();

            //categoryDictionary.Add("All","All Categories");
            using (var unitOfWork = UnitOfWorkFactory.BeginNew())
            {
                try
                {
                    var allAlbums = unitOfWork.AlbumRepository.Find(a => a.Type != AppConstants.Profile).ToList();
                    foreach (var album in allAlbums)
                    {
                        categoryDictionary.Add(album.Id, album.Name);
                    }
                }
                catch (Exception exception)
                {
                }
            }
            var result = from option in categoryDictionary
                         select new
            {
                Id          = option.Key,
                Description = option.Value
            };

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Esempio n. 2
0
        public ActionResult FBLogOff()
        {
            if (Session["UserId"] != null)
            {
                var user   = UnitOfWorkFactory.BeginNew().MemberRepository.FindMemberByFBId(Session["UserId"].ToString());
                var client = new FacebookClient(user.FBAccessToken);
                FormsAuthentication.SignOut();
                Session["UserId"] = null;
            }

            return(Redirect("/Home"));
        }
Esempio n. 3
0
        public ActionResult FbAuth(string returnUrl)
        {
            var client      = new FacebookClient();
            var oauthResult = client.ParseOAuthCallbackUrl(Request.Url);

            // Build the Return URI form the Request Url
            var redirectUri = new UriBuilder(Request.Url);

            redirectUri.Path = Url.Action("FbAuth", "Login");

            // Exchange the code for an access token
            dynamic result = client.Get("/oauth/access_token", new
            {
                client_id     = ConfigurationManager.AppSettings["FacebookAppId"],
                redirect_uri  = redirectUri.Uri.AbsoluteUri,
                client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"],
                code          = oauthResult.Code,
            });

            // Read the auth values
            string   accessToken = result.access_token;
            DateTime expires     = DateTime.UtcNow.AddSeconds(Convert.ToDouble(result.expires));

            // Get the user's profile information
            dynamic me = client.Get("/me",
                                    new
            {
                fields       = "first_name,last_name,email,gender",
                access_token = accessToken
            });

            // Read the Facebook user values
            string facebookId = me.id;
            string firstName  = me.first_name;
            string lastName   = me.last_name;
            string email      = me.email;
            string gender     = me.gender;

            // Add the user to our persistent store
            using (var unitOfWork = UnitOfWorkFactory.BeginNew())
            {
                try
                {
                    var existingMember = unitOfWork.MemberRepository.FindMemberByFBId(facebookId);
                    if (existingMember != null)
                    {
                        existingMember.Email            = email;
                        existingMember.FBAccessToken    = accessToken;
                        existingMember.FBTokenExpiresAt = expires;
                    }
                    else
                    {
                        unitOfWork.MemberRepository.Add(
                            new Member
                        {
                            Id               = facebookId,
                            FirstName        = firstName,
                            LastName         = lastName,
                            Email            = email,
                            FBAccessToken    = accessToken,
                            FBTokenExpiresAt = expires,
                            Gender           = gender ?? AppConstants.Unknown
                        });
                    }
                    unitOfWork.Commit();
                }
                catch (Exception exception)
                {
                    unitOfWork.Rollback();
                }
            }


            Session["UserId"] = facebookId;

            // Set the Auth Cookie
            FormsAuthentication.SetAuthCookie(email, false);

            //string adminIds = ConfigurationManager.AppSettings["MooreMarketAdmin"];
            //if (!string.IsNullOrEmpty(adminIds)
            //    && adminIds.Contains(facebookId))
            //{
            //    return Redirect("/Admin/Index");
            //}
            //return Redirect("/Market/WantToBuy");
            return(Redirect("/Market/Index"));
        }
Esempio n. 4
0
        public static bool SyncWithFB()
        {
            var result           = false;
            var incomingAlbumIds = new List <string>();

            using (var unitOfWork = UnitOfWorkFactory.BeginNew())
            {
                try
                {
                    var user =
                        unitOfWork.MemberRepository.FindMemberByFBId(AppConstants.AppSettings.MasterAdminFacebookId);
                    var client = new FacebookClient(user.FBAccessToken);

                    var     albumFeedUrl             = AppConstants.AppSettings.FeedUrl;
                    dynamic albumFeed                = client.Get(albumFeedUrl);
                    bool    haveAllAlbumsBeenFetched = false;
                    do
                    {
                        if (albumFeed == null ||
                            albumFeed.data == null)
                        {
                            haveAllAlbumsBeenFetched = true;
                        }
                        else
                        {
                            List <Album> albums = Mapper.MapAlbums(albumFeed);

                            if (albums != null &&
                                albums.Any())
                            {
                                foreach (var album in albums)
                                {
                                    var existingAlbum = unitOfWork.AlbumRepository.FindById(album.Id);
                                    incomingAlbumIds.Add(album.Id);
                                    if (existingAlbum != null)
                                    {
                                        if (existingAlbum.UpdatedTimeStamp != album.UpdatedTimeStamp)
                                        {
                                            existingAlbum.Name             = album.Name;
                                            existingAlbum.Description      = album.Description;
                                            existingAlbum.Count            = album.Count;
                                            existingAlbum.CoverPhoto       = album.CoverPhoto;
                                            existingAlbum.UpdatedTimeStamp = album.UpdatedTimeStamp;
                                        }
                                    }
                                    else
                                    {
                                        unitOfWork.AlbumRepository.Add(album);
                                    }
                                    SyncPhotos(album);
                                    unitOfWork.Commit();
                                }
                            }

                            // Getting next page
                            if (albumFeed.paging != null &&
                                albumFeed.paging.cursors != null &&
                                albumFeed.paging.cursors.after != null)
                            {
                                var nextCursor = albumFeed.paging.cursors.after;
                                albumFeedUrl = AppConstants.AppSettings.FeedUrl + "?after=" + nextCursor;
                                albumFeed    = client.Get(albumFeedUrl);
                            }
                            else
                            {
                                albumFeed = null;
                            }
                        }
                    } while (!haveAllAlbumsBeenFetched);


                    //Clean removed albums
                    var storedalbums = unitOfWork.AlbumRepository.Find().ToList();
                    foreach (var storedalbum in storedalbums)
                    {
                        if (!incomingAlbumIds.Contains(storedalbum.Id))
                        {
                            unitOfWork.PictureRepository.Remove(p => p.AlbumId == storedalbum.Id);
                            unitOfWork.AlbumRepository.Remove(storedalbum);
                            unitOfWork.Commit();;
                        }
                    }
                    result = true;
                }
                catch (Exception exception)
                {
                }
            }
            return(result);
        }
Esempio n. 5
0
        public static void SyncPhotos(Album album)
        {
            var    incomingPictureIds = new List <string>();
            string albumId            = album.Id;

            using (var unitOfWork = UnitOfWorkFactory.BeginNew())
            {
                try
                {
                    var user =
                        unitOfWork.MemberRepository.FindMemberByFBId(AppConstants.AppSettings.MasterAdminFacebookId);
                    var client = new FacebookClient(user.FBAccessToken);

                    string  photosFeedUrl            = albumId + "/photos";
                    dynamic photosFeed               = client.Get(photosFeedUrl);
                    bool    haveAllPhotosBeenFetched = false;
                    do
                    {
                        if (photosFeed == null ||
                            photosFeed.data == null)
                        {
                            haveAllPhotosBeenFetched = true;
                        }
                        else
                        {
                            List <Picture> pictures = Mapper.MapPictures(photosFeed);

                            if (pictures != null &&
                                pictures.Any())
                            {
                                foreach (var picture in pictures)
                                {
                                    incomingPictureIds.Add(picture.Id);
                                    var existingPicture = unitOfWork.PictureRepository.FindById(picture.Id);
                                    if (existingPicture != null)
                                    {
                                        //This condition doesn't seem to working with change in description
                                        if (existingPicture.UpdatedTimeStamp != picture.UpdatedTimeStamp)
                                        {
                                            existingPicture.Description      = picture.Description;
                                            existingPicture.UpdatedTimeStamp = picture.UpdatedTimeStamp;
                                        }
                                    }
                                    else
                                    {
                                        picture.AlbumId = albumId;
                                        unitOfWork.PictureRepository.Add(picture);
                                    }

                                    unitOfWork.Commit();
                                }
                            }

                            // Getting next page
                            if (photosFeed.paging != null &&
                                photosFeed.paging.cursors != null &&
                                photosFeed.paging.cursors.after != null)
                            {
                                var nextCursor = photosFeed.paging.cursors.after;
                                photosFeedUrl = albumId + "/photos?after=" + nextCursor;
                                photosFeed    = client.Get(photosFeedUrl);
                            }
                            else
                            {
                                photosFeed = null;
                            }
                        }
                    } while (!haveAllPhotosBeenFetched);

                    //Clean removed photos
                    var storedPictures = unitOfWork.PictureRepository.Find(p => p.AlbumId == albumId).ToList();
                    foreach (var storedPicture in storedPictures)
                    {
                        if (!incomingPictureIds.Contains(storedPicture.Id))
                        {
                            unitOfWork.PictureRepository.Remove(storedPicture);
                            unitOfWork.Commit();;
                        }
                    }
                }
                catch (Exception exception)
                {
                }
            }
        }
Esempio n. 6
0
        public ActionResult GetProductsByCategory(List <CategoryInfo> categoryInfoList)
        {
            JsonResult result              = null;
            var        aggregatedPictures  = new List <Picture>();
            int        numberOfPostToFetch = 25;
            int        unusedCount         = 0;

            using (var unitOfWork = UnitOfWorkFactory.BeginNew())
            {
                try
                {
                    var selectedCategories = categoryInfoList.Where(c => c.IsSelected).ToList();
                    if (!selectedCategories.Any())
                    {
                        selectedCategories = categoryInfoList;
                    }
                    numberOfPostToFetch = numberOfPostToFetch / selectedCategories.Count();
                    foreach (var categoryInfo in selectedCategories)
                    {
                        IEnumerable <Picture> pictures = null;
                        if (System.String.CompareOrdinal(categoryInfo.Key, "All") == 0)
                        {
                            pictures = unitOfWork.PictureRepository.Find().OrderByDescending(p => p.UpdatedTimeStamp)
                                       .Skip(categoryInfo.Count).
                                       Take(unusedCount + numberOfPostToFetch);
                        }
                        else
                        {
                            var currentCategory =
                                unitOfWork.AlbumRepository.Find(c => c.Id == categoryInfo.Key).FirstOrDefault();
                            pictures = currentCategory.Pictures
                                       .OrderByDescending(p => p.UpdatedTimeStamp)
                                       .Skip(categoryInfo.Count).
                                       Take(unusedCount + numberOfPostToFetch);
                        }

                        if (pictures.Any())
                        {
                            aggregatedPictures.AddRange(pictures.ToList());
                            int postsFetched = pictures.Count();
                            if (postsFetched == (numberOfPostToFetch + unusedCount))
                            {
                                unusedCount = 0;
                            }
                            else
                            {
                                unusedCount = numberOfPostToFetch + unusedCount - postsFetched;
                            }
                        }
                        else
                        {
                            unusedCount += numberOfPostToFetch;
                        }
                    }

                    var uiProducts = AutoMapper.Mapper.Map <IList <Picture>, IList <UIProduct> >(aggregatedPictures.ToList());
                    result = Json(uiProducts, JsonRequestBehavior.AllowGet);
                }
                catch (Exception exception)
                {
                }
            }
            return(result);
        }