Ejemplo n.º 1
0
                                       new [] { "application/vnd.tmaturano.author.full+json" })] //should only accept requests with this media type
        public IActionResult CreateAuthor([FromBody] AuthorInputDto authorDto)
        {
            if (authorDto == null)
            {
                return(BadRequest());
            }

            var result = _authorService.Add(authorDto);

            if (!result.sucess)
            {
                //Throwing an exception is expensive, but at this case, we have at the global level on Startup handling all the
                //500 Error, so to keep it in one place, I'm throwing .
                throw new Exception("Creating an author failed on save.");
                //return StatusCode(500, "");
            }

            var authorToReturn = _mapper.Map <AuthorOutputDto>(authorDto);

            authorToReturn.Id = result.id;

            //CreatedAtRoute will contain the URI where the newly author can be found 1st parameter
            //also, the id of the generated author in 2nd parameter
            //The URI is sent through response's header Location
            return(CreatedAtRoute("GetAuthor",
                                  new { id = authorToReturn.Id },
                                  authorToReturn));
        }
Ejemplo n.º 2
0
        public IActionResult CreateAuthor([FromBody] AuthorInputDto newAuthor,
                                          [FromHeader(Name = "Content-Type")] string mediaType)
        {
            if (validator.DontMatchRules(newAuthor as IAuthorInputDto, ModelState))
            {
                return(BadRequest(ModelState));
            }

            var authorToAdd = mapper.Map <Author>(newAuthor);

            var authorFromRepo = blogRepository.GetAuthor(authorToAdd.Id);

            if (authorFromRepo != null)
            {
                return(BadRequest());
            }

            blogRepository.AddAuthor(authorToAdd);
            blogRepository.SaveChanges();

            var     mappedAuthor = mapper.Map <AuthorOutputDto>(authorToAdd);
            dynamic toReturn     = mappedAuthor;

            if (IncludeLinks(mediaType))
            {
                var shapedAuthor = properties.ShapeSingleData(mappedAuthor);
                var idsSet       = new AuthorIdsSet(authorToAdd.Id);
                toReturn = GetLinkedResource(shapedAuthor, idsSet);
            }

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToAdd.Id }, toReturn));
        }
Ejemplo n.º 3
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorInputDto authoeInput)
        {
            var authorEntity = _mapper.Map <Entities.Author>(authoeInput);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var authorOutput = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor",
                                  new { authorId = authorOutput.Id },
                                  authorOutput));
        }
Ejemplo n.º 4
0
        public (bool sucess, Guid id) Add(AuthorInputDto obj)
        {
            try
            {
                var author = _mapper.Map <AuthorInputDto, Author>(obj);
                _authorService.Add(author);


                return(unitOfWork.Commit(), author.Id);
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates the author.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public bool CreateAuthor(AuthorInputDto input)
        {
            if (String.IsNullOrEmpty(input.AuthorDto.Name))
            {
                return(false);
            }

            var result = _authorRepository.CreateEntity(new EAuthor
            {
                Name            = input.AuthorDto.Name.Trim(),
                CreatedDateTime = DateTime.Now
            });

            return(result != null);
        }
Ejemplo n.º 6
0
        public BaseResponse <bool> CreateAuthor(AuthorInputDto authorInputDto)
        {
            if (Contains(x => x.Name.Equals(authorInputDto.Name, StringComparison.InvariantCultureIgnoreCase)))
            {
                throw new BadRequestException($"Tác giả {authorInputDto.Name} đã tồn tại");
            }

            var author = Mapper.Map <Author>(authorInputDto);

            Create(author, out var isSaved);
            if (!isSaved)
            {
                throw new BadRequestException($"Không thể tạo author {author.Name}");
            }

            return(new BaseResponse <bool>(HttpStatusCode.OK, data: true));
        }
Ejemplo n.º 7
0
        public BaseResponse <bool> DeleteAuthor(Guid id, AuthorInputDto authorInputDto)
        {
            var author = Find(id);

            if (author == null)
            {
                throw new BadRequestException($"Không tìm thấy tác giả {id}");
            }

            var deleted = Delete(author);

            if (!deleted)
            {
                throw new InternalServerErrorException($"Không thể delete tác giả {author.Name}");
            }

            return(new BaseResponse <bool>(HttpStatusCode.OK, data: true));
        }
Ejemplo n.º 8
0
        public IActionResult CreateAuthor([FromBody] AuthorInputDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            var newAuthor = AutoMapper.Mapper.Map <Author>(author);

            _libraryRepository.AddAuthor(newAuthor);

            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save");
            }

            var authorToReturn = AutoMapper.Mapper.Map <AuthorDto>(newAuthor);

            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 9
0
 public async Task Post([FromBody] AuthorInputDto input)
 {
     var author = _mapper.Map <Author>(input);
     await _authorManager.InsertAsync(author);
 }
Ejemplo n.º 10
0
 public BaseResponse <bool> DeleteAuthor(Guid id, [FromBody] AuthorInputDto authorInputDto)
 {
     return(_authorService.DeleteAuthor(id, authorInputDto));
 }
Ejemplo n.º 11
0
 public BaseResponse <bool> Create([FromBody] AuthorInputDto authorInputDto)
 {
     return(_authorService.CreateAuthor(authorInputDto));
 }
Ejemplo n.º 12
0
 public bool Update(AuthorInputDto obj)
 {
     throw new NotImplementedException();
 }