public async Task <ActionResult <IEnumerable <CourseDto> > > GetCoursesForAnAuthor(Guid authorId) { if (!await mRepository.AuthorExistsAsync(authorId)) { return(NotFound()); } var courses = await mRepository.GetCoursesAsync(authorId); return(Ok(mMapper.Map <IEnumerable <CourseDto> >(courses))); }
public async Task <ActionResult <IEnumerable <CourseDto> > > GetCoursesForAuthorAsync(Guid authorId) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var coursesFromRepo = await _courseLibraryRepository.GetCoursesAsync(authorId); return(Ok(_mapper.Map <IEnumerable <CourseDto> >(coursesFromRepo))); }
public async Task <ActionResult <IEnumerable <AuthorDto> > > GetCoursesForAuthor(Guid authorId) { if (!await _authorRepository.AuthorExistsAsync(authorId)) { return(NotFound()); } var courses = await _courseRepository.GetCoursesAsync(authorId); var coursesToReturn = _mapper.Map <IEnumerable <CourseDto> >(courses); return(Ok(coursesToReturn)); }
[ResponseCache(Duration = 120)] // We also add middleware public async Task <ActionResult <IEnumerable <CourseDto> > > GetCoursesForAuthor(Guid authorId) { // Handling Faults // If the environment is development client got an stack trace with (500)error // on the other side if the environment is production we got only 500 error, when we throw an exception. // We can override empty 500 error in Startup class in production. // throw new Exception("An error occured."); var courses = await _courseLibraryRepository.GetCoursesAsync(authorId); if (!courses.Any()) { return(NotFound()); } return(Ok(_mapper.Map <IEnumerable <CourseDto> >(courses))); }
public async Task <IActionResult> DeleteAuthor(Guid authorId) { var authorEntity = await _courseLibraryRepository.GetAuthorAsync(authorId); if (authorEntity == null) { return(NotFound()); } var courses = await _courseLibraryRepository.GetCoursesAsync(authorId); // We delete all author's courses. courses.ToList().ForEach(c => { _courseLibraryRepository.DeleteCourse(c); }); _courseLibraryRepository.DeleteAuthor(authorEntity); await _courseLibraryRepository.SaveAsync(); return(NoContent()); }