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); }
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)); }
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); }
/// <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); }