Esempio n. 1
0
        protected override void InitializeStorage(IFoodOrderingContextFactory factory, int count)
        {
            using (var context = factory.Create())
            {
                var restaurant = new RestaurantEntity()
                {
                    Name = "testRestaurant"
                };

                var insertedRestauran = context.Restaurant.Add(restaurant).Entity;
                var dailyMenu         = new DailyMenuEntity()
                {
                    RestaurantId = insertedRestauran.Id
                };

                var foods = Enumerable.Range(1, count).Select(x => new FoodEntity()
                {
                    Id           = x,
                    Name         = $"Name {x}",
                    RestaurantId = insertedRestauran.Id
                });

                context.Food.AddRange(foods);
                context.SaveChanges();
            }
        }
Esempio n. 2
0
        public Either <Error, int> Insert(DailyMenuInsertModel entity)
        {
            try
            {
                using (var context = _factory.Create())
                {
                    var dailyMenuEntity = new DailyMenuEntity
                    {
                        RestaurantId = entity.RestaurantId,
                        Foods        = entity.Foods.Select(food => context.Food.Find(food.Id)).ToList()
                    };

                    var addedEntity = context.DailyMenu.Add(dailyMenuEntity).Entity;
                    context.SaveChanges();
                    return(new Right <Error, int>(addedEntity.Id));
                }
            }
            catch (Exception ex)
            {
                return(new Left <Error, int>(new UnknownError(ex.ToString())));
            }
        }