Example #1
0
    public async Task <AuthorDto> CreateAsync(CreateAuthorDto input)
    {
        var author = await _authorManager.CreateAsync(
            input.Name,
            input.BirthDate,
            input.ShortBio
            );

        await _authorRepository.InsertAsync(author);

        return(ObjectMapper.Map <Author, AuthorDto>(author));
    }
Example #2
0
        public async Task <AuthorDto> AddNewAuthorAsync(CreateAuthorDto newAuthor)
        {
            if (string.IsNullOrEmpty(newAuthor.Email))
            {
                throw new Exception("Author can not have empty email.");
            }

            var author = _mapper.Map <Author>(newAuthor);
            var result = await _authorRepository.AddAsync(author);

            return(_mapper.Map <AuthorDto>(result));
        }
Example #3
0
        public int Create(int bookId, CreateAuthorDto dto)
        {
            //var book = GetBookById(bookId);

            var authorEntity = _mapper.Map <Author>(dto);

            authorEntity.BookId = bookId;

            _dbContext.Authors.Add(authorEntity);
            _dbContext.SaveChanges();

            return(authorEntity.Id);
        }
Example #4
0
        public ActionResult <AuthorDto> CreateAuthor([FromBody] CreateAuthorDto authorDto)
        {
            if (authorDto == null)
            {
                return(BadRequest());
            }

            var author = AuthorMapper.ToAuthor(authorDto);

            AuthorRepository.Create(author);
            _unitOfWork.Commit();

            return(CreatedAtRoute("GetAuthor", new { id = author.Id }, AuthorMapper.ToAuthorDto(author)));
        }
Example #5
0
        public AuthorDto CreateAuthor(CreateAuthorDto createAuthor, string currentUserId)
        {
            CreateAuthorValidator authorValidator = new CreateAuthorValidator();

            if (!authorValidator.Validate(createAuthor).IsValid)
            {
                throw new Exception("Empty_Null");
            }
            Author author = _mapper.Map <CreateAuthorDto, Author>(createAuthor);

            author.CreatedOn = DateTime.Now;
            author.CreatedBy = currentUserId;
            _unitOfWork.AuthorRepository.Add(author);
            _unitOfWork.Save();
            return(_mapper.Map <Author, AuthorDto>(author));
        }
Example #6
0
        public IActionResult CreateAuthor([FromBody] CreateAuthorDto createAuthorDto)
        {
            if (createAuthorDto == null)
            {
                return(BadRequest());
            }

            var author = _mapper.Map <Author>(createAuthorDto);

            _libraryRepository.AddAuthor(author);
            _libraryRepository.Save();

            var authorDto = _mapper.Map <AuthorDto>(author);

            return(CreatedAtAction("GetAuthor", new { id = author.Id }, authorDto));
        }
Example #7
0
        public async Task <ActionResult <AuthorDto> > CreateAuthor(CreateAuthorDto author)
        {
            Guard.Against.Null(author, nameof(author));

            var command = new CreateAuthorCommand(author.FirstName, author.LastName, author.DateOfBirth, author.MainCategory, author.Books);

            Result <Author> result = await _messages.Dispatch(command);

            if (result.IsFailure)
            {
                return(BadRequest(result.Error));
            }

            var authorToReturn = _mapper.Map <AuthorDto>(result.Value);

            return(CreatedAtRoute(nameof(GetAuthor), new { authorId = authorToReturn.Id }, authorToReturn));
        }
        public async Task AddAuthorAsync_ShouldThrowExceptionForInvalidDataAnnotationRequirement()
        {
            // given (arrange)
            Filler <CreateAuthorDto> authorFiller = new Filler <CreateAuthorDto>();

            CreateAuthorDto invalidAuthorToAddDto = authorFiller.Create();

            invalidAuthorToAddDto.ContactEmail = "badaddress";

            // when (act)
            var actualAuthorTask = subject.AddAuthorAsync(invalidAuthorToAddDto);

            // then (assert)
            await Assert.ThrowsAsync <ValidationException>(() => actualAuthorTask);

            appDbContextMock.VerifyNoOtherCalls();
        }
Example #9
0
        public IActionResult Create([FromBody] CreateAuthorDto model)
        {
            if (model == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }

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

            _libraryRepository.AddAuthor(author);

            if (!_libraryRepository.Save())
            {
                throw new Exception("Error");
            }

            return(Created($"/api/authors/{author.Id}", Mapper.Map <AuthorDto>(author)));
        }
Example #10
0
        public IActionResult CreateAuthor([FromBody] CreateAuthorDto author)
        {
            var create = _mapper.Map <Author>(author);

            _courseLibraryRepository.AddAuthor(create);
            var ans = _courseLibraryRepository.Save();

            //_courseLibraryRepository.Save();
            if (ans)
            {
                var authorReturn = _mapper.Map <AuthorDto>(create);
                return(CreatedAtAction(
                           "GetAuthor",
                           new { authorId = authorReturn.Id },
                           authorReturn));
            }

            return(BadRequest("Unable to save changes"));
        }
Example #11
0
        public async Task <AuthorDto> Add(CreateAuthorDto book)
        {
            try
            {
                Author author = Author.Create(book);

                await _authorRepository.AddAsync(author);

                await _unitOfWork.CommitAsync();

                return(_mapper.Map <Author, AuthorDto>(author));
            }
            catch (Exception ex)
            {
                await _unitOfWork.RollbackAsync();

                throw ErrorResponse.InternalServerError(ex);
            }
        }
Example #12
0
        public IActionResult CreateAuthor([FromBody] CreateAuthorDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            this.repository.AddAuthor(authorEntity);
            if (!this.repository.Save())
            {
                throw new Exception("A problem occured while handing your request");
            }

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

            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn));
        }
        public async Task <IActionResult> UpdateAuthor(long id, [FromBody] CreateAuthorDto authorDto)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo(GenerateLogMessage(location, MESSAGE_UPDATE_ATTEMPTED, id));
                if (id < 0 || authorDto == null)
                {
                    _logger.LogInfo(GenerateLogMessage(location, MESSAGE_UPDATE_ATTEMPTED, id));
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogInfo(GenerateLogMessage(location, MESSAGE_UPDATE_ATTEMPTED, id));
                    return(BadRequest());
                }

                if (!await _authorRepository.IsInDatabase(id))
                {
                    _logger.LogError(GenerateLogMessage(location, MESSAGE_UPDATE_ID_NOT_FOUND, id));
                    return(BadRequest());
                }

                var author = _mapper.Map <Author>(authorDto);
                author.Id = id;
                var isSuccess = await _authorRepository.Update(author);

                if (isSuccess)
                {
                    _logger.LogInfo(GenerateLogMessage(location, MESSAGE_UPDATE_SUCCESSFUL, id));
                    return(NoContent());
                }

                _logger.LogError(GenerateLogMessage(location, MESSAGE_UPDATE_FAILED));
                return(BadRequest());
            }
            catch (Exception e)
            {
                return(InternalError(GenerateLogMessage(location, e)));
            }
        }
Example #14
0
        public async Task <IActionResult> AddAuthor([FromBody] CreateAuthorDto createAuthorDto)
        {
            Author author = mapper.Map <Author>(createAuthorDto);
            var    result = await courseLibraryService.AddAuthor(author);

            if (result.Success)
            {
                SuccessOperationResult <Author> successOperation = result as SuccessOperationResult <Author>;
                AuthorDto authorDto = mapper.Map <AuthorDto>(successOperation.Result);
                return(Ok(new SuccessOperationResult <AuthorDto>
                {
                    Result = authorDto,
                    Code = ConstOperationCodes.AUTHOR_CREATED,
                }));
            }
            var failedOperation = result as FailedOperationResult <Author>;

            return(Ok(failedOperation));
        }
Example #15
0
        public async Task <AuthorDto> Update(Guid id, CreateAuthorDto updatedAuthor)
        {
            try
            {
                Author author = _mapper.Map <CreateAuthorDto, Author>(updatedAuthor);

                await _authorRepository.UpdateAsync(id, author);

                await _unitOfWork.CommitAsync();

                return(_mapper.Map <Author, AuthorDto>(author));
            }
            catch (Exception ex)
            {
                await _unitOfWork.RollbackAsync();

                throw ErrorResponse.InternalServerError(ex);
            }
        }
        public IActionResult CreateAuthor([FromBody] CreateAuthorDto createAuthorDto)
        {
            if (createAuthorDto == null)
            {
                return(BadRequest());
            }

            var author = createAuthorDto.ToAuthor();

            _libraryRepository.AddAuthor(author);

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

            var authorDto = new AuthorDto(author);
            var data      = (IDictionary <string, object>)authorDto.ShapeData(null);
            var links     = CreateLinksForAuthor(authorDto.Id, null);

            data.Add("links", links);

            return(CreatedAtRoute(nameof(GetAuthor), new { id = author.Id }, data));
        }
Example #17
0
 public Authors()
 {
     NewAuthor     = new CreateAuthorDto();
     EditingAuthor = new UpdateAuthorDto();
 }
Example #18
0
 private void OpenCreateAuthorModal()
 {
     NewAuthor = new CreateAuthorDto();
     CreateAuthorModal.Show();
 }
Example #19
0
 public Task <AuthorDto> CreateAsync(CreateAuthorDto input)
 {
     return(_authorAppService.CreateAsync(input));
 }
Example #20
0
 public ActionResult Post([FromBody] CreateAuthorDto createAuthor)
 {
     return(Ok(_authorService.CreateAuthor(createAuthor, CurrentUserId())));
 }
Example #21
0
        public ActionResult CreateAuthor([FromRoute] int bookId, [FromBody] CreateAuthorDto dto)
        {
            var newAuthorId = _authorService.Create(bookId, dto);

            return(Created($"api/book/{bookId}/author/{newAuthorId}", null));
        }
 public IActionResult Post([FromBody] CreateAuthorDto dto,
                           [FromServices] ICreateAuthorCommand command)
 {
     _executor.ExecuteCommand(command, dto);
     return(StatusCode(StatusCodes.Status201Created));
 }
Example #23
0
 public async Task <ServiceResult> Create([FromBody] CreateAuthorDto item) => await _authorService.Create(item);
        public async Task <Response <AuthorDto> > Add([FromBody] CreateAuthorDto author)
        {
            AuthorDto createdAuthor = await _authorService.Add(author);

            return(Response <AuthorDto> .Post(_httpContextAccessor, createdAuthor));
        }
        public async Task <Response <AuthorDto> > Update(Guid id, [FromBody] CreateAuthorDto author)
        {
            AuthorDto updatedAuthor = await _authorService.Update(id, author);

            return(Response <AuthorDto> .Patch(_httpContextAccessor, updatedAuthor));
        }
Example #26
0
        public async Task <IActionResult> Create(CreateAuthorDto newAuthor)
        {
            var author = await _authorService.AddNewAuthorAsync(newAuthor);

            return(Created($"api/author/{author.Id}", new Response <AuthorDto>(author)));
        }