public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdationDto course)
        {
            if (!_courseLibraryRepository.AuthorExist(authorId))
            {
                return(NotFound());
            }

            var courseFromRepo = _courseLibraryRepository.GetCourses(authorId, courseId);

            if (courseFromRepo == null)
            {
                return(NotFound());
            }

            if (course == null)
            {
                //Upserting Using Put request
                var courseEntities = _mapper.Map <Entities.Course>(course);
                courseEntities.Id = courseId;
                _courseLibraryRepository.AddCourses(authorId, courseEntities);
                _courseLibraryRepository.Save();

                var courseToReturn = _mapper.Map <CoursesDto>(courseEntities);

                return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn));
            }

            _mapper.Map(course, courseFromRepo);

            _courseLibraryRepository.UpdateCourse(authorId, courseFromRepo);

            _courseLibraryRepository.Save();

            return(NoContent());
        }
        public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdationDto courseForUpdationDto)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var course = _courseLibraryRepository.GetCourse(authorId, courseId);

            if (course == null)
            {
                course    = _mapper.Map <Course>(courseForUpdationDto);
                course.Id = courseId;
                _courseLibraryRepository.AddCourse(authorId, course);
                _courseLibraryRepository.Save();

                var courseToReturn = _mapper.Map <CourseDto>(course);
                return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn));
            }

            _mapper.Map(courseForUpdationDto, course);
            _courseLibraryRepository.UpdateCourse(course);
            _courseLibraryRepository.Save();

            return(NoContent());
        }
        public ActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId,
                                                           JsonPatchDocument <CourseForUpdationDto> patchDocument)
        {
            if (!_courseLibraryRepository.AuthorExist(authorId))
            {
                return(NotFound());
            }

            var courseFromRepo = _courseLibraryRepository.GetCourses(authorId, courseId);

            if (courseFromRepo == null)
            {
                //Upserting Using Patch request
                var courseDto = new CourseForUpdationDto();
                patchDocument.ApplyTo(courseDto, ModelState);

                if (!TryValidateModel(courseDto))
                {
                    return(BadRequest());
                }

                var courseToAdd = _mapper.Map <Entities.Course>(courseDto);
                courseToAdd.Id = courseId;
                _courseLibraryRepository.AddCourses(authorId, courseToAdd);
                _courseLibraryRepository.Save();

                var courseToReturn = _mapper.Map <CoursesDto>(courseToAdd);

                return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn));
            }

            var courseToPatch = _mapper.Map <CourseForUpdationDto>(courseFromRepo);

            patchDocument.ApplyTo(courseToPatch, ModelState);

            if (!TryValidateModel(courseToPatch))
            {
                return(BadRequest());
            }

            _mapper.Map(courseToPatch, courseFromRepo);

            _courseLibraryRepository.UpdateCourse(authorId, courseFromRepo);
            _courseLibraryRepository.Save();

            return(NoContent());
        }