示例#1
0
        private void Show()
        {
            DBService dbService = new DBService();

            OutputService.DisplayConsole(dbService.GetAllServices());
            dbService.Dispose();
        }
示例#2
0
        public static int GetValidatedUserChoice(Enum inputEnum)
        {
            OutputService.DisplayConsole("Choose option: ");

            int i = 0;

            foreach (var item in Enum.GetValues(inputEnum.GetType()))
            {
                OutputService.DisplayConsole("   " + i + " - " + item);
                i++;
            }

            int  index       = -1;
            bool parseResult = false;

            while (index < 0 || index > i - 1 || !parseResult)
            {
                string input = ReadDataFromConsole();
                parseResult = Int32.TryParse(input, out index);
                if (index < 0 || index >= i || !parseResult)
                {
                    OutputService.DisplayConsole("Error, please enter correct number");
                }
            }

            return(index);
        }
示例#3
0
        public List <Service> ReadXmlFromFile()
        {
            string         path     = Global.InputPath;
            List <Service> services = new List <Service>();

            try
            {
                if (!File.Exists(path))
                {
                    using (File.Create(path))
                    {
                    }
                }

                using (StreamReader stream = new StreamReader(path))
                {
                    services = (List <Service>) new XmlSerializer(typeof(List <Service>)).Deserialize(stream);
                }
            }
            catch (Exception ex)
            {
                OutputService.DisplayConsole("Error: " + ex.InnerException);
                InputService.ReadDataFromConsole();
            }
            return(services);
        }
示例#4
0
        private void Sort()
        {
            SortType       sortType = (SortType)GetValidatedUserChoice(new SortType());
            ServiceHandler sH       = new ServiceHandler();
            List <Service> list     = sH.GetListSortedBy(sortType);

            OutputService.DisplayConsole(list);
            OutputService.DisplayConsole("");
        }
示例#5
0
        public void RemoveService(int id)
        {
            Service removeService = db.Services.FirstOrDefault(x => x.Id == id);

            if (removeService != null)
            {
                db.Services.Remove(removeService);
                OutputService.DisplayConsole("Service was successfully removed");
            }
            else
            {
                OutputService.DisplayConsole("There is no element with such Id. Please try again.");
            }
            db.SaveChanges();
        }
示例#6
0
        public void Run()
        {
            while (true)
            {
                Console.Clear();
                OutputService.DisplayConsole("");
                OutputService.DisplayConsole("Please enter command:");
                Commands command = GetCommand();
                //Actions[(int)Global.Command]();
                switch (command)
                {
                case Commands.Show:
                    Show();
                    break;

                case Commands.Sort:
                    Sort();
                    break;

                case Commands.Add:
                    AddService();
                    break;

                case Commands.Load:
                    Load();
                    break;

                case Commands.Export:
                    Export();
                    break;

                case Commands.Exit:
                    Exit();
                    break;

                case Commands.Edit:
                    Edit();
                    break;

                case Commands.Delete:
                    Delete();
                    break;
                }
                ReadDataFromConsole();
            }
        }
示例#7
0
 private static void GetInputPath()
 {
     OutputService.DisplayConsole(@"Please enter file location (example c:\myFile.txt):");
     while (true)
     {
         string path = ReadDataFromConsole();
         if (File.Exists(path))
         {
             Global.InputPath = path;
             break;
         }
         else
         {
             OutputService.DisplayConsole(@"Your path is not correct");
         }
     }
 }
示例#8
0
        private void Delete()
        {
            OutputService.DisplayConsole("Enter Id of Service:");
            string    choice    = ReadDataFromConsole();
            int       i         = StringValidation.ValidatePositiveInt(choice);
            DBService dbService = new DBService();

            if (i > 0 && dbService.ServiceExist(i))
            {
                dbService.RemoveService(i);
            }
            else
            {
                OutputService.DisplayConsole("Id is not valid");
            }
            dbService.Dispose();
        }
示例#9
0
        public List <Service> ReadJsonFromFile()
        {
            List <Service> items = new List <Service>();

            try
            {
                using (StreamReader r = new StreamReader(Global.InputPath))
                {
                    string json = r.ReadToEnd();
                    items = JsonConvert.DeserializeObject <List <Service> >(json);
                }
            }
            catch (Exception ex)
            {
                OutputService.DisplayConsole("Error: " + ex.Message);
                InputService.ReadDataFromConsole();
            }
            return(items);
        }
示例#10
0
        private void Export()
        {
            Global.OutputDataSource = (DataSource)GetValidatedUserChoice(new DataSource());

            if (Global.OutputDataSource != DataSource.DefaultLocation)
            {
                Global.OutputDataType = (DataType)GetValidatedUserChoice(new DataType());

                OutputService.DisplayConsole(@"Please enter file location (example c:\myFile.txt):");
                while (true)
                {
                    Global.OutputPath = ReadDataFromConsole();
                    break;
                }
            }
            else
            {
                Global.OutputPath = @"..\..\Data\services.xml";
            }


            DBService      dbService = new DBService();
            List <Service> services  = dbService.GetAllServices();

            dbService.Dispose();

            switch (Global.OutputDataType)
            {
            case DataType.JSON:
                JsonService jsonWriteService = new JsonService();
                jsonWriteService.WriteServiceToJsonFile(services);
                break;

            case DataType.XML:
                XmlService xmlWriteService = new XmlService();
                xmlWriteService.WriteServicesToXmlFile();
                break;
            }
        }
示例#11
0
        private void Edit()
        {
            OutputService.DisplayConsole("Enter Id of Service:");
            string    choice    = ReadDataFromConsole();
            int       i         = StringValidation.ValidatePositiveInt(choice);
            DBService dbService = new DBService();

            if (i > 0 && dbService.ServiceExist(i))
            {
                OutputService.DisplayConsole("Your service to edit:");
                OutputService.DisplayConsole(dbService.GetService(i));
                ServiceHandler serviceHandler = new ServiceHandler();
                Service        newService     = serviceHandler.CreateServiceFromConsole();
                newService.Id = i;

                dbService.Update(newService);
            }
            else
            {
                OutputService.DisplayConsole("Id is not valid");
            }
            dbService.Dispose();
        }