Exemple #1
0
        public IActionResult CreateAuthor(AuthorForCreationDto authorForCreation)
        {
            var createdEntity = mMapper.Map <Author>(authorForCreation);

            mRepository.AddAuthor(createdEntity);
            mRepository.SaveChangesAsync();
            var createdDtoToReturn = mMapper.Map <AuthorDto>(createdEntity);
            var linkedResource     = createdDtoToReturn.ShapeData(null) as IDictionary <string, object>;

            linkedResource.Add("links", CreateLinksForAuthor(createdEntity.Id, null));

            return(CreatedAtRoute("GetAuthor",
                                  new { authorId = linkedResource["Id"] }, linkedResource));
        }
Exemple #2
0
        public async Task <CreatedAtRouteResult> CreateAuthorsCollection(
            IEnumerable <AuthorForCreationDto> authors)
        {
            var authorEntities = mMapper.Map <IEnumerable <Author> >(authors);

            foreach (var author in authorEntities)
            {
                mRepository.AddAuthor(author);
            }
            await mRepository.SaveChangesAsync();

            var authorDtosToReturn = mMapper.Map <IEnumerable <AuthorDto> >(authorEntities);
            var idsAsString        = string.Join(",", authorDtosToReturn.Select(a => a.Id));

            return(CreatedAtRoute("GetAuthorCollection", new{ ids = idsAsString },
                                  authorDtosToReturn));
        }
Exemple #3
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));
        }