public IActionResult GetAuthor(Guid authorId, string fields,
                                       [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest());
            }
            if (!_propertyCheckerService.TypeHasProperties <AuthorDto>(fields))
            {
                return(BadRequest());
            }
            var author = _context.GetAuthor(authorId);

            if (author == null)
            {
                return(NotFound());
            }
            if (parsedMediaType.MediaType == "application/vnd.marvin.hateoas+json")
            {
                var links = CreateLinksForAuthor(authorId, fields);
                var linkedResourceToReturn = _mapper.Map <AuthorDto>(author).ShapeData(fields)
                                             as IDictionary <string, object>;
                linkedResourceToReturn.Add("links", links);
                return(Ok(linkedResourceToReturn));
            }
            return(Ok(_mapper.Map <AuthorDto>(author).ShapeData(fields)));
        }
        public IActionResult GetAuthor(Guid authorId, string fields, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest());
            }

            if (!_propertyCheckerService.TypeHasProperties <AuthorDto>(fields))
            {
                return(BadRequest());
            }

            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

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

            var includeLinks =
                parsedMediaType.SubTypeWithoutSuffix.EndsWith("hateoas",
                                                              StringComparison.InvariantCultureIgnoreCase);

            IEnumerable <LinkDto> links = new List <LinkDto>();

            if (includeLinks)
            {
                links = CreateLinksForAuthor(authorId, fields);
            }

            var primaryMediaType = includeLinks
                ? parsedMediaType.SubTypeWithoutSuffix
                                   .Substring(0, parsedMediaType.SubTypeWithoutSuffix.Length - 8) // cut off '.hateoas' part
                : parsedMediaType.SubTypeWithoutSuffix;

            // full author
            if (primaryMediaType == "vnd.marvin.author.full")
            {
                var fullResourceToReturn = _mapper.Map <AuthorFullDto>(authorFromRepo)
                                           .ShapeData(fields) as IDictionary <string, object>;

                if (includeLinks)
                {
                    fullResourceToReturn.Add("links", links);
                }

                return(Ok(fullResourceToReturn));
            }

            // friendly author
            var friendlyResourceToReturn = _mapper.Map <AuthorDto>(authorFromRepo)
                                           .ShapeData(fields) as IDictionary <string, object>;

            if (includeLinks)
            {
                friendlyResourceToReturn.Add("links", links);
            }

            return(Ok(friendlyResourceToReturn));
        }
Beispiel #3
0
        public IActionResult GetAuthor(Guid authorId, string fields)
        {
            if (!_propertyCheckerService.TypeHasProperties <AuthorDto>
                    (fields))
            {
                return(BadRequest());
            }

            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

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

            var links = CreateLinksForAuthor(authorId, fields);

            var linkedResourceToReturn =
                _mapper.Map <AuthorDto>(authorFromRepo).ShapeData(fields)
                as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(Ok(linkedResourceToReturn));
        }
Beispiel #4
0
        public ActionResult <IEnumerable <AuthorDto> > GetAuthor(Guid id)
        {
            var author = _repository.GetAuthor(id);

            return(author == null
                ? (ActionResult <IEnumerable <AuthorDto> >)NotFound()
                : Ok(_mapper.Map <AuthorDto>(author)));
        }
Beispiel #5
0
        public ActionResult <AuthorDto> GetAuthor(Guid authorId)
        {
            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

            return(authorFromRepo == null
                ? (ActionResult <AuthorDto>)NotFound()
                : Ok(_mapper.Map <AuthorDto>(authorFromRepo)));
        }
Beispiel #6
0
        public IActionResult GetAuthors(Guid authorId, string fields, [FromHeader(Name = "Accept")] string mediatype)
        {
            if (!MediaTypeHeaderValue.TryParse(mediatype, out MediaTypeHeaderValue parsedMeidaType))
            {
                return(BadRequest());
            }

            if (!_propertyCheckingService.TypeHasProperties <AuthorDto>(fields))
            {
                return(BadRequest());
            }

            var author = _courselibraryRepository.GetAuthor(authorId);

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

            IEnumerable <LinkDto> links = new List <LinkDto>();
            var includeLinks            = parsedMeidaType.SubTypeWithoutSuffix.EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase);

            if (includeLinks)
            {
                links = CreateLinkForAuthor(authorId, fields);
            }

            var primaryMedieType = includeLinks ? parsedMeidaType.SubTypeWithoutSuffix.Substring(0, parsedMeidaType.SubTypeWithoutSuffix.Length - 8) : parsedMeidaType.SubTypeWithoutSuffix;

            if (primaryMedieType == "vnd.marvin.author.full")
            {
                var fullResourceReturn = _mapper.Map <AuthorFullDto>(author).ShapeData(fields) as IDictionary <string, object>;
                if (includeLinks)
                {
                    fullResourceReturn.Add("links", links);
                }
                return(Ok(fullResourceReturn));
            }

            var ResourceToReturn = _mapper.Map <AuthorDto>(author).ShapeData(fields) as IDictionary <string, object>;

            if (includeLinks)
            {
                ResourceToReturn.Add("links", links);
            }
            return(Ok(ResourceToReturn));

            //if (parsedMeidaType.MediaType == "application/vnd.marvin.hateoas+json")
            //{
            //   // var links = CreateLinkForAuthor(authorId, fields);
            //    var linksResourceToReturn = _mapper.Map<AuthorDto>(author).ShapeData(fields) as IDictionary<string, object>;
            //    linksResourceToReturn.Add("links", links);
            //    return Ok(linksResourceToReturn);
            //}


            // return Ok(_mapper.Map<AuthorDto>(author).ShapeData(fields));
        }
Beispiel #7
0
        public ActionResult <CourseDTO> GetAuthor(Guid authorId)
        {
            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

            if (authorFromRepo != null)
            {
                return(Ok(_mapper.Map <AuthorDTO>(authorFromRepo)));
            }
            return(NotFound());
        }
Beispiel #8
0
        public IActionResult GetAuthor(Guid authorId)
        {
            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

            if (authorFromRepo == null)
            {
                return(NotFound("Author Not Found"));
            }
            return(Ok(_mapper.Map <AuthorDto>(authorFromRepo)));
        }
        public IActionResult GetAuthor(Guid authorId)
        {
            var author = _courseLibraryRepository.GetAuthor(authorId);

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

            return(Ok(author));
        }
        public IActionResult GetAuthors(Guid authorId)
        {
            var author = _courseRepository.GetAuthor(authorId);

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

            return(Ok(_mapper.Map <AuthorDto>(author)));
        }
        public ActionResult <AuthorDTO> GetAuthor(Guid authorId)
        {
            var author = _clRepo.GetAuthor(authorId);

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

            return(new JsonResult(_mapper.Map <AuthorDTO>(author)));
        }
        public ActionResult <AuthorDto> GetAuthors(Guid authorId)
        {
            var authors = _courseLibraryRepository.GetAuthor(authorId);

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

            return(_mapper.Map <AuthorDto>(authors));
        }
Beispiel #13
0
        public ActionResult <AuthorDto> GetAuthor(Guid authorId)
        {
            var author = repository.GetAuthor(authorId);

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

            return(Ok(mapper.Map <AuthorDto>(author)));
        }
        public ActionResult <IEnumerable <AuthorDto> > GetAuthor(Guid authorId)
        {
            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

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

            return(Ok(_mapper.Map <AuthorDto>(authorFromRepo)));
        }
        public IActionResult GetAuthor(Guid authorId)
        {
            /*if (!_courseLibraryRepository.AuthorExists(authorId))
             *  return NotFound();*/
            var author = _courseLibraryRepository.GetAuthor(authorId);

            if (author == null)
            {
                return(NotFound());
            }
            return(Ok(_mapper.Map <AuthorDto>(author)));
        }
Beispiel #16
0
        public IActionResult GetAuthor(Guid authorId)
        {
            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

            if (authorFromRepo is null)
            {
                return(NotFound());
            }
            var author = _mapper.Map <AuthorDto>(authorFromRepo);

            return(Ok(author));
        }
        public bool DeleteAuthor(Guid authorId)
        {
            var x = _repo.GetAuthor(authorId);

            if (x == null)
            {
                return(false);
            }
            _repo.DeleteAuthor(x);
            _repo.Save();
            return(true);
        }
        public IActionResult GetAuthor(Guid authorId)
        {
            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

            // Check if author exists
            if (authorFromRepo == null)
            {
                return(NotFound());
            }
            // Using mapper to map authorFromRepo to AuthorDto
            return(Ok(_mapper.Map <AuthorDto>(authorFromRepo)));
        }
        public ActionResult Get(Guid authorId,
                                [ModelBinder(typeof(ArrayModelBinder))] IEnumerable <string> fields,
                                [FromHeader(Name = "Accept")] string acceptHeader
                                )
        {
            if (!MediaTypeHeaderValue.TryParse(acceptHeader, out MediaTypeHeaderValue mediaType))
            {
                return(BadRequest());
            }
            var author = _repository.GetAuthor(authorId);

            if (author == null)
            {
                return(NotFound());
            }
            ExpandoObject shapedData = null;

            if (mediaType.Facets.Contains("friendly") ||
                acceptHeader == "application/vnd.marvin.hateoas+json" ||
                acceptHeader == "application/json")
            {
                var authorDto = _mapper.Map <AuthorDto>(author);
                try
                {
                    shapedData = _dataShapingService.ShapeData(authorDto, fields);
                }
                catch (InvalidPropertyMappingException ex)
                {
                    ModelState.AddModelError(nameof(AuthorsResourceParameters.Fields), ex.Message);
                    return(UnprocessableEntity());
                }
            }
            else if (mediaType.Facets.Contains("full"))
            {
                var authorFullDto = _mapper.Map <AuthorFullDto>(author);
                try
                {
                    shapedData = _dataShapingService.ShapeData(authorFullDto, fields);
                }
                catch (InvalidPropertyMappingException ex)
                {
                    ModelState.AddModelError(nameof(AuthorsResourceParameters.Fields), ex.Message);
                    return(UnprocessableEntity());
                }
            }
            if (mediaType.Facets.Contains("hateoas"))
            {
                var links = CreateAuthorLinks(authorId, fields);
                ((IDictionary <string, object>)shapedData).Add("links", links);
            }
            return(Ok(shapedData));
        }
Beispiel #20
0
        public IActionResult GetAuthor(Guid authorId)
        {
            var singleAuthor = _courseLibraryRepository.GetAuthor(authorId);

            if (singleAuthor == null)
            {
                return(NotFound("No result found"));
            }
            var singleAuthorDto = _mapper.Map <AuthorDto>(singleAuthor);


            return(Ok(singleAuthorDto));
        }
Beispiel #21
0
        public ActionResult <IEnumerable <CourseDto> > GetCourses(int authorId)
        {
            var author = _courseLibraryRepository.GetAuthor(authorId);

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

            var coursesFromRepo = _courseLibraryRepository.GetAllCourse(authorId);

            return(Ok(_mapper.Map <IEnumerable <CourseDto> >(coursesFromRepo)));
        }
Beispiel #22
0
        public async Task <ActionResult <AuthorForReturn> > GetAuthor(Guid authorId)
        {
            var authorFromRepo = await _repository.GetAuthor(authorId);

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

            var author = _mapper.Map <AuthorForReturn>(authorFromRepo);

            return(Ok(author));
        }
Beispiel #23
0
        public IActionResult GetAuthor(Guid authorId)
        {
            var author = _courseLibraryRepository.GetAuthor(authorId);

            if (author == null)
            {
                return(NotFound(author));  //status code 404 =>Not Found Result
            }
            // return new JsonResult(author);

            // return Ok(_mapper.Map<Author, AuthorDTO>(author));

            return(Ok(_mapper.Map <AuthorDTO>(author)));
        }
        public async Task <ActionResult <AuthorDto> > getAuthor(Guid AuthorId)
        {
            var Author = await _courseLibaryService.GetAuthor(AuthorId);

            var test = _courseLibaryService.GetAuthor(AuthorId);
            var x    = await test;

            if (Author == null)
            {
                return(NotFound());
            }
            var AuthorResult = _mapper.Map <AuthorDto>(Author);

            return(Ok(AuthorResult));
        }
        public IActionResult GetAuthor(Guid authorId, string fields)
        {
            if (!_propertyCheckerService.TypeHasProperties <AuthorDto>(fields))
            {
                return(BadRequest());
            }

            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

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

            return(Ok(_mapper.Map <AuthorDto>(authorFromRepo).ShapeData(fields)));
        }
        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
            //    }
            //});
        }
        public IActionResult GetAuthor(Guid authorId)
        {
            // This way is not recomended for high concurrency scenarios, the resource may be deleted between the 2 db calls
            //if (!_courseLibraryRepository.AuthorExists(authorId))
            //{ return NotFound(); }

            // This way, only one DB call
            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId: authorId);

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

            return(Ok(authorFromRepo));
        }
        public ActionResult <AuthorDto> GetAuthor(Guid authorId)
        {
            //if (!_courseLibraryRepository.AuthorExists(authorId))
            //{
            //    return NotFound();
            //} // this will be in a seperate request, though. To be changed

            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);

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

            return(Ok(_mapper.Map <AuthorDto>(authorFromRepo)));
        }
Beispiel #29
0
        public IActionResult GetAuthor(Guid authorId)
        {
            var authorFromRepo = _courseLibraryRepository.GetAuthor(authorId);



            return(Ok(_Mapper.Map <AuthorDTO>(authorFromRepo)));
        }
        public ActionResult <IEnumerable <CourseDto> > GetCoursesForAuthor(Guid authorId)
        {
            var author = _repository.GetAuthor(authorId);

            return(author == null
                ? (ActionResult <IEnumerable <CourseDto> >)NotFound()
                : Ok(_mapper.Map <IEnumerable <CourseDto> >(_repository.GetCourses(authorId))));
        }