public async Task <int> UpdateCourseAndSaveAsync(CourseAddEditDto dto)
        {
            var result = 0;

            try
            {
                var courseInDb = await _context.Courses
                                 .FirstOrDefaultAsync(m => m.CourseId == dto.CourseId);

                if (courseInDb == null)
                {
                    _logger.LogError("", $"Record does not exist for CourseId={dto.CourseId}");
                    return(-1);
                }

                courseInDb.Title        = dto.Title;
                courseInDb.Credits      = dto.Credits;
                courseInDb.DepartmentId = dto.DepartmentId;

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

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

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(
            CourseAddEditDto courseDto, [FromServices] ICourseService service)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await service.CreateCourseAndSaveAsync(courseDto);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception)
                {
                    ModelState.AddModelError("", Constants.ERROR_MESSAGE_SAVE);
                }
            }
            await PopulateDepartmentsDropDownListAsync(service);

            return(View($"{_viewFolder}Create.cshtml", courseDto));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PostEdit(
            CourseAddEditDto courseDto, [FromServices] ICourseService service)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await service.UpdateCourseAndSaveAsync(courseDto);
                }
                catch (Exception)
                {
                    //_logger.LogError(ex, "Failed to edit Course: id={id}", id);
                    ModelState.AddModelError("", Constants.ERROR_MESSAGE_SAVE);
                }
                return(RedirectToAction(nameof(Index)));
            }
            await PopulateDepartmentsDropDownListAsync(service, courseDto.DepartmentId);

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <int> CreateCourseAndSaveAsync(CourseAddEditDto dto)
        {
            var result = 0;

            try
            {
                _context.Add(new Course {
                    CourseId     = dto.CourseId,
                    Title        = dto.Title,
                    Credits      = dto.Credits,
                    DepartmentId = dto.DepartmentId
                });
                result = await _context.SaveChangesAsync();
            }
            catch (DbUpdateException ex)
            {
                _logger.LogError(ex, "Failed to create new Course: {Course}", dto.ToString());
                throw ex;
            }

            return(result);
        }