public FriendshipDTO DeleteFriend(int userId, int friendId)
        {
            using (var uow = new UnitOfWork())
            {
                var friendshipRepository = uow.GetRepository <Friendship>();


                Friendship existFriendship = friendshipRepository.GetAll().FirstOrDefault(f => (
                                                                                              (f.UserOneId == userId & f.UserTwoId == friendId) |
                                                                                              (f.UserOneId == friendId & f.UserTwoId == userId)));

                if (existFriendship == null)
                {
                    return(null);
                }
                else
                {
                    FriendshipMapper fm    = new FriendshipMapper();
                    FriendshipDTO    fmDTO = fm.MapToDTO(existFriendship);
                    friendshipRepository.Delete(existFriendship.Id);
                    uow.SaveChanges();
                    return(fmDTO);
                }
            }
        }
        public bool Save(FriendshipDTO friendsDTO)
        {
            Friendship friend = new Friendship
            {
                user1_id       = friendsDTO.user1_id,
                user2_id       = friendsDTO.user2_id,
                befriend_date  = friendsDTO.befriend_date,
                pending        = friendsDTO.pending,
                friendshipTier = friendsDTO.friendshipTier
            };

            try
            {
                using (UnitOfWork unitOfWork = new UnitOfWork())
                {
                    unitOfWork.FriendshipRepo.Insert(friend);
                    unitOfWork.Save();
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public bool Update(FriendshipDTO friendDTO)
        {
            Friendship toUpdate = null;

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                toUpdate = unitOfWork.FriendshipRepo.GetByID(friendDTO.Id);
            }
            if (toUpdate != null)
            {
                toUpdate.user1_id       = friendDTO.user1_id;
                toUpdate.user2_id       = friendDTO.user2_id;
                toUpdate.befriend_date  = friendDTO.befriend_date;
                toUpdate.pending        = friendDTO.pending;
                toUpdate.friendshipTier = friendDTO.friendshipTier;
            }
            try
            {
                using (UnitOfWork unitOfWork = new UnitOfWork())
                {
                    unitOfWork.FriendshipRepo.Update(toUpdate);
                    unitOfWork.Save();
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Delete([FromHeader] string Authentication, string userId)
        {
            if (SessionManager.GetSessionState(Authentication) != SessionManager.SessionState.Authorized)
            {
                return(Unauthorized());
            }
            SessionInfo sessionInfo = SessionManager.GetSessionInfo(Authentication);

            if (sessionInfo == null)
            {
                return(Unauthorized());
            }

            int userIdNum = 0;

            if (!int.TryParse(userId, out userIdNum))
            {
                return(NotFound(userId));
            }

            using (UnitOfWork uow = new UnitOfWork())
            {
                FriendshipsRepository friendshipsRepository = new FriendshipsRepository(uow);
                FriendshipDTO         friendship            = await friendshipsRepository.GetFriendship(userIdNum, sessionInfo.UserId);

                if (friendship != null)
                {
                    await friendshipsRepository.Remove(friendship.Id);
                }

                uow.Commit();
            }

            return(Ok());
        }
        public FriendshipDTO AddFriendship(FriendshipDTO friendshipDTO)
        {
            using (var uow = new UnitOfWork())
            {
                var friendshipRepository = uow.GetRepository <Friendship>();
                var acountRepository     = uow.GetRepository <Account>();

                var existFriendship = friendshipRepository.GetAll().FirstOrDefault(f => (
                                                                                       (f.UserOneId == friendshipDTO.UserOne.Id && f.UserTwoId == friendshipDTO.UserTwo.Id)));

                if (existFriendship != null)
                {
                    return(null);
                }
                else
                {
                    Account          acountOne = acountRepository.GetById(friendshipDTO.UserOne.Id);
                    Account          acountTwo = acountRepository.GetById(friendshipDTO.UserTwo.Id);
                    FriendshipMapper fm        = new FriendshipMapper();
                    friendshipDTO.Since            = DateTime.Now;
                    friendshipDTO.UserOne.FullName = acountOne.FirstName + " " + acountOne.LastName;
                    friendshipDTO.UserTwo.FullName = acountTwo.FirstName + " " + acountTwo.LastName;
                    Friendship friendship = fm.MapFromDTO(friendshipDTO);
                    friendship.UserOne = acountOne;
                    friendship.UserTwo = acountTwo;

                    Friendship add = friendshipRepository.Add(friendship);
                    return(fm.MapToDTO(add));
                }
            }
        }
        public async Task<ActionResult> SendRequest(int id)
        {
            //test if user is still logged in and authorized
            HttpCookie jwtCookie = HttpContext.Request.Cookies.Get("jwt");
            //check if user is logged in or not
            UserDTO current_user = null;
            if (jwtCookie == null)
            {
                ViewData["loggedin"] = false;
                return RedirectToAction("Login", "Users");
            }
            else
            {
                ViewData["loggedin"] = true;
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://localhost:44368/api/users/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtCookie.Value);

                    var content = JsonConvert.SerializeObject(jwtCookie);
                    var buffer = System.Text.Encoding.UTF8.GetBytes(content);
                    var byteContent = new ByteArrayContent(buffer);
                    byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    HttpResponseMessage response = await client.PostAsync("userfromtoken", byteContent);

                    string jsonString = await response.Content.ReadAsStringAsync();
                    current_user = JsonConvert.DeserializeObject<UserDTO>(jsonString);
                }
                ViewData["DisplayName"] = current_user.displayName;
            }
            FriendshipDTO newFriendship = new FriendshipDTO()
            {
                user1_id = current_user.Id,
                user2_id = id,
                pending = true,
                befriend_date = DateTime.Now,
                friendshipTier = "Unnoticed"
            };
            using (var client = new HttpClient()) {
                client.BaseAddress = url;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtCookie.Value);

                var content = JsonConvert.SerializeObject(newFriendship);
                var buffer = System.Text.Encoding.UTF8.GetBytes(content);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                HttpResponseMessage response = await client.PostAsync("create", byteContent);
            }
            return RedirectToAction("Index", "Users");
        }
Exemplo n.º 7
0
        public IHttpActionResult Delete(int id)
        {
            var           friendshipService = new FriendshipService();
            FriendshipDTO friendshipDTO     = friendshipService.RemoveFriendship(id);

            if (friendshipDTO == null)
            {
                return(BadRequest("This account does not exist in DB"));
            }
            return(Ok(friendshipDTO));
        }
 public string UpdateFriend(FriendshipDTO friendDto)
 {
     if (friendsService.Update(friendDto))
     {
         return("Freindship updated!");
     }
     else
     {
         return("Freindship is not updated!");
     }
 }
 public string PostFriend(FriendshipDTO friendDto)
 {
     if (friendsService.Save(friendDto))
     {
         return("Freindship saved!");
     }
     else
     {
         return("Freindship is not saved!");
     }
 }
Exemplo n.º 10
0
        public IHttpActionResult Post(FriendshipDTO friendship)
        {
            var           friendshipService = new FriendshipService();
            FriendshipDTO friendshipDTO     = friendshipService.AddFriendship(friendship);

            if (friendshipDTO == null)
            {
                return(NotFound());
            }

            return(Ok(friendshipDTO));
        }
        public async Task RemoveUsersFromFriends(UserDTO accountDto, UserDTO user2Dto)
        {
            IQuery <FriendshipDTO> query = GetQuery(new FriendshipFilter {
                Account = accountDto, Account2 = user2Dto
            });

            FriendshipDTO frship = query.Execute().FirstOrDefault();

            await RemoveFriendship(frship);

            await Database.SaveAsync();
        }
Exemplo n.º 12
0
        public void AddRemoveFriend()
        {
            SignUpAccountService signUpAccountService = new SignUpAccountService();
            AccountService       accountService       = new AccountService();
            FriendshipService    frService            = new FriendshipService();

            AdressDTO address = new AdressDTO();

            address.latitude  = 50;
            address.longitude = 30;
            address.address   = "Beautiful city";

            SignUpAccountDTO account = new SignUpAccountDTO();

            account.FirstName = "Ion";
            account.LastName  = "Popescu";
            account.UserName  = "******";
            account.Email     = "*****@*****.**";
            account.Adress    = address;
            account.Password  = "******";

            SignUpAccountDTO account1 = new SignUpAccountDTO();

            account1.FirstName = "Dfsadsa";
            account1.LastName  = "Ddsadsad";
            account1.UserName  = "******";
            account1.Email     = "*****@*****.**";
            account1.Adress    = address;
            account1.Password  = "******";



            AccountSimpleDTO acc  = signUpAccountService.addAccount(account);
            AccountSimpleDTO acc1 = signUpAccountService.addAccount(account1);

            frService.AddFriend(acc.Id, acc1.Id);

            var friends = frService.GetAllFriendships(acc.Id);

            FriendshipDTO friendship = friends.Where(fr => (fr.UserOne.Id == acc1.Id | fr.UserTwo.Id == acc1.Id)).FirstOrDefault();

            frService.RemoveFriendship(friendship.Id);

            var friends1 = frService.GetAllFriendships(acc.Id);

            FriendshipDTO friendship1 = friends1.Where(fr => (fr.UserOne.Id == acc1.Id | fr.UserTwo.Id == acc1.Id)).FirstOrDefault();

            accountService.deleteAccount(acc.Id);
            accountService.deleteAccount(acc1.Id);

            Assert.IsNotNull(friendship);
            Assert.IsNull(friendship1);
        }
Exemplo n.º 13
0
        public FriendshipDTO MapToDTO(Friendship source)
        {
            FriendshipDTO target = new FriendshipDTO();

            target.Id    = source.Id;
            target.Since = source.Since;

            AccountSimpleMapper accountMapper = new AccountSimpleMapper();

            target.UserOne = accountMapper.MapToDTO(source.UserOne);
            target.UserTwo = accountMapper.MapToDTO(source.UserTwo);

            return(target);
        }
        public List <FriendshipDTO> GetAll(string tier)
        {
            Expression <Func <Friendship, bool> > filter = f => f.friendshipTier.Contains(tier);
            List <FriendshipDTO> friends = new List <FriendshipDTO>();

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                foreach (var item in unitOfWork.FriendshipRepo.Get(filter))
                {
                    FriendshipDTO fdto = new FriendshipDTO
                    {
                        Id             = item.Id,
                        user1_id       = item.user1_id,
                        user2_id       = item.user2_id,
                        befriend_date  = item.befriend_date,
                        pending        = item.pending,
                        friendshipTier = item.friendshipTier
                    };
                    if (fdto.friendshipTier == "Friends")
                    {
                        List <ParticipantToEvent> user_1events = unitOfWork.PteRepo.Get(p => p.Participant_id == fdto.user1_id).ToList();
                        List <ParticipantToEvent> user_2events = unitOfWork.PteRepo.Get(p => p.Participant_id == fdto.user2_id).ToList();
                        int common_events = 0;
                        for (int i = 0; i < user_1events.Count; i++)
                        {
                            for (int j = 0; j < user_2events.Count; j++)
                            {
                                if (user_1events[i].Id == user_2events[j].Id)
                                {
                                    common_events++;
                                }
                            }
                        }
                        if (common_events >= 10)
                        {
                            fdto.friendshipTier = "Best Friends";
                        }
                    }
                    if (fdto.friendshipTier == "Acquaintances" && (DateTime.Now > fdto.befriend_date.AddDays(7)))
                    {
                        fdto.friendshipTier = "Friends";
                        Update(fdto);
                    }
                    friends.Add(fdto);
                }
            }

            return(friends);
        }
Exemplo n.º 15
0
        public IHttpActionResult AcceptFriend(FriendshipDTO fdto)
        {
            ResponseMessage response = new ResponseMessage();

            if (_service.AcceptFriendship(fdto.user1_id, fdto.user2_id))
            {
                response.Code = 201;
                response.Body = "Friendship has been saved.";
            }
            else
            {
                response.Code = 200;
                response.Body = "Friendship has not been saved.";
            }
            return(Json(response));
        }
        public FriendshipDTO FriendshipToDTO(Friendship fr)
        {
            if (fr == null)
            {
                return(null);
            }
            FriendshipDTO frDto = new FriendshipDTO
            {
                Id             = fr.Id,
                user1_id       = fr.user1_id,
                user2_id       = fr.user2_id,
                befriend_date  = fr.befriend_date,
                pending        = fr.pending,
                friendshipTier = fr.friendshipTier
            };

            return(frDto);
        }
Exemplo n.º 17
0
        public FriendshipDTO RemoveFriendship(int friendshipID)
        {
            using (var uow = new UnitOfWork())
            {
                var friendshipRepository = uow.GetRepository <Friendship>();

                Friendship fr = friendshipRepository.GetById(friendshipID);

                if (fr == null)
                {
                    return(null);
                }
                else
                {
                    FriendshipMapper fm    = new FriendshipMapper();
                    FriendshipDTO    fmDTO = fm.MapToDTO(fr);
                    friendshipRepository.Delete(friendshipID);
                    uow.SaveChanges();
                    return(fmDTO);
                }
            }
        }
Exemplo n.º 18
0
        public IHttpActionResult Create(FriendshipDTO frndDto)
        {
            ResponseMessage response = new ResponseMessage();

            if (frndDto.Validate() == false)
            {
                response.Code = 400;
                response.Body = "Invalid data - Friendship has not been saved";

                return(Json(response));
            }
            if (_service.Save(frndDto))
            {
                response.Code = 201;
                response.Body = "Friendship has been saved.";
            }
            else
            {
                response.Code = 200;
                response.Body = "Friendship has not been saved.";
            }

            return(Json(response));
        }
        public bool AcceptFriendship(int id1, int id2)
        {
            List <FriendshipDTO> friendships = GetAll();
            FriendshipDTO        fr          = null;

            for (int i = 0; i < friendships.Count; i++)
            {
                if ((friendships[i].user1_id == id1 && friendships[i].user2_id == id2) || (friendships[i].user1_id == id2 && friendships[i].user2_id == id1))
                {
                    fr = friendships[i];
                    break;
                }
            }
            if (fr == null)
            {
                return(false);
            }
            else
            {
                fr.pending        = false;
                fr.friendshipTier = "Acquaintances";
                return(Update(fr));
            }
        }
Exemplo n.º 20
0
        public async Task <ActionResult> Add([FromHeader] string Authentication, [FromQuery] string userId, [FromQuery] string username)
        {
            if (SessionManager.GetSessionState(Authentication) != SessionManager.SessionState.Authorized)
            {
                return(Unauthorized());
            }
            SessionInfo sessionInfo = SessionManager.GetSessionInfo(Authentication);

            if (sessionInfo == null)
            {
                return(Unauthorized());
            }

            using (UnitOfWork uow = new UnitOfWork())
            {
                int secondUserId = 0;
                if (!string.IsNullOrEmpty(userId))
                {
                    int.TryParse(userId, out secondUserId);
                }

                if (secondUserId <= 0)
                {
                    if (string.IsNullOrEmpty(username))
                    {
                        return(NotFound());
                    }

                    UsersRepository usersRepository = new UsersRepository(uow);
                    UserDTO         user            = await usersRepository.GetByName(username);

                    if (user == null)
                    {
                        return(NotFound("User {username} not found"));
                    }

                    secondUserId = user.Id;
                }

                if (secondUserId == sessionInfo.UserId)
                {
                    return(Conflict("Cannot invite yourself"));
                }

                FriendshipsRepository friendshipsRepository = new FriendshipsRepository(uow);
                FriendshipDTO         friendship            = await friendshipsRepository.GetFriendship(secondUserId, sessionInfo.UserId);

                if (friendship == null)
                {
                    friendship = new FriendshipDTO
                    {
                        UserOneId = sessionInfo.UserId,
                        UserTwoId = secondUserId,
                        Status    = 0
                    };
                    await friendshipsRepository.Add(friendship);
                }
                else
                {
                    if (friendship.Status == 0 && friendship.UserOneId == secondUserId)
                    {
                        friendship.Status = 1;
                        await friendshipsRepository.Update(friendship);
                    }
                }

                uow.Commit();
                return(Ok(friendship));
            }
        }
        public async Task RemoveFriendship(FriendshipDTO friendship)
        {
            await Database.FriendshipManager.Delete(await Database.FriendshipManager.GetById(friendship.Id));

            await Database.SaveAsync();
        }