Esempio n. 1
0
        public IDtoOutObjects Accept(DtoInAddFriend dtoInFriend)
        {
            DtoOutError error = new DtoOutError();

            if (TokenTools.Authentication(dtoInFriend.Token, dtoInFriend.DeviceName))
            {
                User   acceptant = TokenTools.getUserFromToken(dtoInFriend.Token);
                Friend friend    = _friendsRepository.FindBy(x => x.IdReciever == acceptant.Id && x.IsDeleted == false && x.ObjectApplicant.Email == dtoInFriend.EmailReciever).FirstOrDefault();
                if (friend == null)
                {
                    UserDoesNotAskedForFriendshipException ex = new UserDoesNotAskedForFriendshipException();
                    error.Exception = ex;
                    return(error);
                }
                if (friend.Accepted == true)
                {
                    YouAreAlreadyFriendsExceptions ex = new YouAreAlreadyFriendsExceptions();
                    error.Exception = ex;
                    return(error);
                }
                friend.Accepted = true;
                _friendsRepository.Edit(friend);
                _friendsRepository.Save();
                ChatRoomTools.Create(new List <User>()
                {
                    acceptant, _usersRepository.FindBy(x => x.Email == dtoInFriend.EmailReciever).FirstOrDefault()
                });
                var          config       = new MapperConfiguration(cfg => { cfg.CreateMap <Friend, DtoOutFriend>(); });
                IMapper      mapper       = config.CreateMapper();
                DtoOutFriend dtoOutFriend = new DtoOutFriend();
                mapper.Map(friend, dtoOutFriend);
                return(dtoOutFriend);
            }
            else
            {
                NotAuthenticatedException ex = new NotAuthenticatedException();
                error.Exception = ex;
                return(error);
            }
        }
Esempio n. 2
0
        public IDtoOutObjects AddFriend(DtoInAddFriend dtoInFriend)

        {
            DtoOutError error = new DtoOutError();

            if (TokenTools.Authentication(dtoInFriend.Token, dtoInFriend.DeviceName))
            {
                User userApplicant = TokenTools.getUserFromToken(dtoInFriend.Token);
                User userReciever  = _usersRepository.FindBy(x => x.Email == dtoInFriend.EmailReciever && x.IsDeleted == false).FirstOrDefault();
                if (userReciever == null)
                {
                    UserWithThisEmailDoesntExistException ex = new UserWithThisEmailDoesntExistException();
                    error.Exception = ex;
                    return(error);
                }
                if (FriendTools.areAlreadyFriends(userApplicant.Id, userReciever.Id))
                {
                    YouAreAlreadyFriendsExceptions ex = new YouAreAlreadyFriendsExceptions();
                    error.Exception = ex;
                    return(error);
                }
                Friend friend = new Friend();
                friend.ObjectApplicant = userApplicant;
                friend.ObjectReciever  = userReciever;
                _friendsRepository.Add(friend);
                _friendsRepository.Save();
                var config = new MapperConfiguration(cfg => { cfg.CreateMap <Friend, DtoOutFriend>(); });

                IMapper      mapper       = config.CreateMapper();
                DtoOutFriend dtoOutFriend = new DtoOutFriend();
                mapper.Map(friend, dtoOutFriend);
                return(dtoOutFriend);
            }
            else
            {
                NotAuthenticatedException ex = new NotAuthenticatedException();
                error.Exception = ex;
                return(error);
            }
        }