예제 #1
0
        /// <summary>
        /// Updates existing Employee to XML
        /// </summary>
        /// <param name="empName">Name of Employee</param>
        /// <returns>Update Status</returns>
        public bool UpdateEmployee(string empName)
        {
            _logger.LogTrace("UpdateEmployee starts. Params name: {0}", empName);
            if (empName == null)
            {
                System.Console.WriteLine("Name of Employee cannot be null");
                return(false);
            }
            Employee employee = new Employee();

            try
            {
                var emp = GetEmployeeByName(empName);

                if (emp.Name == null)
                {
                    System.Console.WriteLine("Name of Employee provided did not matched with existing records. Please retry");
                    return(false);
                }
                var doc = XDocument.Load(Directory.GetCurrentDirectory() + xmlPath);
                employee.ID = emp.ID;
                System.Console.WriteLine("Existing value is {0}. If you want to change the Name of Employee. Enter new Name else Press # to skip", emp.Name);
                string updateName = GetInputsFromConsole.GetDetails <string>();
                employee.Name = updateName == "#" ? emp.Name : updateName;

                System.Console.WriteLine("Existing value is {0}. If you want to change the Age of Employee. Enter new Age else Press 0 to skip", emp.Age);
                string updatedAge = GetInputsFromConsole.GetDetails <string>();
                employee.Age = updatedAge == "0" ? emp.Age : int.Parse(updatedAge);

                System.Console.WriteLine("Existing value is {0}. If you want to change the Designation of Employee. Enter new Designation else Press # to skip", emp.Designation);
                string updatedDesig = GetInputsFromConsole.GetDetails <string>();
                employee.Designation = updatedDesig == "#" ? emp.Designation : updatedDesig;

                XElement updatedEmployee = doc.Descendants("employee").Where(_ => _.Descendants("name").FirstOrDefault().Value == emp.Name).FirstOrDefault();
                updatedEmployee.Descendants("name").FirstOrDefault().Value        = employee.Name;
                updatedEmployee.Descendants("age").FirstOrDefault().Value         = employee.Age.ToString();
                updatedEmployee.Descendants("designation").FirstOrDefault().Value = employee.Designation;

                SaveXML(doc);
                System.Console.WriteLine("Successfully Updated new Employee");
                System.Console.WriteLine(JsonConvert.SerializeObject(employee));
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception occured: " + e.Message);
                return(false);
            }
            _logger.LogTrace("AddEmployee Ends.");
            return(true);
        }
예제 #2
0
        /// <summary>
        /// Deletes existing Employee from XML
        /// </summary>
        /// <param name="empName">Name of Employee</param>
        /// <returns>Delete Status</returns>
        public bool DeleteEmployee(string empName)
        {
            _logger.LogTrace("DeleteEmployee starts. Params name: {0}", empName);
            if (empName == null)
            {
                System.Console.WriteLine("Name of Employee cannot be null. Please retry");
                return(false);
            }
            Employee employee = new Employee();

            try
            {
                var emp = GetEmployeeByName(empName);
                if (emp.Name == null)
                {
                    System.Console.WriteLine("Name of Employee provided did not matched with existing records. Please retry");
                    return(false);
                }
                else
                {
                    System.Console.WriteLine("Data found with Name: {0}. Are you sure to delete the Employee then Press Y", emp.Name);


                    if (GetInputsFromConsole.GetDetails <string>().ToLower() == "y")
                    {
                        var doc = XDocument.Load(Directory.GetCurrentDirectory() + xmlPath);
                        doc.Root.Descendants("employee").Where(n => n.Descendants("name").FirstOrDefault().Value == emp.Name).Remove();
                        SaveXML(doc);
                        System.Console.WriteLine("Successfully deleted an Employeewith Name: {0}", emp.Name);
                    }
                    else
                    {
                        System.Console.WriteLine("Delete operation cancelled");
                        return(true);
                    }

                    // return true;
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception occured: " + e.Message);
                return(false);
            }
            _logger.LogTrace("DeleteEmployee Ends.");
            return(true);
        }
예제 #3
0
        /// <summary>
        /// Adds new Employee to XML
        /// </summary>
        /// <returns>Add Status</returns>
        public bool AddEmployee()
        {
            Employee employee = new Employee();

            _logger.LogTrace("AddEmployee starts.");
            try
            {
                var doc = XDocument.Load(Directory.GetCurrentDirectory() + xmlPath);
                employee.ID = GetAllEmployeesCount() + 1;
                System.Console.WriteLine("Please Enter Name of Employee and press Enter");
                employee.Name = GetInputsFromConsole.GetDetails <string>();
                System.Console.WriteLine("Please Enter Age of {0} and press Enter", employee.Name);
                employee.Age = Int32.Parse(GetInputsFromConsole.GetDetails <int>());
                System.Console.WriteLine("Please Enter Designation of {0} and press Enter", employee.Name);
                employee.Designation = GetInputsFromConsole.GetDetails <string>();



                XElement newEmployee = new XElement("employee");
                XElement id          = new XElement("id"); id.Value = employee.ID.ToString();
                XElement name        = new XElement("name"); name.Value = employee.Name;
                XElement age         = new XElement("age"); age.Value = employee.Age.ToString();
                XElement designation = new XElement("designation"); designation.Value = employee.Designation;
                newEmployee.Add(id, name, age, designation);
                doc.Root.Add(newEmployee);
                SaveXML(doc);
                System.Console.WriteLine("Successfully Added new Employee");
                System.Console.WriteLine(JsonConvert.SerializeObject(newEmployee));
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception occured: " + e.Message);
                return(false);
            }
            _logger.LogTrace("AddEmployee Ends.");
            return(true);
        }
예제 #4
0
        /// <summary>
        /// This method controls the flow. All Employee CRUD happens here.
        /// </summary>
        public void Run()
        {
            System.Console.WriteLine($"This is a console application for {_config.ConsoleTitle}");
            System.Console.WriteLine($"Choose option from below to perform the operation.");
            System.Console.WriteLine($"1 - Read the XML file.");
            System.Console.WriteLine($"2 - Add new employee.");
            System.Console.WriteLine($"3 - Update an existing employee.");
            System.Console.WriteLine($"4 - Delete existing employee.");

            bool retry = true; int counter = 0; string empName = "";
            int  op = 0;

            op = Int32.Parse(GetInputsFromConsole.GetDetails <int>());
            while (retry && counter < _config.MaxRepeatAllowed)
            {
                switch (op)
                {
                case 1:     //ReadAll
                    System.Console.WriteLine("Reading the XML data: Started.");
                    retry = !_xmlService.ReadAllEmployees();
                    break;

                case 3:     // Update
                    System.Console.WriteLine("Enter the Employee Name to be updated. Should be a Complete match.");
                    empName = GetInputsFromConsole.GetDetails <string>();
                    retry   = !_xmlService.UpdateEmployee(empName);
                    break;

                case 2:     // Add
                    System.Console.WriteLine("Add Employee.");
                    retry = !_xmlService.AddEmployee();
                    break;

                case 4:     // Delete
                    System.Console.WriteLine("Delete operation started. \n Enter the Employee Name to be deleted. Should be a Complete match.");
                    empName = GetInputsFromConsole.GetDetails <string>();
                    retry   = !_xmlService.DeleteEmployee(empName);
                    break;

                default:
                    break;
                }
                System.Console.WriteLine("{0} repeats left. Press R, to repeat the same operation. " +
                                         "\n For switching operation, Press respective operation number.\n " +
                                         "Press Enter to exit if earlier operation was successful", _config.MaxRepeatAllowed - counter - 1);
                string repeatInput  = GetInputsFromConsole.GetDetails <string>();
                int    parsedResult = 0;
                Int32.TryParse(repeatInput, out parsedResult);
                if (repeatInput.ToLower() == "r")
                {
                    retry = true;
                }
                else if (parsedResult > 0 && parsedResult <= _config.ValidOperationCount)
                {
                    op    = Int32.Parse(repeatInput);
                    retry = true;
                }
                counter++;
            }

            System.Console.WriteLine($"Thank you. Exiting now. To increase retries change value in appsettings.json");
            System.Console.ReadKey();
        }
예제 #5
0
        /// <summary>
        /// Updates existing Employee to XML
        /// </summary>
        /// <param name="empName">Name of Employee</param>
        /// <returns>Update Status</returns>
        public bool UpdateEmployee(string empName)
        {
            _logger.LogTrace("UpdateEmployee starts. Params name: {0}", empName);
            if (empName == null)
            {
                System.Console.WriteLine("Name of Employee cannot be null");
                return(false);
            }
            EmployeeExtended employee = new EmployeeExtended();

            try
            {
                var emp = GetEmployeeByName(empName);

                if (emp.Name == null)
                {
                    System.Console.WriteLine("Name of Employee provided did not matched with existing records. Please retry");
                    return(false);
                }
                var doc = XDocument.Load(Directory.GetCurrentDirectory() + xmlPath);
                employee.ID = emp.ID;
                System.Console.WriteLine("Existing value is {0}. If you want to change the Name of Employee. Enter new Name else Press # to skip", emp.Name);
                string updateName = GetInputsFromConsole.GetDetails <string>();
                employee.Name = updateName == "#" ? emp.Name : updateName;

                System.Console.WriteLine("Existing value is {0}. If you want to change the Age of Employee. Enter new Age else Press 0 to skip", emp.Age);
                string updatedAge = GetInputsFromConsole.GetDetails <string>();
                employee.Age = updatedAge == "0" ? emp.Age : int.Parse(updatedAge);

                System.Console.WriteLine("Existing value is {0}. If you want to change the Designation of Employee. Enter new Designation else Press # to skip", emp.Designation);
                string updatedDesig = GetInputsFromConsole.GetDetails <string>();
                employee.Designation = updatedDesig == "#" ? emp.Designation : updatedDesig;

                System.Console.WriteLine("If you want to change the Address of Employee. Enter any letter else Press # and Enter to skip address");
                string updatedAddress = GetInputsFromConsole.GetDetails <string>();



                XElement updatedEmployee = doc.Descendants("employee").Where(_ => _.Descendants("name").FirstOrDefault().Value == emp.Name).FirstOrDefault();
                updatedEmployee.Descendants("name").FirstOrDefault().Value        = employee.Name;
                updatedEmployee.Descendants("age").FirstOrDefault().Value         = employee.Age.ToString();
                updatedEmployee.Descendants("designation").FirstOrDefault().Value = employee.Designation;


                if (updatedAddress == "#")
                {
                    employee.Address = emp.Address;
                }
                else
                {
                    employee.Address = emp.Address;
                    System.Console.WriteLine("If you want to change the Address of Employee. Enter new Door Number else Press # to skip");
                    string doorNoValue = GetInputsFromConsole.GetDetails <string>();
                    employee.Address.DoorNo = doorNoValue == "#" ? emp.Address.DoorNo : doorNoValue;

                    System.Console.WriteLine("If you want to change the street of Employee. Enter new street else Press # to skip");
                    string streetValue = GetInputsFromConsole.GetDetails <string>();
                    employee.Address.street = streetValue == "#" ? emp.Address.street : streetValue;

                    System.Console.WriteLine("If you want to change the Town of Employee. Enter new Town else Press # to skip");
                    string townValue = GetInputsFromConsole.GetDetails <string>();
                    employee.Address.Town = townValue == "#" ? emp.Address.Town : townValue;

                    System.Console.WriteLine("If you want to change the state of Employee. Enter new state else Press # to skip");
                    string stateValue = GetInputsFromConsole.GetDetails <string>();
                    employee.Address.State = stateValue == "#" ? emp.Address.Town : stateValue;


                    updatedEmployee.Descendants("doorNo").FirstOrDefault().Value = employee.Address.DoorNo;
                    updatedEmployee.Descendants("street").FirstOrDefault().Value = employee.Address.street;
                    updatedEmployee.Descendants("town").FirstOrDefault().Value   = employee.Address.Town;
                    updatedEmployee.Descendants("state").FirstOrDefault().Value  = employee.Address.State;
                }


                SaveXML(doc);
                System.Console.WriteLine("Successfully Updated new Employee");
                System.Console.WriteLine(JsonConvert.SerializeObject(employee));
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception occured: " + e.Message);
                return(false);
            }
            _logger.LogTrace("UpdateEmployee Ends.");
            return(true);
        }
예제 #6
0
        /// <summary>
        /// Adds new Employee to XML
        /// </summary>
        /// <returns>Add Status</returns>
        public bool AddEmployee()
        {
            EmployeeExtended employee = new EmployeeExtended();

            _logger.LogTrace("AddEmployee starts.");
            try
            {
                var doc = XDocument.Load(Directory.GetCurrentDirectory() + xmlPath);
                employee.ID = GetAllEmployeesCount() + 1;
                System.Console.WriteLine("Please Enter Name of Employee and press Enter");
                employee.Name = GetInputsFromConsole.GetDetails <string>();
                System.Console.WriteLine("Please Enter Age of {0} and press Enter", employee.Name);
                employee.Age = Int32.Parse(GetInputsFromConsole.GetDetails <int>());
                System.Console.WriteLine("Please Enter Designation of {0} and press Enter", employee.Name);
                employee.Designation = GetInputsFromConsole.GetDetails <string>();
                System.Console.WriteLine("If you don't want to Enter Address of {0} then press Enter else Please enter DoorNo", employee.Name);
                string doorNoValue = GetInputsFromConsole.GetDetails <string>();
                if (doorNoValue == "")
                {
                    employee.Address = new Address();
                }
                else
                {
                    System.Console.WriteLine("Please enter street");
                    string streetValue = GetInputsFromConsole.GetDetails <string>();
                    System.Console.WriteLine("Please enter town");
                    string townValue = GetInputsFromConsole.GetDetails <string>();
                    System.Console.WriteLine("Please enter state");
                    string stateValue = GetInputsFromConsole.GetDetails <string>();
                    employee.Address = new Address()
                    {
                        DoorNo = doorNoValue,
                        State  = streetValue,
                        street = townValue,
                        Town   = stateValue,
                    };
                }



                XElement newEmployee = new XElement("employee");
                XElement newAddress  = new XElement("address");
                XElement id          = new XElement("id"); id.Value = employee.ID.ToString();
                XElement name        = new XElement("name"); name.Value = employee.Name;
                XElement age         = new XElement("age"); age.Value = employee.Age.ToString();
                XElement designation = new XElement("designation"); designation.Value = employee.Designation;
                XElement doorNo      = new XElement("doorNo"); doorNo.Value = employee.Address?.DoorNo ?? "";
                XElement street      = new XElement("street"); street.Value = employee.Address?.street ?? "";
                XElement town        = new XElement("town"); town.Value = employee.Address?.Town ?? "";
                XElement state       = new XElement("state"); state.Value = employee.Address?.State ?? "";
                newAddress.Add(doorNo, street, town, state);
                newEmployee.Add(id, name, age, designation, newAddress);
                doc.Root.Add(newEmployee);
                SaveXML(doc);
                System.Console.WriteLine("Successfully Added new Employee");
                System.Console.WriteLine(JsonConvert.SerializeObject(newEmployee));
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception occured: " + e.Message);
                return(false);
            }
            _logger.LogTrace("AddEmployee Ends.");
            return(true);
        }