Exemplo n.º 1
0
        public static CalculatorItem ToCalculatorItemModel(this ContentsCalculatorItem source)
        {
            if (source == null)
            {
                return(null);
            }

            var criteria = new CalculatorItem
            {
                Id    = source.Id,
                Name  = source.Name,
                Value = source.Value,
                ContentsCategoryType = source.ContentsCategoryType?.Name
            };

            return(criteria);
        }
        public int?Create(string name, double value, string category)
        {
            using (var ctx = new CalculatorDbContext())
            {
                var categoryType = ctx.ContentsCategoryTypes.FirstOrDefault(x => x.Name == category);
                if (categoryType != null)
                {
                    var contentsCalculatorItem = new ContentsCalculatorItem
                    {
                        Name = name, ContentsCategoryTypeId = categoryType.Id, Value = value
                    };

                    ctx.ContentsCalculatorItems.Add(contentsCalculatorItem);
                    ctx.SaveChanges();

                    return(contentsCalculatorItem.Id);
                }

                return(null);
            }
        }