Example #1
0
        public IActionResult Edit(EmployeeEditViewModel model) //* part 56 - step1  - update Employee
        {
            if (ModelState.IsValid)                            //* Part 42 - step2 model validation - next step in Create.cshtml
            {
                Employee employee = _employeeRepositry.GetEmployee(model.Id);
                employee.Name  = model.Name;
                employee.Email = model.Email;
                employee.Role  = model.Role;
                if (model.Photo != null) //* Part 56 - check if the user select new photo
                {
                    if (model.ExistingPhotoPath != null)
                    {
                        string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images",
                                                       model.ExistingPhotoPath);
                        System.IO.File.Delete(filePath);
                    }
                    employee.PhotoPath = ProccessUplodedFile(model); //* part 56 - step2 - to reduce code for upload file (Right click on the method -> Quick Actions and refactoring -> Creates "ProccessUplodedFile"
                }

                _employeeRepositry.Update(employee);
                return(RedirectToAction("List")); //* Part 56 - need to comment this line
            }

            return(View());
        }
        public async Task <ActionResult <Employee> > UpdateEmployee(Employee employee)
        {
            try
            {
                var res = await employeeRepositry.Update(employee.ID, employee);

                if (res == null)
                {
                    return(NotFound());
                }
                return(AcceptedAtAction(nameof(GetEmployee), new { id = employee.ID }, employee));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error"));
            }
        }