public async Task <ActionResult> Hire([FromBody] PostEmployeeRequest employeeToHire)
        {
            //// 1. validate and return 400 for bad
            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}
            //else
            //{
            // 2. Add it to the db
            //      a. Map it from the model to the employee
            var employee = Mapper.Map <Employee>(employeeToHire);

            //      b. Add it to the context
            Context.Employees.Add(employee);  // the id is zero
                                              //      c. Save the changes
            await Context.SaveChangesAsync(); // the id is whatever the db assigned to it

            // 3. Response
            //      a. Return a 201 created status code
            //      b. Add a location header to the response 201 Created Location: like http://localhost:1337/99 or whatever
            //      c. Retunr a copy of whatever they would get if they did a get rewuest to the location, because they probably will
            var employeeToReturn = Mapper.Map <GetEmployeeDetailsResponse>(employee);

            return(CreatedAtRoute("employees-get-by-id", new { id = employeeToReturn.Id }, employeeToReturn));
            //}
        }
Exemple #2
0
        public IActionResult New()
        {
            var model = new PostEmployeeRequest();

            model.StartingSalary = 35000;
            return(View(model));
        }
Exemple #3
0
        public async Task <IActionResult> CreateAsync(PostEmployeeRequest request)
        {
            await Task.Delay(3000);

            if (!ModelState.IsValid)
            {
                return(View("New", request));
            }
            else
            {
                // uh, we should save it to the database...
                var employee = new Employee
                {
                    FirstName = request.FirstName,
                    LastName  = request.LastName,
                    Email     = $"{request.FirstName.ToLower()}@aol.com",
                    Salary    = request.StartingSalary
                };

                _context.Employees.Add(employee);
                await _context.SaveChangesAsync();

                TempData["flash"] = $"Successfully hired {employee.FirstName} {employee.LastName}. Get them working";
                return(RedirectToAction("Index"));
            }
        }
        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));
            }
        }
Exemple #5
0
        public ActionResult Hire([FromBody] PostEmployeeRequest employeeToHire)
        {
            // POSTing To a Collection
            // 1. Validate it. If Not, return a 400. (we'll do this tomorrow)
            // 2. Change the world, man. Add it to the database, whever hiring someone means to you.
            var response = new GetEmployeeDetailsResponse
            {
                Id         = new Random().Next(40, 2000),
                Name       = employeeToHire.Name,
                Department = employeeToHire.Department,
                Manager    = "Sue Jones",
                Salary     = employeeToHire.StartingSalary * 1.3M
            };

            // Return:
            //   - At least a 200, but 201 is "More" Correct (201 means 'created')
            //   - a birth announcement - Location: http://localhost:1337/employees/500
            //   - Also send them a copy of the newly created entity.
            return(CreatedAtRoute("demo-getemployee", new { employeeId = response.Id }, response));
        }
Exemple #6
0
        public async Task <IActionResult> CreateAsync(PostEmployeeRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(View("New", request));
            }
            else
            {
                // uh, we should save it to the database...
                var employee = new Employee
                {
                    FirstName = request.FirstName,
                    LastName  = request.LastName,
                    Email     = $"{request.FirstName.ToLower()}@aol.com",
                    Salary    = request.StartingSalary
                };

                _context.Employees.Add(employee);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
        }