Exemple #1
0
        public async Task <IActionResult> Create(
            InstructorAddEditDto instructorDto, [FromServices] IInstructorService service)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await service.CreateInstructorAndSaveAsync(instructorDto);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (GeneralException ex)
                {
                    ViewBag.HasError = true;
                    ViewBag.Message  = ex.Message;
                }
                catch (Exception ex)
                {
                    _logger.LogError("Error in Create: " + ex.Message);
                    ViewBag.HasError = true;
                    ViewBag.Message  = Constants.ERROR_MESSAGE_SAVE + ": " + ex.Message;
                }
            }

            await PopulateCourseAssignedDataAsync(instructorDto.CoursesAssigned, service);

            return(View($"{_viewFolder}Create.cshtml", instructorDto));
        }
Exemple #2
0
        public async Task <IActionResult> PostEdit(
            int?id, InstructorAddEditDto instructorDto, [FromServices] IInstructorService service)
        {
            if (!id.HasValue || id.Value != instructorDto.InstructorId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await service.UpdateInstructorAndSaveAsync(instructorDto);
                }
                catch (GeneralException ex)
                {
                    ViewBag.HasError = true;
                    ViewBag.Message  = ex.Message;
                }
                catch (Exception ex)
                {
                    ViewBag.HasError = true;
                    ViewBag.Message  = Constants.ERROR_MESSAGE_STANDARD + ": " + ex.Message;
                }

                return(RedirectToAction(nameof(Index)));
            }

            await PopulateCourseAssignedDataAsync(instructorDto.CoursesAssigned, service);

            return(View($"{_viewFolder}Edit.cshtml", instructorDto));
        }
Exemple #3
0
        public async Task <int> UpdateInstructorAndSaveAsync(InstructorAddEditDto dto)
        {
            var result = 0;

            try
            {
                var instructorToUpdate = await _context.Instructors
                                         .Include(i => i.OfficeAssignment)
                                         .Include(i => i.CourseAssignments)
                                         .ThenInclude(ca => ca.Course)
                                         .FirstOrDefaultAsync(m => m.InstructorId == dto.InstructorId);

                if (instructorToUpdate == null)
                {
                    var errMsg = $"Record does not exist or has been deleted for InstructorId={dto.InstructorId}";
                    _logger.LogError("", errMsg);
                    throw new GeneralException(errMsg);
                }

                // update Instructor
                instructorToUpdate.LastName     = dto.LastName;
                instructorToUpdate.FirstMidName = dto.FirstMidName;
                instructorToUpdate.HireDate     = dto.HireDate;

                // update OfficeAssignment
                if (string.IsNullOrWhiteSpace(dto.OfficeLocation))
                {
                    instructorToUpdate.OfficeAssignment = null;
                }
                else if (instructorToUpdate.OfficeAssignment != null)
                {
                    instructorToUpdate.OfficeAssignment.Location = dto.OfficeLocation;
                }
                else
                {
                    instructorToUpdate.OfficeAssignment = new OfficeAssignment
                    {
                        InstructorId = dto.InstructorId,
                        Location     = dto.OfficeLocation
                    };
                }

                // update CourseAssignments
                UpdateInstructorCourses(dto.CoursesAssigned, instructorToUpdate);

                // Comment out this line to enable updating only the fields with changed values
                //_context.Departments.Update(courseInDb);

                result = await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Failed to update Instructor: {dto}");
                throw ex;
            }

            return(result);
        }
Exemple #4
0
        public async Task <int> CreateInstructorAndSaveAsync(InstructorAddEditDto dto)
        {
            int result;

            try
            {
                OfficeAssignment officeAssignment = null;
                if (!string.IsNullOrWhiteSpace(dto.OfficeLocation))
                {
                    officeAssignment = new OfficeAssignment
                    {
                        InstructorId = dto.InstructorId,
                        Location     = dto.OfficeLocation
                    };
                }

                List <CourseAssignment> courseAssignments = null;
                if (dto.CoursesAssigned != null)
                {
                    courseAssignments = new List <CourseAssignment>();
                    foreach (var courseId in dto.CoursesAssigned)
                    {
                        courseAssignments.Add(
                            new CourseAssignment
                        {
                            InstructorId = dto.InstructorId,
                            CourseId     = courseId
                        });
                    }
                }

                _context.Add(new Instructor
                {
                    InstructorId      = dto.InstructorId,
                    LastName          = dto.LastName,
                    FirstMidName      = dto.FirstMidName,
                    HireDate          = dto.HireDate,
                    OfficeAssignment  = officeAssignment,
                    CourseAssignments = courseAssignments
                });

                result = await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Failed to create new Instructor: {dto}");
                throw ex;
            }

            return(result);
        }