Esempio n. 1
0
 public Client()
 {
     this.authentication = new AuthenticationDisconnect();
     Task.Run(async() => {
         short connectionErrorCounter = 0;
         while (true)
         {
             ConnectorResult <Guid> getResult = await WinApiConnector.RequestGet <Guid>(path);
             if (getResult.IsSuccess)
             {
                 connectionErrorCounter = 0;
             }
             else
             {
                 connectionErrorCounter += 1;
                 if (connectionErrorCounter > 10)
                 {
                     //co zrobić na błąd połączebnia
                     //stworzyć obiekt odpowiadający na zbieranie danych do czasu odzyskania połączenia???
                 }
             }
             await Task.Delay(2000);
         }
     });
 }
Esempio n. 2
0
        protected override CommandResult Execute(ConsoleContext context, RoleArgument arguments)
        {
            if (string.IsNullOrEmpty(arguments.Rename) || string.IsNullOrEmpty(arguments.Name))
            {
                System.Console.WriteLine("Przy update wymagany parametr -n <NAZWA> -r <NAZWA>");
                return(new CommandResult());
            }

            Task <ConnectorResult <Response <IEnumerable <Role> > > > role = WinApiConnector.RequestGet <Response <IEnumerable <Role> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetRole/" +
                                                                                                                                          $"{arguments.Name.ToLower()}");
            ConnectorResult <Response <IEnumerable <Role> > > preResult = role.Result;

            if (!preResult.IsSuccess)
            {
                return(new CommandResult(preResult.Message, false));
            }
            if (!preResult.Response.IsSuccess)
            {
                return(new CommandResult(preResult.Response.Code, false));
            }
            if (!preResult.Response.Payload.Any())
            {
                return(new CommandResult($"The Role {arguments.Name} doesn't exist in database", false));
            }

            if (preResult.Response.Payload.Count() != 1)
            {
                return(new CommandResult(preResult.Response.Payload.Count() == 0 ? "Nie można zmienić roli, która nie istnieje" : "Wiecej niż jeden wynik", false));
            }

            Task <ConnectorResult <Response <string> > > result = WinApiConnector.RequestPut <Role, Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/UpdateRole",
                                                                                                                        new Role()
            {
                Identifier = preResult.Response.Payload.FirstOrDefault().Identifier,
                Name       = arguments.Rename.ToLower()
            });
            ConnectorResult <Response <string> > connectorResult = result.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }
            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }
            if (connectorResult.Response.Payload == null)
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }

            return(new CommandResult($"The Role {arguments.Rename} has been updated", true));
        }
        protected override CommandResult Execute(ConsoleContext context, RoleArgument arguments)
        {
            Role role = new Role()
            {
                Name = arguments.Name.ToLower()
            };
            Task <ConnectorResult <Response <IEnumerable <Role> > > > checkIfExist = WinApiConnector.RequestGet <Response <IEnumerable <Role> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetRole/" +
                                                                                                                                                  $"{arguments.Name.ToLower()}");
            ConnectorResult <Response <IEnumerable <Role> > > preResult = checkIfExist.Result;

            if (!preResult.IsSuccess)
            {
                return(new CommandResult(preResult.Message, false));
            }
            if (!preResult.Response.IsSuccess)
            {
                return(new CommandResult(preResult.Response.Code, false));
            }
            if (preResult.Response.Payload.Any())
            {
                return(new CommandResult($"The Role {arguments.Name} exists in database"));
            }

            Task <ConnectorResult <Response <string> > > result          = WinApiConnector.RequestPost <Role, Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/AddRole", role);
            ConnectorResult <Response <string> >         connectorResult = result.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }
            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }
            if (connectorResult.Response.Payload == null)
            {
                return(new CommandResult("The payload of reguest is null or empty", false));
            }
            return(new CommandResult($"The Role {arguments.Name} has been added"));
        }
        protected override CommandResult Execute(ConsoleContext context, RoleArgument arguments)
        {
            Task <ConnectorResult <Response <IEnumerable <Role> > > > role = WinApiConnector.RequestGet <Response <IEnumerable <Role> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetRole/" +
                                                                                                                                          $"{arguments.Name.ToLower()}");
            ConnectorResult <Response <IEnumerable <Role> > > preResult = role.Result;

            if (!preResult.IsSuccess)
            {
                return(new CommandResult(preResult.Message, false));
            }
            if (!preResult.Response.IsSuccess)
            {
                return(new CommandResult(preResult.Response.Code, false));
            }
            if (!preResult.Response.Payload.Any())
            {
                return(new CommandResult($"The Role {arguments.Name} doesn't exist in database"));
            }

            Task <ConnectorResult <Response <string> > > result = WinApiConnector.RequestDelete <Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/DeleteRole" +
                                                                                                                     $"/{preResult.Response.Payload.FirstOrDefault().Identifier}");
            ConnectorResult <Response <string> > connectorResult = result.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }
            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }
            if (string.IsNullOrEmpty(connectorResult.Response.Payload))
            {
                return(new CommandResult("The payload of response <DELETE USER > is null or empty ", false));
            }
            return(new CommandResult($"The Role {arguments.Name} has been deleted", true));
        }
Esempio n. 5
0
        public CommandResult Execute(ConsoleContext context)
        {
            var result = WinApiConnector.RequestGet <Response <IEnumerable <User> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/User/GetAll");
            ConnectorResult <Response <IEnumerable <User> > > connectorResult = result.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }

            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }

            if (!connectorResult.Response.Payload.Any())
            {
                return(new CommandResult("There are no users.", false));
            }

            TablePresenter presenter = new TablePresenter(new [] { "Identifier", "Name", "Surname", "Login", "Password", "Issued" }, connectorResult.Response.Payload);

            return(new CommandResult(presenter.Render(), true));
        }
        protected override CommandResult Execute(ConsoleContext context, QuarryArguments arguments)
        {
            IList <Quarry> quarriesList = new List <Quarry>();
            Quarry         quarry       = new Quarry();

            if (string.IsNullOrEmpty(arguments.Identifier))
            {
                Task <ConnectorResult <Response <IEnumerable <Quarry> > > > quarries  = WinApiConnector.RequestGet <Response <IEnumerable <Quarry> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Hunting/GetQuarries");
                ConnectorResult <Response <IEnumerable <Quarry> > >         preResult = quarries.Result;
                if (!preResult.IsSuccess)
                {
                    return(new CommandResult(preResult.Message, false));
                }
                if (!preResult.Response.IsSuccess)
                {
                    return(new CommandResult(preResult.Response.Code, false));
                }
                if (!preResult.Response.Payload.Any())
                {
                    return(new CommandResult("The payload of response is null or empty", false));
                }
                int index = 1;
                foreach (var item in preResult.Response.Payload)
                {
                    System.Console.WriteLine($"{index++}. {item.Animal.Name} - amount: {item.Amount} szt.");
                    quarriesList.Add(item);
                }
                bool shouldWork = true;
                int  choisenOne = 0;

                do
                {
                    System.Console.Write("Wybierz zwierzyne do aktualizacji: ");
                    string userChoice = System.Console.ReadLine();
                    bool   isParsed   = int.TryParse(userChoice, out choisenOne);
                    shouldWork = isParsed ? choisenOne > quarriesList.Count(): true;
                }while(shouldWork);

                quarry.Identifier = quarriesList[choisenOne - 1].Identifier;
            }
            if (!string.IsNullOrEmpty(arguments.Identifier))
            {
                Task <ConnectorResult <Response <IEnumerable <Quarry> > > > result = WinApiConnector.RequestGet <Response <IEnumerable <Quarry> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Hunting/GetQuarry/" +
                                                                                                                                                    $"{arguments.Identifier}");
                ConnectorResult <Response <IEnumerable <Quarry> > > connectorResult = result.Result;
                if (!connectorResult.IsSuccess)
                {
                    return(new CommandResult(connectorResult.Message, false));
                }
                if (!connectorResult.Response.IsSuccess)
                {
                    return(new CommandResult(connectorResult.Response.Code, false));
                }
                if (!connectorResult.Response.Payload.Any())
                {
                    return(new CommandResult("The payload od response is null or empty", false));
                }
                if (connectorResult.Response.Payload.Count() != 1)
                {
                    return(new CommandResult("Too many results in payload", false));
                }
                quarry.Identifier = connectorResult.Response.Payload.FirstOrDefault().Identifier;
            }
            quarry.Amount = arguments.Amount;
            Task <ConnectorResult <Response <string> > > update     = WinApiConnector.RequestPut <Quarry, Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Hunting/UpdateQuarry", quarry);
            ConnectorResult <Response <string> >         postResult = update.Result;

            if (!postResult.IsSuccess)
            {
                return(new CommandResult(postResult.Message, false));
            }
            if (!postResult.Response.IsSuccess)
            {
                return(new CommandResult(postResult.Response.Code, false));
            }
            if (string.IsNullOrEmpty(postResult.Response.Payload))
            {
                return(new CommandResult("The paylod of response is null or empty", false));
            }
            return(new CommandResult(postResult.Response.Payload, true));
        }
Esempio n. 7
0
        protected override CommandResult Execute(ConsoleContext context, UserArguments arguments)
        {
            Task <ConnectorResult <Response <IEnumerable <Role> > > > roles           = WinApiConnector.RequestGet <Response <IEnumerable <Role> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetAllRoles");
            ConnectorResult <Response <IEnumerable <Role> > >         connectorResult = roles.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }

            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }

            if (!connectorResult.Response.Payload.Any())
            {
                return(new CommandResult("There are no roles for choice.", false));
            }

            Role[] roleArray = connectorResult.Response.Payload.ToArray();
            System.Console.WriteLine($"Wybierz role dla nowego użytkownika: (1 - {roleArray.Length})");

            int index = 1;

            foreach (Role item in roleArray)
            {
                System.Console.WriteLine($"{ index++ }. { item.Name.ToUpper() }");
            }

            int  chosenRoleIndex = 0;
            bool shouldWork      = true;

            do
            {
                System.Console.Write("Wybierz numer roli: ");
                string choice   = System.Console.ReadLine();
                bool   isParsed = int.TryParse(choice, out chosenRoleIndex);
                shouldWork = isParsed ? chosenRoleIndex > roleArray.Length : true;
            }while(shouldWork);

            var role = new Role()
            {
                Identifier = roleArray[chosenRoleIndex - 1].Identifier
            };

            var result = WinApiConnector.RequestPost <Model.User, Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/User/SignUp",
                                                                                      new Model.User()
            {
                Login    = arguments.Login.ToLower(),
                Name     = arguments.Name.ToLower(),
                Password = arguments.Password,
                Surname  = arguments.Surname.ToLower(),
                Role     = role,
                Email    = arguments.Email.ToLower()
            });

            ConnectorResult <Response <string> > postResult = result.Result;

            if (!postResult.IsSuccess)
            {
                return(new CommandResult(postResult.Message, false));
            }

            if (!postResult.Response.IsSuccess)
            {
                return(new CommandResult(postResult.Response.Code, false));
            }

            if (string.IsNullOrEmpty(postResult.Response.Payload))
            {
                return(new CommandResult("The payload of user is empty.", false));
            }

            return(new CommandResult("The user has been added.", true));
        }
        protected override CommandResult Execute(ConsoleContext context, AnimalArguments arguments)
        {
            IList <Animal> animals      = new List <Animal>();
            Animal         updateAnimal = new Animal();

            if (string.IsNullOrEmpty(arguments.Rename))
            {
                return(new CommandResult("Przy update wymagany parametr -r <NAZWA>"));
            }

            if (string.IsNullOrEmpty(arguments.Name))
            {
                Task <ConnectorResult <Response <IEnumerable <Animal> > > > animalsList = WinApiConnector.RequestGet <Response <IEnumerable <Animal> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetAllAnimals");
                ConnectorResult <Response <IEnumerable <Animal> > >         preResponse = animalsList.Result;
                if (!preResponse.IsSuccess)
                {
                    return(new CommandResult(preResponse.Message, false));
                }
                if (!preResponse.Response.IsSuccess)
                {
                    return(new CommandResult(preResponse.Response.Code, false));
                }
                if (!preResponse.Response.Payload.Any())
                {
                    return(new CommandResult("The payload od request is null or empty", false));
                }

                foreach (var preRes in preResponse.Response.Payload)
                {
                    animals.Add(preRes);
                    System.Console.WriteLine($"{preRes.Name}");
                }

                bool shouldWork = true;
                int  choisenOne = 0;
                do
                {
                    string readLine = System.Console.ReadLine();
                    bool   isParsed = int.TryParse(readLine, out choisenOne);
                    shouldWork = isParsed ? choisenOne > animals.Count() : true;
                }while(shouldWork);

                updateAnimal.Identifier = animals[choisenOne - 1].Identifier;
            }
            if (!string.IsNullOrEmpty(arguments.Name))
            {
                Task <ConnectorResult <Response <IEnumerable <Animal> > > > existAnimal = WinApiConnector.RequestGet <Response <IEnumerable <Animal> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetAnimal/" +
                                                                                                                                                         $"{arguments.Name.ToLower()}");
                ConnectorResult <Response <IEnumerable <Animal> > > connectorResult = existAnimal.Result;

                if (!connectorResult.IsSuccess)
                {
                    return(new CommandResult(connectorResult.Message, false));
                }
                if (!connectorResult.Response.IsSuccess)
                {
                    return(new CommandResult(connectorResult.Response.Code, false));
                }
                if (!connectorResult.Response.Payload.Any())
                {
                    return(new CommandResult("The Payload of request is null or empty", false));
                }
                if (connectorResult.Response.Payload.Count() != 1)
                {
                    return(new CommandResult("There is too many results", true));
                }

                updateAnimal.Identifier = connectorResult.Response.Payload.FirstOrDefault().Identifier;
            }

            updateAnimal.Name = arguments.Rename.ToLower();
            Task <ConnectorResult <Response <string> > > result     = WinApiConnector.RequestPut <Animal, Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/UpdateAnimal", updateAnimal);
            ConnectorResult <Response <string> >         postResult = result.Result;

            if (!postResult.IsSuccess)
            {
                return(new CommandResult(postResult.Message, false));
            }
            if (!postResult.Response.IsSuccess)
            {
                return(new CommandResult(postResult.Response.Code, false));
            }
            if (postResult.Response.Payload == null)
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }

            return(new CommandResult($"The Animal {updateAnimal.Identifier} has been updated", true));
        }
Esempio n. 9
0
        protected override CommandResult Execute(ConsoleContext context, UserArgumentsExtension arguments)
        {
            IList <User> users      = new List <User>();
            User         updateUser = new User();
            Guid         identifier = Guid.Empty;

            if (string.IsNullOrEmpty(arguments.Identifier) && string.IsNullOrEmpty(arguments.Login))
            {
                Task <ConnectorResult <Response <IEnumerable <User> > > > usersRequest = WinApiConnector.RequestGet <Response <IEnumerable <User> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/User/GetAll");
                ConnectorResult <Response <IEnumerable <User> > >         preResponse  = usersRequest.Result;

                if (!preResponse.IsSuccess)
                {
                    return(new CommandResult(preResponse.Message, false));
                }
                if (!preResponse.Response.IsSuccess)
                {
                    return(new CommandResult(preResponse.Response.Code, false));
                }
                if (!preResponse.Response.Payload.Any())
                {
                    return(new CommandResult("The payload od request is null or empty", false));
                }
                int index = 1;
                foreach (var ur in preResponse.Response.Payload)
                {
                    users.Add(ur);
                    System.Console.WriteLine($"{index++}. {ur.Login} - {ur.Identifier}");
                }

                System.Console.WriteLine($"Wybierz numer użytkownika do aktualizacji 1-{users.Count}");
                bool shouldWork = true;
                int  choisenOne = 0;
                do
                {
                    string readLine = System.Console.ReadLine();
                    bool   isParsed = int.TryParse(readLine, out choisenOne);
                    shouldWork = isParsed ? choisenOne > users.Count : true;
                }while(shouldWork);

                updateUser.Identifier = users[choisenOne - 1].Identifier;
            }

            if (!string.IsNullOrEmpty(arguments.Identifier))
            {
                var tmp = Guid.TryParse(arguments.Identifier, out identifier);
                if (!tmp)
                {
                    return(new CommandResult("Cannot parse identifier from -i param", false));
                }

                updateUser.Identifier = identifier;
            }

            if (!string.IsNullOrEmpty(arguments.Login) && string.IsNullOrEmpty(arguments.Identifier))
            {
                User tmpUser = new User()
                {
                    Login = arguments.Login
                };
                Task <ConnectorResult <Response <IEnumerable <User> > > > userByLogin = WinApiConnector.RequestGet <Response <IEnumerable <User> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/User/GetByLogin/"
                                                                                                                                                     + $"{arguments.Login}");
                ConnectorResult <Response <IEnumerable <User> > > preResponse = userByLogin.Result;

                if (!preResponse.IsSuccess)
                {
                    return(new CommandResult(preResponse.Message, false));
                }
                if (!preResponse.Response.IsSuccess)
                {
                    return(new CommandResult(preResponse.Response.Code, false));
                }
                if (!preResponse.Response.Payload.Any())
                {
                    return(new CommandResult("The Payload of request is null or empty", false));
                }
                if (preResponse.Response.Payload.Count() != 1)
                {
                    return(new CommandResult("There is too many results", true));
                }

                updateUser.Identifier = preResponse.Response.Payload.FirstOrDefault().Identifier;
            }

            updateUser.Name     = arguments.Name;
            updateUser.Surname  = arguments.Surname;
            updateUser.Password = arguments.Password;
            updateUser.Email    = arguments.Email;
            Task <ConnectorResult <Response <User> > > result       = WinApiConnector.RequestPut <User, Response <User> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/User/Update", updateUser);
            ConnectorResult <Response <User> >         postResponse = result.Result;

            if (!postResponse.IsSuccess)
            {
                return(new CommandResult(postResponse.Message, false));
            }
            if (!postResponse.Response.IsSuccess)
            {
                return(new CommandResult(postResponse.Response.Code, false));
            }
            if (postResponse.Response.Payload == null)
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }

            return(new CommandResult($"The User {updateUser.Identifier} has been updated", true));
        }
Esempio n. 10
0
        public CommandResult Execute(ConsoleContext context)
        {
            Task <ConnectorResult <Response <IEnumerable <Quarry> > > > quarrier        = WinApiConnector.RequestGet <Response <IEnumerable <Quarry> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Hunting/GetQuarries");
            ConnectorResult <Response <IEnumerable <Quarry> > >         connectorResult = quarrier.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }
            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }
            if (!connectorResult.Response.Payload.Any())
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }

            int index = 1;

            foreach (var quarry in connectorResult.Response.Payload)
            {
                System.Console.WriteLine($"{index++}. {quarry.Animal.Name.ToUpper()} - {quarry.Amount}");
            }
            return(new CommandResult("OK", true));
        }
        protected override CommandResult Execute(ConsoleContext context, AnimalArguments arguments)
        {
            Animal         deleteAnimal = new Animal();
            IList <Animal> animals      = new List <Animal>();

            if (string.IsNullOrEmpty(arguments.Name))
            {
                Task <ConnectorResult <Response <IEnumerable <Animal> > > > existsAnimals = WinApiConnector.RequestGet <Response <IEnumerable <Animal> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/");
                ConnectorResult <Response <IEnumerable <Animal> > >         preResult     = existsAnimals.Result;
                if (!preResult.IsSuccess)
                {
                    return(new CommandResult(preResult.Message, false));
                }
                if (!preResult.Response.IsSuccess)
                {
                    return(new CommandResult(preResult.Response.Code, false));
                }
                if (!preResult.Response.Payload.Any())
                {
                    return(new CommandResult("The payload of request is null or empty", false));
                }
                foreach (var animal in preResult.Response.Payload)
                {
                    animals.Add(animal);
                    System.Console.WriteLine($"{animal}");
                }
                bool shouldWork = true;
                int  choisenOne = 0;

                do
                {
                    string readLine = System.Console.ReadLine();
                    bool   isParsed = int.TryParse(readLine, out choisenOne);
                    shouldWork = isParsed ? choisenOne > animals.Count() : true;
                }while(shouldWork);

                deleteAnimal.Identifier = animals[choisenOne - 1].Identifier;
            }

            if (!string.IsNullOrEmpty(arguments.Name))
            {
                Task <ConnectorResult <Response <IEnumerable <Animal> > > > existAnimal     = WinApiConnector.RequestGet <Response <IEnumerable <Animal> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetAnimal" + $"/{arguments.Name.ToLower()}");
                ConnectorResult <Response <IEnumerable <Animal> > >         connectorResult = existAnimal.Result;

                if (!connectorResult.IsSuccess)
                {
                    return(new CommandResult(connectorResult.Message, false));
                }
                if (!connectorResult.Response.IsSuccess)
                {
                    return(new CommandResult(connectorResult.Response.Code, false));
                }
                if (!connectorResult.Response.Payload.Any())
                {
                    return(new CommandResult("The paylod of request is null or empty", false));
                }
                if (connectorResult.Response.Payload.Count() != 1)
                {
                    return(new CommandResult("There is too many results", false));
                }
                deleteAnimal.Identifier = connectorResult.Response.Payload.FirstOrDefault().Identifier;
            }

            Task <ConnectorResult <Response <string> > > deleteResult = WinApiConnector.RequestDelete <Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/DeleteAnimal" + $"/{deleteAnimal.Identifier}");
            ConnectorResult <Response <string> >         postResult   = deleteResult.Result;

            if (!postResult.IsSuccess)
            {
                return(new CommandResult(postResult.Message, false));
            }
            if (!postResult.Response.IsSuccess)
            {
                return(new CommandResult(postResult.Response.Code, false));
            }
            if (string.IsNullOrEmpty(postResult.Response.Payload))
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }

            return(new CommandResult(postResult.Response.Payload, true));
        }
Esempio n. 12
0
        public CommandResult Execute(ConsoleContext context)
        {
            Task <ConnectorResult <Response <IEnumerable <Animal> > > > result          = WinApiConnector.RequestGet <Response <IEnumerable <Animal> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetAllAnimals");
            ConnectorResult <Response <IEnumerable <Animal> > >         connectorResult = result.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }
            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }
            if (!connectorResult.Response.Payload.Any())
            {
                return(new CommandResult("The payload of request is null or empty", false));
            }

            int index = 1;

            foreach (var animal in connectorResult.Response.Payload)
            {
                System.Console.WriteLine($"{index++}. {animal.Name.ToUpper()}");
            }

            return(new CommandResult("OK", true));
        }
        protected override CommandResult Execute(ConsoleContext context, QuarryArguments arguments)
        {
            List <Quarry> quarries = new List <Quarry>();
            Quarry        quarry   = new Quarry();

            if (string.IsNullOrEmpty(arguments.Identifier))
            {
                Task <ConnectorResult <Response <IEnumerable <Quarry> > > > result    = WinApiConnector.RequestGet <Response <IEnumerable <Quarry> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Hunting/GetQuarries");
                ConnectorResult <Response <IEnumerable <Quarry> > >         preResult = result.Result;
                if (!preResult.IsSuccess)
                {
                    return(new CommandResult(preResult.Message, false));
                }
                if (!preResult.Response.IsSuccess)
                {
                    return(new CommandResult(preResult.Response.Code, false));
                }
                if (!preResult.Response.Payload.Any())
                {
                    return(new CommandResult("The payload of response is null or empty", false));
                }

                foreach (var item in preResult.Response.Payload)
                {
                    System.Console.WriteLine($"{item.Animal.Name} - {item.Amount}");
                    quarries.Add(item);
                }

                bool shoudWork  = true;
                int  choisenOne = 0;

                do
                {
                    System.Console.Write("WYbierz zwierzyne do usunięcia");
                    string readLine = System.Console.ReadLine();
                    bool   isParsed = int.TryParse(readLine, out choisenOne);
                    shoudWork = isParsed ? choisenOne > quarries.Count() : true;
                }while(shoudWork);

                quarry.Identifier = quarries[choisenOne - 1].Identifier;
            }
            if (!string.IsNullOrEmpty(arguments.Identifier))
            {
                Guid deleteGuid   = Guid.Empty;
                bool isParsedGuid = Guid.TryParse(arguments.Identifier, out deleteGuid);
                if (isParsedGuid)
                {
                    quarry.Identifier = deleteGuid;
                }
                return(new CommandResult("The Identifier parsing error. Please use the correct identifier or do not use the -i param"));
            }

            Task <ConnectorResult <Response <string> > > deleteResult = WinApiConnector.RequestDelete <Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Hunting/DeleteQuarry" +
                                                                                                                           $"{quarry.Identifier}");
            ConnectorResult <Response <string> > connectorResult = deleteResult.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }
            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }
            if (string.IsNullOrEmpty(connectorResult.Response.Payload))
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }
            return(new CommandResult(connectorResult.Response.Payload, true));
        }
        protected override CommandResult Execute(ConsoleContext context, HuntingArguments arguments)
        {
            IList <User>           usersList       = new List <User>();
            IList <UserHunting>    users           = new List <UserHunting>();
            IList <Animal>         animals         = new List <Animal>();
            IList <Quarry>         quarries        = new List <Quarry>();
            IList <PartialHunting> partialHuntings = new List <PartialHunting>();
            User    leader  = new User();
            Hunting hunting = new Hunting();
            Task <ConnectorResult <Response <IEnumerable <User> > > > usersRequest = WinApiConnector.RequestGet <Response <IEnumerable <User> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/User/GetAll");
            ConnectorResult <Response <IEnumerable <User> > >         preResult    = usersRequest.Result;

            if (!preResult.IsSuccess)
            {
                return(new CommandResult(preResult.Message, false));
            }
            if (!preResult.Response.IsSuccess)
            {
                return(new CommandResult(preResult.Response.Code, false));
            }
            if (!preResult.Response.Payload.Any())
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }

            foreach (var item in preResult.Response.Payload)
            {
                usersList.Add(item);
            }

            Task <ConnectorResult <Response <IEnumerable <Animal> > > > animalsList   = WinApiConnector.RequestGet <Response <IEnumerable <Animal> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/COnfiguration/GetAllAnimals");
            ConnectorResult <Response <IEnumerable <Animal> > >         animalsResult = animalsList.Result;

            if (!animalsResult.IsSuccess)
            {
                return(new CommandResult(animalsResult.Message, false));
            }
            if (!animalsResult.Response.IsSuccess)
            {
                return(new CommandResult(animalsResult.Response.Code, false));
            }
            if (!animalsResult.Response.Payload.Any())
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }

            foreach (var item in animalsResult.Response.Payload)
            {
                animals.Add(item);
            }

            if (string.IsNullOrEmpty(arguments.Leader))
            {
                System.Console.WriteLine("Wybierz lidera");
                int index = 1;
                foreach (var item in usersList)
                {
                    System.Console.WriteLine($"{index++}. {item.Name}, {item.Surname}");
                }
                bool shouldWork = true;
                int  choisenOne = 0;

                do
                {
                    string choiceLeader = System.Console.ReadLine();
                    bool   isParsed     = int.TryParse(choiceLeader, out choisenOne);
                    shouldWork = isParsed ? choisenOne > usersList.Count() : true;
                }while(shouldWork);

                leader = usersList[choisenOne - 1];
            }
            foreach (var item in animals)
            {
                quarries.Add(new Quarry()
                {
                    Animal = item,
                    Amount = 3
                });
            }

            foreach (var item in usersList)
            {
                users.Add(new UserHunting()
                {
                    UserId = item.Identifier
                });
            }

            hunting.Leader   = leader;
            hunting.Quarries = quarries;
            hunting.Users    = users;

            for (int i = 0; i < arguments.PartialHunting; i++)
            {
                partialHuntings.Add(new PartialHunting()
                {
                    Number         = i + 1,
                    Status         = Status.Create,
                    PartialHunters = new List <PartialHuntersList>()
                });
            }

            hunting.PartialHuntings = partialHuntings;
            hunting.Description     = "Pierwsze";

            Task <ConnectorResult <Response <string> > > addResult       = WinApiConnector.RequestPost <Hunting, Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Hunting/AddHunting", hunting);
            ConnectorResult <Response <string> >         connectorResult = addResult.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }
            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }
            if (string.IsNullOrEmpty(connectorResult.Response.Payload))
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }
            return(new CommandResult("OK", true));
        }
Esempio n. 15
0
        public CommandResult Execute(ConsoleContext context)
        {
            Task <ConnectorResult <Response <IEnumerable <Role> > > > result          = WinApiConnector.RequestGet <Response <IEnumerable <Role> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetAllRoles");
            ConnectorResult <Response <IEnumerable <Role> > >         connectorResult = result.Result;

            if (!connectorResult.IsSuccess)
            {
                return(new CommandResult(connectorResult.Message, false));
            }
            if (!connectorResult.Response.IsSuccess)
            {
                return(new CommandResult(connectorResult.Response.Code, false));
            }
            if (!connectorResult.Response.Payload.Any())
            {
                return(new CommandResult("The payload of reguest is null or empty", false));
            }

            TablePresenter table = new TablePresenter(new [] { "Identifier", "Name" }, connectorResult.Response.Payload);

            return(new CommandResult(table.Render(), true));
        }
Esempio n. 16
0
        protected override CommandResult Execute(ConsoleContext context, QuarryArguments arguments)
        {
            Animal animal = new Animal();

            if (string.IsNullOrEmpty(arguments.Animal))
            {
                Task <ConnectorResult <Response <IEnumerable <Animal> > > > animals   = WinApiConnector.RequestGet <Response <IEnumerable <Animal> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetAllAnimals");
                ConnectorResult <Response <IEnumerable <Animal> > >         preResult = animals.Result;
                if (!preResult.IsSuccess)
                {
                    return(new CommandResult(preResult.Message, false));
                }
                if (!preResult.Response.IsSuccess)
                {
                    return(new CommandResult(preResult.Response.Code, false));
                }
                if (!preResult.Response.Payload.Any())
                {
                    return(new CommandResult("The payload of response is empty", false));
                }

                foreach (var item in preResult.Response.Payload)
                {
                    System.Console.WriteLine($"{item.Name.ToUpper()}");
                }
                System.Console.Write("Wpisz nazwę zwierzęcia: ");
                var readLine    = System.Console.ReadLine();
                var existAnimal = animals.Result.Response.Payload.Where(n => n.Name == readLine.ToLower()).SingleOrDefault();
                animal = existAnimal;
            }

            if (!string.IsNullOrEmpty(arguments.Animal))
            {
                Task <ConnectorResult <Response <IEnumerable <Animal> > > > existsAnimal = WinApiConnector.RequestGet <Response <IEnumerable <Animal> > >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Configuration/GetAnimal/" +
                                                                                                                                                          $"{arguments.Animal.ToLower()}");
                ConnectorResult <Response <IEnumerable <Animal> > > connectorResult = existsAnimal.Result;
                if (!connectorResult.IsSuccess)
                {
                    return(new CommandResult(connectorResult.Message, false));
                }
                if (!connectorResult.Response.IsSuccess)
                {
                    return(new CommandResult(connectorResult.Response.Code, false));
                }
                if (!connectorResult.Response.Payload.Any())
                {
                    return(new CommandResult($"The Animal {arguments.Animal.ToUpper()} doesn't exists", false));
                }
                if (connectorResult.Response.Payload.Count() != 1)
                {
                    return(new CommandResult("Too many results", false));
                }

                animal = connectorResult.Response.Payload.FirstOrDefault();
            }

            Task <ConnectorResult <Response <string> > > result = WinApiConnector.RequestPost <Model.Quarry, Response <string> >($"{context.ConsoleSettings.ServerAddress}:{context.ConsoleSettings.Port}/Api/Hunting/AddQuarry",
                                                                                                                                 new Model.Quarry()
            {
                Animal = animal,
                Amount = arguments.Amount
            });
            ConnectorResult <Response <string> > postResult = result.Result;

            if (!postResult.IsSuccess)
            {
                return(new CommandResult(postResult.Message, false));
            }
            if (!postResult.Response.IsSuccess)
            {
                return(new CommandResult(postResult.Response.Code, false));
            }
            if (string.IsNullOrEmpty(postResult.Response.Payload))
            {
                return(new CommandResult("The payload of response is null or empty", false));
            }
            return(new CommandResult(postResult.Response.Payload, true));
        }