public IHttpActionResult UpdateEmployee(Models.Employee emp)
        {
            using (var context = new WebAPIDemoDBEntities())
            {
                var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == emp.Id);
                employee = MapToDAL(emp, employee);

                context.SaveChanges();

                //var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
                //if (employee == null)
                //{
                //    return NotFound();
                //}
                return(Ok(employee));
            }
        }
Exemplo n.º 2
0
        // POST api/<controller>
        //public void Post([FromBody]string value)
        //{
        //    string s = value;
        //}

        // PUT api/<controller>/5
        //public void Put(int id, [FromBody]Models.Employee emp)
        //{
        //    string text = emp.Name;
        //}

        // DELETE api/<controller>/5
        public IHttpActionResult Delete(int id)
        {
            using (var context = new WebAPIDemoDBEntities())
            {
                var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
                if (employee == null)
                {
                    return(NotFound());
                }
                else
                {
                    context.Employees.Remove(employee);
                    context.SaveChanges();

                    return(Get());
                }
            }
        }
Exemplo n.º 3
0
        public IHttpActionResult PostEmployee([FromBody] Models.Employee emp)
        {
            //string s = employee.Name;
            //return Ok(employee);

            using (var context = new WebAPIDemoDBEntities())
            {
                WebAPI_DB.Employee employee = null;
                if (emp.Id == 0) //Add employee
                {
                    employee = new WebAPI_DB.Employee();
                    employee = MapToDAL(emp, employee);
                    //employee.Id = DBNull.Value;

                    context.Employees.Add(employee);
                }
                else
                {
                    employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == emp.Id);
                    employee = MapToDAL(emp, employee);
                }

                context.SaveChanges();

                //var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
                //if (employee == null)
                //{
                //    return NotFound();
                //}
                return(Ok(employee));
            }

            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}
            //_context.employee.Add(employee);
            //await _context.SaveChangesAsync();
            //return CreatedAtAction("GetEmployee", new
            //{
            //    id = employee.ID
            //}, employee);
        }