コード例 #1
0
ファイル: MenuService.cs プロジェクト: Piotrassin/SzefOS
 public static List <Menu> ToList()
 {
     using (var context = new MASContext())
     {
         return(context.Menus.ToList());
     }
 }
コード例 #2
0
ファイル: MenuService.cs プロジェクト: Piotrassin/SzefOS
 public static Menu GetMenu(int menuId)
 {
     using (var context = new MASContext())
     {
         return(context.Menus.Find(menuId));
     }
 }
コード例 #3
0
        private static void DeleteExample()
        {
            using var context = new MASContext();

            context.City.Remove(context.City.FirstOrDefault() ?? throw new InvalidOperationException());
            context.SaveChanges();
        }
コード例 #4
0
ファイル: MenuService.cs プロジェクト: Piotrassin/SzefOS
 public static void New()
 {
     using (var context = new MASContext())
     {
         context.Menus.Add(new Menu(0.0d));
         context.SaveChanges();
     }
 }
コード例 #5
0
        private static void UpdateExample()
        {
            using var context = new MASContext();
            var city = context.City.FirstOrDefaultAsync();

            city.Result.Name = "Warszawa";
            context.City.Update(city.Result);
            context.SaveChanges();
        }
コード例 #6
0
        public static void Main(string[] args)
        {
            using var context = new MASContext();

            //AddExample();
            //DeleteExample();
            //UpdateExample();

            context.City.ToList().ForEach(Console.WriteLine);
        }
コード例 #7
0
ファイル: DishService.cs プロジェクト: Piotrassin/SzefOS
 public static List <DishContent> GetContents(int dishId)
 {
     using (var context = new MASContext())
     {
         return(context.DishContents
                .Include(c => c.Ingredient)
                .Where(a => a.DishId == dishId)
                .ToList());
     }
 }
コード例 #8
0
ファイル: DishService.cs プロジェクト: Piotrassin/SzefOS
 public static Dish Get(Dish dish)
 {
     if (dish == null)
     {
         throw new Exception("Passed argument is null");
     }
     using (var context = new MASContext())
     {
         return(context.Dishes.FirstOrDefault(a => a.Id == dish.Id));
     }
 }
コード例 #9
0
ファイル: MenuService.cs プロジェクト: Piotrassin/SzefOS
 public static IEnumerable <Dish> GetDishes(int menuId)
 {
     using (var context = new MASContext())
     {
         return(context.Dishes
                .Include(c => c.Menu)
                .Include(a => a.DishIngredients)
                .ThenInclude(b => b.Ingredient)
                .Where(d => d.Menu.Id == menuId)
                .ToList());
     }
 }
コード例 #10
0
ファイル: MenuService.cs プロジェクト: Piotrassin/SzefOS
        public static void Delete(Menu menu)
        {
            if (menu == null)
            {
                throw new Exception("Passed argument is null");
            }

            using (var context = new MASContext())
            {
                context.Menus.Remove(menu);
                context.SaveChanges();
            }
        }
コード例 #11
0
ファイル: DishService.cs プロジェクト: Piotrassin/SzefOS
        public static void AddIngredient(int dishId, double quantity, Ingredient ingredient)
        {
            using (var context = new MASContext())
            {
                var dish = context.Dishes.Include(a => a.DishIngredients).Where(x => x.Id == dishId).FirstOrDefault();

                if (dish != null && !dish.DishIngredients.Any(a => a.IngredientId == ingredient.Id))
                {
                    context.DishContents.Add(new DishContent(dish, ingredient, quantity));
                    context.SaveChanges();
                }
            }
        }
コード例 #12
0
        private static void AddExample()
        {
            using var context = new MASContext();
            User person = new PremiumUser()
            {
                PasswordHash = "secret",
                Name         = "Michal",
                CreatedAt    = DateTime.Today,
                Email        = "*****@*****.**"
            };

            var city = new City
            {
                Name = "Piaseczno"
            };

            city.User.Add(person);

            context.City.Add(city);
            context.SaveChanges();
        }
コード例 #13
0
ファイル: MenuService.cs プロジェクト: Piotrassin/SzefOS
        public static void ChangePrices(Menu menu, double newMargin)
        {
            using (var context = new MASContext())
            {
                menu.ProfitMargin = newMargin;
                context.Menus.Update(menu);
                context.SaveChanges();

                using (var transaction = context.Database.BeginTransaction())
                {
                    var dishes = GetDishes(menu.Id);
                    if (dishes != null)
                    {
                        foreach (var dish in dishes)
                        {
                            DishService.ChangePrice(dish, newMargin);
                        }
                    }
                    transaction.Commit();
                }
                context.SaveChanges();
            }
        }
コード例 #14
0
ファイル: DishService.cs プロジェクト: Piotrassin/SzefOS
        public static void ChangePrice(Dish dish, double newMargin)
        {
            using (var context = new MASContext())
            {
                double sum      = 0.0;
                var    contents = context.DishContents.Include(a => a.Ingredient).Where(a => a.DishId == dish.Id).ToList();

                foreach (var content in contents)
                {
                    var quantity   = (double)content.Quantity;
                    var pricePerKg = (double)content.Ingredient.PricePerKg;
                    sum += quantity * pricePerKg;
                }

                sum += sum * newMargin;

                dish.Price = (Decimal)sum;
                Dish EditedDish = context.Dishes.Where(a => a.Id == dish.Id).FirstOrDefault();
                EditedDish.Price = (Decimal)Math.Round(sum, 2);
                context.Update(EditedDish);
                context.SaveChanges();
            }
        }