예제 #1
0
        //PUT
        public IHttpActionResult PutEmployee(EmployeeViewModel employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid Entries.!!"));
            }
            using (var x = new WebAPIEntities())
            {
                var checkIfExits = x.Employees.Where(e => e.id == employee.Id).FirstOrDefault <Employee>();
                if (checkIfExits != null)
                {
                    checkIfExits.id          = employee.Id;
                    checkIfExits.name        = employee.Name;
                    checkIfExits.location    = employee.Location;
                    checkIfExits.designation = employee.Designation;

                    x.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }
            return(Ok());
        }
예제 #2
0
        //DELETE
        public IHttpActionResult Delete(int id)
        {
            if (id <= 0)
            {
                return(BadRequest("Please Enter valid id"));
            }
            using (var x = new WebAPIEntities())
            {
                var employee = x.Employees.Where(e => e.id == id).FirstOrDefault();

                x.Entry(employee).State = System.Data.Entity.EntityState.Deleted;
                x.SaveChanges();
            }
            return(Ok());
        }
예제 #3
0
 //POST
 public IHttpActionResult PostEmployee(EmployeeViewModel employee)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest("Invalid Entries.!!"));
     }
     using (var x = new WebAPIEntities())
     {
         x.Employees.Add(new Employee()
         {
             id          = employee.Id,
             name        = employee.Name,
             location    = employee.Location,
             designation = employee.Designation
         });
         x.SaveChanges();
     }
     return(Ok());
 }
예제 #4
0
        //GET
        public IHttpActionResult GetAllEmployee()
        {
            IList <EmployeeViewModel> employeeList = null;

            using (var x = new WebAPIEntities())
            {
                employeeList = x.Employees
                               .Select(e => new EmployeeViewModel()
                {
                    Id          = e.id,
                    Name        = e.name,
                    Location    = e.location,
                    Designation = e.designation
                }).ToList <EmployeeViewModel>();
            }
            if (employeeList.Count == 0)
            {
                return(NotFound());
            }
            return(Ok(employeeList));
        }
예제 #5
0
 /// <summary>
 /// Public Constructor,initializes privately declared local variables.
 /// </summary>
 /// <param name="context"></param>
 public GenericRepository(WebAPIEntities context)
 {
     this.Context = context;
     this.DbSet   = context.Set <TEntity>();
 }
예제 #6
0
 public UnitOfWork()
 {
     _context = new WebAPIEntities();
 }