示例#1
0
        public async Task <IActionResult> Post([FromBody] Author author)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            await _authorRepository.AddAsync(author);

            return(Ok(author));
        }
示例#2
0
        public async Task <IActionResult> Post([FromBody] Author author)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            await _authorRepository.AddAsync(author);

            return(CreatedAtRoute("GetAuthor", new { Id = author.Id },
                                  author));
        }
示例#3
0
        public async Task RegisterAsync(Guid authorId, string firstName, string lastName,
                                        string description, string imageUrl, DateTime?dateOfBirth, DateTime?dateOfDeath,
                                        string birthPlace, string authorWebsite, string authorSource, Guid userId)
        {
            var author = await _authorRepository.GetByIdAsync(authorId);

            if (author != null)
            {
                throw new ServiceException(ErrorCodes.AuthorAlreadyExist, $"Author with id '{authorId}' already exist.");
            }

            try
            {
                var authorImage = imageUrl.DefaultAuthorImageNotEmpty();

                author = new Author(authorId, firstName, lastName, description, authorImage,
                                    dateOfBirth, dateOfDeath, birthPlace, authorWebsite, authorSource, userId);
            }

            catch (DomainException ex)
            {
                throw new ServiceException(ex, ErrorCodes.InvalidInput, ex.Message);
            }

            await _authorRepository.AddAsync(author);
        }
示例#4
0
        public async Task <AuthorViewModel> AddAsync(AuthorInputModel authorInputModel)
        {
            var author = mapper.Map <Author>(authorInputModel);
            var result = await repository.AddAsync(author);

            var authorViewModel = mapper.Map <AuthorViewModel>(result);

            return(authorViewModel);
        }
示例#5
0
        //[ValidationAspect(Validator = typeof(CreateAuthorValidator),AspectPriority = 1)]
        public async Task <IResult> Handle(CreateAuthorCommand request, CancellationToken cancellationToken)
        {
            var author = _mapper.Map <Author>(request);
            var result = await _authorRepository.AddAsync(author);

            if (!result.Success)
            {
                return(new ErrorResult(result.Message));
            }
            return(new SuccessResult(result.Message));
        }
示例#6
0
        public async Task <ActionResult <AuthorApiModel> > Create([FromBody] Author author)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var newAuthor = await _authorRepository.AddAsync(author);

            return(CreatedAtAction(nameof(Create), AuthorToApiModel(newAuthor)));
        }
        public async Task AddAsync(AuthorModel item)
        {
            if (string.IsNullOrEmpty(item.Name))
            {
                throw new UserException(HttpStatusCode.BadRequest, new List <string> {
                    ExceptionsInfo.InvalidName
                });
            }

            await _authorRepository.AddAsync(_authorMapper.Map(item));
        }
示例#8
0
        public async Task SaveAsync(AuthorEdit authorEdit)
        {
            Author author = authorEdit.Id == null ? new Author() : await authorRepository.GetEntityAsync(authorEdit.Id.Value, true);

            mapper.Map(authorEdit, author);

            if (authorEdit.Id == null)
            {
                await authorRepository.AddAsync(author);
            }
            await authorRepository.SaveChangesAsync();
        }
示例#9
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));
        }
示例#10
0
        private async Task CreateNewAuthor(CreateBookCommand request)
        {
            var names     = request.AuthorName.Split(' ');
            var firstName = names[0];
            var lastName  = names[1];

            CreateAuthorCommand createAuthorCommand = new CreateAuthorCommand()
            {
                FirstName = firstName, LastName = lastName
            };

            await _authorRepository.AddAsync(_mapper.Map <Author>(createAuthorCommand));
        }
示例#11
0
        public async Task <string> SaveAsync(Author author)
        {
            try {
                await _authorRepository.AddAsync(author);

                await _unitOfWork.CompleteAsync();

                return(MessageConstants.Saved);;
            } catch (Exception ex) {
                // Do some logging stuff
                return($"{MessageConstants.SavedWarning}: {ex.Message}");;
            }
        }
示例#12
0
        public async Task <Result <Author> > CreateAuthor(Author newAuthor)
        {
            var validationResult = await _authorValidator.ValidateAsync(newAuthor);

            if (!validationResult.IsValid)
            {
                return(Result.Failure(newAuthor, validationResult.Errors.Select(s => s.ErrorMessage).ToList()));
            }

            var createdAuthor = await _authorRepository.AddAsync(newAuthor);

            return(Result.Success(createdAuthor));
        }
示例#13
0
        public async Task <ActionResult <Author> > CreateAuthor(AuthorCreateDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            var returnedEntity = await _authorRepository.AddAsync(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { authorId = returnedEntity.Id }, returnedEntity));
        }
示例#14
0
        public async Task <IdResponse> CreateAuthorAsync(AuthorProxy author)
        {
            Validate(author);

            await ValidateUniqueness(author.FirstName, author.LastName, null);

            var entity = new Author();

            MapToEntity(author, entity);

            var id = await _repository.AddAsync(entity);

            return(new IdResponse(id));
        }
示例#15
0
        public async Task <AuthorSaveResponse> SaveAsync(Author author)
        {
            try
            {
                await _authorRepository.AddAsync(author);

                await _transaction.CompleteAsync();

                return(new AuthorSaveResponse(author));
            }
            catch (Exception ex)
            {
                return(new AuthorSaveResponse($"An error occurred while attempting to save the author: {ex.Message}"));
            }
        }
示例#16
0
        public async Task <AuthorResponse> SaveAsync(Author Author)
        {
            try
            {
                await authorRepository.AddAsync(Author);

                await unitOfWork.CompleteAsync();

                return(new AuthorResponse(Author));
            }
            catch (Exception ex)
            {
                return(new AuthorResponse($"Error when saving the Author: {ex.Message}"));
            }
        }
示例#17
0
        public async Task <AuthorModel> Handle(CreateAuthorCommand request, CancellationToken cancellationToken)
        {
            IList <Book> books = new List <Book>();

            if (request.BookIds != null && request.BookIds.Any())
            {
                books = await bookRepository.FindWhereInAsync(request.BookIds);
            }

            var author = new Author(request.Name, books);

            await authorRepository.AddAsync(author);

            await authorRepository.UnitOfWork.SaveChangesAsync(cancellationToken);

            return(mapper.MapEntityToModel <Author, AuthorModel>(author));
        }
示例#18
0
        public async Task <IActionResult> Create(Author newAuthor, IFormCollection collection)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(newAuthor));
                }
                await _authorRepository.AddAsync(newAuthor);

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(View(newAuthor));
        }
示例#19
0
        public async Task <AuthorResponse> SaveAsync(Author author)
        {
            try
            {
                await _authorRepository.AddAsync(author);

                await _unitOfWork.CompleteAsync();

                author = await _authorRepository.FindByIdAsync(author.Id);

                return(new AuthorResponse(author));
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());

                return(new AuthorResponse("An error ocurred while saving the Author: " +
                                          $"{e.Message} {e.InnerException?.Message}"));
            }
        }
示例#20
0
        public async Task <MContentResult <AuthorDto> > AddAsync(AuthorDto authorDto)
        {
            //Validate
            var validationResult = await _validationService.ValidateAdd(authorDto);

            if (!validationResult.IsValid)
            {
                return(validationResult.ConvertFromValidationResult <AuthorDto>());
            }
            //Map to domain object
            var entityForDb = _mapper.Map <Author>(authorDto);
            var addedEntity = await _repository.AddAsync(entityForDb);

            //Map to dto
            var resultEntity = _mapper.Map <AuthorDto>(addedEntity);

            ResultDto.Data       = resultEntity;
            ResultDto.StatusCode = (int)StatusCodes.Created;
            return(ResultDto);
        }
示例#21
0
        public async Task <AuthorModel> AddAsync(AuthorModel model)
        {
            if (model is null)
            {
                throw new ServerException(HttpStatusCode.BadRequest,
                                          ExceptionMessage.EMPTY_AUTHOR);
            }
            if (await _repository.ExistsAsync(model.Id))
            {
                throw new ServerException(HttpStatusCode.BadRequest,
                                          ExceptionMessage.AUTHOR_ALREADY_EXISTS);
            }

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

            await _repository.AddAsync(author);

            var mapped = _mapper.Map <AuthorModel>(author);

            return(mapped);
        }
示例#22
0
        /// <summary>
        /// Create a new author.
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        public async Task <Result <AuthorInfo> > AddAsync(AuthorInfo author)
        {
            var authorDb = _mapper.Map <AuthorInfo, AuthorDb>(author);

            try
            {
                await _authorRepository.AddAsync(authorDb);

                return(Result <AuthorInfo> .Ok(_mapper.Map <AuthorInfo>(authorDb)));
            }
            catch (DbUpdateConcurrencyException e)
            {
                return((Result <AuthorInfo>) Result <AuthorInfo> .Fail($"Cannot save author. {e.Message}"));
            }
            catch (DbUpdateException e)
            {
                return((Result <AuthorInfo>) Result <AuthorInfo> .Fail($"Cannot save author. Duplicate field. {e.Message}"));
            }
            catch (DbEntityValidationException e)
            {
                return((Result <AuthorInfo>) Result <AuthorInfo> .Fail($"Invalid user. {e.Message}"));
            }
        }
示例#23
0
        public async Task <BookResponse> SaveAsync(Book book)
        {
            try
            {
                if (!string.IsNullOrEmpty(book.Genre.Name))
                {
                    var genre = await genreRepository.FindByKeyFieldsAsync(book.Genre.Name);

                    if (genre == null)
                    {
                        await genreRepository.AddAsync(book.Genre);

                        book.GenreId = book.Genre.Id;
                    }
                    else
                    {
                        book.GenreId = genre.Id;
                    }
                }

                if (!string.IsNullOrEmpty(book.Publisher.Name))
                {
                    var publisher = await publisherRepository.FindByKeyFieldsAsync(book.Publisher.Name, book.Publisher.City);

                    if (publisher == null)
                    {
                        await publisherRepository.AddAsync(book.Publisher);

                        book.PublisherId = book.Publisher.Id;
                    }
                    else
                    {
                        book.PublisherId = publisher.Id;
                    }
                }

                foreach (var author in book.Authors)
                {
                    var authorInBase = await authorRepository.FindByKeyFieldsAsync(author.Author.LastName, author.Author.FirstName, author.Author.Patronymic);

                    if (authorInBase == null)
                    {
                        await authorRepository.AddAsync(author.Author);

                        author.AuthorId = author.Author.Id;
                    }
                    else
                    {
                        author.AuthorId = authorInBase.Id;
                    }
                }

                await bookRepository.AddAsync(book);

                await unitOfWork.CompleteAsync();

                return(new BookResponse(book));
            }
            catch (Exception ex)
            {
                return(new BookResponse($"Error when saving the book: {ex.Message}"));
            }
        }
示例#24
0
        public async Task CreateAuthorAsync(Author author)
        {
            await _authorRepository.AddAsync(author);

            await _unitOfWork.CompleteAsync();
        }
示例#25
0
 public async Task AddAuthorAsync(Author newAuthor)
 {
     await _authorRepository.AddAsync(newAuthor);
 }
示例#26
0
 public async Task <Author> Handle(CreateAuthorCommand request, CancellationToken cancellationToken)
 {
     return(await authorRepository.AddAsync(request.Author));
 }
示例#27
0
 public async Task AddAuthorAsync(AuthorDTO authorDTO)
 {
     await _authorRepository.AddAsync(new Author(authorDTO.Firstname, authorDTO.Surname));
 }
        public async Task <IActionResult> Post([FromBody] Author author)
        {
            await _authorRepository.AddAsync(author);

            return(Ok(author));
        }