Пример #1
0
        public IHttpActionResult PutTblUser(int id, TblUser tblUser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != tblUser.Id)
            {
                return(BadRequest());
            }

            db.Entry(tblUser).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TblUserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PutEmployeeInfo(int id, EmployeeInfo employeeInfo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employeeInfo.Id)
            {
                return(BadRequest());
            }

            db.Entry(employeeInfo).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeInfoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
 public HttpResponseMessage Put(int id, [FromBody] Employee employee)
 {
     try
     {
         using (EmployeeDbEntities entities = new EmployeeDbEntities())
         {
             var entity = entities.Employees.FirstOrDefault(e => e.ID == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                    "employee id: " + id.ToString() + " not found to update"));
             }
             else
             {
                 entity.FirstName = employee.FirstName;
                 entity.Salary    = employee.Salary;
                 entity.Gender    = employee.Gender;
                 entity.LastName  = employee.LastName;
                 entities.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK, entity));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (EmployeeDbEntities entities = new EmployeeDbEntities())
         {
             var entity = entities.Employees.FirstOrDefault(e => e.ID == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NoContent,
                                                    "employee id : " + id.ToString() + " not found to delete"));
             }
             else
             {
                 entities.Employees.Remove(entity);
                 entities.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Пример #5
0
        // DELETE
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (EmployeeDbEntities db = new EmployeeDbEntities())
                {
                    Employee employee = db.Employees.Find(id);

                    if (employee != null)
                    {
                        db.Employees.Remove(employee);
                        db.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with ID = " + id.ToString() + " not found to delete."));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Пример #6
0
        // PUT
        public HttpResponseMessage Put(int id, [FromBody] Employee employee)
        {
            try
            {
                using (EmployeeDbEntities db = new EmployeeDbEntities())
                {
                    Employee entity = db.Employees.Find(id);

                    if (entity != null)
                    {
                        entity.FirstName = employee.FirstName;
                        entity.LastName  = employee.LastName;
                        entity.Gender    = employee.Gender;
                        entity.Salary    = employee.Salary;

                        db.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, entity));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with ID = " + id.ToString() + " not found to update."));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Пример #7
0
        public void DeleteEmployee(long id)
        {
            using (var db = new EmployeeDbEntities())
            {
                var existingEmployee = db.Employees.Where(x => x.Id == id).FirstOrDefault();

                if (existingEmployee != null)
                {
                    db.Entry(existingEmployee).State = System.Data.Entity.EntityState.Deleted;
                    db.SaveChanges();
                }
            }
        }
Пример #8
0
        public void SaveEmployee(Employee employee)
        {
            using (var db = new EmployeeDbEntities())
            {
                db.Employees.Add(new Employee()
                {
                    Name          = employee.Name,
                    Email         = employee.Email,
                    Type          = string.IsNullOrEmpty(employee.Type) ? "Permanent" : employee.Type,
                    DateOfJoining = employee.DateOfJoining == null ? DateTime.Now.Date : employee.DateOfJoining
                });

                db.SaveChanges();
            }
        }
Пример #9
0
        public void UpdateEmployee(long id, Employee employee)
        {
            using (var db = new EmployeeDbEntities())
            {
                var existingEmployee = db.Employees.Where(x => x.Id == id).FirstOrDefault();

                if (existingEmployee != null)
                {
                    existingEmployee.Name          = employee.Name;
                    existingEmployee.Email         = employee.Email;
                    existingEmployee.Type          = employee.Type;
                    existingEmployee.DateOfJoining = employee.DateOfJoining;

                    db.Entry(existingEmployee).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }
 public HttpResponseMessage Post([FromBody] Employees employee)
 {
     try
     {
         using (EmployeeDbEntities db = new EmployeeDbEntities())
         {
             db.Employees.Add(employee);
             db.SaveChanges();
             var message = Request.CreateResponse(HttpStatusCode.Created, employee);
             message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString());
             return(message);
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Пример #11
0
 //Employee
 public void AddEmployee(Employee employee)
 {
     Ee.Employees.Add(employee);
     Ee.SaveChanges();
 }
Пример #12
0
 //Country
 public void AddCountry(Country country)
 {
     Ee.Countries.Add(country);
     Ee.SaveChanges();
 }
 public void SaveEmployee(Employee emp)
 {
     _dbContext = new EmployeeDbEntities();
     _dbContext.Employees.Add(emp);
     _dbContext.SaveChanges();
 }