public Task <AuthorQueryResponse> HandleAsync(AuthorQuery query)
        {
            AuthorDto mappedAuthor = new AuthorDto();

            try
            {
                var author = _authorRepository.Get(query.AuthorId);

                if (author == null)
                {
                    throw new AuthorNotFound("Author with requested id not found");
                }

                mappedAuthor = _mapper.Map <AuthorDto>(author);
            }

            catch (AuthorNotFound)
            {
                throw;
            }

            catch (Exception ex)
            {
                throw new BaseApiException(System.Net.HttpStatusCode.InternalServerError, ex.ToString());
            }

            return(Task.FromResult(new AuthorQueryResponse()
            {
                Author = mappedAuthor,
                Links = _mediaTypeCheckService.CanCreateHATEOASLink() ? _hATEOASLinksService.CreateLinksForAuthors(query.AuthorId) : null
            }));
        }
        public Task <CreateAuthorCommandResponse> HandleAsync(CreateAuthorCommand command)
        {
            try
            {
                Guid authorId = Guid.NewGuid();

                var mappedAuthor = _mapper.Map <Author>(command);

                mappedAuthor.Id        = authorId;
                mappedAuthor.AddedDate = DateTime.Now;

                foreach (var course in mappedAuthor.Courses)
                {
                    course.Id = Guid.NewGuid();
                }

                _authorRepository.Insert(mappedAuthor);

                return(Task.FromResult(new CreateAuthorCommandResponse()
                {
                    Id = authorId,
                    Links = _mediaTypeCheckService.CanCreateHATEOASLink() ? _hATEOASLinksService.CreateLinksForAuthors(authorId) : null
                }));
            }

            catch (Exception ex)
            {
                throw new BaseApiException(System.Net.HttpStatusCode.InternalServerError, ex.ToString());
            }
        }
 private Task <AuthorsQueryResponse> CreateResponse(List <Author> authors)
 {
     return(Task.FromResult(new AuthorsQueryResponse()
     {
         Authors = MapAuthorToAuthorDto(authors),
         Links = _mediaTypeCheckService.CanCreateHATEOASLink() ? _hATEOASLinksService.CreateLinksForAuthors(new Guid()) : null
     }));
 }
        public Task <AuthorCourseQueryResponse> HandleAsync(AuthorCourseQuery query)
        {
            CourseDto mappedCourse = new CourseDto();

            try
            {
                bool authorExists = _authorRepository.Exists(query.AuthorId);

                if (!authorExists)
                {
                    throw new AuthorNotFound("Author with requested id not found");
                }

                var course = _courseRepository
                             .Get(query.CourseId);

                if (course == null)
                {
                    throw new CourseNotFound("Course for this author not found");
                }

                mappedCourse = _mapper.Map <CourseDto>(course);
            }

            catch (AuthorNotFound)
            {
                throw;
            }

            catch (CourseNotFound)
            {
                throw;
            }

            catch (Exception ex)
            {
                throw new BaseApiException(System.Net.HttpStatusCode.InternalServerError, ex.ToString());
            }

            return(Task.FromResult(new AuthorCourseQueryResponse()
            {
                Course = mappedCourse,
                Links = _mediaTypeCheckService.CanCreateHATEOASLink() ? _hATEOASLinksService.CreateLinksForAuthors(query.AuthorId) : null
            }));
        }
Example #5
0
        public Task <CreateCourseForAuthorCommandResponse> HandleAsync(CreateCourseForAuthorCommand command)
        {
            try
            {
                Guid authorId = new Guid(_actionContextAccessor.ActionContext.RouteData.Values["AuthorId"].ToString());

                bool authorExists = _authorsRepository.Exists(authorId);

                if (!authorExists)
                {
                    throw new AuthorNotFound("Author with this id not found in the system.");
                }

                Guid courseId = Guid.NewGuid();

                var mappedCourse = _mapper.Map <Course>(command);

                mappedCourse.Id        = courseId;
                mappedCourse.AuthorId  = authorId;
                mappedCourse.AddedDate = DateTime.Now;

                _coursesRepository.Insert(mappedCourse);

                return(Task.FromResult(new CreateCourseForAuthorCommandResponse()
                {
                    AuthorId = authorId,
                    CourseId = courseId,
                    Links = _mediaTypeCheckService.CanCreateHATEOASLink() ? _hATEOASLinksService.CreateLinksForAuthors(authorId) : null
                }));
            }

            catch (AuthorNotFound)
            {
                throw;
            }

            catch (Exception ex)
            {
                throw new BaseApiException(System.Net.HttpStatusCode.InternalServerError, ex.ToString());
            }
        }
Example #6
0
        public Task <AuthorCoursesQueryResponse> HandleAsync(AuthorCoursesQuery query)
        {
            IEnumerable <CourseDto> mappedCourses = new List <CourseDto>();

            try
            {
                bool authorExists = _authorRepository.Exists(query.AuthorId);

                if (!authorExists)
                {
                    throw new AuthorNotFound("Author with requested id not found");
                }

                var courses = _courseRepository
                              .GetAll()
                              .Where(c => c.AuthorId == query.AuthorId);

                mappedCourses = _mapper.Map <List <CourseDto> >(courses);
            }

            catch (AuthorNotFound)
            {
                throw;
            }

            catch (Exception ex)
            {
                throw new BaseApiException(System.Net.HttpStatusCode.InternalServerError, ex.ToString());
            }

            return(Task.FromResult(new AuthorCoursesQueryResponse()
            {
                Courses = mappedCourses,
                Links = _mediaTypeCheckService.CanCreateHATEOASLink() ? _hATEOASLinksService.CreateLinksForAuthors(query.AuthorId) : null
            }));
        }