Example #1
0
        public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, CourseForCreationDto course)
        {
            try
            {
                if (!_courseLibraryRepository.AuthorExists(authorId))
                {
                    return(NotFound());
                }

                var courseEntity = _mapper.Map <Course>(course);
                _courseLibraryRepository.AddCourse(authorId, courseEntity);
                _courseLibraryRepository.Save();

                var courseToReturn = _mapper.Map <CourseDto>(courseEntity);
                return(CreatedAtRoute("GetCourseForAuthor",
                                      new
                {
                    authorId = authorId,
                    courseId = courseToReturn.Id
                }, courseToReturn));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{ex.Message}");
                return(StatusCode(500, "Internal server error, try again later."));
            }
        }
Example #2
0
        public ActionResult <CourseDto> CreateCoursesForAuthor(Guid authorId, [FromBody] CreateCourseDto createCourseDto)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound("Author does not exist please check and try again"));
            }


            var createCourse = _mapper.Map <Course>(createCourseDto);

            _courseLibraryRepository.AddCourse(authorId, createCourse);
            var solution     = _courseLibraryRepository.Save();
            var courseReturn = _mapper.Map <CourseDto>(createCourse);


            if (solution)
            {
                return(CreatedAtRoute(
                           "GetCourseForAuthor",
                           new{
                    authorId = courseReturn.AuthorId,
                    courseId = courseReturn.Id
                },
                           courseReturn));
            }

            return(BadRequest("An error occurred unable to save changes"));
        }
        public ActionResult <CourseDto> CreateCourseForAuthor(
            Guid authorId,
            [FromBody] CourseCreationDto courseCreationDto)
        {
            if (!_repository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var course = _mapper.Map <Course>(courseCreationDto);

            _repository.AddCourse(authorId, course);
            var isSaved = _repository.Save();

            if (!isSaved)
            {
                throw new InvalidOperationException("Error occurred: Unable to create course");
            }

            var courseDto = _mapper.Map <CourseDto>(course);

            return(CreatedAtAction(
                       "GetCourseForAuthor",
                       new
            {
                authorId,
                courseId = courseDto.Id
            },
                       courseDto));
        }
Example #4
0
        public ActionResult <CourseDTO> CreateCouse(
            Guid authorId, CourseForCreationDTO course
            )
        {
            if (_clRepo.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseEntity = _mapper.Map <Course>(course);

            _clRepo.AddCourse(authorId, courseEntity);
            _clRepo.Save();

            var courseToReturn = _mapper.Map <CourseDTO>(courseEntity);

            return(CreatedAtRoute(
                       "GetCourseForAuthor",
                       new {
                authorId = courseToReturn.AuthorId,
                courseId = courseToReturn.Id
            },
                       courseToReturn
                       ));
        }
Example #5
0
        public IActionResult AddCourseForAuthor(Guid authorId, CourseForCreationDto courseDto)
        {
            var entity = mapper.Map <Course>(courseDto);

            courseLibraryRepository.AddCourse(authorId, entity);
            courseLibraryRepository.Save();
            return(CreatedAtRoute("GetCourse", new { authorId = authorId, courseId = entity.Id }, entity));
        }
        public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, CourseForCreationDto courseDtoForCreation)
        {
            var courseEntity = _mapper.Map <Course>(courseDtoForCreation);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            _courseLibraryRepository.Save();
            var courseToReturn = _mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn));
        }
        public ActionResult <CourseDto> Post(Guid authorId, CourseForCreationDto course)
        {
            if (!_repository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var newCourse = _mapper.Map <Course>(course);

            _repository.AddCourse(authorId, newCourse);
            _repository.Save();
            return(CreatedAtRoute("GetCourse", new { authorId, courseId = newCourse.Id }, newCourse));
        }
        public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, CourseForCreationDto courseForCreation)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var courseEntity = _mapper.Map <Course>(courseForCreation);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            _courseLibraryRepository.Save();
            var courseToReturn = _mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtAction("GetCourseForAuthor", new { authorId = authorId, CourseId = courseToReturn.Id }, courseToReturn));
        }
        public ActionResult <IEnumerable <CourseDto> > CreateCourseForAuthor(Guid authorId, CourseForCreationDto courseForCreationDto)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }


            var courseForCreation = _mapper.Map <Course>(courseForCreationDto);

            _courseLibraryRepository.AddCourse(authorId, courseForCreation);
            _courseLibraryRepository.Save();
            return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseForCreation.Id }, _mapper.Map <CourseDto>(courseForCreation)));
        }
Example #10
0
        public ActionResult <CoursesDto> CreateCourseForAuthor(Guid authorId, CourseForCreationDto courseForCreationDto)
        {
            if (!_courselibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseEntity = _mapper.Map <Entities.Course>(courseForCreationDto);

            _courselibraryRepository.AddCourse(authorId, courseEntity);
            _courselibraryRepository.Save();
            var courseDto = _mapper.Map <CoursesDto>(courseEntity);

            return(CreatedAtRoute("GetCoursesForAuthor", new { authorId = authorId, courseId = courseDto.Id }, courseDto));
        }
Example #11
0
        public ActionResult <CourseDto> CreateCourse(Guid authorId, CreateCourseDto createCourseDto)
        {
            if (!_courseRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var course = _mapper.Map <Course>(createCourseDto);

            _courseRepository.AddCourse(authorId, course);
            _courseRepository.Save();
            var courseDto = _mapper.Map <CourseDto>(course);

            return(CreatedAtRoute("GetCoursesForAuthor", new { authorId = course.AuthorId, courseId = course.Id }, courseDto));
        }
        public ActionResult <CoursesDTO> CreateCourseForAuthor(int authorId, createCourseForAuthorDTOW createCourse)
        {
            //check if the author exists
            var singleauthor = _courseLibrary.GetAuthor(authorId);

            if (singleauthor == null)
            {
                return(NotFound(new JsonResponse <string>()
                {
                    Success = false,
                    ErrorMessage = "AuthorId is Invalid."
                }));
            }
            var course = _mapper.Map <Entities.Course>(createCourse);

            //course.ID = authorId;
            _courseLibrary.AddCourse(authorId, course);
            _courseLibrary.Save();

            var createdCourse = _mapper.Map <CoursesDTO>(course);

            return(CreatedAtRoute("getCourseForAnAuthor", new { authorId = authorId, courseId = createdCourse.ID }, createdCourse));
            //return Ok(new JsonResponses<AuhtorDTO>()
            //{
            //    Success = true,
            //    Result = new List<AuhtorDTO>() {
            //        createdAuthor
            //    }
            //});
        }
Example #13
0
        public async Task <ActionResult <CourseDto> > CreateCourseForAnAuthor(Guid authorId, CourseForCreateDTO dto)
        {
            if (!await mRepository.AuthorExistsAsync(authorId))
            {
                return(NotFound());
            }
            var createdEntity = mMapper.Map <Course>(dto);

            mRepository.AddCourse(authorId, createdEntity);
            await mRepository.SaveChangesAsync();

            var createdDto = mMapper.Map <CourseDto>(createdEntity);

            return(CreatedAtRoute("GetCourseOfAnAuthor", new { authorId, courseId = createdDto.Id },
                                  createdDto));
        }
        public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, [FromBody] CourseForCreationDto course)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseEntity = _mapper.Map <Entities.Course>(course);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            _courseLibraryRepository.Save();

            var courseDtoToReturn = _mapper.Map <Models.CourseDto>(courseEntity);

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseDtoToReturn.Id }, courseDtoToReturn));
        }
        public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, CourseForCreationDto courseForCreation)
        {
            if (!courseRepo.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var course = mapper.Map <Course>(courseForCreation);

            courseRepo.AddCourse(authorId, course);
            courseRepo.Save();

            var courseResult = mapper.Map <CourseDto>(course);

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseResult.Id }, courseResult));
        }
        public ActionResult <CourseDto> createCourseForAuthor(Guid authorId, CourseForCreationDto course)
        {
            if (!_courseLibaryService.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseEntity = _mapper.Map <Course>(course);

            _courseLibaryService.AddCourse(authorId, courseEntity);
            _courseLibaryService.Save();

            var courseToReturn = _mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute("getCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn));
        }
Example #17
0
        public ActionResult <CourseGetDto> CreateCourseForAuthor(Guid authorId, CourseCreateDto course)
        {
            if (!_repo.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var courseEntity = _mapper.Map <Entities.Course>(course);

            _repo.AddCourse(authorId, courseEntity);
            _repo.Save();

            var courseToReturn = _mapper.Map <CourseGetDto>(courseEntity);

            return(CreatedAtAction(nameof(GetCoursesForAuthor),
                                   new { authorId = authorId, courseId = courseToReturn.Id },
                                   courseToReturn));
        }
Example #18
0
        public ActionResult <CourseDto> CreateCourseForAuthor([FromRoute] Guid authorId, [FromBody] CourseForCreationDto courseForCreation)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var course = _mapper.Map <Course>(courseForCreation);

            _courseLibraryRepository.AddCourse(authorId, course);

            _courseLibraryRepository.Save();

            var result = _mapper.Map <CourseDto>(course);

            return(CreatedAtRoute("GetAuthorCourse", new { authorId = authorId, courseId = course.Id }, result));
        }
Example #19
0
        public ActionResult <CourseDTO> CreateCourse(Guid authorId, CourseForCreationDTO courseForCreationDTO)
        {
            if (!courseLibraryRepository.AuthorExists(authorId))
            {
                return(BadRequest());
            }

            var courseEntiy = _mapper.Map <Course>(courseForCreationDTO);

            courseLibraryRepository.AddCourse(authorId, courseEntiy);
            courseLibraryRepository.Save();


            var courseToReturn = _mapper.Map <CourseDTO>(courseEntiy);

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, (courseToReturn)));
        }
Example #20
0
        public async Task <IActionResult> CreateCourseForAuthor(Guid authorId, CourseCreationDto course)
        {
            if (!await _courseLibraryRepository.AuthorExistsAsync(authorId))
            {
                return(NotFound());
            }

            var courseEntity = _mapper.Map <Course>(course);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            await _courseLibraryRepository.SaveAsync();

            var courseToReturn = _mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute("GetCourseForAuthor",
                                  new { authorId, courseId = courseToReturn.Id },
                                  courseToReturn));
        }
        public async Task <ActionResult <CourseForReturn> > CreateCourseForAuthor(Guid authorId, CourseForCreation course)
        {
            if (!await _repository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var courseForAdd = _mapper.Map <Course>(course);

            _repository.AddCourse(authorId, courseForAdd);
            if (!await _repository.Save())
            {
                return(BadRequest("Error Happens when saving in Data Base"));
            }

            var courseToReturn = _mapper.Map <CourseForReturn>(courseForAdd);

            return(CreatedAtRoute("getCourse", new { authorId = authorId, courseId = courseForAdd.Id }, courseToReturn));
        }
Example #22
0
        public IActionResult CreateCourseForAuthor([FromRoute] Guid authorId,
                                                   [FromBody] CourseForCreationDto course)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseEntity = _mapper.Map <Course>(course);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            _courseLibraryRepository.Save();

            var courseToReturn = _mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute(nameof(GetCourseForAuthor),
                                  new { authorId = authorId, courseId = courseToReturn.Id },
                                  courseToReturn));
        }
        public ActionResult <CourseDto> CreateCourseForAuthor(
            [FromRoute] Guid authorId,
            [FromBody] CourseForCreationDto courseForCreationDto)
        {
            if (!AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseEntity = _mapper.Map <Course>(courseForCreationDto);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            _courseLibraryRepository.Save();

            var courseToReturn = _mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute(
                       routeName: "GetCourseForAuthor",
                       routeValues: new { authorId = courseToReturn.AuthorId, courseId = courseToReturn.Id },
                       value: courseToReturn));
        }
        public CourseDto AddCourseForAuthor(Guid authorId, NewCourseDto course)
        {
            if (!_repo.AuthorExists(authorId))
            {
                return(null);
            }
            var courseEntity = _mapper.Map <Course>(course);

            _repo.AddCourse(authorId, courseEntity);
            _repo.Save();
            return(_mapper.Map <CourseDto>(courseEntity));
        }
Example #25
0
        public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, CourseForCreationDto course)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            // map source CourseForCreationDto to destination courseEntity
            var courseEntity = _mapper.Map <Course>(course);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            _courseLibraryRepository.Save();

            // map source courseEntity to destination CourseDto
            var courseToReturn = _mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute("GetCourseForAuthor", new {
                authorId,
                courseId = courseToReturn.Id
            }, courseToReturn));
        }
Example #26
0
        public ActionResult <CourseForCreationDto> CreateCOurseForAuthor(Guid authorId, CourseForCreationDto course)
        {
            //Validate {authorId}
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            if (course == null)
            {
                return(BadRequest());
            }

            var courseEntity = _mapper.Map <Course>(course);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            _courseLibraryRepository.Save();

            var courseReturn = _mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute("GetCourseFOrAuthor",
                                  new { authorId = authorId, courseId = courseReturn.Id }, courseReturn));
        }
Example #27
0
        public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, CourseForCreationDto course)
        {
            if (!repository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var newCourse = mapper.Map <Course>(course);

            repository.AddCourse(authorId, newCourse);
            repository.Save();

            var courseToReturn = mapper.Map <CourseDto>(newCourse);

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn));
        }
Example #28
0
        public ActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument)
        {
            if (!this.courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var courseForAuthorFromRepo = this.courseLibraryRepository.GetCourse(authorId, courseId);

            if (courseForAuthorFromRepo == null)
            {
                var courseDto = new CourseForUpdateDto();
                patchDocument.ApplyTo(courseDto, ModelState);

                if (!TryValidateModel(courseDto))
                {
                    return(ValidationProblem(ModelState));
                }

                var courseToAdd = this.mapper.Map <Entities.Course>(courseDto);
                courseToAdd.Id = courseId;

                courseLibraryRepository.AddCourse(authorId, courseToAdd);
                courseLibraryRepository.Save();

                var courseToReturn = mapper.Map <CourseDto>(courseToAdd);

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

            var courseToPatch = this.mapper.Map <CourseForUpdateDto>(courseForAuthorFromRepo);

            //add validation
            patchDocument.ApplyTo(courseToPatch, ModelState);
            if (!TryValidateModel(courseToPatch))
            {
                return(ValidationProblem(ModelState));
            }


            this.mapper.Map(courseToPatch, courseForAuthorFromRepo);

            this.courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo);

            this.courseLibraryRepository.Save();

            return(NoContent());
        }
        public IActionResult CreateCourseForAuthor([FromRoute] Guid authorId, CreateCourseDTO course)
        {
            if (!_courseLibrary.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseToMap = _mapper.Map <Course>(course);

            _courseLibrary.AddCourse(authorId, courseToMap);
            _courseLibrary.Save();

            var courseToReturn = _mapper.Map <CourseDTO>(courseToMap);

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }
                                  , courseToReturn));
        }
        public ActionResult <CourseDto> ADDNewCourse(Guid authorid, courseforCreation courseforCreation)
        {
            if (!_repo.AuthorExists(authorid))
            {
                return(NotFound());
            }

            var courseEntity = _map.Map <Course>(courseforCreation);

            _repo.AddCourse(authorid, courseEntity);
            _repo.Save();

            var courseToReturn = _map.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute("GetCourse", new
            {
                AuthorId = authorid, courseId = courseToReturn.Id
            }, courseToReturn));;
        }