コード例 #1
0
 public IActionResult Details(int id, CreateCommentViewModel vm)
 {
     using (var context = new ReviewDbContext())
     {
         vm.Product  = _productService.GetById(id);
         vm.Comments = _commentService.GetByProductId(id);
         foreach (var comment in vm.Comments)
         {
             var upCount   = 0;
             var downCount = 0;
             foreach (var rating in _ratingService.GetByCommentId(comment.Id))
             {
                 if (rating.ThumbsUp)
                 {
                     upCount++;
                 }
                 else
                 {
                     downCount++;
                 }
             }
             var dbComments = context.Comments.Where(c => c.productId == id);
             var dbComment  = dbComments.Single(c => c.Id == comment.Id);
             dbComment.numThumbsUp   = upCount;
             dbComment.numThumbsDown = downCount;
             context.SaveChanges();
         }
     }
     vm.Comments = vm.Comments.OrderByDescending(c => c.numThumbsUp).ToList();
     return(View(vm));
 }
コード例 #2
0
 public IEnumerable <Review> Get()
 {
     using (var db = new ReviewDbContext())
     {
         return(db.Reviews);
     }
 }
コード例 #3
0
 public Comment GetById(int commentId)
 {
     using (var context = new ReviewDbContext())
     {
         return(context.Comments.Single(c => c.Id == commentId));
     }
 }
コード例 #4
0
 public Product GetById(int productId)
 {
     using (var context = new ReviewDbContext())
     {
         return(context.Products.SingleOrDefault(p => p.Id == productId));
     }
 }
コード例 #5
0
 public Rating GetById(int ratingId)
 {
     using (var context = new ReviewDbContext())
     {
         return(context.Ratings.Single(r => r.Id == ratingId));
     }
 }
コード例 #6
0
        public void ShouldBeAbleToAddToInMemoryDb()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <ReviewDbContext>()
                          .EnableSensitiveDataLogging()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());

            using (var context = new ReviewDbContext(builder.Options))
            {
                // Act
                var product = new Product {
                    Active = true, Description = "Poor quality fake faux leather cover loose enough to fit any mobile device.", Name = "Wrap It and Hope Cover"
                };


                var review = new Review {
                    ProductId = 1, Rating = 3, Comment = "Test comment", Active = true
                };

                context.Products.Add(product);
                context.Reviews.Add(review);
                context.SaveChanges();

                // Asser
                Assert.Equal(1, context.Reviews.Count(r => r.Comment == "Test comment"));
            };
        }
コード例 #7
0
 public async System.Threading.Tasks.Task PostAsync([FromBody] Review review)
 {
     using (var db = new ReviewDbContext())
     {
         db.Reviews.Add(review);
         await db.SaveChangesAsync();
     }
 }
コード例 #8
0
 public void DeleteById(int commentId)
 {
     using (var context = new ReviewDbContext())
     {
         var commentToBeDeleted = GetById(commentId);
         context.Remove(commentToBeDeleted);
         context.SaveChanges();
     }
 }
コード例 #9
0
 public ICollection <Rating> GetByCommentUserId(int commentId, string userId)
 {
     using (var context = new ReviewDbContext())
     {
         return(context.Ratings
                .Where(r => r.CommentId == commentId && r.UserId == userId)
                .ToList());
     }
 }
コード例 #10
0
 public ICollection <Comment> GetByProductId(int productId)
 {
     using (var context = new ReviewDbContext())
     {
         return(context.Comments
                .Where(c => c.productId == productId)
                .ToList());
     }
 }
コード例 #11
0
 public ICollection <Rating> GetByUserId(string userId)
 {
     using (var context = new ReviewDbContext())
     {
         return(context.Ratings
                .Where(r => r.UserId == userId)
                .ToList());
     }
 }
コード例 #12
0
 public ICollection <Product> GetByType(string type)
 {
     using (var context = new ReviewDbContext())
     {
         return(context.Products
                .Where(p => p.Type == type)
                .ToList());
     }
 }
コード例 #13
0
 public ICollection <Comment> GetByUserId(string userId)
 {
     using (var context = new ReviewDbContext())
     {
         return(context.Comments
                .Where(c => c.UserId == userId)
                .ToList());
     }
 }
コード例 #14
0
        public Comment Create(Comment newComment)
        {
            using (var context = new ReviewDbContext())
            {
                context.Comments.Add(newComment);
                context.SaveChanges();

                return(newComment);
            }
        }
コード例 #15
0
        public Rating Create(Rating newRating)
        {
            using (var context = new ReviewDbContext())
            {
                context.Ratings.Add(newRating);
                context.SaveChanges();

                return(newRating);
            }
        }
コード例 #16
0
 public SubscriberService(IMessageTracker messageTracker,
                          IMapper mapper,
                          ReviewDbContext context,
                          ILogger <SubscriberService> logger)
 {
     _messageTracker = messageTracker;
     _mapper         = mapper;
     _context        = context;
     _logger         = logger;
 }
コード例 #17
0
        public Product Create(Product newProduct)
        {
            using (var context = new ReviewDbContext())
            {
                context.Products.Add(newProduct);
                context.SaveChanges();

                return(newProduct);
            }
        }
コード例 #18
0
 public ReviewService(ReviewDbContext context,
                      IMapper mapper,
                      ICapPublisher capPublisher,
                      ILogger <ReviewService> logger)
 {
     _context      = context;
     _mapper       = mapper;
     _capPublisher = capPublisher;
     _logger       = logger;
 }
コード例 #19
0
        public Product Update(Product updatedProduct)
        {
            using (var context = new ReviewDbContext())
            {
                var existingProduct = GetById(updatedProduct.Id);

                context.Entry(existingProduct).CurrentValues.SetValues(updatedProduct);
                context.SaveChanges();

                return(existingProduct);
            }
        }
コード例 #20
0
        public Comment Update(Comment updatedComment)
        {
            using (var context = new ReviewDbContext())
            {
                var existingComment = GetById(updatedComment.Id);

                context.Entry(existingComment).CurrentValues.SetValues(updatedComment);
                context.SaveChanges();

                return(existingComment);
            }
        }
コード例 #21
0
        public void Index_WhenCalled_ReturnsOkResult()
        {
            //Arrange
            var builder = new DbContextOptionsBuilder <ReviewDbContext>()
                          .EnableSensitiveDataLogging()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context     = new ReviewDbContext(builder.Options);
            var _controller = new ReviewsController(context);
            // Act
            var okResult = _controller.Index();

            // Assert
            ViewResult okObjectResult = Assert.IsType <ViewResult>(okResult.Result);
        }
コード例 #22
0
        //method to delete a review
        public bool DeleteReview(int reviewId)
        {
            using (var ctx = new ReviewDbContext())
            {
                var entity =
                    ctx
                    .Reviews
                    .Single(e => e.ReviewId == reviewId);

                ctx.Reviews.Remove(entity);

                return(ctx.SaveChanges() == 1);
            }
        }
コード例 #23
0
 public bool DeleteById(int productId)
 {
     using (var context = new ReviewDbContext())
     {
         var productToBeDeleted = GetById(productId);
         context.Remove(productToBeDeleted);
         context.SaveChanges();
     }
     if (GetById(productId) == null)
     {
         return(true);
     }
     return(false);
 }
コード例 #24
0
 public ReviewController(ReviewDbContext context,
                         IMapper mapper,
                         ICapPublisher capBus,
                         IRequestContext requestContext,
                         ILogger <ReviewController> logger,
                         IReviewService reviewService)
 {
     _context        = context;
     _requestContext = requestContext;
     _mapper         = mapper;
     _capBus         = capBus;
     _logger         = logger;
     _reviewService  = reviewService;
 }
コード例 #25
0
        public async Task <IActionResult> SearchPage(string productType, string searchString)
        {
            var context = new ReviewDbContext();

            var products = from p in context.Products
                           select p;

            if (!String.IsNullOrEmpty(searchString))
            {
                products = products.Where(p => p.Name.Contains(searchString));
                return(View(await products.ToListAsync()));
            }

            return(View());
        }
コード例 #26
0
 public IActionResult Edit(Product product)
 {
     if (ModelState.IsValid)
     {
         var context    = new ReviewDbContext();
         var dbProducts = context.Products.Where(p => p.Id == product.Id);
         var dbProduct  = dbProducts.Single(p => p.Id == product.Id);
         dbProduct.Name        = product.Name;
         dbProduct.Type        = product.Type;
         dbProduct.Description = product.Description;
         dbProduct.ImageURL    = product.ImageURL;
         dbProduct.ShopURL     = product.ShopURL;
         context.SaveChanges();
         return(RedirectToAction(product.Type));
     }
     return(View(product));
 }
コード例 #27
0
        //method to create a Review
        public bool CreateBrewery(ReviewCreate model)
        {
            var entity = new Review()
            {
                BeerName    = model.BeerName,
                Brewery     = model.Brewery,
                ABV         = model.ABV,
                Rating      = model.Rating,
                Description = model.Description
            };

            using (var ctx = new ReviewDbContext())
            {
                ctx.Reviews.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }
コード例 #28
0
        //method to update a review
        public bool UpdateReview(ReviewEdit model)
        {
            using (var ctx = new ReviewDbContext())
            {
                var entity =
                    ctx
                    .Reviews
                    .Single(e => e.BeerName == model.BeerName);


                entity.BeerName    = model.BeerName;
                entity.Brewery     = model.Brewery;
                entity.ABV         = model.ABV;
                entity.Rating      = model.Rating;
                entity.Description = model.Description;


                return(ctx.SaveChanges() == 1);
            }
        }
コード例 #29
0
 //method to get a list of reviews
 public IEnumerable <ReviewListItem> GetReviews()
 {
     using (var ctx = new ReviewDbContext())
     {
         var query =
             ctx
             .Reviews
             .Select(
                 e =>
                 new ReviewListItem
         {
             ReviewId    = e.ReviewId,
             BeerName    = e.BeerName,
             Brewery     = e.Brewery,
             ABV         = e.ABV,
             Rating      = e.Rating,
             Description = e.Description
         }
                 );
         return(query.ToArray());
     }
 }
コード例 #30
0
        public async Task Add_ValidObjectPassed_ReturnedResponseHasCreatedItemAsync()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder<ReviewDbContext>()
               .EnableSensitiveDataLogging()
               .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context = new ReviewDbContext(builder.Options);
            var _controller = new ReviewsController(context);
            
            var testReview = new Review()
            {
                ProductId = 1,
                Rating = 7,
                Active = true
            };

            // Act
            await _controller.Create(testReview);

            // Assert
            Assert.IsType<Review>(testReview);
            Assert.Equal(1, testReview.ProductId);
        }