Esempio n. 1
0
        public async Task <MenuItems> AddMenuItemAsync(long ownerId, long restaurantId, long menuCategoryId, Dictionary <long, string> itemName, Dictionary <long, string> itemDescription, Dictionary <long, string> itemWarnings, Dictionary <long, float> itemPrice)
        {
            CheckTheLoggedInPerson();

            EmployersRestaurants connection = await CheckEmployerRestaurantAsync(ownerId, restaurantId);

            Menus currentMenu = await CheckMenuExistanceAsync(restaurantId);

            MenuCategories category = await CheckMenuCategoryExistance(menuCategoryId);

            MenuItems menuItem = new MenuItems
            {
                MenuId         = currentMenu.Id,
                MenuCategoryId = category.Id,
            };
            await MenuItemRepo.AddAsync(menuItem, this.ModifierId);

            List <MenuLanguages> menuLanguages = await MenuLanguagesRepo.GetItemsByMenuId(currentMenu.Id);

            foreach (var menuLang in menuLanguages)
            {
                bool checkName        = itemName.TryGetValue(menuLang.Id, out string name);
                bool checkDescription = itemDescription.TryGetValue(menuLang.Id, out string description);
                itemWarnings.TryGetValue(menuLang.Id, out string warning);

                if (!checkName)
                {
                    name = "<< no name >>";
                }
                if (!checkDescription)
                {
                    description = "<< no description >>";
                }

                MenuItemContents contents = new MenuItemContents
                {
                    ItemName        = name,
                    ItemDescription = description,
                    ItemWarnings    = warning,
                    MenuItemId      = menuItem.Id,
                    MenuLanguageId  = menuLang.Id
                };

                await MenuItemContentRepo.AddAsync(contents, this.ModifierId);
            }

            List <MenuCurrencies> menuCurrencies = await MenuCurrencyRepo.GetItemsByMenuId(currentMenu.Id);

            foreach (var currency in menuCurrencies)
            {
                bool checkValue = itemPrice.TryGetValue(currency.Id, out float price);

                if (!checkValue)
                {
                    price = 0F;
                }

                MenuItemValues value = new MenuItemValues
                {
                    Price          = price,
                    MenuCurrencyId = currency.Id,
                    MenuItemId     = menuItem.Id
                };

                await MenuItemValueRepo.AddAsync(value, this.ModifierId);
            }

            return(menuItem);
        }
Esempio n. 2
0
        public async Task <MenuItems> UpdateMenuItemAsync(long ownerId, long restaurantId, long menuItemId, Dictionary <long, string> itemName, Dictionary <long, string> itemDescription, Dictionary <long, string> itemWarnings, Dictionary <long, float> itemPrice)
        {
            CheckTheLoggedInPerson();

            EmployersRestaurants connection = await CheckEmployerRestaurantAsync(ownerId, restaurantId);

            Menus currentMenu = await CheckMenuExistanceAsync(restaurantId);

            MenuItems menuItem = await CheckMenuItemExistance(menuItemId);

            List <MenuLanguages> menuLanguages = await MenuLanguagesRepo.GetItemsByMenuId(currentMenu.Id);

            List <MenuItemContents> itemContents = await MenuItemContentRepo.GetByMenuItemId(menuItem.Id);

            foreach (var menuLang in menuLanguages)
            {
                bool checkName        = itemName.TryGetValue(menuLang.Id, out string name);
                bool checkDescription = itemDescription.TryGetValue(menuLang.Id, out string description);
                itemWarnings.TryGetValue(menuLang.Id, out string warning);

                if (!checkName)
                {
                    name = "<< no name >>";
                }
                if (!checkDescription)
                {
                    description = "<< no description >>";
                }

                MenuItemContents contents = itemContents.Where(x => x.MenuLanguageId == menuLang.Id).SingleOrDefault();
                if (contents == null)
                {
                    contents = new MenuItemContents
                    {
                        ItemName        = name,
                        ItemDescription = description,
                        ItemWarnings    = warning,
                        MenuItemId      = menuItem.Id,
                        MenuLanguageId  = menuLang.Id
                    };
                    itemContents.Add(contents);
                    await MenuItemContentRepo.AddAsync(contents, this.ModifierId);
                }
                else
                {
                    contents.ItemName        = name;
                    contents.ItemDescription = description;
                    contents.ItemWarnings    = warning;
                    await MenuItemContentRepo.UpdateAsync(contents, this.ModifierId);
                }
            }

            List <MenuCurrencies> menuCurrencies = await MenuCurrencyRepo.GetItemsByMenuId(currentMenu.Id);

            List <MenuItemValues> itemValues = await MenuItemValueRepo.GetByMenuItemId(menuItem.Id);

            foreach (var currency in menuCurrencies)
            {
                bool checkValue = itemPrice.TryGetValue(currency.Id, out float price);

                if (!checkValue)
                {
                    price = 0F;
                }

                MenuItemValues value = itemValues.Where(x => x.MenuCurrencyId == currency.Id).SingleOrDefault();
                if (value == null)
                {
                    value = new MenuItemValues
                    {
                        Price          = price,
                        MenuCurrencyId = currency.Id,
                        MenuItemId     = menuItem.Id
                    };
                    itemValues.Add(value);
                    await MenuItemValueRepo.AddAsync(value, this.ModifierId);
                }
                else
                {
                    value.Price = price;
                    await MenuItemValueRepo.UpdateAsync(value, this.ModifierId);
                }
            }

            return(menuItem);
        }
        static async Task Main(string[] args)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddOperations(typeof(Restaurant).Assembly);
            serviceCollection.AddOperations(typeof(User).Assembly);
            var serviceProvider = serviceCollection.BuildServiceProvider();

            var expr =
                from restaurantResult in RestaurantDomain.CreateRestaurant("mcdonalds")
                let restaurant = (restaurantResult as RestaurantCreated)?.Restaurant
                                 from newMenu in RestaurantDomain.CreateMenu(restaurant, "burgers", MenuType.Meat)
                                 select restaurantResult;

            var interpreter = new LiveInterpreterAsync(serviceProvider);

            var result = await interpreter.Interpret(expr, Unit.Default);

            var finalResult = result.Match(OnRestaurantCreated, OnRestaurantNotCreated);

            Assert.True(finalResult);


            // User expression
            var userExpr =
                from userResult in RestaurantDomain.CreateUser("Hanz", "HanzPassword", 45)
                let user = (userResult as UserCreated)?.User
                           select userResult;

            var userRez = await interpreter.Interpret(userExpr, Unit.Default);

            var userFinalResult = userRez.Match(OnUserCreated, OnUserNotCreated);

            Assert.True(userFinalResult);

            //MenuItem expression
            var menuItemExpresion =
                from menuItemResult in RestaurantDomain.CreateMenuItem(
                    "Pizza", 13.5m, MenuItemValues.getIngredients(), MenuItemValues.getAlergens(), MenuItemValues.getMenu())
                let menuItem = (menuItemResult as MenuItemCreated)?.MenuItem
                               select menuItemResult;

            var menuItemRez = await interpreter.Interpret(menuItemExpresion, Unit.Default);

            var manuItemFinalResult = menuItemRez.Match(OnMenuItemCreated, OnMenuItemNotCreated);

            Assert.True(manuItemFinalResult);

            //Employee expression
            var employeeExpr =
                from employeeResult in RestaurantDomain.CreateEmployee("Hanz", "Bauer", 30, "Timisoara nr.45",
                                                                       500m, Domain.Models.Gender.Male, Domain.Models.Position.Manager, new Restaurant("Papanasii"))
                let employee = (employeeResult as EmployeeCreated)?.Employee
                               select employeeResult;

            var employeeRez = await interpreter.Interpret(employeeExpr, Unit.Default);

            var employeeFinalResult = employeeRez.Match(OnEmployeeCreated, OnEmployeeNotCreated);

            Assert.True(employeeFinalResult);

            Console.WriteLine("Hello World!");
        }