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); } }