//Ta bort vän
        public ActionResult RemoveFriend(string userId)
        {
            var currentUserId          = User.Identity.GetUserId();
            var ctx                    = new FriendsDbContext();
            var friendRelationToremove = ctx.Friends.Where(p =>
                                                           ((p.FriendUserId == userId) && (p.UserID == currentUserId)) ||
                                                           ((p.FriendUserId == currentUserId) && (p.UserID == userId))).ToList();

            ctx.Friends.RemoveRange(friendRelationToremove);
            ctx.SaveChanges();
            return(RedirectToAction("ViewProfile", "User"));
        }
        public ActionResult Remove(string id)
        {
            var friendsContext      = new FriendsDbContext();
            var userId              = User.Identity.GetUserId();
            var friendshipsToRemove = friendsContext.Friends.Where(u =>
                                                                   u.UserId == userId && u.FriendId == id ||
                                                                   u.UserId == id && u.FriendId == userId);

            foreach (var friendship in friendshipsToRemove)
            {
                friendsContext.Friends.Remove(friendship);
            }
            friendsContext.SaveChanges();

            return(RedirectToAction("Index"));
        }
        public ActionResult Index()
        {
            var userId      = User.Identity.GetUserId();
            var userContext = new UserDbContext();

            var friendsContext = new FriendsDbContext();
            var friends        = friendsContext.Friends.Where(u => u.UserId == userId);
            var friendList     = new List <FriendsViewModel>();

            foreach (var friend in friends)
            {
                var user = userContext.Users.FirstOrDefault(u => u.UserId == friend.FriendId);
                friendList.Add(new FriendsViewModel {
                    UserId        = friend.FriendId,
                    ProfilePicUrl = user.ProfilePicUrl,
                    FullName      = user.FirstName + ' ' + user.LastName,
                    BirthYear     = user.BirthDate.Value.Year,
                    Category      = friend.Category
                });
            }

            var requestsContext = new FriendRequestDbContext();
            var requests        = requestsContext.FriendRequests.Where(u => u.FriendId == userId);
            var requestList     = new List <FriendsViewModel>();

            foreach (var request in requests)
            {
                var user = userContext.Users.FirstOrDefault(u => u.UserId == request.UserId);
                requestList.Add(new FriendsViewModel {
                    UserId        = request.UserId,
                    ProfilePicUrl = user.ProfilePicUrl,
                    FullName      = user.FirstName + ' ' + user.LastName,
                    BirthYear     = user.BirthDate.Value.Year,
                });
            }

            var friendCollection = new FriendCollectionViewModel {
                Friends  = friendList,
                Requests = requestList
            };

            ViewBag.CategoryList = GetList.Categories();
            return(View(friendCollection));
        }
        public ActionResult Edit(FriendsModel model, string id)
        {
            var friendsContext = new FriendsDbContext();
            var userId         = User.Identity.GetUserId();
            var friendship     = friendsContext.Friends.FirstOrDefault(f => f.UserId == userId && f.FriendId == id);

            if (model.Category != null)
            {
                friendship.Category = model.Category;
                friendsContext.SaveChanges();
                return(RedirectToAction("Index"));
            }

            var userContext = new UserDbContext();
            var friend      = userContext.Users.FirstOrDefault(u => u.UserId == id);
            var fullName    = friend.FirstName + " " + friend.LastName;

            ViewBag.Name         = fullName;
            ViewBag.CategoryList = GetList.Categories();
            return(View(friendship));
        }
        //Acceptera vänförfrågan
        public ActionResult AcceptFriendRequest(string userId)
        {
            var currentUserId  = User.Identity.GetUserId();
            var ctx            = new FriendsDbContext();
            var alreadyFriends = ctx.Friends.Any(u => u.UserID == currentUserId && u.FriendUserId == userId);

            if (!alreadyFriends)
            {
                ctx.Friends.Add(new Friend()
                {
                    UserID       = currentUserId,
                    FriendUserId = userId
                });
                ctx.Friends.Add(new Friend()
                {
                    FriendUserId = currentUserId,
                    UserID       = userId
                });
                RemoveFromRequests(currentUserId, userId);
                ctx.SaveChanges();
            }
            return(Redirect(Request.UrlReferrer.ToString()));
        }
        //Skapar en viewmodel för visning av profilsidan
        private ProfileViewModel GetProfileViewModel(string userId)
        {
            var profileCtx = new ProfileDbContext();
            var friendCtx  = new FriendsDbContext();
            var postCtx    = new PostDbContext();
            var ImgCtx     = new ImageDbContext();

            var image = ImgCtx.Images.FirstOrDefault(u => u.UserID == userId);

            var profile = profileCtx.Profiles.FirstOrDefault(p => p.UserId == userId);

            if (profile.Interests == null)
            {
                profile.Interests = new HashSet <string>();
            }

            var posts      = postCtx.Posts.Where(po => po.UserId == userId).ToList();
            var friends    = friendCtx.Friends.Where(f => f.UserID == userId).ToList();
            var friendList = new List <FriendViewModel>();

            if (friends != null)
            {
                foreach (var f in friends)
                {
                    var friendProfile = profileCtx.Profiles.SingleOrDefault(p => p.UserId == f.FriendUserId);
                    friendList.Add(FriendViewModel.FromProfile(friendProfile));
                }
            }

            return(new ProfileViewModel()
            {
                Profile = profile,
                Friends = friendList,
                Posts = posts,
                Image = image
            });
        }
        // TODO: Move this action to ProfileController when it's done
        public ActionResult Add(string id)
        {
            var userContext     = new UserDbContext();
            var friendsContext  = new FriendsDbContext();
            var requestsContext = new FriendRequestDbContext();
            var userId          = User.Identity.GetUserId();

            // Check that the sender hasn't already sent a request
            var sentRequest = requestsContext.FriendRequests.Where(u => u.UserId == userId && u.FriendId == id);

            if (sentRequest.Count() == 0)
            {
                // Check that the recipient hasn't already sent a request (redirect to requests page if they have)
                var receivedRequest = requestsContext.FriendRequests.Where(u => u.UserId == id && u.FriendId == userId);
                if (receivedRequest.Count() == 0)
                {
                    // Check that the users aren't already friends && that the user isn't trying to add themself
                    var friends = friendsContext.Friends.Where(u => u.User.UserId == userId && u.Friend.UserId == id);
                    if (friends.Count() == 0 && userId != id)
                    {
                        requestsContext.FriendRequests.Add(new FriendRequestModel {
                            UserId   = userId,
                            FriendId = id,
                            Seen     = false
                        });
                        requestsContext.SaveChanges();
                    }
                }
                else
                {
                    return(RedirectToAction("Index", "Friends"));
                }
            }
            // TODO: Change to appropriate action once the method has been moved to ProfileController
            return(RedirectToAction("All", "User"));
        }
 public FriendsController(FriendsDbContext context) => this.context = context;
Exemple #9
0
 public MessageRepository(FriendsDbContext context) : base(context)
 {
 }
Exemple #10
0
        private int CheckCompatibility(string id)
        {
            var compatibility = 0;
            var userContext   = new UserDbContext();
            var userId        = User.Identity.GetUserId();

            if (id == userId)
            {
                return(-1);
            }

            var currentUser = userContext.Users.FirstOrDefault(u => u.UserId == userId);
            var compareUser = userContext.Users.FirstOrDefault(u => u.UserId == id);

            // Check age compatibility
            if (currentUser.BirthDate.Value.Year <= compareUser.BirthDate.Value.Year + 1 ||
                currentUser.BirthDate.Value.Year >= compareUser.BirthDate.Value.Year - 1)
            {
                compatibility += 20;
            }
            else if (currentUser.BirthDate.Value.Year <= compareUser.BirthDate.Value.Year + 3 ||
                     currentUser.BirthDate.Value.Year >= compareUser.BirthDate.Value.Year - 3)
            {
                compatibility += 10;
            }
            else if (currentUser.BirthDate.Value.Year <= compareUser.BirthDate.Value.Year + 5 ||
                     currentUser.BirthDate.Value.Year >= compareUser.BirthDate.Value.Year - 5)
            {
                compatibility += 5;
            }

            // Check zodiac sign compatibility
            if (currentUser.ZodiacSign == compareUser.ZodiacSign)
            {
                compatibility += 20;
            }

            // Check gender & sexual orientation compatibility
            if (currentUser.SexualOrientation == "Heterosexual" && compareUser.SexualOrientation == "Heterosexual" &&
                currentUser.Gender != compareUser.Gender)
            {
                compatibility += 20;
            }
            else if ((currentUser.SexualOrientation == "Homosexual" || currentUser.SexualOrientation == "Bisexual") &&
                     (compareUser.SexualOrientation == "Homosexual" || compareUser.SexualOrientation == "Bisexual") &&
                     currentUser.Gender == compareUser.Gender)
            {
                compatibility += 20;
            }
            else if (currentUser.SexualOrientation == "Other" && compareUser.SexualOrientation == "Other")
            {
                compatibility += 20;
            }

            // Check country compatibility
            if (currentUser.Country == compareUser.Country)
            {
                compatibility += 20;
            }

            // Check mutual friends compatibility
            var friendsContext = new FriendsDbContext();
            var userFriends    = friendsContext.Friends.Where(f => f.UserId == userId);
            var compareFriends = friendsContext.Friends.Where(f => f.UserId == id);
            int mutualFriends  = 0;

            foreach (var friend in userFriends)
            {
                if (compareFriends.Any(f => f.FriendId == friend.FriendId))
                {
                    mutualFriends++;
                }
            }

            if (mutualFriends >= 5)
            {
                compatibility += 20;
            }
            else if (mutualFriends >= 3)
            {
                compatibility += 10;
            }
            else if (mutualFriends >= 1)
            {
                compatibility += 5;
            }

            return(compatibility);
        }
Exemple #11
0
        // GET: Profile
        public ActionResult Index(ProfileViewModel model, string profileId)
        {
            var userId = User.Identity.GetUserId();

            if (userId != null)
            {
                var userContext     = new UserDbContext();
                var friendContext   = new FriendsDbContext();
                var postContext     = new PostDbContext();
                var interestContext = new InterestsDbContext();

                //Checks if the current user is the owner of the profile
                if (userId != profileId && profileId != null)
                {
                    userId = profileId;
                }
                ;
                var user = userContext.Users.FirstOrDefault(u => u.UserId == userId);
                List <InterestsModel> interestsList = interestContext.Friends.Where(u => u.UserId == userId).ToList();
                List <FriendsModel>   friendList    = friendContext.Friends.Where
                                                          (u => u.UserId == userId).ToList();
                List <PostModel> PostList = postContext.Friends.Where(u => u.RecipientId == userId).ToList();

                var UserPostList = new List <UserPostViewModel>();
                //Puts all posts to the user in a viewmodel with post- and user info
                foreach (var item in PostList)
                {
                    UserModel userInfo  = userContext.Users.FirstOrDefault(u => u.UserId == item.PosterId);
                    var       modelView = new UserPostViewModel
                    {
                        PostId        = item.PostId,
                        PosterId      = item.PosterId,
                        RecipientId   = item.RecipientId,
                        Content       = item.Content,
                        Date          = item.Date,
                        ProfilePicUrl = userInfo.ProfilePicUrl,
                        FirstName     = userInfo.FirstName,
                        LastName      = userInfo.LastName,
                    };
                    UserPostList.Add(modelView);
                }

                var UserDict = new Dictionary <UserModel, string>();
                //Puts all friends in a dictionary and pairs them with their appointed category
                foreach (var item in friendList)
                {
                    UserDict.Add(userContext.Users.FirstOrDefault(u => u.UserId == item.FriendId), item.Category);
                }
                model.UserId            = user.UserId;
                model.FirstName         = user.FirstName;
                model.LastName          = user.LastName;
                model.Gender            = user.Gender;
                model.SexualOrientation = user.SexualOrientation;
                model.ZodiacSign        = user.ZodiacSign;
                model.ProfilePicUrl     = user.ProfilePicUrl;
                model.BirthDate         = user.BirthDate;
                model.Country           = user.Country;
                model.Friends           = UserDict;
                model.Compatibility     = CheckCompatibility(user.UserId);
                model.Posts             = UserPostList;
                model.Interests         = interestsList;

                return(View(model));
            }

            return(View("~/Views/Home/index.cshtml"));
        }
Exemple #12
0
 public FriendsService(FriendsDbContext db, IMapper mapper)
     : base(db)
     => this.mapper = mapper;
Exemple #13
0
 public UserRepository(FriendsDbContext context, IChatRepository chatRepository, IMapper mapper) : base(context)
 {
     _chatRepository = chatRepository;
     _mapper         = mapper;
 }
Exemple #14
0
 public ChatRepository(FriendsDbContext context) : base(context)
 {
 }