Esempio n. 1
0
        public UserRepositoryAccount selectRepository(UserRepositoryAccount userRepositoryAccount)
        {
            Console.WriteLine("Enter name of repository to work with:");
            string repositoryName = Console.ReadLine();
            var    user           = userRepositoryAccount.User;
            var    repository     = repositoryClient.findRepository(repositoryName);

            if (repository == null)
            {
                throw new RepositoryNotFound(repositoryName);
            }

            var repositoryAccess = user.RepositoryAccesses.Find(it => it.Repository.Name.Equals(repositoryName));

            if (repositoryAccess == null)
            {
                if (!(repository.GetType() == typeof(PublicRepository)))
                {
                    throw new AccessDenied(repositoryName, user.Login);
                }
                repositoryAccess = new RepositoryAccess(repository, RepositoryAccessType.CONTRIBUTOR, user);
            }

            var userRepositorySession = new UserRepositorySession(user, repositoryAccess);

            userRepositorySessionHandler.handle(userRepositorySession);
            return(userRepositoryAccount);
        }
        private Branch getBranch(UserRepositorySession userRepositorySession)
        {
            Console.WriteLine("Enter name of branch:");
            string branchName = Console.ReadLine();
            var    branch     = userRepositorySession.Current.Repository.Branches.Find(it => it.Name.Equals(branchName));

            if (branch == null)
            {
                throw new BranchNotFound(branchName);
            }
            return(branch);
        }
        private Tuple <User, string> getAccess(UserRepositorySession userRepositorySession)
        {
            var repositoryAccess = userRepositorySession.Current;
            var user             = userRepositorySession.User;

            if (!user.getContributedRepositories().Contains(repositoryAccess.Repository))
            {
                user.RepositoryAccesses.Add(repositoryAccess);
                repositoryAccess.Repository.addContributor(user);
                return(new Tuple <User, string>(user, repositoryAccess.Repository.Name));
            }

            throw new OperationNotPermitted(user.Login, RepositoryOperationType.INVITE_USER);
        }
        public void handle(UserRepositorySession userRepositorySession)
        {
            var repositoryAccess     = userRepositorySession.Current;
            var user                 = userRepositorySession.User;
            var repositoryOperations = prepareRepositoryOperations(repositoryAccess, user);

            do
            {
                repositoryOperations.ForEach(it =>
                                             Console.WriteLine($"{it.Id} - {it.Name}")
                                             );
                int choice    = ReadUserInputChoice();
                var operation = repositoryOperations.Find(it => it.Id == choice);
                userRepositorySession = handle(userRepositorySession, operation);
                repositoryOperations  = userRepositorySession.RepositoryOperations;
            } while (repositoryOperations.Any());
        }
        private Tuple <User, string> grantAccess(UserRepositorySession userRepositorySession)
        {
            var repositoryAccess = userRepositorySession.Current;
            var user             = userRepositorySession.User;

            if (repositoryAccess.isOwner(user.Login))
            {
                Console.WriteLine("Enter login of user:");
                string login   = Console.ReadLine();
                User   invitee = userService.getUser(login);
                invitee.RepositoryAccesses.Add(new RepositoryAccess(repositoryAccess.Repository, RepositoryAccessType.CONTRIBUTOR, user));
                repositoryAccess.Repository.addContributor(invitee);
                return(new Tuple <User, string>(invitee, repositoryAccess.Repository.Name));
            }

            throw new OperationNotPermitted(user.Login, RepositoryOperationType.INVITE_USER);
        }
        private Branch createBranch(UserRepositorySession userRepositorySession)
        {
            Console.WriteLine("Enter name of branch:");
            string branchName = Console.ReadLine();

            Console.WriteLine("Enter name of file:");
            string fileName = Console.ReadLine();

            Console.WriteLine("Enter content of file:");
            string fileContent = Console.ReadLine();
            var    files       = new List <File>();
            var    file        = new File(fileName, fileContent);

            files.Add(file);
            var branch = Branch.New(branchName, files);

            userRepositorySession.Current.Repository.Branches.Add(branch);
            return(branch);
        }
        private Branch pushToBranch(UserRepositorySession userRepositorySession)
        {
            Console.WriteLine("Enter name of branch:");
            string branchName = Console.ReadLine();

            Console.WriteLine("Enter name of file:");
            string fileName = Console.ReadLine();

            Console.WriteLine("Enter content of file:");
            string fileContent = Console.ReadLine();
            var    branch      = userRepositorySession.Current.Repository.Branches.Find(it => it.Name.Equals(branchName));

            if (branch == null)
            {
                throw new BranchNotFound(branchName);
            }
            var contributableAdapter = new ContributableAdapter(branch);
            var result = (ContributableAdapter)contributableAdapter.Push(userRepositorySession.Current, branchName, new File(fileName, fileContent));

            return((Branch)result.Contributable);
        }
        private UserRepositorySession handle(UserRepositorySession userRepositorySession,
                                             RepositoryOperation repositoryOperation)
        {
            User             user             = userRepositorySession.User;
            RepositoryAccess repositoryAccess = userRepositorySession.Current;

            switch (repositoryOperation.RepositoryOperationType)
            {
            case RepositoryOperationType.MAIN_MENU:
            {
                return(new UserRepositorySession(user, repositoryAccess, new List <RepositoryOperation>()));
            }

            case RepositoryOperationType.INVITE_USER:
            {
                var result = grantAccess(userRepositorySession);
                Console.WriteLine($"{result.Item1.Login} got access to {result.Item2}");
                return(new UserRepositorySession(user, repositoryAccess, prepareRepositoryOperations(repositoryAccess, user)));
            }

            case RepositoryOperationType.CREATE_BRANCH:
            {
                var result = createBranch(userRepositorySession);
                Console.WriteLine($"Branch: {result.Name} was created");
                return(new UserRepositorySession(user, repositoryAccess, prepareRepositoryOperations(repositoryAccess, user)));
            }

            case RepositoryOperationType.JOIN_AS_CONTRIBUTOR:
            {
                var result = getAccess(userRepositorySession);
                Console.WriteLine($"{result.Item1.Login} got access to {result.Item2}");
                return(new UserRepositorySession(user, repositoryAccess, prepareRepositoryOperations(repositoryAccess, user)));
            }

            case RepositoryOperationType.LIST_BRANCHES:
            {
                var branches = listBranches(userRepositorySession);
                Console.WriteLine($"Branches:{String.Join("\n", branches.Select(it=> it.Name ))}");
                return(new UserRepositorySession(user, repositoryAccess, prepareRepositoryOperations(repositoryAccess, user)));
            }

            case RepositoryOperationType.PICK_BRANCH:
            {
                var branch = getBranch(userRepositorySession);
                Console.WriteLine($"Branch:{branch.Name}\nFiles:{String.Join("\n",branch.Files.Select(it=>it.Title))}");
                return(new UserRepositorySession(user, repositoryAccess, prepareRepositoryOperations(repositoryAccess, user)));
            }

            case RepositoryOperationType.PUSH:
            {
                var branch = pushToBranch(userRepositorySession);
                Console.WriteLine($"Branch:{branch.Name}\nFiles:\n{String.Join("\n",branch.Files.Select(it=>it.Title))}");
                return(new UserRepositorySession(user, repositoryAccess, prepareRepositoryOperations(repositoryAccess, user)));
            }

            case RepositoryOperationType.CLONE:
            {
                var access     = userRepositorySession.Current;
                var repository = userRepositorySession.Current.Repository.cloneRepository(access);
                userRepositorySession.User.ClonedRepositories.Add(repository);
                var session = new UserRepositorySession(user, repositoryAccess, prepareRepositoryOperations(repositoryAccess, user));
                Console.WriteLine($"Repository {repository.Name} was successfully cloned");
                return(session);
            }
            }

            return(userRepositorySession);
        }
        private List <Branch> listBranches(UserRepositorySession userRepositorySession)
        {
            var branches = userRepositorySession.Current.Repository.Branches;

            return(branches);
        }