示例#1
0
        public bool AddReview(int id, int numStars, string comment, string voterName)
        {
            var book = _context.Books.Include(x => x.Reviews)
                       .SingleOrDefault(x => x.Id == id);

            if (book == null)
            {
                return(false);
            }

            book.AddReview(numStars, comment, voterName);
            _context.SaveChanges();
            return(true);
        }
示例#2
0
        public void TestAddReviewToBookOk()
        {
            //SETUP1
            var options = SqliteInMemory.CreateOptions <PocoOnlyDbContext>();

            using (var context = new PocoOnlyDbContext(options))
            {
                context.Database.EnsureCreated();
                var book = new Book("test", null, DateTime.UtcNow, "Me", 123, null,
                                    new List <Author> {
                    new Author("Me", "*****@*****.**")
                });
                context.Add(book);
                context.SaveChanges();
            }
            using (var context = new PocoOnlyDbContext(options))
            {
                var rep = new Repository(context, null);

                //ATTEMPT
                var ok = rep.AddReview(1, 2, "OK", "Me");

                //VERIFY
                ok.ShouldBeTrue();
            }
            using (var context = new PocoOnlyDbContext(options))
            {
                context.Set <Review>().Count().ShouldEqual(1);
                context.Books.Include(x => x.Reviews)
                .Single().Reviews.SingleOrDefault().VoterName.ShouldEqual("Me");
            }
        }
示例#3
0
        public void TestCreateBookWithAuthorOk()
        {
            //SETUP1
            var options = SqliteInMemory.CreateOptions <PocoOnlyDbContext>();

            using (var context = new PocoOnlyDbContext(options))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                var book = new Book("test", null, DateTime.UtcNow, "Me", 123, null,
                                    new List <Author> {
                    new Author("Me", "*****@*****.**")
                });
                context.Add(book);
                context.SaveChanges();
            }
            using (var context = new PocoOnlyDbContext(options))
            {
                //VERIFY
                context.Books.Count().ShouldEqual(1);
                context.Authors.Count().ShouldEqual(1);
                context.Books.Include(x => x.AuthorsLink).ThenInclude(x => x.Author)
                .Single().AuthorsLink.SingleOrDefault().Author.Name.ShouldEqual("Me");
            }
        }