// Update PUT public List<Employee> Put(Employee obj) { EmployeeEntities empObj = new EmployeeEntities(); if (obj!=null && obj.EmployeeID!=0) { // Select the record ( LINQ ) Employee empUpdate = (from temp in empObj.Employees where temp.EmployeeID == obj.EmployeeID select temp).ToList<Employee>().SingleOrDefault<Employee>(); //Updates the details as per the details from the front page if(empUpdate!=null) { empUpdate.Name = obj.Name; empUpdate.Age = obj.Age; empUpdate.Address = obj.Address; empUpdate.Active = obj.Active; empUpdate.Employment = obj.Employment; empObj.SaveChanges(); } } List<Employee> empList = empObj.Employees.ToList<Employee>(); return empList; }
// Select public List<Employee> Get() // All the record { // Read the query string var allUrlKeyValues = ControllerContext.Request.GetQueryNameValuePairs(); string EmployeeID = allUrlKeyValues. SingleOrDefault(x => x.Key == "EmployeeID").Value; string EmployeeName = allUrlKeyValues. SingleOrDefault(x => x.Key == "EmployeeName").Value; EmployeeEntities empObj = new EmployeeEntities(); List<Employee> empList = new List<Employee>(); if (EmployeeName != null) { empList = (from t in empObj.Employees where t.Name == EmployeeName select t).ToList<Employee>(); } else if (EmployeeID!=null) { int empID = Convert.ToInt32(EmployeeID); empList = (from t in empObj.Employees where t.EmployeeID == empID select t).ToList<Employee>(); } else { empList = empObj.Employees.ToList<Employee>(); } return empList; }
// Insert public List<Employee> Post(Employee obj) { EmployeeEntities empObj = new EmployeeEntities(); if (ModelState.IsValid) { // insert the customer object to database //Gets the max value of emp id int maxEmpId = empObj.Employees.Max<Employee>(x => x.EmployeeID); //makes sure that the emp id is unique and in order obj.EmployeeID = maxEmpId + 1; empObj.Employees.Add(obj); // Still in memory, not committed empObj.SaveChanges(); //Physical Commit } List<Employee> employeeList = empObj.Employees.ToList<Employee>(); //Gets the employee list from the employee table return employeeList; //Returns the employee list }
// Delete Delete public List<Employee> Delete(Employee obj) { EmployeeEntities empObj = new EmployeeEntities(); if (obj!=null && obj.EmployeeID!=0) { // Delete Employee empDelete = (from temp in empObj. Employees where temp.EmployeeID == obj.EmployeeID select temp).ToList<Employee>()[0]; empObj.Employees.Remove(empDelete); // changes still inside memory empObj.SaveChanges();// Physical commit } List<Employee> empList = empObj.Employees.ToList<Employee>(); return empList; }