Esempio n. 1
0
        private string[] ExecAddProductToShop(string args)
        {
            Regex regex = new Regex(
                @".*add-to-shop\s+?shop-id='(\d+?)';\s*product-name='(.+?)'\s*;\s*price='((\d+?)(\.(\d+?))?)';\s*quantity='(\d+?)'\s*");

            if (!regex.IsMatch(args))
            {
                return(new [] { "Err. Incorrect data format" });
            }

            var    matcher     = regex.Match(args);
            int    shopId      = int.Parse(matcher.Groups[1].Value);
            string productName = matcher.Groups[2].Value;

            if (!double.TryParse(matcher.Groups[3].Value, out var productPrice))
            {
                return(new [] { "Err. Incorrect data format" });
            }
            int productQuantity = int.Parse(matcher.Groups[7].Value);

            try
            {
                _dao.AddProductToShop(shopId, productName, productPrice, productQuantity);
            }
            catch (ShopNotExistsException)
            {
                return(new[] { $"Err. Product adding error: shop on ShopId={shopId} not exists" });
            }
            catch (ProductNotExistsException)
            {
                return(new[] { $"Err. Product adding error: product with ProductName={productName} not exists" });
            }
            catch (MissingDataConsistencyException)
            {
                return(new[] { $"Err. Product already exists in shop. To update data use upd-product-* commands" });
            }

            return(new[]
            {
                $"Creation successful, product added to shop #{shopId} - {_dao.GetShopName(shopId)}",
                $"({productName}, {productPrice}, {productQuantity})"
            });
        }