public void TestRemovePromotionBookOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); } using (var context = new EfCoreContext(options)) { var utData = context.SetupSingleDtoAndEntities <AddRemovePromotionDto>(); var service = new CrudServices(context, utData.Wrapped); var dto = new AddRemovePromotionDto { BookId = 1, ActualPrice = 20, PromotionalText = "Half price today!" }; service.UpdateAndSave(dto); var book1 = context.Find <Book>(1); //ATTEMPT service.UpdateAndSave(dto, nameof(Book.RemovePromotion)); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); var book = context.Find <Book>(1); book.ActualPrice.ShouldEqual(40); } }
public void TestQueryFilterOnBookOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using var context = new EfCoreContext(options); context.Database.EnsureCreated(); var books = context.SeedDatabaseFourBooks(); books[3].SoftDeleted = true; context.SaveChanges(); context.ChangeTracker.Clear(); //ATTEMPT var booksCount = context.Books.Count(); var findBook1 = context.Find <Book>(books[1].BookId); var findBook3 = context.Find <Book>(books[3].BookId); var singleBook3 = context.Books.SingleOrDefault(x => x.BookId == books[3].BookId); var sqlBooksCount = context.Books.FromSqlRaw("SELECT * FROM Books").Count(); //VERIFY booksCount.ShouldEqual(3); findBook1.ShouldNotBeNull(); findBook3.ShouldBeNull(); singleBook3.ShouldBeNull(); sqlBooksCount.ShouldEqual(3); }
private void RunHandCodedAddReview(EfCoreContext context) { var book = context.Find <Book>(1); book.AddReview(5, "comment", "user", context); context.SaveChanges(); }
public void TestCreateBookUseCtorOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); var utData = context.SetupSingleDtoAndEntities <CreatorOfBooksDto>(); var service = new CrudServices(context, utData.Wrapped); //ATTEMPT var dto = new CreatorOfBooksDto { Title = "Hello", Price = 50, PublishedOn = new DateTime(2010, 1, 1), Authors = new List <Author> { new Author { Name = "test" } } }; service.CreateAndSave(dto, "ctor"); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); context.Set <Book>().Count().ShouldEqual(1); var book = context.Find <Book>(1); book.ActualPrice.ShouldEqual(dto.Price); } }
public Book UpdateBook(ChangePubDateDto dto) //#D { var book = _context.Find <Book>(dto.BookId); //#E book.PublishedOn = dto.PublishedOn; //#F _context.SaveChanges(); //#G return(book); //#H }
public Book AddPromotion(AddRemovePromotionDto dto) { var book = _context.Find <Book>(dto.BookId); if (book == null) { throw new InvalidOperationException($"Could not find the book with Id of {dto.BookId}."); } Status = book.AddPromotion(dto.ActualPrice, dto.PromotionalText); if (Status.HasErrors) { return(null); } _context.SaveChanges(); return(book); }
public Product UpdateProduct(ChangePriceDto dto) { var product = _context.Find <Product>(dto.ProductId); product.Price = dto.Price; _context.SaveChanges(); return(product); }
public Book UpdateBook(ChangePubDateDto dto) { var book = _context.Find <Book>(dto.BookId); book.UpdatePublishedOn(dto.PublishedOn); _context.SaveChanges(); return(book); }
public Book AddPromotion(AddRemovePromotionDto dto) { var book = _context.Find <Book>(dto.BookId); if (book == null) { AddError("Sorry, I could not find the book you were looking for."); return(null); } CombineStatuses(book.AddPromotion(dto.ActualPrice, dto.PromotionalText)); if (!IsValid) { return(null); } _context.SaveChanges(); return(book); }
public void TestChangeAllAuthorsViaForeignKeyOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); ChangeAuthorDto dto; using var context = new EfCoreContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var book = context.Books .Include(p => p.AuthorsLink) .Single(p => p.Title == "Quantum Networking"); var newAuthor = context.Authors .Single(p => p.Name == "Martin Fowler"); dto = new ChangeAuthorDto { BookId = book.BookId, AuthorId = book.AuthorsLink.First().AuthorId, NewAuthorId = newAuthor.AuthorId }; context.ChangeTracker.Clear(); //ATTEMPT var orgBookAuthor = context //#A .Find <BookAuthor>(dto.BookId, dto.AuthorId); //#A context.Set <BookAuthor>().Remove(orgBookAuthor); //#B context.Set <BookAuthor>().Add(new BookAuthor //#C { //#C BookId = dto.BookId, //#C AuthorId = dto.NewAuthorId, //#C Order = 0 //#C }); //#C context.SaveChanges(); //#D /***************************************************** #A I find the existing BookAuthor link using the BookId and the Authorid of the original author #B I then delete the original link #C Now I create a new BookAuthor link to the Author chosen by the user and add it the BookAuthor table #D Finally I call SaveChanges which find the deleted BookAuthor entry and the new BookAuthor entry and deletes/adds then respectively * **************************************************/ context.ChangeTracker.Clear(); //VERIFY var bookAgain = context.Books .Include(p => p.AuthorsLink).ThenInclude(p => p.Author) .Single(p => p.BookId == dto.BookId); bookAgain.AuthorsLink.ShouldNotBeNull(); bookAgain.AuthorsLink.Count.ShouldEqual(1); bookAgain.AuthorsLink.First().Author.Name.ShouldEqual("Martin Fowler"); }
public void TestPerformanceReadSingleDirect() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); } using (var context = new EfCoreContext(options)) { var utData = context.SetupEntitiesDirect(); var service = new CrudServices <EfCoreContext>(context, utData.ConfigAndMapper); using (new TimeThings(_output, "RunHandCoded Find", 1)) { context.Find <Book>(1); } using (new TimeThings(_output, "RunGenericService Find", 1)) { service.ReadSingle <Book>(1); } using (new TimeThings(_output, "RunGenericService Find", 1000)) { for (int i = 0; i < 1000; i++) { service.ReadSingle <Book>(1); } } using (new TimeThings(_output, "RunHandCoded Find", 1000)) { for (int i = 0; i < 1000; i++) { context.Find <Book>(1); } } } }
public void TestAddAuthorDisconnectedOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); ChangeAuthorDto dto; using var context = new EfCoreContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var book1 = context.Books .Include(p => p.AuthorsLink) .Single(p => p.Title == "Quantum Networking"); var newAuthor1 = context.Authors .Single(p => p.Name == "Martin Fowler"); dto = new ChangeAuthorDto { BookId = book1.BookId, AuthorId = book1.AuthorsLink.First().AuthorId, NewAuthorId = newAuthor1.AuthorId }; context.ChangeTracker.Clear(); //ATTEMPT var book = context.Books .Include(p => p.AuthorsLink) .Single(p => p.BookId == dto.BookId); var newAuthor = context.Find <Author>(dto.NewAuthorId); book.AuthorsLink.Add(new BookAuthor { Book = book, Author = newAuthor, Order = (byte)book.AuthorsLink.Count }); context.SaveChanges(); context.ChangeTracker.Clear(); //VERIFY var bookAgain = context.Books .Include(p => p.AuthorsLink).ThenInclude(p => p.Author) .Single(p => p.BookId == dto.BookId); bookAgain.AuthorsLink.ShouldNotBeNull(); bookAgain.AuthorsLink.Count.ShouldEqual(2); var authorsInOrder = bookAgain.AuthorsLink.OrderBy(p => p.Order).ToList(); authorsInOrder.First().Author.Name.ShouldEqual("Future Person"); authorsInOrder.Last().Author.Name.ShouldEqual("Martin Fowler"); }
public Book AddReviewToBook(AddReviewDto dto) { var book = _context.Find <Book>(dto.BookId); if (book == null) { throw new InvalidOperationException($"Could not find the book with Id of {dto.BookId}."); } book.AddReview(dto.NumStars, dto.Comment, dto.VoterName, _context); _context.SaveChanges(); return(book); }
public Book AddReviewToBook(AddReviewDto dto) { var book = _context.Find <Book>(dto.BookId); if (book == null) { AddError("Sorry, I could not find the book you were looking for."); return(null); } book.AddReview(dto.NumStars, dto.Comment, dto.VoterName, _context); _context.SaveChanges(); return(book); }
public Book UpdateBook(int id, ChangePubDateDto dto) { //Should return error message on not found var book = _context.Find <Book>(id); if (book == null) { AddError("Sorry, I could not find the book you were looking for."); return(null); } book.UpdatePublishedOn(dto.PublishedOn); _context.SaveChanges(); return(book); }
public void RunHandCoded() { //SETUP using (var context = new EfCoreContext(_options)) { var numReviews = context.Set <Review>().Count(); //ATTEMPT var book = context.Find <Book>(1); book.AddReview(5, "comment", "user", context); context.SaveChanges(); //VERIFY context.Set <Review>().Count().ShouldEqual(numReviews + 1); } }
public void TestChangeReviewViaForeignKeyOk() { //SETUP ChangeReviewDto dto; var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using var context = new EfCoreContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var book = context.Books.First(); var review = context.Set <Review>().First(); dto = new ChangeReviewDto { ReviewId = review.ReviewId, NewBookId = book.BookId }; context.ChangeTracker.Clear(); //ATTEMPT var reviewToChange = context //#A .Find <Review>(dto.ReviewId); //#A reviewToChange.BookId = dto.NewBookId; //#B context.SaveChanges(); //#C /***************************************************** #A I find the review that I want to move using the primary key returned from the browser #C I then change the foreign key in the review to point to the book it should be linked to #D Finally I call SaveChanges which finds the foreign key in the Review changed, so it updates that column in the database * **************************************************/ //VERIFY var bookAgain = context.Books .Include(p => p.Reviews) .First(); bookAgain.Reviews.ShouldNotBeNull(); bookAgain.Reviews.Count.ShouldEqual(1); }
public void TestChangeReviewViaForeignKeyOk() { //SETUP var options = this.NewMethodUniqueDatabaseSeeded4Books(); ChangeReviewDto dto; using (var context = new EfCoreContext(options)) { var book = context.Books.First(); var review = context.Set <Review>().First(); dto = new ChangeReviewDto { ReviewId = review.ReviewId, NewBookId = book.BookId }; } using (var context = new EfCoreContext(options)) { //ATTEMPT var reviewToChange = context //#A .Find <Review>(dto.ReviewId); //#A reviewToChange.BookId = dto.NewBookId; //#B context.SaveChanges(); //#C /***************************************************** #A I find the review that I want to move using the primary key returned from the browser #C I then change the foreign key in the review to point to the book it should be linked to #D Finally I call SaveChanges which finds the foreign key in the Review changed, so it updates that column in the database * **************************************************/ //VERIFY var bookAgain = context.Books .Include(p => p.Reviews) .First(); bookAgain.Reviews.ShouldNotBeNull(); bookAgain.Reviews.Count.ShouldEqual(1); } }
public void SoftDeleteOneBookFind() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var firstBook = context.Books.First(); firstBook.SoftDeleted = true; context.SaveChanges(); //ATTEMPT var softBook = context.Find <Book>(firstBook.BookId); //VERIFY softBook.ShouldNotBeNull(); } }
public bool HasValidDescipline(int desciplineId) { return(_context.Find <Descipline>(desciplineId) != null); }
public OrderBooksDto BuildBooksDto(int bookId, byte numBooks) { return(new OrderBooksDto(bookId, _context.Find <Book>(bookId), numBooks)); }
public MDRStatus GetMdrStatus(int mdrStatusId) { return(_context.Find <MDRStatus>(mdrStatusId)); }
public PunchCategory GetPunchCategory(int punchcategoryid) { return(_context.Find <PunchCategory>(punchcategoryid)); }
public Project GetProject(Guid projectId) { return(_context.Find <Project>(projectId)); }
public FormDictionary GetFormDictionary(long formDictionaryId) { return(_context.Find <FormDictionary>(formDictionaryId)); }
public WorkPackageStep GetWorkPackageStep(int workPackagestepId) { var item = _context.Find <WorkPackageStep>(workPackagestepId); return(item); }
public ValueUnit GetValueUnit(int valueUnitId) { return(_context.Find <ValueUnit>(valueUnitId)); }
public Person GetPerson(int personId) { return(_context.Find <Person>(personId)); }
public PunchType GetPunchType(int punchTypeid) { return(_context.Find <PunchType>(punchTypeid)); }
public ProjectSubSystem GetSubSystme(long subsystemId) { return(_context.Find <ProjectSubSystem>(subsystemId)); }