示例#1
0
        public async Task <IHttpActionResult> PutEmployee(int id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#2
0
        public async Task <ActionResult> Create([Bind(Include = "Id,FullName,Department")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(employee));
        }
        public async Task <ActionResult> Hire([FromBody] PostEmployeeRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                var employee = Mapper.Map <Employee>(request);
                Context.Employees.Add(employee);
                await Context.SaveChangesAsync();

                var employeeResponse = Mapper.Map <GetEmployeeDetailsResponse>(employee);
                return(CreatedAtRoute("employee-get-by-id", new { id = employeeResponse.Id }, employeeResponse));
            }
        }
        public async Task <ActionResult> UpdateFirstName(int id, [FromBody] string firstname)
        {
            var employee = await Context.Employees.SingleOrDefaultAsync(e => e.Id == id && e.Active);

            if (employee == null)
            {
                return(NotFound());
            }
            else
            {
                employee.FirstName = firstname;
                await Context.SaveChangesAsync();

                return(NoContent());
            }
        }
        public async Task <ActionResult> FireAnEmployee(int id)
        {
            Thread.Sleep(3000);

            if (id == 1)
            {
                return(StatusCode(403, new { message = "You cannot fire the CEO!!" }));
            }
            var employee = await Context.Employees
                           .Where(e => e.Id == id && e.IsActive)
                           .SingleOrDefaultAsync();

            if (employee != null)
            {
                employee.IsActive = false;
                await Context.SaveChangesAsync();
            }

            return(NoContent());
        }