Beispiel #1
0
        public async Task <ActionResult> CreateSpecificProduct(int id, [FromForm] CreateSpecificProductDTO createSpecificProductDTO)
        {
            if (createSpecificProductDTO.sizeIds == null)
            {
                return(BadRequest(new { SizeIds = "Invalid sizeIds" }));
            }
            for (var i = 0; i < createSpecificProductDTO.sizeIds.Count(); i += 1)
            {
                var sizeId = createSpecificProductDTO.sizeIds[i];

                string specificProductId = $"P{id}C{createSpecificProductDTO.ColorId}S{sizeId}".ToString();

                // Check exist product
                var existingProduct = await _productService.FindByIdAsync(id);

                if (existingProduct == null)
                {
                    return(BadRequest(new { ProductId = $"Product {id} not found".ToString() }));
                }

                // Check exist specific product
                var existingSpecificProduct = await _productService.FindSpecificProductByIdAsync(specificProductId);

                if (existingSpecificProduct != null)
                {
                    return(BadRequest(new
                    {
                        ColorId = "Specific product with this color and this size allready exist(choose another color or size)",
                        SizeId = "Specific product with this color and this size allready exist(choose another color or size)"
                    }));
                }

                // Check is valid color
                var validColor = await _colorService.FindByIdAsync(createSpecificProductDTO.ColorId);

                if (validColor == null)
                {
                    return(BadRequest(new
                    {
                        ColorId = "Color not found"
                    }));
                }

                // Check is valid size
                var validSize = await _sizeService.FindByIdAsync(sizeId);

                if (validSize == null)
                {
                    return(BadRequest(new
                    {
                        SizeId = $"Size ID {sizeId} not found".ToString()
                    }));
                }
            }

            await _productService.CreateSpecificProduct(id, createSpecificProductDTO);

            return(NoContent());
        }