Example #1
0
        /// <summary>
        /// Verify that the Dynamic object(Json) contains the same object as is stored in XML with correct data
        /// </summary>
        /// <param name="JSONresp"></param>
        /// <param name="xdoc"></param>
        /// <returns></returns>
        public static bool ContainsCorrectObject(dynamic JSONresp, XDocument xdoc)
        {
            bool status = false;

            var items = (from xe in xdoc.Descendants("Employee")
                         select new XMLEmployee
            {
                Name = xe.Element("Name").Value,
                Experience = xe.Element("Experience").Value,
                Position = xe.Element("Position").Value,
                Salary = xe.Element("Salary").Value,
                Specialization = xe.Element("Specialization").Value,
                ID = (string)xe.Element("ID")
            }).ToList();

            //Now convert to the Employe format
            //also If the ID field is empty - assign new ID to it
            var emps = XMLActions.ToEmployee(items.Where(x => x.ID.ToString() == JSONresp.ID.ToString()));


            if (JSONresp.ID == emps.ElementAt(0).ID.ToString() &&
                JSONresp.Name == emps.ElementAt(0).Name &&
                JSONresp.Experience == emps.ElementAt(0).Experience &&
                JSONresp.Position == emps.ElementAt(0).Position &&
                JSONresp.Specialization == emps.ElementAt(0).Specialization &&
                JSONresp.Salary == emps.ElementAt(0).Salary.ToString())
            {
                status = true;
            }
            else
            {
                return(false);
            }
            return(status);
        }
        public Employee Get(int Id)
        {
            XDocument xdoc  = XMLActions.ReadXML();
            var       items = (from xe in xdoc.Descendants("Employee")
                               select new XMLEmployee
            {
                Name = xe.Element("Name").Value,
                Experience = xe.Element("Experience").Value,
                Position = xe.Element("Position").Value,
                Salary = xe.Element("Salary").Value,
                Specialization = xe.Element("Specialization").Value,
                ID = (string)xe.Element("ID")
            }).ToList();

            //Now convert to the Employe format
            //also If the ID field is empty - assign new ID to it
            var      emp = XMLActions.ToEmployee(items);
            Employee employee;

            try
            {
                employee = emp.Where(x => x.ID == Id).First();
            }
            catch
            {
                employee = null;
            }
            return(employee);
        }
Example #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestWebAPI");
            });
            //app.

            XMLActions.CorrectXMLIDs();
        }
Example #4
0
        public IActionResult EmployeeAdd(XMLEmployee employee)
        {
            if (!ModelState.IsValid)
            {
                BadRequest(ModelState);
            }
            var lempxml = new List <XMLEmployee>();

            lempxml.Add(employee);

            var lemp = XMLActions.ToEmployee(lempxml);
            var emp  = lemp.First();

            //Check if we have already the same object with criteria Matching Name
            var emps = _repo.GetAll().Where(x => x.Name.Replace(" ", "").Replace(",", "").Replace(".", "").Contains(emp.Name.Replace(" ", "").Replace(",", "").Replace(".", "")));

            if (emps.Count() > 0)
            {
                ModelState.AddModelError("Error", "Person with the same name exists. You may want to use Update functionality to update this person");
                return(BadRequest(ModelState));
            }

            _repo.Create(emp);
            return(Ok(emp));
        }
Example #5
0
        private void Load(object sender, RoutedEventArgs e)
        {
            var emp = new Employee();

            emp = XMLActions.ReadEmployees(ConfigurationManager.AppSettings["employee"]);
            DataEmployee.Text = emp.ToString();

            var dep = new Department();

            dep = XMLActions.ReadDepartment(ConfigurationManager.AppSettings["department"]);
            DataDepatment.Text = dep.ToString();
        }
Example #6
0
        private void DeleteDep_Click(object sender, RoutedEventArgs e)
        {
            var res = MessageBox.Show("Czy na pewno chcesz usunąc oddział?", "Usunąć?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

            if (res == MessageBoxResult.Yes)
            {
                var dep = new Department();
                dep.Address  = "";
                dep.BossName = "";
                dep.Name     = "";
                XMLActions.SaveDepartment(ConfigurationManager.AppSettings["department"], dep);
                DataDepatment.Text = dep.ToString();
            }
        }
Example #7
0
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            var dep = new Department();

            dep.Name     = Name.Text;
            dep.BossName = BossName.Text;
            dep.Address  = Address.Text;

            XMLActions.SaveDepartment(ConfigurationManager.AppSettings["department"], dep);

            var main = new MainWindow();

            main.Show();
            Close();
        }
Example #8
0
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            var emp = new Employee();

            emp.FirstName = FirstName.Text;
            emp.LastName  = LastName.Text;
            emp.Phone     = Phone.Text;
            emp.Job       = Job.Text;

            XMLActions.SaveEmployees(ConfigurationManager.AppSettings["employee"], emp);

            var main = new MainWindow();

            main.Show();
            Close();
        }
Example #9
0
        private void DeleteEmp_Click(object sender, RoutedEventArgs e)
        {
            var res = MessageBox.Show("Czy na pewno chcesz usunąc pracownika?", "Usunąć?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

            if (res == MessageBoxResult.Yes)
            {
                var emp = new Employee();
                emp.FirstName = "";
                emp.LastName  = "";
                emp.Job       = "";
                emp.Phone     = "";

                XMLActions.SaveEmployees(ConfigurationManager.AppSettings["employee"], emp);
                DataEmployee.Text = emp.ToString();
            }
        }
        public bool Update(Employee employee)
        {
            XDocument xdoc = XMLActions.ReadXML();
            //XElement root = xdoc.Element("EmployeeList");

            var items = from item in xdoc.Descendants("Employee")
                        where int.Parse(item.Element("ID").Value) == employee.ID
                        select item;

            foreach (XElement itemElement in items)
            {
                itemElement.SetElementValue("Name", employee.Name);
                itemElement.SetElementValue("Experience", employee.Experience);
                itemElement.SetElementValue("Position", employee.Position);
                itemElement.SetElementValue("Salary", employee.Salary);
                itemElement.SetElementValue("Specialization", employee.Specialization);
            }

            return(XMLActions.UpdateXML(xdoc));
        }
        public List <Employee> GetAll()
        {
            XDocument xdoc = XMLActions.ReadXML();

            var items = (from xe in xdoc.Descendants("Employee")
                         select new XMLEmployee
            {
                Name = xe.Element("Name").Value,
                Experience = xe.Element("Experience").Value,
                Position = xe.Element("Position").Value,
                Salary = xe.Element("Salary").Value,
                Specialization = xe.Element("Specialization").Value,
                ID = (string)xe.Element("ID")
            }).ToList();

            //Now convert to the Employe format
            //also If the ID field is empty - assign new ID to it
            var emp = XMLActions.ToEmployee(items);

            return(emp);
        }
Example #12
0
        /// <summary>
        /// Verify that the Dynamic object(Json) contains the at least one object that is stored in XML with correct data with the specified criteria
        /// </summary>
        /// <param name="JSONresp"></param>
        /// <param name="xdoc"></param>
        /// /// <param name="position"></param>
        /// <returns></returns>
        public static bool ContainsAtLeastOne(dynamic JSONresp, XDocument xdoc, string position)
        {
            bool status = false;

            var items = (from xe in xdoc.Descendants("Employee")
                         select new XMLEmployee
            {
                Name = xe.Element("Name").Value,
                Experience = xe.Element("Experience").Value,
                Position = xe.Element("Position").Value,
                Salary = xe.Element("Salary").Value,
                Specialization = xe.Element("Specialization").Value,
                ID = (string)xe.Element("ID")
            }).ToList();

            //Now convert to the Employe format
            var emps = XMLActions.ToEmployee(items.Where(x => x.Position.ToLower() == position.ToLower()));

            //now based on whether we have JArray or just single JProperty verify the match
            var type = JSONresp.Type;
            var cnt  = ((JArray)JSONresp).Count;

            if (type == JTokenType.Array && emps.Count != cnt)
            {
                return(false);
            }
            else if (type == JTokenType.Array || emps.Count > 1)
            {
                for (int i = 0; i < emps.Count; i++)
                {
                    if (JSONresp[i].ID == emps.ElementAt(i).ID.ToString() &&
                        JSONresp[i].Name == emps.ElementAt(i).Name &&
                        JSONresp[i].Experience == emps.ElementAt(i).Experience &&
                        JSONresp[i].Position == emps.ElementAt(i).Position &&
                        JSONresp[i].Specialization == emps.ElementAt(i).Specialization &&
                        JSONresp[i].Salary == emps.ElementAt(i).Salary.ToString())
                    {
                        status = true;
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                if (JSONresp.ID == emps.ElementAt(0).ID.ToString() &&
                    JSONresp.Name == emps.ElementAt(0).Name &&
                    JSONresp.Experience == emps.ElementAt(0).Experience &&
                    JSONresp.Position == emps.ElementAt(0).Position &&
                    JSONresp.Specialization == emps.ElementAt(0).Specialization &&
                    JSONresp.Salary == emps.ElementAt(0).Salary.ToString())
                {
                    status = true;
                }
                else
                {
                    return(false);
                }
            }

            return(status);
        }
Example #13
0
 public void InitWebFactory()
 {
     _factory = new CustomWebApplicationFactory();
     _client  = _factory.CreateClient();
     XMLActions.CorrectXMLIDs();
 }
        public bool Create(Employee employee)
        {
            XDocument xdoc = XMLActions.ReadXML();

            //need to assign new ID, because the ID that may come from the user may be busy already
            var items = (from xe in xdoc.Descendants("Employee")
                         select new
            {
                ID = (string)xe.Element("ID")
            }).ToList();
            int newId;

            //Avoid overflow of int
            if (items.Count() < int.MaxValue)
            {
                newId = items.Count() + 1;
            }
            else
            {
                return(false);
            }

            var tmpId = items.Where(x => x.ID != null).Where(x => x.ID.Contains(newId.ToString())).Select(x => x.ID);

            if (tmpId.Count() < 1)
            {
                employee.ID = newId;
            }
            else
            {
                int ID;
                var LastUsedId = tmpId.OrderBy(x => x).Last();
                //Avoid overflow of int
                if (int.Parse(LastUsedId) < int.MaxValue)
                {
                    ID = int.Parse(LastUsedId) + 1;
                }
                else
                {
                    //We may want to implement logic to find not used ID to correctly handle this situation where we are close to the maximum int value
                    // But as for now we are OK with that for test porposes
                    return(false);
                }

                employee.ID = ID;
            }

            XElement root = xdoc.Element("EmployeeList");

            //Add new employee to XML tree
            root.Add(new XElement("Employee",
                                  new XElement("ID", employee.ID),
                                  new XElement("Name", employee.Name),
                                  new XElement("Experience", employee.Experience),
                                  new XElement("Position", employee.Position),
                                  new XElement("Salary", employee.Salary),
                                  new XElement("Specialization", employee.Specialization)
                                  ));

            return(XMLActions.UpdateXML(xdoc));
        }