Exemplo n.º 1
0
        public static int?VerifyUser(string json)
        {
            var dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);

            using (var ctx = new MenuDbContext())
            {
                try
                {
                    int?id = Token.Verify(dict["token"]);
                    if (id.HasValue)
                    {
                        return(id);
                    }

                    return(null);
                }catch (Exception)
                {
                    try
                    {
                        var user = ctx.User.Where(u => u.Email.Equals(Token.Decrypt(dict["email"]))).First();
                        if (user.ValidatePassword(Token.Decrypt(dict["password"])))
                        {
                            return(user.Id);
                        }
                        return(null);
                    }
                    catch (Exception)
                    {
                        return(null);
                    }
                }
            }
        }
        public void FixtureSetUp()
        {
            ObjectReflectorConfiguration.NoValidate = true;
            var context = new MenuDbContext();

            InitializeNakedObjectsFramework(this);
        }
Exemplo n.º 3
0
        private static void SeedDatabase(MenuDbContext menuContext)
        {
            var menuRepo = new MenuOptionRepository(menuContext);

            menuRepo.Add(new MenuOption()
            {
                Name  = "Bacon, Lettuce, and Tomato Sandwich",
                Price = 5
            }).Wait();
            menuRepo.Add(new MenuOption()
            {
                Name  = "Turkey Bacon Sandwich",
                Price = 6
            }).Wait();
            menuRepo.Add(new MenuOption()
            {
                Name  = "Bacon Cheeseburger",
                Price = 7
            }).Wait();
            menuRepo.Add(new MenuOption()
            {
                Name  = "Small Soda",
                Price = 1.5
            }).Wait();
            menuRepo.Add(new MenuOption()
            {
                Name  = "Large Soda",
                Price = 2.5
            }).Wait();
            menuRepo.Add(new MenuOption()
            {
                Name  = "Fries",
                Price = 3
            }).Wait();
        }
Exemplo n.º 4
0
        public OrderForm()
        {
            InitializeComponent();
            orderItems = new List <OrderItem>();
            _context   = new MenuDbContext();

            Demo();
        }
Exemplo n.º 5
0
 public static void DeleteFood(int id)
 {
     using (var ctx = new MenuDbContext())
     {
         var toRemove = ctx.Food.FirstOrDefault(f => f.Id == id);
         ctx.Food.Remove(toRemove);
         ctx.SaveChanges();
     }
 }
Exemplo n.º 6
0
        public static Food GetFood(int id)
        {
            Food f;

            using (var ctx = new MenuDbContext())
            {
                f = ctx.Food.Where(food => food.Id == id).First();
            }
            return(f);
        }
Exemplo n.º 7
0
        public static IEnumerable <Category> GetCategories()
        {
            IEnumerable <Category> list;

            using (var ctx = new MenuDbContext())
            {
                list = ctx.Category.ToList().OrderBy(c => c.ParentId);
            }
            return(list);
        }
Exemplo n.º 8
0
        public static Category GetCategory(int id)
        {
            Category cat;

            using (var ctx = new MenuDbContext())
            {
                cat = ctx.Category.Where(c => c.Id == id).First();
            }
            return(cat);
        }
Exemplo n.º 9
0
        public static Order GetOrder()
        {
            Order order;

            using (var ctx = new MenuDbContext())
            {
                order = ctx.Order.LastOrDefault();
            }

            return(order);
        }
Exemplo n.º 10
0
        public static Order AddOrder(string json)
        {
            var order = JsonConvert.DeserializeObject <Order>(json);

            using (var ctx = new MenuDbContext())
            {
                ctx.Order.Add(order);
                ctx.SaveChanges();
            }
            return(order);
        }
Exemplo n.º 11
0
 public static Category AddCategory(Category c)
 {
     using (var ctx = new MenuDbContext())
     {
         //Test if parentId exists
         ctx.Category.Where(cat => cat.Id == c.ParentId).First();
         ctx.Category.Add(c);
         ctx.SaveChanges();
     }
     return(c);
 }
Exemplo n.º 12
0
        public void CanCreateContext()
        {
            var options = new DbContextOptionsBuilder <MenuDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test")
                          .Options;

            using (var ctx = new MenuDbContext(options))
            {
                var menus = ctx.Menus.ToList();
            }
        }
Exemplo n.º 13
0
 public static void DeleteCategory(int id)
 {
     using (var ctx = new MenuDbContext())
     {
         var toRemove = ctx.Category.FirstOrDefault(c => c.Id == id);
         ctx.Category.Remove(toRemove);
         var toRemoveChild = ctx.Category.Where(c => c.ParentId == id);
         ctx.Category.RemoveRange(toRemoveChild);
         ctx.SaveChanges();
     }
 }
Exemplo n.º 14
0
        private static void InitializeDatabase(IConfiguration configuration)
        {
            var menuOptsBuilder = new DbContextOptionsBuilder <MenuDbContext>();

            menuOptsBuilder.UseSqlServer(configuration.GetConnectionString("MenuConnection"));
            var menuContext = new MenuDbContext(menuOptsBuilder.Options, configuration);

            if (menuContext.Database.EnsureCreated())
            {
                SeedDatabase(menuContext);
            }
        }
Exemplo n.º 15
0
        public async Task CanHandleMenuEvents()
        {
            var options = new DbContextOptionsBuilder <MenuDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test")
                          .Options;

            using (var ctx = new MenuDbContext(options))
            {
                var restaurantId = Guid.NewGuid();
                var menuId       = Guid.NewGuid();
                var menuGroupId  = Guid.NewGuid();

                await new RestaurantRegisteredEventHandler(ctx).HandleAsync(new MyLunch.Domain.Menu.Events.RestaurantRegistered
                {
                    AggregateRootId = restaurantId,
                    Name            = "Taste it Gent",
                    ContactEmail    = new MyLunch.Domain.Shared.EmailAddress("*****@*****.**")
                });
                await new MenuCreatedEventHandler(ctx).HandleAsync(new MyLunch.Domain.Menu.Events.MenuCreated
                {
                    AggregateRootId = menuId,
                    RestaurantId    = restaurantId
                });
                await new MenuGroupAddedEventHandler(ctx).HandleAsync(new MyLunch.Domain.Menu.Events.MenuGroupAdded
                {
                    AggregateRootId = menuId,
                    Id   = menuGroupId,
                    Name = "Broodjes"
                });
                await new MenuItemAddedEventHandler(ctx).HandleAsync(new MyLunch.Domain.Menu.Events.MenuItemAdded
                {
                    AggregateRootId = menuId,
                    Id                 = Guid.NewGuid(),
                    GroupId            = menuGroupId,
                    ProductName        = "Hoevekaas",
                    ProductDescription = "Hoevekaas, light mosterddressing of mayo, wortel, tomaat, krokante sla",
                    Price              = 3.3
                });
                await new MenuItemAddedEventHandler(ctx).HandleAsync(new MyLunch.Domain.Menu.Events.MenuItemAdded
                {
                    AggregateRootId = menuId,
                    Id                 = Guid.NewGuid(),
                    GroupId            = menuGroupId,
                    ProductName        = "Krab",
                    ProductDescription = "Huisbereide krabsalade met bieslook, tomaat, krokante sla",
                    Price              = 3.6
                });

                (await ctx.Menus.FirstAsync()).PrettyPrint(Console.Out);
            }
        }
Exemplo n.º 16
0
        public static IEnumerable <Food> GetFood()
        {
            IEnumerable <Food> list;

            using (var ctx = new MenuDbContext())
            {
                list = ctx.Food.Include(f => f.FoodAllergen).ToList();
                foreach (Food f in list)
                {
                    f.Allergenes = f.FoodAllergen.Select(fa => fa.AllergenId).ToArray();
                }
            }
            return(list);
        }
Exemplo n.º 17
0
 public static Food AddFood(Food food)
 {
     using (var ctx = new MenuDbContext())
     {
         ctx.Category.Where(c => c.Id == food.CategoryId).First();
         food.FoodAllergen = new HashSet <FoodAllergen>();
         foreach (int AllergenId in food.Allergenes)
         {
             var allergen = ctx.Allergens.Single(a => a.Id == AllergenId);
             food.FoodAllergen.Add(new FoodAllergen(food, allergen));
         }
         ctx.Food.Add(food);
         ctx.SaveChanges();
     }
     return(food);
 }
Exemplo n.º 18
0
        public static ICollection <Order> GetHistory(int userId)
        {
            ICollection <Order> orders;

            using (var ctx = new MenuDbContext())
            {
                orders = ctx.Order.Include(o => o.OrderFood)
                         .ThenInclude(of => of.Food)
                         .Where(o => o.UserId == userId).OrderBy(o => o.OrderedAt).Take(10).ToArray();
                foreach (Order order in orders)
                {
                    order.Date = order.OrderedAt.ToString("dd.MM.yyyy");
                }
            }

            return(orders);
        }
Exemplo n.º 19
0
 private void StartEvents(string address)
 {
     serverSentEvents = new HttpListener();
     serverSentEvents.Prefixes.Add("http://" + address + ":1234/SSE/");
     serverSentEvents.Start();
     Task.Run(() =>
     {
         while (true)
         {
             var ctx = serverSentEvents.GetContext();
             Task.Run(() =>
             {
                 var response             = ctx.Response;
                 response.StatusCode      = 200;
                 response.ContentType     = "text/event-stream";
                 response.ContentEncoding = System.Text.Encoding.UTF8;
                 try
                 {
                     while (true)
                     {
                         waitHandle.Reset();
                         string order = "data: ";
                         using (var dbCtx = new MenuDbContext())
                         {
                             //dbCtx.Order.OrderByDescending(o => o.Id).Include( o => o.OrderFood).First()
                             order += JsonConvert.SerializeObject(dbCtx.Order.OrderByDescending(o => o.Id)
                                                                  .Include(o => o.OrderFood).ThenInclude(of => of.Food).First());
                         }
                         order     += "\n\n";
                         var msg    = string.Format("data: Time is now {0}\n\n", DateTime.Now);
                         var buffer = System.Text.Encoding.UTF8.GetBytes(order);
                         //response.ContentLength64 = buffer.Length;
                         response.OutputStream.Write(buffer, 0, buffer.Length);
                         response.OutputStream.Flush();
                         waitHandle.WaitOne();
                     }
                 }
                 catch (HttpListenerException ex)
                 {
                     Console.WriteLine(ex.ToString());
                 }
             });
         }
     });
 }
Exemplo n.º 20
0
        public static Food AddFood(string jsonObject)
        {
            var food = JsonConvert.DeserializeObject <Food>(jsonObject);

            using (var ctx = new MenuDbContext())
            {
                food.FoodAllergen = new HashSet <FoodAllergen>();
                foreach (int AllergenId in food.Allergenes)
                {
                    var allergen = ctx.Allergens.Single(a => a.Id == AllergenId);
                    food.FoodAllergen.Add(new FoodAllergen(food, allergen));
                }
                ctx.Food.Add(food);
                ctx.SaveChanges();
            }

            return(food);
        }
Exemplo n.º 21
0
        public static User AddUser(string json)
        {
            var  dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);
            User user = new User(Token.Decrypt(dict["email"]), Token.Decrypt(dict["password"]));

            try
            {
                using (var ctx = new MenuDbContext())
                {
                    ctx.User.Add(user);
                    ctx.SaveChanges();
                }
            }
            catch (Exception)
            {
                return(null);
            }
            return(user);
        }
Exemplo n.º 22
0
        public static Order AddOrder(string json, int id)
        {
            var   order      = JsonConvert.DeserializeObject <Dictionary <string, string[]> >(json);
            var   duplicates = new List <string>();
            Order realOrder;

            using (var ctx = new MenuDbContext())
            {
                var user = ctx.User.Where(u => u.Id == id).First();

                realOrder = new Order
                {
                    OrderedAt  = DateTime.Now,
                    User       = user,
                    UserId     = id,
                    TotalPrice = Decimal.Parse(order["totalprice"][0])
                };

                realOrder.OrderFood = new List <OrderFood>();

                ctx.Order.Add(realOrder);

                ctx.SaveChanges();

                foreach (string foodId in order["food"])
                {
                    if (duplicates.Contains(foodId))
                    {
                        continue;
                    }

                    realOrder.OrderFood.Add(new OrderFood {
                        Order = realOrder, Food = ctx.Food.Find(Int32.Parse(foodId)), FoodCount = order["food"].Duplicates(foodId)
                    });
                    duplicates.Add(foodId);
                }

                ctx.SaveChanges();
            }

            return(realOrder);
        }
Exemplo n.º 23
0
        public static Category AddCategory(string postText)
        {
            var      categoryText = JsonConvert.DeserializeObject <Dictionary <string, string> >(postText);
            Category newCategory;

            //var category = JsonConvert.DeserializeObject<Category>(postText);
            using (var ctx = new MenuDbContext())
            {
                if (categoryText["ParentName"].Equals("Vybrat...", StringComparison.CurrentCultureIgnoreCase))
                {
                    newCategory = new Category(categoryText["Name"], null);
                }
                else
                {
                    var parent = (from c in ctx.Category.ToList() where c.Name == categoryText["ParentName"] select c).First();
                    newCategory = new Category(categoryText["Name"], parent.Id);
                }
                ctx.Category.Add(newCategory);
                ctx.SaveChanges();
            }
            return(newCategory);
        }
Exemplo n.º 24
0
 public Server()
 {
     //Vytvoří se nová instance třídy pro RSA šifrování
     this.cryption = new RsaCryption();
     //Přiradí se reference na třídy
     Http.server    = this;
     Http.cryptor   = this.cryption;
     Token.cryption = this.cryption;
     this.win       = new MainWindow(this);
     try
     {
         //Připojení k databázi pomocí dat z app.config
         database = new MenuDbContext();
         database.Database.Initialize(true);
         this.win.statbar.Push(0, @"Připojeno k databázi");
         this.isConnected = true;
     }
     catch (Exception)
     {
         connect();
     }
     win.Show();
 }
Exemplo n.º 25
0
 public DataController(MenuDbContext menuDbContext, ILogger <DataController> logger)
 {
     _menuDb = menuDbContext;
     _logger = logger;
 }
 public void FixtureTearDown()
 {
     CleanupNakedObjectsFramework(this);
     MenuDbContext.Delete();
 }
Exemplo n.º 27
0
 public List<Menu> GetMenus()
 {
     var context=new MenuDbContext();
     return context.Menus.ToList();
 }
Exemplo n.º 28
0
 protected RepositoryBase(MenuDbContext dbContext)
 {
     this.dbContext = dbContext;
     entityContext  = dbContext.Set <TEntity>();
 }
Exemplo n.º 29
0
        public MenuItemAddedEventHandler(MenuDbContext db)
        {
            Guard.ArgumentNotNull(() => db);

            _db = db;
        }
Exemplo n.º 30
0
 public MenuItemRepository(MenuDbContext dbContext) : base(dbContext)
 {
 }
Exemplo n.º 31
0
 public MenuUnitofWork(MenuDbContext _db)
 {
     db = _db;
 }