Beispiel #1
0
        public Product Update(string name, int cost, string description, string isAvailable, int category, List <int> specificationOptions, ApplicationContext context)
        {
            Name        = name;
            Cost        = cost;
            Description = description;
            IsAvailable = isAvailable == "IsAvailable";

            if (category > 0)
            {
                Category = context.Category.First(c => c.Id == category);
            }


            SpecificationOptions = context
                                   .SpecificationOption
                                   .Include(sOp => sOp.Products)
                                   .Where(sOp => sOp
                                          .Products
                                          .Select(p => p.Id)
                                          .Contains(Id))
                                   .ToList();

            foreach (var specificationOption in SpecificationOptions)
            {
                specificationOption.Products.Remove(this);
            }
            SpecificationOptions.RemoveAll(sOp => true);
            context.SaveChanges();

            SpecificationOptions.AddRange(context
                                          .SpecificationOption
                                          .Where(sOp => specificationOptions.Contains(sOp.Id)));

            return(this);
        }
Beispiel #2
0
        public async Task <ActionResult <PagedCollection <RecipeResource> > > ListAllRecipes(
            [FromQuery] SpecificationOptions <RecipeResource> options)
        {
            options ??= new SpecificationOptions <RecipeResource>();
            options.Paging ??= defaultPagingOptions;
            options.Paging.Offset ??= defaultPagingOptions.Offset;
            options.Paging.Limit ??= defaultPagingOptions.Limit;

            var spec    = new Specification <RecipeResource>(options);
            var recipes = await recipeService.ListAsync(spec).ConfigureAwait(false);

            return(PagedCollectionHelper.Create(
                       Link.ToCollection(nameof(ListAllRecipes)),
                       recipes.Items.ToArray(),
                       recipes.TotalSize,
                       options.Paging));
        }
        public async Task <ActionResult <PagedCollection <IngredientResource> > > ListAllUserIngredients(
            [FromQuery] SpecificationOptions <IngredientResource> options)
        {
            options ??= new SpecificationOptions <IngredientResource>();
            options.Paging ??= defaultPagingOptions;
            options.Paging.Offset ??= defaultPagingOptions.Offset;
            options.Paging.Limit ??= defaultPagingOptions.Limit;

            var user = await userManager.GetUserAsync(User).ConfigureAwait(false);

            if (user == null)
            {
                return(Forbid());
            }

            var spec  = new Specification <IngredientResource>(options);
            var items = await ingredientService.ListAsync(spec, user.Id).ConfigureAwait(false);

            return(PagedCollectionHelper.Create(
                       Link.ToCollection(nameof(ListAllUserIngredients)),
                       items.Items.ToArray(),
                       items.TotalSize,
                       options.Paging));
        }