public async Task <bool> Update(int courseId, CourseForUpdate course)
        {
            var courseFromRepo = _context.Courses.FirstOrDefault(x => x.Id == courseId);

            if (courseFromRepo == null)
            {
                _logger.LogWarning($"Course with given id { courseId } does not exists");
                throw new CourseNotFoundException();
            }

            try
            {
                courseFromRepo.Name        = course.Name;
                courseFromRepo.Description = course.Description;
                courseFromRepo.CourseType  = (CourseTypeEnum)course.CourseType;

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An error occurred during update course");
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        public async Task <IActionResult> UpdateCourse(int id, CourseForUpdate courseForUpdate)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var accountId = _userRepository.GetUserAccountId(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            if (!await _courseRepository.CanEdit(id, accountId))
            {
                return(Unauthorized());
            }

            try
            {
                if (await _courseRepository.Update(id, courseForUpdate))
                {
                    return(Ok());
                }

                return(StatusCode(500, new ErrorResponse {
                    ErrorMessage = "An error occurred during update course. Try again later."
                }));
            }
            catch (CourseNotFoundException)
            {
                return(BadRequest(new ErrorResponse {
                    ErrorMessage = "Course has not been found."
                }));
            }
        }
        public async Task <ActionResult <CourseForReturn> > UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdate course)
        {
            if (!await _repository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseFromRepo = await _repository.GetCourse(authorId, courseId);

            if (courseFromRepo == null)
            {
                var courseToAdd = _mapper.Map <Course>(course);
                _repository.AddCourse(authorId, courseToAdd);
                if (!await _repository.Save())
                {
                    return(BadRequest("Error Happens when saving in Data Base"));
                }

                var courseToReturn = _mapper.Map <CourseForReturn>(courseToAdd);
                return(CreatedAtRoute("getCourse", new { authorId = authorId, courseId = courseToAdd.Id }, courseToReturn));
            }

            _mapper.Map(course, courseFromRepo);
            _repository.UpdateCourse(courseFromRepo);
            if (!await _repository.Save())
            {
                return(BadRequest("Error Happens when saving in Data Base"));
            }

            return(NoContent());
        }