public void AddAuthorTest() { Assert.AreEqual(0, _dataLayer.GetAllAuthors().Count()); Author tolkien = new Author(Guid.NewGuid(), "John Ronald Reuel", "Tolkien"); _dataLayer.AddAuthor(tolkien); Assert.AreEqual(1, _dataLayer.GetAllAuthors().Count()); Author fDostojewski = new Author(Guid.NewGuid(), "Fiodor", "Dostojewski"); _dataLayer.AddAuthor(fDostojewski); Assert.AreEqual(2, _dataLayer.GetAllAuthors().Count()); Assert.ThrowsException <ArgumentException>(() => _dataLayer.AddAuthor(fDostojewski)); }
public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorForCreationWithDateOfDeathDto author) { if (author == null) { return(BadRequest()); } Author authorEntity = Mapper.Map <Author>(author); LibraryRepository.AddAuthor(authorEntity); if (!LibraryRepository.Save()) { throw new Exception("Ocorreu um problema ao salvar a criação do autor"); } ; var authorToReturn = Mapper.Map <AuthorDto>(authorEntity); var links = CreateLinksForAuthor(authorToReturn.Id, null); var linkedResourceToReturn = authorToReturn.ShapeData(null) as IDictionary <string, object>; linkedResourceToReturn.Add("links", links); return(CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn)); }
public async Task <IActionResult> Create([Bind("Id,Name")] Author author) { if (ModelState.IsValid) { _repository.AddAuthor(author); return(RedirectToAction(nameof(Index))); } return(View(author)); }
public IHttpActionResult PostAuthor(Author author) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.AddAuthor(author); return(CreatedAtRoute("DefaultApi", new { id = author.Auth_Id }, author)); }
public void AddAuthor() { using (var context = new LibraryContext(options)) { var service = new LibraryRepository(context); service.AddAuthor(new Data.Entities.Author() { FirstName = "Mark", LastName = "Asd" }); service.Save(); Assert.IsNotNull(context.Authors.Any(a => a.FirstName == "John" && a.LastName == "Asd")); } }
public void IsRepositoryAddsAuthor() { Author productToInsert = new Author { Auth_Id = 4, First_Name = "Author FirstName 004", Last_Name = "Author LastName 004", Biography = "Author 4th Bio" }; Repo.AddAuthor(productToInsert); // If Product inserts successfully, //number of records will increase to 4 var result = Repo.GetAuthors(); var numberOfRecords = result.ToList().Count; Assert.AreEqual(4, numberOfRecords); }
public void AddAuthor_PassAuthorObject_AuthorAddedToDatabse() { //Arrange var options = new DbContextOptionsBuilder <DataContext>().UseInMemoryDatabase("Add_author_to_database").Options; var author = new Author { Name = "Test", Surname = "Test" }; //Act using (var context = new DataContext(options)) { var repo = new LibraryRepository(context); repo.AddAuthor(author); context.SaveChanges(); } //Assert using (var context = new DataContext(options)) { Assert.Equal(1, context.Authors.Count()); Assert.Equal("Test", context.Authors.Single().Name); } }