public ActionResult GetSuggestFollow(int pageIndex, int pageSize)
        {
            var userId       = User.Identity.GetUserId();
            var _userService = this.Service <IAspNetUserService>();
            List <FollowSuggestViewModel> userList = new List <FollowSuggestViewModel>();
            var  curUser     = _userService.FirstOrDefaultActive(p => p.Id == userId);
            var  Coord       = new GeoCoordinate();
            bool checkNearBy = false;

            if (curUser.Longitude != null && curUser.Latitude != null)
            {
                Coord       = new GeoCoordinate(curUser.Latitude.Value, curUser.Longitude.Value);
                checkNearBy = true;
            }


            var users = _userService.GetActive(p => p.Id != userId && p.Follows.Where(f => f.FollowerId == userId).ToList().Count == 0).ToList();

            foreach (var user in users)
            {
                FollowSuggestViewModel model = Mapper.Map <FollowSuggestViewModel>(user);
                model.weight = 0;
                foreach (var follow in user.Follows)
                {
                    if (follow.UserId == curUser.Id)
                    {
                        model.weight += 1;
                        break;
                    }
                }

                if (checkNearBy && (user.Longitude != null && user.Latitude != null))
                {
                    var userCoord = new GeoCoordinate(user.Latitude.Value, user.Longitude.Value);
                    var dis       = Coord.GetDistanceTo(userCoord);
                    if (Coord.GetDistanceTo(userCoord) < 5000)
                    {
                        model.weight += 2;
                    }
                }

                int hobbyCount = 1;
                foreach (var hobby in user.Hobbies)
                {
                    foreach (var curHobby in curUser.Hobbies)
                    {
                        if (hobby.SportId == curHobby.SportId)
                        {
                            model.weight    = model.weight + hobbyCount * 3;
                            model.sameSport = hobbyCount;
                            hobbyCount++;
                        }
                    }
                }
                userList.Add(model);
            }
            var suggestUserList = userList.OrderByDescending(p => p.weight).Skip(pageIndex * pageSize).Take(pageSize).ToList();

            return(Json(suggestUserList, JsonRequestBehavior.AllowGet));
        }
        public ActionResult GetSearchUserResult(string keyword, int skip, int take)
        {
            var result      = new AjaxOperationResult <ShowUserSearchViewModel>();
            var userService = this.Service <IAspNetUserService>();
            //var resultList = new List<AspNetUser>();
            //using (var db = new SSNEntities())
            //{
            //    resultList = userService.FindUserByName(db, keyword, 0, 5).ToList();
            //}
            var followService = this.Service <IFollowService>();
            var userCount     = userService.GetActive(p => p.AspNetRoles.Where(k =>
                                                                               k.Name != "Quản trị viên" && k.Name != "Moderator").ToList().Count > 0 && (p.FullName.Contains(keyword) || p.UserName.Contains(keyword) ||
                                                                                                                                                          p.Email.Contains(keyword))).OrderBy(p => p.FullName).ToList().Count;
            var resultList = userService.GetActive(p => p.AspNetRoles.Where(k =>
                                                                            k.Name != "Quản trị viên" && k.Name != "Moderator").ToList().Count > 0 && (p.FullName.Contains(keyword) || p.UserName.Contains(keyword) ||
                                                                                                                                                       p.Email.Contains(keyword))).OrderBy(p => p.FullName).Skip(skip).Take(take).ToList();
            var curUserId = User.Identity.GetUserId();
            var curUser   = userService.FirstOrDefaultActive(p => p.Id == curUserId);
            List <FollowSuggestViewModel> searchResultList = new List <FollowSuggestViewModel>();

            //List<FollowSuggestViewModel> searchResultList = Mapper.Map<List<FollowSuggestViewModel>>(ResultList);
            foreach (var user in resultList)
            {
                FollowSuggestViewModel model = Mapper.Map <FollowSuggestViewModel>(user);
                var hobbyCount = 1;
                foreach (var hobby in user.Hobbies.ToList())
                {
                    foreach (var curHobby in curUser.Hobbies)
                    {
                        if (hobby.SportId == curHobby.SportId)
                        {
                            model.sameSport = hobbyCount;
                            hobbyCount++;
                        }
                    }
                }
                var tmp = followService.GetActive(p => p.FollowerId == curUserId && p.UserId == user.Id).ToList().Count;
                if (tmp > 0)
                {
                    model.isFollowed = true;
                }
                else
                {
                    model.isFollowed = false;
                }
                searchResultList.Add(model);
            }
            ShowUserSearchViewModel searchResult = new ShowUserSearchViewModel();

            searchResult.userCount = userCount;
            searchResult.userList  = searchResultList;
            if (searchResultList.Count > 0)
            {
                result.AdditionalData = searchResult;
            }

            result.Succeed = true;
            return(Json(result));
        }
        public ActionResult GetUserBySport(int sportId, int skip, int take)
        {
            var result      = new AjaxOperationResult <List <FollowSuggestViewModel> >();
            var userService = this.Service <IAspNetUserService>();
            var userId      = User.Identity.GetUserId();
            var curUser     = userService.FirstOrDefaultActive(p => p.Id == userId);
            List <FollowSuggestViewModel> userList = new List <FollowSuggestViewModel>();
            var  Coord       = new GeoCoordinate();
            bool checkNearBy = false;

            if (curUser.Longitude != null && curUser.Latitude != null)
            {
                Coord       = new GeoCoordinate(curUser.Latitude.Value, curUser.Longitude.Value);
                checkNearBy = true;
            }

            var users = userService.GetActive(p => p.Id != userId && p.AspNetRoles.Where(k =>
                                                                                         k.Name != "Quản trị viên" && k.Name != "Moderator").ToList().Count > 0 &&
                                              p.Hobbies.Where(m => m.SportId == sportId).ToList().Count > 0).ToList();

            foreach (var user in users)
            {
                FollowSuggestViewModel model = Mapper.Map <FollowSuggestViewModel>(user);
                if (checkNearBy && (user.Longitude != null && user.Latitude != null))
                {
                    var userCoord = new GeoCoordinate(user.Latitude.Value, user.Longitude.Value);
                    var dis       = Coord.GetDistanceTo(userCoord);
                    if (Coord.GetDistanceTo(userCoord) < 5000)
                    {
                        userList.Add(model);
                    }
                }
            }
            List <FollowSuggestViewModel> suggestUserList = userList.OrderBy(p => p.FullName).Skip(skip).Take(take).ToList();

            result.AdditionalData = suggestUserList;
            result.Succeed        = true;
            return(Json(result));
        }
        public ActionResult SearchDetail(string keyword)
        {
            var _sportService = this.Service <ISportService>();
            var sports        = _sportService.GetActive()
                                .Select(s => new SelectListItem
            {
                Text  = s.Name,
                Value = s.Id.ToString()
            }).OrderBy(s => s.Value);

            ViewBag.Sport = sports;
            var _userService = this.Service <IAspNetUserService>();
            var userId       = User.Identity.GetUserId();
            var curUser      = _userService.FirstOrDefaultActive(p => p.Id == userId);

            if (curUser == null)
            {
                return(RedirectToAction("PageNotFound", "Errors"));
            }
            ViewBag.User    = curUser;
            ViewBag.Keyword = keyword;
            //suggest news
            var         _newsService = this.Service <INewsService>();
            List <News> newsList     = new List <News>();

            foreach (var hobby in curUser.Hobbies)
            {
                List <News> list = _newsService.GetActive(p => p.Category.CategorySports.Where(f =>
                                                                                               f.SportId == hobby.SportId).ToList().Count > 0).ToList();
                newsList.AddRange(list);
            }

            if (newsList.Count == 0)
            {
                newsList = _newsService.GetActive().ToList();
            }
            if (newsList != null)
            {
                ViewBag.SuggestNews = newsList.FirstOrDefault();
            }

            //load group name
            var          _groupService = this.Service <IGroupService>();
            List <Group> groupList     = _groupService.GetActive(p => p.GroupMembers.Where(f =>
                                                                                           f.UserId == userId).ToList().Count > 0).ToList();

            if (groupList != null)
            {
                ViewBag.GroupList = groupList;
            }

            //suggest follower
            List <FollowSuggestViewModel> userList = new List <FollowSuggestViewModel>();
            var  Coord       = new GeoCoordinate();
            bool checkNearBy = false;

            if (curUser.Longitude != null && curUser.Latitude != null)
            {
                Coord       = new GeoCoordinate(curUser.Latitude.Value, curUser.Longitude.Value);
                checkNearBy = true;
            }
            var users = _userService.GetActive(p => p.Id != userId && p.AspNetRoles.Where(k =>
                                                                                          k.Name != "Quản trị viên" && k.Name != "Moderator").ToList().Count > 0 &&
                                               p.Follows.Where(f => f.Active == true && (f.FollowerId == userId)).ToList().Count == 0).ToList();

            foreach (var user in users)
            {
                FollowSuggestViewModel model = Mapper.Map <FollowSuggestViewModel>(user);
                model.weight = 0;
                foreach (var follow in user.Follows)
                {
                    if (follow.UserId == curUser.Id)
                    {
                        model.weight += 1;
                        break;
                    }
                }

                if (checkNearBy && (user.Longitude != null && user.Latitude != null))
                {
                    var userCoord = new GeoCoordinate(user.Latitude.Value, user.Longitude.Value);
                    var dis       = Coord.GetDistanceTo(userCoord);
                    if (Coord.GetDistanceTo(userCoord) < 5000)
                    {
                        model.weight += 2;
                    }
                }

                int hobbyCount = 1;
                foreach (var hobby in user.Hobbies)
                {
                    foreach (var curHobby in curUser.Hobbies)
                    {
                        if (hobby.SportId == curHobby.SportId)
                        {
                            model.weight    = model.weight + hobbyCount * 3;
                            model.sameSport = hobbyCount;
                            hobbyCount++;
                        }
                    }
                }
                userList.Add(model);
            }
            List <FollowSuggestViewModel> suggestUserList = userList.OrderByDescending(p => p.weight).Take(10).ToList();

            if (suggestUserList != null)
            {
                ViewBag.suggestUserList = suggestUserList;
            }

            //load follow numbers
            var _followService = this.Service <IFollowService>();
            var _postService   = this.Service <IPostService>();

            ViewBag.Following = _followService.GetActive(p => p.FollowerId == curUser.Id).Count();
            ViewBag.Follower  = _followService.GetActive(p => p.UserId == curUser.Id).Count();
            ViewBag.PostCount = _postService.GetActive(p => p.UserId == curUser.Id).Count();
            return(View());
        }
Beispiel #5
0
        // GET: Profile
        public ActionResult Index(string userId)
        {
            var     _userService   = this.Service <IAspNetUserService>();
            var     _sportService  = this.Service <ISportService>();
            var     _followService = this.Service <IFollowService>();
            var     _groupService  = this.Service <IGroupService>();
            Country vietnam        = AddressUtil.GetINSTANCE().GetCountry(Server.MapPath(AddressUtil.PATH));
            var     province       = vietnam.VietNamese.ToList();
            IEnumerable <SelectListItem> provinceList = province.Select(m => new SelectListItem
            {
                Text  = m.Type + " " + m.Name,
                Value = m.Type + " " + m.Name
            }).OrderBy(s => s.Value).ToArray();

            ViewBag.ProvinceList = provinceList;

            AspNetUser user = _userService.FirstOrDefaultActive(u => u.Id.Equals(userId));

            if (user == null)
            {
                return(RedirectToAction("PageNotFound", "Errors"));
            }
            AspNetUserFullInfoViewModel model = Mapper.Map <AspNetUserFullInfoViewModel>(user);

            this.PrepareUserInfo(model);

            //suggest follower
            List <FollowSuggestViewModel> userList = new List <FollowSuggestViewModel>();
            var  Coord       = new GeoCoordinate();
            bool checkNearBy = false;

            if (user.Longitude != null && user.Latitude != null)
            {
                Coord       = new GeoCoordinate(user.Latitude.Value, user.Longitude.Value);
                checkNearBy = true;
            }
            var users = _userService.GetActive(p => p.Id != userId && p.Follows.Where(f => f.Active == true && (f.FollowerId == userId)).ToList().Count == 0).ToList();

            foreach (var item in users)
            {
                FollowSuggestViewModel followsug = Mapper.Map <FollowSuggestViewModel>(item);
                followsug.weight = 0;
                foreach (var follow in item.Follows)
                {
                    if (follow.UserId == user.Id)
                    {
                        followsug.weight += 1;
                        break;
                    }
                }

                if (checkNearBy && (item.Longitude != null && item.Latitude != null))
                {
                    var userCoord = new GeoCoordinate(item.Latitude.Value, item.Longitude.Value);
                    var dis       = Coord.GetDistanceTo(userCoord);
                    if (Coord.GetDistanceTo(userCoord) < 5000)
                    {
                        followsug.weight += 2;
                    }
                }

                int hobbyCount = 1;
                foreach (var hobby in user.Hobbies)
                {
                    foreach (var curHobby in user.Hobbies)
                    {
                        if (hobby.SportId == curHobby.SportId)
                        {
                            followsug.weight    = followsug.weight + hobbyCount * 3;
                            followsug.sameSport = hobbyCount;
                            hobbyCount++;
                        }
                    }
                }
                userList.Add(followsug);
            }
            List <FollowSuggestViewModel> suggestUserList = userList.OrderByDescending(p => p.weight).Take(10).ToList();

            if (suggestUserList != null)
            {
                ViewBag.suggestUserList = suggestUserList;
            }

            //get sport list for post
            var sports = _sportService.GetActive()
                         .Select(s => new SelectListItem
            {
                Text  = s.Name,
                Value = s.Id.ToString()
            }).OrderBy(s => s.Value);

            ViewBag.Sport = sports;

            //get all image of user
            ViewBag.userPostImages = this.GetAllPostImageOfUser(userId, 0, 12).ToList();

            //get all people that this user follow
            List <Follow>     following      = _followService.GetFollowingList(userId).ToList();
            List <AspNetUser> followingUsers = new List <AspNetUser>();

            foreach (var item in following)
            {
                AspNetUser us = _userService.FirstOrDefaultActive(u => u.Id == item.UserId);
                followingUsers.Add(us);
            }
            ViewBag.followingUsers = followingUsers;

            //get list group that this user joined
            List <Group> groupList = _groupService.GetActive(p => p.GroupMembers.Where(f =>
                                                                                       f.UserId == userId && f.Status == (int)GroupMemberStatus.Approved).ToList().Count > 0).ToList();

            if (groupList != null)
            {
                ViewBag.GroupList = groupList;
            }
            IEnumerable <SelectListItem> districtList = new List <SelectListItem>();

            if (model.City != null && model.City != "")
            {
                province = vietnam.VietNamese.Where(p => model.City.Equals(p.Type + " " + p.Name)).ToList();
                if (province != null && province.Count > 0)
                {
                    var district = province.First().Districts.ToList();
                    districtList = district.Select(m => new SelectListItem
                    {
                        Text  = m.Type + " " + m.Name,
                        Value = m.Type + " " + m.Name
                    }).OrderBy(s => s.Value).ToArray();
                }
            }
            ViewBag.DistrictList = districtList;

            string curUserId = User.Identity.GetUserId();
            //get list of user that this user is following
            List <Follow> followingList = _followService.GetActive(f => f.FollowerId == curUserId).ToList();
            List <FollowDetailViewModel> followingListVM = Mapper.Map <List <FollowDetailViewModel> >(followingList);

            foreach (var item in followingListVM)
            {
                AspNetUser          ur     = _userService.FirstOrDefaultActive(u => u.Id.Equals(item.UserId));
                AspNetUserViewModel userVM = Mapper.Map <AspNetUserViewModel>(ur);
                item.User = userVM;
            }
            ViewBag.followingList = followingListVM;

            IEnumerable <SelectListItem> wardList = new List <SelectListItem>();

            if (model.District != null && model.District != "")
            {
                province = vietnam.VietNamese.Where(p => model.City.Equals(p.Type + " " + p.Name) && p.Districts.Where(f =>
                                                                                                                       model.District.Equals(f.Type + " " + f.Name)).ToList().Count > 0).ToList();
                if (province != null && province.Count > 0)
                {
                    var districts = province.First().Districts.Where(p => model.District.Equals(p.Type + " " + p.Name)).ToList();
                    if (districts != null && districts.Count > 0)
                    {
                        var ward = districts.First().Wards.ToList();
                        wardList = ward.Select(m => new SelectListItem
                        {
                            Text  = m.Type + " " + m.Name,
                            Value = m.Type + " " + m.Name
                        }).OrderBy(s => s.Value).ToArray();
                    }
                }
            }
            ViewBag.WardList = wardList;
            return(View(model));
        }
Beispiel #6
0
        public List <FollowSuggestViewModel> FollowSuggest(string userId, ScoutUpDB _db, bool nearby = false)
        {
            var user = _db.Users.Find(userId);
            //benzerlik listesi
            List <Similarity> similarties = new List <Similarity>();
            //Geri dönecek takip listesi
            var usersToFollow = new List <FollowSuggestViewModel>();
            //user ın puan verdiği itemler ile diğer userların puan verdiği itemin kesişimi alınacak
            //birinin puan verip diğerinin vermediği ile ilgilenmiyoruz
            var usersRatedItems = _db.UserRatings.Where(e => e.UserId == userId).
                                  Select(e => e.CategoryItemID).ToArray();
            //Kullanıcının puanları öğelerin Listesi UserandRating classından oluşuyor bu liste.Kullanıcının puanladığı öğelerin diğer kullanıcılarla kesişimini alırken kullanılacak.
            //Puanlanan öğelere ve itemlere daha kolay ulaşım sağlar
            var usersRatedItemsAndRatings = _db.UserRatings.Where(e => e.UserId == userId).Select(e => new UserAndRating {
                UserId = e.UserId, ItemId = e.CategoryItemID, UserRating = e.UserRating
            }).ToList();
            //Kullanıcın zaten takip ettikleriyle işimiz yok.
            var followlist = _db.UserFollow.Where(e => e.UserId == userId).Select(e => e.UserBeingFollowedUserId)
                             .ToArray();
            IQueryable <string> allUserIds;

            // followlananlar hariç tüm kullanıcıları çekiyoruz
            if (nearby)
            {
                allUserIds = _db.Users.Where(e => e.Id != userId).Where(e => !followlist.Contains(e.Id)).Where(e => e.UserCity == user.UserCity)
                             .Select(e => e.Id);
            }
            else
            {
                allUserIds = _db.Users.Where(e => e.Id != userId).Where(e => !followlist.Contains(e.Id))
                             .Select(e => e.Id);
            }

            //tüm öğelerin ayrı ayrı rating ortalamasını hesaplar Up olarak geçiyor formülde
            var allRatingsDictionary = new Dictionary <int, float>();

            foreach (var itemId in usersRatedItems)
            {
                var allRatedItemsRating = _db.UserRatings.Where(e => e.CategoryItemID == itemId)
                                          .Select(e => new UserAndRating {
                    ItemId = e.CategoryItemID, UserRating = e.UserRating
                }).ToList();
                allRatingsDictionary.Add(itemId, ((float)allRatedItemsRating.Sum(e => e.UserRating) / allRatedItemsRating.Count));
            }
            //Diğer kullanıcıları çekiyoruz sadece kullanıcıyla aynı puanladığı itemler geliyor ?????
            var other = _db.UserRatings.Where(e => allUserIds.Contains(e.UserId)).
                        //Where(e => usersRatedItems.Contains(e.CategoryItemID)).
                        Select(e => new UserAndRating {
                UserId = e.UserId, ItemId = e.CategoryItemID, UserRating = e.UserRating
            });
            //Diğer kullanıcı ratinglerine kolay ulaşmak için bir sözlük.
            var otherUsersAndRatings = new Dictionary <string, List <UserAndRating> >();

            foreach (var ids in allUserIds)
            {
                var list = new List <UserAndRating>();
                foreach (var userAndRating in other)
                {
                    if (ids == userAndRating.UserId)
                    {
                        list.Add(userAndRating);
                    }
                }
                otherUsersAndRatings.Add(ids, list);
            }
            //Kullanıcının rating ortalaması
            float userMean = (float)usersRatedItemsAndRatings.Sum(e => e.UserRating) / (float)usersRatedItemsAndRatings.Count;

            //Diğer kullanıcılar
            foreach (var otherUser in otherUsersAndRatings)
            {
                float otherUserMean  = (float)otherUser.Value.Sum(e => e.UserRating) / (float)otherUser.Value.Count;
                float ortakPuanlanan = 0;
                float Proximity      = 0;
                float Significance   = 0;
                float Singularity    = 0;
                float PSS            = 0;
                float RoUser         = 0;
                float RoOther        = 0;
                float URP            = 0;
                float JPSS           = 0;
                float Jaccord        = 0;
                //diğer kullanıcının Ratingleri
                foreach (var otherUserRating in otherUser.Value)
                {
                    foreach (var usersRated in usersRatedItemsAndRatings)
                    {
                        if (usersRated.ItemId == otherUserRating.ItemId)
                        {
                            Proximity    = 1 - (1 / (1 + ((float)Math.Exp(-Math.Abs(usersRated.UserRating - otherUserRating.UserRating)))));
                            Significance = (1 / (1 + ((float)Math.Exp(-((Math.Abs(usersRated.UserRating - userMean)) * Math.Abs(otherUserRating.UserRating - otherUserMean))))));
                            Singularity  = 1 - (1 / (1 + ((float)Math.Exp(-(Math.Abs((usersRated.UserRating + otherUserRating.UserRating) / 2) - allRatingsDictionary[usersRated.ItemId])))));
                            PSS         += Proximity * Significance * Singularity;

                            ortakPuanlanan++;
                        }

                        RoUser += (float)(Math.Pow(usersRated.UserRating - userMean, 2) / usersRatedItemsAndRatings.Count);
                    }

                    RoOther += (float)(Math.Pow(otherUserRating.UserRating - otherUserMean, 2) / otherUser.Value.Count);
                }

                RoUser  = (float)Math.Sqrt(RoUser);
                RoOther = (float)Math.Sqrt(RoOther);
                Jaccord = (ortakPuanlanan) / (otherUser.Value.Count * usersRatedItemsAndRatings.Count);

                JPSS = PSS * Jaccord;

                URP = 1 - (1 / (1 + (float)(Math.Exp(-(Math.Abs(userMean - otherUserMean) * Math.Abs((RoUser - RoOther)))))));

                float sim = JPSS * URP;
                similarties.Add(new Similarity {
                    OtherUserID = otherUser.Key, UserId = userId, UsersSimilarity = sim, RatedByUsersCount = otherUser.Value.Count
                });
            }
            var similartiesOrdered = similarties.OrderByDescending(e => e.UsersSimilarity);

            foreach (var similarity in similartiesOrdered)
            {
                var tempUser = _db.Users.Find(similarity.OtherUserID);
                var temp     = new FollowSuggestViewModel {
                    UserId     = similarity.OtherUserID, Name = tempUser.UserFirstName + " " + tempUser.UserSurname, ImagePath = tempUser.UserProfilePhoto,
                    Similarity = Math.Round((1000 * similarity.UsersSimilarity), 2), UserCity = tempUser.UserCity
                };
                usersToFollow.Add(temp);
            }

            return(usersToFollow);
        }