示例#1
0
        public async Task <int> Handle(CreateIngredientCommand request, CancellationToken cancellationToken)
        {
            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                await Task.Delay(1000, cancellationToken);
            }
            catch (Exception ex) when(ex is TaskCanceledException)
            {
                throw new TaskCanceledException("The user has cancelled the task!");
            }


            var ingredient = new IngredientsFromShop
            {
                Id    = request.Id,
                Name  = request.Name,
                Price = request.Price
            };
            await repository.AddAsync(ingredient);



            return(ingredient.Id);
        }
示例#2
0
        public async Task <ActionResult> Add(IngredientsFromShop ingredient)
        {
            bool alreadyExists = _context.IngredientsFromShops.Any(i => i.Id == ingredient.Id);

            if (alreadyExists)
            {
                return(UnprocessableEntity());
            }
            await _context.IngredientsFromShops.AddAsync(ingredient);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("Get", new { Id = ingredient.Id }, ingredient));
        }
        public async Task <GetShopDto> Handle(GetShopByIdQuery request, CancellationToken cancellationToken)
        {
            var shop = await repository.GetById(request.Id);

            if (shop == null)
            {
                throw new ArgumentNullException(nameof(request));
            }


            List <int> ingredientsId = await context.JoinIngredientShop
                                       .Where(p => p.ShopsId == request.Id)
                                       .Select(p => p.IngredientsId)
                                       .ToListAsync(cancellationToken);

            GetShopDto shopDto = new GetShopDto()
            {
                Id        = shop.Id,
                Name      = shop.Name,
                LocationX = shop.LocationX,
                LocationY = shop.LocationY,
            };

            foreach (int i in ingredientsId)
            {
                IngredientsFromShop ingredient = await context
                                                 .Ingredients
                                                 .FindAsync(i, cancellationToken);

                GetIngredientDto getIngredientDto = new GetIngredientDto();
                getIngredientDto.Id    = ingredient.Id;
                getIngredientDto.Name  = ingredient.Name;
                getIngredientDto.Price = ingredient.Price;
                //voi lua si cantitatea specifica ingredientului din shopul respectiv
                int quantity = context.JoinIngredientShop
                               .Where(p => (p.IngredientsId == ingredient.Id) && (p.ShopsId == shopDto.Id))
                               .Select(p => p.Quantity)
                               .FirstOrDefault();
                getIngredientDto.Quantity = quantity;
                shopDto.Ingredients.Add(getIngredientDto);
            }
            return(shopDto);
        }
示例#4
0
        public async Task <ActionResult <IEnumerable <GetShopDto> > > Get()
        {
            List <Shop> shops = await _context.Shops.ToListAsync();

            List <GetShopDto> shopDtos = new List <GetShopDto>();

            foreach (Shop shop in shops)
            {
                List <int> ingredientsId = await _context.IngredientFromShopShops
                                           .Where(s => s.ShopsId == shop.Id)
                                           .Select(s => s.IngredientsId)
                                           .ToListAsync();

                GetShopDto shopDto = new GetShopDto()
                {
                    Id        = shop.Id,
                    Name      = shop.Name,
                    LocationX = shop.LocationX,
                    LocationY = shop.LocationY,
                };
                foreach (int i in ingredientsId)
                {
                    IngredientsFromShop ingredient = await _context
                                                     .IngredientsFromShops
                                                     .FindAsync(i);

                    int quantity = await _context
                                   .IngredientFromShopShops
                                   .Where(s => s.IngredientsId == i && s.ShopsId == shop.Id)
                                   .Select(s => s.Quantity).FirstOrDefaultAsync();

                    shopDto.Ingredients
                    .Add(new GetShopIngredientDto()
                    {
                        Id = ingredient.Id, Name = ingredient.Name, Price = ingredient.Price, Quantity = quantity
                    });
                }
                shopDtos.Add(shopDto);
            }
            return(Ok(shopDtos));
        }
示例#5
0
        public async Task <IEnumerable <GetShopDto> > Handle(GetShopsQuery request, CancellationToken cancellationToken)
        {
            var shops = repository.GetAll();
            List <GetShopDto> listShops = new List <GetShopDto>();

            foreach (var shop in shops)
            {
                List <int> ingredientsId = context.JoinIngredientShop
                                           .Where(p => p.ShopsId == shop.Id)
                                           .Select(p => p.IngredientsId)
                                           .ToList();
                GetShopDto shopDto = new GetShopDto()
                {
                    Id        = shop.Id,
                    Name      = shop.Name,
                    LocationX = shop.LocationX,
                    LocationY = shop.LocationY,
                };
                foreach (int i in ingredientsId)
                {
                    IngredientsFromShop ingredient = await context.Ingredients.FindAsync(i);

                    GetIngredientDto getIngredientDto = new GetIngredientDto();
                    getIngredientDto.Id    = ingredient.Id;
                    getIngredientDto.Name  = ingredient.Name;
                    getIngredientDto.Price = ingredient.Price;
                    //voi lua si cantitatea specifica ingredientului din shopul respectiv
                    int quantity = context.JoinIngredientShop
                                   .Where(p => (p.IngredientsId == ingredient.Id) && (p.ShopsId == shopDto.Id))
                                   .Select(p => p.Quantity)
                                   .FirstOrDefault();
                    getIngredientDto.Quantity = quantity;
                    shopDto.Ingredients.Add(getIngredientDto);
                }
                listShops.Add(shopDto);
            }
            return(listShops);
        }