Beispiel #1
0
        public ProductType(ProductReviewRepository reviewRepository, IDataLoaderContextAccessor dataLoaderAccessor)
        {
            // Basic fields, GraphQL can map them
            Field(t => t.Id);
            Field(t => t.Name).Description("The name of the product."); // .Description() adds description for the field
            Field(t => t.Description);
            Field(t => t.IntroducedAt);
            Field(t => t.PhotoFileName);
            Field(t => t.Price);
            Field(t => t.Rating);
            Field(t => t.Stock);

            // Enums, scalar types (int, string)
            Field <ProductTypeEnumType>("Type", "The type of product");  // Name, desc shown in schema explorer

            // Complex types (classes), loads reviews for each product
            Field <ListGraphType <ProductReviewType> >(
                "reviews",
                resolve: context =>
            {
                var user = (ClaimsPrincipal)context.UserContext;        // (ClaimsPrincipal) => just changing type
                // user. ...    // authorization

                // Cashes data so we don't have many unnecessary queries
                // DataLoader needs to be added in Startup config
                // GetOrAddCollectionBatchLoader will crate a new or use an existing data loader by name
                // data loader uses a dictionary to cash data, has INT as a key and ProductReview object as a value
                // Gets data through method GetForProducts from repository
                var loader = dataLoaderAccessor.Context.GetOrAddCollectionBatchLoader <int, ProductReview>(
                    "GetReviewsByProductId", reviewRepository.GetForProducts);

                return(loader.LoadAsync(context.Source.Id));        // Source if the Product entity
            });
        }
Beispiel #2
0
        public IEnumerable <ProductReview> GetAllReviewsForProduct(int productId)
        {
            var repo          = new ProductReviewRepository();
            var productreview = repo.GetAllReviewsForProduct(productId);

            return(productreview);
        }
        public CarvedRockQuery(ProductRepository productRepository, ProductReviewRepository reviewRepository)
        {
            Field <ListGraphType <ProductType> >(
                "products",
                resolve: context => productRepository.GetAll());

            Field <ProductType>(
                "product",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> >
            {
                Name = "id"
            }),
                resolve: context =>
            {
                ////Use this for authentication and authorization
                //var user = (ClaimsPrincipal) context.UserContext;
                var id = context.GetArgument <int>("id");
                return(productRepository.GetOne(id));
            });

            Field <ListGraphType <ProductReviewType> >(
                "reviews",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> >
            {
                Name = "productId"
            }),
                resolve: context =>
            {
                var id = context.GetArgument <int>("productId");
                return(reviewRepository.GetForProduct(id));
            });
        }
 public ProductType(ProductReviewRepository reviewRepository, IDataLoaderContextAccessor dataLoaderAccessor)
 {
     Field(t => t.Id);
     Field(t => t.Name).Description("The name of the product");
     Field(t => t.Description);
     Field(t => t.IntroducedAt).Description("When the product was first introduced in the catalog");
     Field(t => t.PhotoFileName).Description("The file name of the photo so the client can render it");
     Field(t => t.Price);
     Field(t => t.Rating).Description("The (max 5) star customer rating");
     Field(t => t.Stock);
     Field <ProductTypeEnumType>("Type", "The type of product");
     Field <ListGraphType <ProductReviewType> >(
         "reviews",         //definuje nazov ktory je treba zadavat do query pri dopytovani
         //resolve: context => reviewRepository.GetForProduct(context.Source.Id) //definicia sposobu resolvovania
         resolve: context =>
     {
         var user   = (ClaimsPrincipal)context.UserContext;           //ziskanie uzivatela z kontextu
         var loader = dataLoaderAccessor.Context.GetOrAddCollectionBatchLoader <int, ProductReview>("GetReviewsByProductId", reviewRepository.GetForProducts);
         //data loader je ziskany volanim metody GetOrAddCollectionBatchLoader (vytvori alebo vrati data loader podla nazvu)
         //data loader vyuziva cache dictionary pre uchovanie dat
         return(loader.LoadAsync(context.Source.Id));
     }
         );
     //je vykonavanych vela DB queries --> pre ziskanie zoznamu produktov + pre kazdy produkt je vykonane dopytovanie pre ziskanie ProductReview z DB --> mozne vyuzit Data loader
     //pri vyuziti Data Loaderu je ziskany zoznam produktov, nasledne su ziskane ProductPreview pre vsetky produkty, ktore su uchovane v DataLoadery (vykonane 2 dopyty do DB)
 }
        public CarvedRockQuery(ProductRepository productRepository, ProductReviewRepository reviewRepository)
        {
            Field <ListGraphType <ProductType> >(      // I want to return a list => ListGraphType
                "products",
                resolve: context => productRepository.GetAll()
                );

            Field <ProductType>(
                "product",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> >  // NOT optional ID argument
            {
                Name = "id"
            }),
                resolve: context =>
            {
                var id = context.GetArgument <int>("id");       // Get the specific ID from context
                return(productRepository.GetOne(id));
            });

            Field <ListGraphType <ProductReviewType> >(
                "reviews",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "productId"
            }),
                resolve: context =>
            {
                var id = context.GetArgument <int>("productId");
                return(reviewRepository.GetForProduct(id));
            });
        }
 public CarvedRockQuery(
     ProductReviewRepository reviewRepository,
     ProductRepository productRepository)
 {
     Field <ListGraphType <ProductType> >(
         "products",
         resolve: context => productRepository.GetAll()
         );
     Field <ProductType>(
         "product",
         arguments: new QueryArguments(
             new QueryArgument <NonNullGraphType <IdGraphType> > {
         Name = "id"
     }
             ),
         resolve: context =>
     {
         var id = context.GetArgument <int>("id");
         return(productRepository.GetById(id));
     }
         );
     Field <ListGraphType <ProductReviewType> >(
         "reviews",
         arguments: new QueryArguments(
             new QueryArgument <NonNullGraphType <IdGraphType> > {
         Name = "productId"
     }
             ),
         resolve: context => {
         var id = context.GetArgument <int>("productId");
         return(reviewRepository.GetForProduct(id));
     }
         );
 }
        public UrbanCartonQuery(ProductRepository productRepository,
                                ProductReviewRepository productReviewRepository)
        {
            Field <ListGraphType <ProductType> >("products",
                                                 resolve: context => productRepository.GetAllAsync());

            Field <ProductType>("product",
                                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> >
            {
                Name = "id"
            }),
                                resolve: context =>
            {
                var id = context.GetArgument <int>("id");
                return(productRepository.GetOneAsync(id));
            });

            Field <ListGraphType <ProductReviewType> >("reviews",
                                                       arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> >
            {
                Name = "productId"
            }),
                                                       resolve: context =>
            {
                var id = context.GetArgument <int>("productId");
                return(productReviewRepository.GetForProduct(id));
            });

            Field <ListGraphType <ProductReviewType> >("allReviews",
                                                       resolve: context =>
            {
                return(productReviewRepository.GetAll());
            });
        }
Beispiel #8
0
        public ProductType(ProductReviewRepository productReviewRepository,
                           IDataLoaderContextAccessor dataLoaderAccessor)
        {
            Field(t => t.Id);
            Field(t => t.Name).Description("The name of the product");
            Field(t => t.Description);
            Field(t => t.IntroducedAt).Description("When the product was first introduced in the catalog");
            Field(t => t.PhotoFileName).Description("The file name of the photo so the client can render it");
            Field(t => t.Price);
            Field(t => t.Rating).Description("The (max 5) star customer rating");
            Field(t => t.Stock);
            Field <ProductTypeEnumType>("Type", "The type of product");

            Field <ListGraphType <ProductReviewType> >(
                "reviews",
                resolve: context => //productReviewRepository.GetForProduct(context.Source.Id));
            {
                // var user = (ClaimsPrincipal)context.UserContext;
                //user.cl
                var loader =
                    dataLoaderAccessor.Context.GetOrAddCollectionBatchLoader <int, ProductReview>(
                        "GetReviewsByProductId", productReviewRepository.GetForProducts);
                return(loader.LoadAsync(context.Source.Id));
            });
        }
        public CarvedRockMutation(ProductReviewRepository reviewRepository, ReviewMessageService messageService)
        {
            FieldAsync <ProductReviewType>(
                "createReview",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <ProductReviewInputType> >
            {
                Name = "review"
            }),
                resolve: async context =>
            {
                var review = context.GetArgument <ProductReview>("review");
                await reviewRepository.AddReview(review);

                messageService.AddReviewAddedMessage(review);
                return(review);
            });

            FieldAsync <BooleanGraphType>(
                "deleteReview",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> >
            {
                Name = "id"
            }),
                resolve: async context =>
            {
                var id = context.GetArgument <int>("id");
                return(await context.TryAsyncResolve(
                           async c => await reviewRepository.DeleteReview(id)));
            });
        }
Beispiel #10
0
        public ProductReview GetProductReview(int productreviewId)
        {
            var repo          = new ProductReviewRepository();
            var productreview = repo.GetProductReview(productreviewId);

            return(productreview);
        }
Beispiel #11
0
        public CarvedRockQuery(ProductRepository productRepository, ProductReviewRepository reviewRepository)
        {
            Field <ListGraphType <ProductType> >(
                "products",                                    //nazov fieldu , vyuzitelne pri definovany query
                resolve: context => productRepository.GetAll() //definuje ako maju byt data resolvovane --> GetAll() vracia Task ktory nie je potrebne await, riesenie GraphQL (nuget) sa o toto postara
                );

            Field <ProductType>(
                "product",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id"
            }),                                                                                                 //custom argument pre ziskanie konkretneho zaznamu
                resolve: context =>
            {
                context.Errors.Add(new ExecutionError("Error message"));
                var id = context.GetArgument <int>("id");
                return(productRepository.GetOne(id));
            }
                );

            Field <ListGraphType <ProductReviewType> >(
                "reviews",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "productId"
            }
                    ),
                resolve: context =>
            {
                var id = context.GetArgument <int>("productId");
                return(reviewRepository.GetForProduct(id));
            }
                );
        }
        public ProductType(ProductReviewRepository reviewRepository, IDataLoaderContextAccessor dataLoaderContextAccessor)
        {
            Name        = "Product";
            Description = "A representation a product";

            Field(t => t.Id).Description("Unique Id of the product");
            Field(t => t.Name).Description("Name of the product");
            Field(t => t.Description).Description("Description of the product");
            Field(t => t.IntroducedAt).Description("When the product was first introduced");
            Field(t => t.PhotoFileName).Description("File name of the product photo");
            Field(t => t.Price).Description("Price of the product");
            Field(t => t.Rating).Description("Customer average rating of the product");
            Field(t => t.Stock).Description("Number of the products left in stock");
            Field <ProductTypeEnumType>("Type", "Type of the product");

            ////Will result in N+1 query issue, avoid this approach
            //Field<ListGraphType<ProductReviewType>>(
            //    "reviews",
            //    resolve: context => reviewRepository.GetForProduct(context.Source.Id));

            Field <ListGraphType <ProductReviewType> >(
                "reviews",
                resolve: context =>
            {
                var loader = dataLoaderContextAccessor.Context.GetOrAddCollectionBatchLoader <int, ProductReview>(
                    "GetReviewsByProductId", reviewRepository.GetForProducts);
                return(loader.LoadAsync(context.Source.Id));
            }
                );
        }
        public ProductType(ProductReviewRepository reviewRepository, IDataLoaderContextAccessor dataLoaderContextAccessor)
        {
            Field(t => t.Id);
            Field(t => t.Name);
            Field(t => t.Description);
            Field(t => t.IntroducedAt).Description("When the product was first intorudced in the catalog");
            Field(t => t.PhotoFileName).Description("The file name of the photo so the client can render it");
            Field(t => t.Price);
            Field(t => t.Rating).Description("The (max 5) star customer rating");
            Field(t => t.Stock);
            Field <ProductTypeEnumType>("Type", "The type of product");


            //inefficient since it will do multiple calls to the database
            Field <ListGraphType <ProductReviewType> >(
                "reviewsDeprecated",
                resolve: context => reviewRepository.GetProduct(context.Source.Id)
                );

            //Better solution using dataLoader
            //This will get all the productIds that we want to reviews for
            //Gets all the review in cache that links to the productIds from the query
            //Field<ListGraphType<ProductReviewType>>(
            //    "reviews",
            //    resolve: context =>
            //    {
            //        var loader = dataLoaderContextAccessor.Context.GetOrAddCollectionBatchLoader<int, ProductReview>(
            //            "GetR")
            //    });
        }
Beispiel #14
0
        public void Delete(UpdateProductReviewCommand updatedProductReviewCommand, int id)
        {
            var repo = new ProductReviewRepository();
            var deletedProductReview = new ProductReview
            {
                Comment = updatedProductReviewCommand.Comment,
                Rating  = updatedProductReviewCommand.Rating,
            };

            repo.DeleteProductReview(deletedProductReview, id);
        }
 public ApplicationMutation(ProductReviewRepository repo)
 {
     FieldAsync <ProductReviewType>(
         "createReview",
         arguments: new QueryArguments(new QueryArgument <NonNullGraphType <ProductReviewInputType> > {
         Name = "review"
     }),
         resolve: async context =>
     {
         var review = context.GetArgument <ProductReview>("review");
         return(await context.TryAsyncResolve(async c => await repo.AddReview(review)));
     });
 }
Beispiel #16
0
 public ProductType(ProductReviewRepository repo)
 {
     Field(t => t.Id);
     Field(t => t.Name);
     Field(t => t.Description);
     Field(t => t.Price);
     Field(t => t.Rating);
     Field(t => t.Stock);
     Field <ProductTypeEnumType>("Type");
     Field <ListGraphType <ProductReviewType> >(
         "reviews",
         resolve: context => repo.GetForProduct(context.Source.Id)
         );
 }
 public CarvedRockMutation(ProductReviewRepository reviewRepository, ReviewMessageService messageService)
 {
     FieldAsync <ProductReviewType>("createReview",
                                    arguments: new QueryArguments(new QueryArgument <NonNullGraphType <ProductReviewInputType> >
     {
         Name = "review"
     }), resolve: async context =>
     {
         var review = context.GetArgument <ProductReview>("review");
         await reviewRepository.AddReview(review);
         messageService.AddReviewAddedMessage(review);
         return(review);
     });
 }
Beispiel #18
0
        public void TestTablePerType()
        {
            var context          = new ContentDbContext();
            var blogRepository   = new BlogPostRepository(context);
            var reviewRepository = new ProductReviewRepository(context);

            context.Database.OpenConnection();
            context.Database.Migrate();

            List <BlogPost>      blogPosts      = new List <BlogPost>();
            List <ProductReview> productReviews = new List <ProductReview>();

            for (int i = 0; i < 100; i++)
            {
                var post = new BlogPost()
                {
                    Id        = Guid.NewGuid(),
                    Timestamp = DateTime.Now,
                    Author    = "Author" + i,
                    Title     = "Title" + i,
                    Content   = "Content" + i
                };
                blogPosts.Add(post);
                blogRepository.Add(post);

                var review = new ProductReview()
                {
                    Id        = Guid.NewGuid(),
                    Timestamp = DateTime.Now,
                    Author    = "Author" + i,
                    Rating    = i,
                    Review    = "Review" + i
                };
                productReviews.Add(review);
                reviewRepository.Add(review);
            }

            context.SaveChanges();

            var reviewResult = reviewRepository.Reviews.Where(r => r.Rating >= 90).OrderBy(r => r.Rating).ToList();

            Assert.Equal(10, reviewResult.Count);
            for (int i = 0; i < reviewResult.Count; i++)
            {
                Assert.Equal(productReviews[90 + i], reviewResult[i]);
            }
        }
Beispiel #19
0
 public ProductType(ProductReviewRepository reviewRepository, IDataLoaderContextAccessor dataLoaderAccessor)
 {
     Field(t => t.Id);
     Field(t => t.Name).Description("The name of the product");
     Field(t => t.Description);
     Field(t => t.IntroducedAt).Description("When the product was first introduced in the shop.");
     Field(t => t.PhotoFileName).Description("The file name of the photo.");
     Field(t => t.Price);
     Field(t => t.Rating).Description("The (max 5) star customer rating.");
     Field(t => t.Stock);
     Field <ProductTypeEnumType>("Type", "The type of the product.");
     Field <ListGraphType <ProductReviewType> >(
         "reviews",
         resolve: context =>
     {
         var loader = dataLoaderAccessor.Context.GetOrAddCollectionBatchLoader <int, ProductReview>(
             "GetReviewsByProductId", reviewRepository.GetForProducts);
         return(loader.LoadAsync(context.Source.Id));    // return all reviews for current productId
     }
         );
 }
Beispiel #20
0
        public CarvedRockMutation(ProductReviewRepository reviewRepository, ReviewMessageService messageService)
        {
            FieldAsync <ProductReviewType>( //ProductReviewType --> definuje ze tento objekt bude vrateny po tom ako bude mutation vykonana
                "createReview",             //poziadavka na vytvorenie review
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <ProductReviewInputType> > {
                Name = "review"
            }                                                                                       //pozadovany argument
                    ),
                resolve: async context =>
            {
                var review = context.GetArgument <ProductReview>("review");    //ziskanie argumentu a resolvovanie do entity objektu
                //return await context.TryAsyncResolve(async c => await reviewRepository.AddReview(review));
                //vystup z AddReview je pouzitim "TryAsyncResolve" monitorovany (???) , ak je vytvorenie uspesne, review je navratene normalne
                //ak sa vsak vyskytne exception pri volani repository, exception je catchnuta a pridana do ErrorListu ktory je vrateny klientovi aj ked je API nastavena pre not-expose exception

                await reviewRepository.AddReview(review);
                messageService.AddReviewAddedMessage(review);     //po pridani review su subscriberi notifikovani
                return(review);
            }
                );
        }
Beispiel #21
0
        // Adding, updating, deleting data
        public CarvedRockMutation(ProductReviewRepository reviewRepository, ProductRepository productRepository)
        {
            // ProductReviewType is going to be returned when mutation is done
            FieldAsync <ProductReviewType>(
                "createReview",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <ProductReviewInputType> > {
                Name = "review"
            }),
                resolve: async context =>
            {
                // Conversion from GraphType to Entity
                var review = context.GetArgument <ProductReview>("review");

                // The outcome of AddReview is monitored, exception will be catched and added to the error list even when the API is configured to not expose exceptions
                return(await context.TryAsyncResolve(
                           async c => await reviewRepository.AddReview(review)));
            });

            Field <StringGraphType>(
                "deleteProduct",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "productId"
            }),
                resolve: context =>
            {
                var productId = context.GetArgument <int>("productId");
                var product   = productRepository.GetOne(productId).Result;
                if (product == null)
                {
                    context.Errors.Add(new ExecutionError("Couldn't find product in db."));
                    return(null);
                }

                productRepository.Delete(product);
                return($"The owner with the id: {productId} has been successfully deleted from db.");
            }
                );
        }
 public CatalogService(RequestContext c,
                       CategoryRepository categories,
                       CategoryProductAssociationRepository crosses,
                       ProductRepository products,
                       ProductRelationshipRepository relationships,
                       ProductImageRepository productImages,
                       ProductReviewRepository productReviews,
                       VariantRepository productVariants,
                       OptionRepository productOptions,
                       ProductOptionAssociationRepository productsXOptions,
                       ProductFileRepository productFiles,
                       ProductVolumeDiscountRepository volumeDiscounts,
                       ProductPropertyValueRepository propertyValues,
                       ProductInventoryRepository inventory,
                       ProductTypeRepository types,
                       ProductTypePropertyAssociationRepository typesXProperties,
                       ProductPropertyRepository properties,
                       WishListItemRepository wishItems)
 {
     context = c;
     Categories = categories;
     CategoriesXProducts = crosses;
     ProductRelationships = relationships;
     this.Products = products;
     this.ProductImages = productImages;
     this.ProductReviews = productReviews;
     this.ProductVariants = productVariants;
     this.ProductOptions = productOptions;
     this.ProductsXOptions = productsXOptions;
     this.ProductFiles = productFiles;
     this.VolumeDiscounts = volumeDiscounts;
     this.ProductPropertyValues = propertyValues;
     this.ProductInventories = inventory;
     this.ProductTypes = types;
     this.ProductTypesXProperties = typesXProperties;
     this.ProductProperties = properties;
     this.WishListItems = wishItems;
 }
Beispiel #23
0
        public CarvedRockQuery(ProductRepository productRepository, ProductReviewRepository reviewRepository)
        {
            Field <ListGraphType <ProductType> >(
                "products",
                resolve: context => productRepository.GetAll()
                );

            Field <ProductType>(
                "product",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> >
            {
                Name = "id"
            }),
                resolve: (context) =>
            {
                context.Errors.Add(new ExecutionError("Some errror message"));
                var id = context.GetArgument <int>("id");
                return(productRepository.GetOne(id));
            }
                );


            Field <ListGraphType <ProductReviewType> >(
                "reviews",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> >
            {
                Name = "productId"
            }),
                resolve: (context) =>
            {
                context.Errors.Add(new ExecutionError("Some errror message"));
                var id = context.GetArgument <int>("productId");
                return(reviewRepository.GetForProduct(id));
            }
                );
        }
Beispiel #24
0
        public void Create(AddProductReviewCommand newProductReview)
        {
            var repo = new ProductReviewRepository();

            repo.AddProductReview(newProductReview);
        }
Beispiel #25
0
        public IEnumerable <ProductReview> GetProductsReview()
        {
            var repo = new ProductReviewRepository();

            return(repo.GetAll());
        }