public Guid?CreateAuthor(AuthorDto authorDto) { var author = Mapper.Map <AuthorDto, Author>(authorDto); applicationDbContext.Authors.Add(author); applicationDbContext.SaveChanges(); return(author.Id); }
private AuthorDto MapToAuthorDto(Author author) { var dto = new AuthorDto { Id = author.Id, Name = author.FirstName + " " + author.LastName }; return(dto); }
public async Task <ActionResult <AuthorDto> > PostAuthor(AuthorDto author) { var authormod = mapper.Map <Author>(author); _context.Authors.Add(authormod); await _context.SaveChangesAsync(); return(CreatedAtAction("GetAuthor", new { id = authormod.AuthorId }, mapper.Map <AuthorDto>(authormod))); }
public IHttpActionResult GetAuthor(int id) { var author = _authorRepository.GetById(id); AuthorDto authorDto = new AuthorDto(); _mapper.Map(author, authorDto); return(Ok(authorDto)); }
private AuthorDto AddAuthorDtoLinks(AuthorDto author) { author.Links.AddReference("edit", $"api/authors/{author.Id}"); author.Links.AddReference("delete", $"api/authors/{author.Id}"); author.Links.AddReference("self", $"api/authors"); author.Links.AddReference("newsItems", $"api/authors/{author.Id}/newsItems"); author.Links.AddListReference("newsItemsDetailed", _authorRepo.GetNewsItemsByAuthor(author.Id) .Select(n => new { href = $"api/{n.Id}" })); return(author); }
public ActionResult <AuthorDto> CreateAuthor([FromBody] AuthorDto authorDto) { Author authorRepo = _libraryRepository.AddAuthor(authorDto); //return CreatedAtRoute("GetAuthor", new { id = authorDto.Id }, authorDto); AuthorDto author = Mapper.Map <AuthorDto>(authorRepo); return(CreatedAtAction("CreateAuthor", author)); }
public async Task Setup() { _author = AuthorBuilder.WithLibrary(LibraryId).Build(); _authorBooks = BookBuilder.WithLibrary(LibraryId).WithAuthor(_author).IsPublic().Build(25); AuthorBuilder.WithLibrary(LibraryId).WithBooks(3).Build(); _response = await Client.GetAsync($"/libraries/{LibraryId}/books?pageNumber=2&pageSize=10&authorId={_author.Id}&query=itle"); _assert = new PagingAssert <BookView>(_response); }
public void Execute(AuthorDto request) { _validator.ValidateAndThrow(request); var author = _mapper.Map <Author>(request); _context.Authors.Add(author); _context.SaveChanges(); }
public static Author ToAuthorEntity(this AuthorDto authorDto) { return(new Author() { Id = authorDto.Id, FirstName = authorDto.FirstName, LastName = authorDto.LastName, Email = authorDto.Email }); }
public Author UpdateAuthor(AuthorDto authorDto) { Author author = GetAuthor(authorDto.Id); Mapper.Map(authorDto, author); _libraryContext.Update(author); _libraryContext.SaveChanges(); return(author); }
public async Task AddAuthorAsync(AuthorDto author) { var authorEntity = _mapper.Map <AuthorDto, Author>(author); await _uow.Authors.AddAsync(authorEntity); await _uow.SaveAsync(); author.Id = authorEntity.Id; }
public async Task Setup() { var authors = AuthorBuilder.WithLibrary(LibraryId).Build(4); _expected = authors.PickRandom(); _response = await Client.GetAsync($"/libraries/{LibraryId}/authors/{_expected.Id}"); _assert = AuthorAssert.WithResponse(_response).InLibrary(LibraryId); }
public async Task PutAsync(int id, AuthorDto authorDto, CancellationToken cancellationToken = default) { var authorToUpdate = await _dataContext.Authors.SingleAsync(author => author.Id == id, cancellationToken); authorToUpdate.Name = authorDto.Name; authorToUpdate.Biography = authorDto.Biography; await _dataContext.SaveChangesAsync(cancellationToken); }
public async Task CreateOrEditAuthor_ReturnsOkRequestResponse() { var param = new AuthorDto { Id = 0, Name = "Machado de Assis" }; var content = new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8, "application/json"); var response = await _testContext.Client.PostAsync("/api/Author/CreateOrEdit", content); response.EnsureSuccessStatusCode(); response.StatusCode.Should().Be(HttpStatusCode.OK); }
private AuthorDto[] GetAuthorDtoArray() { AuthorDto[] authorDtoArray = new AuthorDto[10]; for (int i = 0; i < 10; i++) { authorDtoArray[i] = GetTestAuthorDto(i + 11); } return(authorDtoArray); }
public Author AddAuthor(AuthorDto authorDto) { authorDto.Id = Guid.NewGuid(); Author author = new Author(); Mapper.Map(authorDto, author); _libraryContext.Authors.Add(author); _libraryContext.SaveChanges(); return(author); }
//// POST: api/Authors //[ResponseType(typeof(Author))] public IHttpActionResult PostAuthor(AuthorDto author) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _authorsService.Add(author); return(CreatedAtRoute("DefaultApi", new { id = author.Id }, author)); }
//// GET: api/Authors/5 //[ResponseType(typeof(Author))] public IHttpActionResult GetAuthor(int id) { AuthorDto author = _authorsService.GetById(id); if (author == null) { return(NotFound()); } return(Ok(author)); }
public async Task <ActionResult <AuthorDto> > UpdateAuthor(Guid authorId, [FromBody] AuthorUpdateRequest authorRequest) { AuthorDto author = await _authorService.UpdateAuthorAsync(authorId, authorRequest); if (author == null) { return(NotFound()); } return(Ok(author)); }
private AuthorDto CreateLinkForAuthor(AuthorDto author) { author.Links.Add(new LinkDto(_urlHelper.Link("GetAuthor", new { id = author.Id }), "self", "GET")); author.Links.Add(new LinkDto(_urlHelper.Link("DeleteAuthor", new { id = author.Id }), "delete_author", "DELETE")); author.Links.Add(new LinkDto(_urlHelper.Link("GetAuthor", new { id = author.Id }), "self", "GET")); return(author); }
public async Task <ActionResult <AuthorDto> > Get([FromRoute] int id) { AuthorDto author = _mapper.Map <AuthorDto>(await _authorService.GetByIdAsync(id)); if (author == null) { throw new ApiException(MessagesResource.AUTHOR_NOT_FOUND, 404); } return(Ok(author)); }
public AuthorDto GetAuthor(string alias) { Author authorEntity = Context.Author.FirstOrDefault(i => i.Alias == alias); AuthorDto author = null; if (authorEntity != null) { author = authorEntity.Adapt <AuthorDto>(); } return(author); }
private void ThrowIfAuthorIncorrect(AuthorDto author) { if (author == null) { throw new ArgumentNullException(nameof(author)); } if (string.IsNullOrEmpty(author.Lastname) || string.IsNullOrEmpty(author.Firstname)) { throw new AuthorIncorrectException(); } }
public static AuthorDto GetAuthorByID(int ID) { using (var uow = new UnitOfWork()) { ENTAuthor nTAuthors = uow.AuthorRepository.GetByID(ID); AuthorDto author = new AuthorDto(); author.ID = nTAuthors.ID; author.Name = nTAuthors.Name; return(author); } }
public void UpdateAuthor_WhenCalledWithInvalidData_ShouldThrowException() { var author = new AuthorDto { FirstName = "Only completed field" }; // Act / Assert Assert.Throws <InvalidDataException>( () => _authorService.UpdateAuthor(new Guid(), author)); }
public void Create_ExistsAuthor_ShouldThrownAuthorDublicateException() { var dto = new AuthorDto() { Lastname = DefaultData.Authors.Flenagan.Lastname, Firstname = DefaultData.Authors.Flenagan.Firstname, Middlename = DefaultData.Authors.Flenagan.Middlename, }; Assert.Throws <AuthorDublicateException>(async() => await AuthorsService.Create(dto)); }
public IActionResult DeleteAuthor([FromBody] AuthorDto authorDto) { var author = _authorService.DeleteAuthor(authorDto); if (author == null) { return(NotFound("Author with those credentials does not exist.")); } return(Ok(author)); }
public async Task <ActionResult> Post([FromBody] AuthorDto dto) { try { return(Ok(await AuthorService.Create(dto))); } catch (BusinessRulesException e) { return(StatusCode((int)e.StatusCode, e.Message)); } }
private AuthorDto CreateLinksForAuthor(AuthorDto author) { author.Links.Clear(); //添加查询author的link author.Links.Add(new Link(HttpMethods.Get, "self", Url.Link(nameof(GetAuthorAsync), new { authorId = author.Id }))); //添加删除author的link author.Links.Add(new Link(HttpMethods.Delete, "delete author", Url.Link(nameof(DeleteAuthorAsync), new { authorId = author.Id }))); //添加查询author下所有book的link author.Links.Add(new Link(HttpMethods.Get, "author's book", Url.Link(nameof(BookController.GetBooksAsync), new { authorId = author.Id }))); return(author); }
public int Save(string firstName, string lastName, string email) { AuthorDto authorDto = new AuthorDto() { FirstName = firstName, LastName = lastName, Email = email }; return(service.Save(authorDto)); }