private async Task <PhaseProduct> AddProduct(PhaseProduct phaseProduct, Phase currentPhase, ListType type)
        {
            var product = await _productService.getProduct(phaseProduct.productId);

            if (currentPhase.phaseProducts == null)
            {
                currentPhase.phaseProducts = new List <PhaseProduct>();
            }
            currentPhase.phaseProducts.Add(phaseProduct);
            await _context.SaveChangesAsync();

            return(phaseProduct);
        }
        public async Task <IActionResult> Post(int phaseId, [FromBody] PhaseProduct PhaseProduct)
        {
            if (ModelState.IsValid)
            {
                PhaseProduct.phaseProductId = 0;
                var phase = await _phaseProductService.addProductToPhase(PhaseProduct, phaseId);

                if (phase != null)
                {
                    return(Ok(phase));
                }
                return(NotFound());
            }
            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> Delete(int phaseId, [FromBody] PhaseProduct PhaseProduct)
        {
            Phase phase = null;

            if (ModelState.IsValid)
            {
                phase = await _phaseProductService.removeProductFromPhase(PhaseProduct.phaseProductId, phaseId);

                if (phase != null)
                {
                    return(Ok(phase));
                }
                return(NotFound());
            }
            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> Delete(int recipeId, [FromBody] PhaseProduct phaseProduct)
        {
            if (ModelState.IsValid)
            {
                var product = await _recipeService.removeProductToRecipe(recipeId, phaseProduct);

                if (product != null)
                {
                    return(Ok(product));
                }
                else
                {
                    return(NotFound());
                }
            }
            return(BadRequest(ModelState));
        }
        public async Task <PhaseProduct> addProductToPhase(PhaseProduct phaseProduct, int phaseId)
        {
            var curPhase = await _phaseService.getPhase(phaseId);

            if (curPhase.phaseProducts != null)
            {
                if (curPhase != null && !curPhase.phaseProducts
                    .Select(x => x.productId).ToList().Contains(phaseProduct.productId))
                {
                    return(await AddProduct(phaseProduct, curPhase, ListType.input));
                }
            }
            else
            {
                return(await AddProduct(phaseProduct, curPhase, ListType.input));
            }
            return(null);
        }
Exemplo n.º 6
0
        public async Task <PhaseProduct> addProductToRecipe(int recipeId, PhaseProduct phaseProduct)
        {
            var curRecipe = await _context.Recipes
                            .OrderBy(x => x.recipeId)
                            .Where(x => x.recipeId == recipeId)
                            .FirstOrDefaultAsync();

            if (curRecipe == null)
            {
                return(null);
            }
            var product = await _productService.getProduct(phaseProduct.productId);

            curRecipe.recipeProduct = phaseProduct;
            _context.Recipes.Update(curRecipe);
            await _context.SaveChangesAsync();

            return(phaseProduct);
        }
Exemplo n.º 7
0
        public async Task <Recipe> removeProductToRecipe(int recipeId, PhaseProduct phaseProduct)
        {
            var curRecipe = await _context.Recipes
                            .OrderBy(x => x.recipeId)
                            .Include(x => x.recipeProduct)
                            .Where(x => x.recipeId == recipeId)
                            .FirstOrDefaultAsync();

            if (curRecipe == null)
            {
                return(null);
            }
            if (curRecipe.recipeProduct.productId != phaseProduct.productId)
            {
                return(null);
            }
            curRecipe.recipeProduct = null;
            _context.Recipes.Update(curRecipe);
            await _context.SaveChangesAsync();

            return(await getRecipe(recipeId));
        }