Exemplo n.º 1
0
        public async Task <ActionResult <Commodity> > CreateCommodity(Commodity commodity)
        {
            try
            {
                // - Validate : CommodityGroup(parent) must be in order.
                if (commodity.CommodityGroupId < 1)
                {
                    throw new Exception("The CommodityGroup(parent) of the Commodity to be created must be specified...");
                }

                bool commodityGroupExists = await mc_CommodityRepository.CommodityGroupExists(commodity.CommodityGroupId);

                if (!commodityGroupExists)
                {
                    throw new Exception("The CommodityGroup(parent) of the Commodity to be created must exist on the system...");
                }

                // - Put it away!
                var commodityCreated = await mc_CommodityRepository.CreateCommodity(commodity);

                return(Ok(commodityCreated));
            }
            catch (Exception exception)
            {
                string message = "Error occurred in CommodityController.CreateCommodity" + Environment.NewLine;
                message += exception.Message;
                return(StatusCode(StatusCodes.Status500InternalServerError, message));
            }
        }