public async Task <ActionResult <Employee> > CreareEmployee(Employee employee)
        {
            try
            {
                if (employee == null)
                {
                    return(BadRequest());
                }

                var isEx = employeeRepositry.GetEmployeeByEmail(employee.Email);
                if (isEx != null)
                {
                    ModelState.AddModelError(nameof(employee.Email), "Ex");
                    return(BadRequest(ModelState));
                }
                var res = await employeeRepositry.Add(employee);


                return(CreatedAtAction(nameof(GetEmployee), new { id = employee.ID }, employee));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error"));
            }
        }
Example #2
0
        public IActionResult Create(EmployeeCreateViewModel model) //* part 53 - step3 - change Employee to EmployeeCreateViewModel
        {
            if (ModelState.IsValid)                                //* Part 42 - step2 model validation - next step in Create.cshtml
            {
                string uniqeFileName = ProccessUplodedFile(model);

                Employee newEmployee = new Employee
                {
                    Name      = model.Name,
                    Email     = model.Email,
                    Role      = model.Role,
                    PhotoPath = uniqeFileName
                };
                _employeeRepositry.Add(newEmployee);
                return(RedirectToAction("details", new { id = newEmployee.Id })); //* Part 44 - need to comment this line
            }

            return(View());
        }