コード例 #1
0
ファイル: ItemService.cs プロジェクト: freshy969/price-1
        public Item EnsureItemWithUnit(string itemDescription)
        {
            itemDescription = itemDescription ?? "";

            var parts = itemDescription.Split(',');

            string name = parts[0].Trim();
            string unit = parts.Count() > 1 ? parts[1] : null;

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new Exception($"Incorrect item description provided: {itemDescription}");
            }

            var entity = _rep.GetByText(name);

            if (entity == null)
            {
                entity = new ItemEntity
                {
                    Text = name,
                    Unit = CodeUtils.CreateCode(unit),
                    Code = CodeUtils.CreateCode(name)
                };
                _rep.Add(entity);
            }

            return(ItemMapper.MapToModel(entity));
        }