Exemplo n.º 1
0
        public IHttpActionResult CreateNewMeal(MealBindingModel mealModel)
        {
            if (mealModel == null)
            {
                return(this.BadRequest("Invalid meal data."));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var mealType = db.MealTypes.FirstOrDefault(mt => mt.Id == mealModel.TypeId);

            if (mealType == null)
            {
                return(this.BadRequest("Invalid meal type id."));
            }

            var restaurant = db.Restaurants.FirstOrDefault(r => r.Id == mealModel.RestaurantId);

            if (restaurant == null)
            {
                return(this.BadRequest("Invalid restaurant id."));
            }

            var currentUserId = User.Identity.GetUserId();

            if (currentUserId == null)
            {
                return(this.Unauthorized());
            }

            var meal = new Meal()
            {
                Name       = mealModel.Name,
                Price      = mealModel.Price,
                Restaurant = restaurant,
                Type       = mealType
            };

            db.Meals.Add(meal);
            db.SaveChanges();

            return(CreatedAtRoute(
                       "DefaultApi",
                       new
            {
                id = meal.Id
            },
                       new MealOutputModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = meal.Type.Name
            }));
        }
        public void EditMeal_WithCorrectData_ShouldAllowMealEdition()
        {
            using (var dbContext = new RestaurantsContext())
            {
                var httpTestServer = TestServer.Create(appBuilder =>
                {
                    var config = new HttpConfiguration();
                    WebApiConfig.Register(config);
                    appBuilder.UseWebApi(config);
                });

                // Arrange

                // Clean Database
                CleanDatabase();

                // Create restaurant

                var restaurant = new Restaurant()
                {
                    Id     = 1,
                    Name   = "Gurlata",
                    TownId = 1
                };

                // Create meal
                var meal = new Meal()
                {
                    Id           = 1,
                    Name         = "Kibap4ita",
                    Price        = 18.2m,
                    TypeId       = 3,
                    RestaurantId = 1
                };

                // Act -> add restaurant and meal to database

                dbContext.Restaurants.Add(restaurant);
                dbContext.Meals.Add(meal);
                dbContext.SaveChanges();

                var mealFromDb = dbContext.Meals.Find(meal.Id);

                mealFromDb.Name = "Kufteta";
                dbContext.SaveChanges();

                var editedMealFromBd = dbContext.Meals.Find(mealFromDb.Id);

                Assert.AreEqual(mealFromDb.Name, editedMealFromBd.Name);

                CleanDatabase();
            }
        }
Exemplo n.º 3
0
        public IHttpActionResult CreateNewRestaurant(RestourantBindingModel restourantModel)
        {
            if (restourantModel == null)
            {
                return(this.BadRequest("Invalid restaurant data."));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUserId = User.Identity.GetUserId();

            if (currentUserId == null)
            {
                return(this.Unauthorized());
            }

            var town = db.Towns.FirstOrDefault(t => t.Id == restourantModel.TownId);

            var restaurant = new Restaurant()
            {
                Name    = restourantModel.Name,
                Town    = town,
                OwnerId = currentUserId
            };

            db.Restaurants.Add(restaurant);
            db.SaveChanges();

            return(CreatedAtRoute(
                       "DefaultApi",
                       new
            {
                id = restaurant.Id
            },
                       new RestourantOutputModel()
            {
                Id = restaurant.Id,
                Name = restaurant.Name,
                Rating = null,
                Town = new TownOutputModel()
                {
                    Id = restaurant.TownId, Name = restaurant.Town.Name
                }
            }));
        }
        public static void CleanDatabase()
        {
            using (var dbContext = new RestaurantsContext())
            {
                foreach (var meal in dbContext.Meals)
                {
                    dbContext.Meals.Remove(meal);
                }

                foreach (var order in dbContext.Orders)
                {
                    dbContext.Orders.Remove(order);
                }

                foreach (var rating in dbContext.Ratings)
                {
                    dbContext.Ratings.Remove(rating);
                }

                foreach (var restaurant in dbContext.Restaurants)
                {
                    dbContext.Restaurants.Remove(restaurant);
                }
                dbContext.SaveChanges();
            }
        }
        private void SeedMeals(RestaurantsContext context, List<MealType> mealTypes)
        {
            var meals = new List<Meal>()
            {
                new Meal()
                {
                    Name = "French salad",
                    Price = 5.40m,
                    Restaurant = new Restaurant()
                    {
                        Name = "Pri baba",
                        Owner = context.Users.FirstOrDefault(u => u.UserName == TestUserUsername),
                        Town = new Town() {Name = "Bracigovo"}
                    },
                    Type = mealTypes.First()
                }
            };

            foreach (var meal in meals)
            {
                context.Meals.Add(meal);
            }

            context.SaveChanges();
        }
Exemplo n.º 6
0
        private static void ClearDatabase()
        {
            var context    = new RestaurantsContext();
            var restourans = context.Restaurants.ToList();

            while (restourans.Count != 0)
            {
                var restorant = restourans[restourans.Count - 1];
                restourans.Remove(restorant);
                context.Restaurants.Remove(restorant);
            }

            var users = context.Users.ToList();

            while (users.Count != 0)
            {
                var user = users[users.Count - 1];
                users.Remove(user);
                context.Users.Remove(user);
            }

            var meals = context.Meals.ToList();

            while (meals.Count != 0)
            {
                var meal = meals[meals.Count - 1];
                meals.Remove(meal);
                context.Meals.Remove(meal);
            }

            context.SaveChanges();
        }
 public void Update(Restaurant r)
 {
     using (var db = new RestaurantsContext()) {
         db.Restaurants.Attach(r);
         db.SaveChanges();
     }
 }
 public void Create(Restaurant r)
 {
     using (var db = new RestaurantsContext()) {
         db.Restaurants.Add(r);
         db.SaveChanges();
     }
 }
Exemplo n.º 9
0
        private void SeedMeals(RestaurantsContext context, List <MealType> mealTypes)
        {
            var meals = new List <Meal>()
            {
                new Meal()
                {
                    Name       = "Chorba",
                    Price      = 5.40m,
                    Restaurant = new Restaurant()
                    {
                        Name  = "Pri baba",
                        Owner = context.Users.FirstOrDefault(u => u.UserName == TestUserUsername),
                        Town  = new Town()
                        {
                            Name = "Bracigovo"
                        }
                    },
                    Type = mealTypes.First()
                }
            };

            foreach (var meal in meals)
            {
                context.Meals.Add(meal);
            }

            context.SaveChanges();
        }
        public void EditExistingMeal_ShouldReturn200OK()
        {
            // Arrange
            RestaurantsContext dbContext = new RestaurantsContext();
            var restaurant = dbContext.Restaurants.FirstOrDefault();

            Meal meal = new Meal()
            {
                Name = "Tarator",
                Price = (decimal)1.0,
                Restaurant = restaurant,
                RestaurantId = restaurant.Id,
                Type = dbContext.MealTypes.Find(1),
                TypeId = 1
            };

            dbContext.Meals.Add(meal);
            dbContext.SaveChanges();

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("name", "Shopska"),
                new KeyValuePair<string, string>("typeId", "3"),
                new KeyValuePair<string, string>("price", "1.1")
            });
            // Act
            var httpResponse = httpClient.PutAsync("/api/meals/" + meal.Id, content).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
            Assert.AreEqual(httpResponse.Content.Headers.ContentType.MediaType, "application/json");
        }
Exemplo n.º 11
0
        public void EditNonExistingMeal_ShouldReturn404NotFound()
        {
            // Arrange
            RestaurantsContext dbContext = new RestaurantsContext();
            var restaurant = dbContext.Restaurants.FirstOrDefault();

            Meal meal = new Meal()
            {
                Name         = "Tarator",
                Price        = (decimal)1.0,
                Restaurant   = restaurant,
                RestaurantId = restaurant.Id,
                Type         = dbContext.MealTypes.Find(1),
                TypeId       = 1
            };

            dbContext.Meals.Add(meal);
            dbContext.SaveChanges();

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("name", "Shopska"),
                new KeyValuePair <string, string>("typeId", "3"),
                new KeyValuePair <string, string>("price", "1.1")
            });
            // Act
            var httpResponse = httpClient.PutAsync("/api/meals/" + meal.Id + 1, content).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.NotFound, httpResponse.StatusCode);
            Assert.AreEqual(httpResponse.Content.Headers.ContentType.MediaType, "application/json");
        }
Exemplo n.º 12
0
        private static void SeedMeals(RestaurantsContext context)
        {
            var restourant = new Restaurant()
            {
                Name    = "Seed restaurant",
                OwnerId = "1",
                Ratings = new List <Rating>(),
                Town    = new Town()
                {
                    Id = 1, Name = "Town"
                }
            };

            context.Meals.Add(new Meal()
            {
                Name       = "Valid 1",
                Price      = 1,
                Restaurant = restourant,
                Type       = new MealType()
                {
                    Name = "Type 1"
                }
            });

            context.Meals.Add(new Meal()
            {
                Name       = "Valid 2",
                Price      = 2,
                Restaurant = restourant,
                Type       = new MealType()
                {
                    Name = "Type 2"
                }
            });

            context.Meals.Add(new Meal()
            {
                Name       = "Invdalid",
                Price      = 2,
                Restaurant = new Restaurant()
                {
                    Name  = "Seed restaurant2",
                    Owner = new ApplicationUser()
                    {
                        UserName = "******", PasswordHash = "212"
                    },
                    Ratings = new List <Rating>(),
                    Town    = new Town()
                    {
                        Id = 2, Name = "Town2"
                    }
                },
                Type = new MealType()
                {
                    Name = "Type 2"
                }
            });
            context.SaveChanges();
        }
        private static void Seed()
        {
            var context = new RestaurantsContext();

            if (!context.Restaurants.Any())
            {
                context.Restaurants.Add(new Restaurant()
                {
                    Name = "Mir",
                    TownId = 1,
                });
                context.SaveChanges();
            }

            if (!context.MealTypes.Any())
            {
                var mealTypes = new[]
                {
                    new MealType {Name = "Salad", Order = 10},
                    new MealType {Name = "Soup", Order = 20},
                    new MealType {Name = "Main", Order = 30},
                    new MealType {Name = "Dessert", Order = 40}
                };

                foreach (var mealType in mealTypes)
                {
                    context.MealTypes.Add(mealType);
                }

                context.SaveChanges();
            }
            if (!context.Meals.Any())
            {
                context.Meals.Add(new Meal()
                {
                    Name = "Rice",
                    Price = 10.0m,
                    TypeId = 1,
                    RestaurantId = 1
                });
                context.SaveChanges();

            }
        }
        public void EditMeal_NonExistingMeal_ShouldNotReturnMealForEditing()
        {
            using (var dbContext = new RestaurantsContext())
            {
                var httpTestServer = TestServer.Create(appBuilder =>
                {
                    var config = new HttpConfiguration();
                    WebApiConfig.Register(config);
                    appBuilder.UseWebApi(config);
                });

                // Arrange

                // Clean Database
                CleanDatabase();

                // Create restaurant

                var restaurant = new Restaurant()
                {
                    Id = 1,
                    Name = "Gurlata",
                    TownId = 1
                };

                // Create meal
                var meal = new Meal()
                {
                    Id = 1,
                    Name = "Shish kebab",
                    Price = 18.2m,
                    TypeId = 3,
                    RestaurantId = 1
                };

                // Act -> add restaurant and meal to database

                dbContext.Restaurants.Add(restaurant);
                dbContext.Meals.Add(meal);
                dbContext.SaveChanges();

                var mealWithNonExistingId = dbContext.Meals
                    .Where(m => m.Id == meal.Id + 1);

                // Assert -> check if non existing meal returns not found

                var fakeMealId = meal.Id + 1;

                var fakeMeal = dbContext.Meals.Find(fakeMealId);

                Assert.IsNull(fakeMeal);

                CleanDatabase();
            }
        }
        public void EditMeal_NonExistingMeal_ShouldNotReturnMealForEditing()
        {
            using (var dbContext = new RestaurantsContext())
            {
                var httpTestServer = TestServer.Create(appBuilder =>
                {
                    var config = new HttpConfiguration();
                    WebApiConfig.Register(config);
                    appBuilder.UseWebApi(config);
                });

                // Arrange

                // Clean Database
                CleanDatabase();

                // Create restaurant

                var restaurant = new Restaurant()
                {
                    Id     = 1,
                    Name   = "Gurlata",
                    TownId = 1
                };

                // Create meal
                var meal = new Meal()
                {
                    Id           = 1,
                    Name         = "Shish kebab",
                    Price        = 18.2m,
                    TypeId       = 3,
                    RestaurantId = 1
                };

                // Act -> add restaurant and meal to database

                dbContext.Restaurants.Add(restaurant);
                dbContext.Meals.Add(meal);
                dbContext.SaveChanges();

                var mealWithNonExistingId = dbContext.Meals
                                            .Where(m => m.Id == meal.Id + 1);

                // Assert -> check if non existing meal returns not found

                var fakeMealId = meal.Id + 1;

                var fakeMeal = dbContext.Meals.Find(fakeMealId);

                Assert.IsNull(fakeMeal);

                CleanDatabase();
            }
        }
        public IHttpActionResult PostRestaurant(CreateRestaurantBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var currentUserId = User.Identity.GetUserId();

            var newRestaurant = new Restaurant()
            {
                Name    = model.Name,
                TownId  = model.TownId,
                OwnerId = currentUserId
            };

            context.Restaurants.Add(newRestaurant);
            context.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = model.TownId }, model));
        }
        public static void CleanDatabase()
        {
            var dbContext = new RestaurantsContext();

            dbContext.Meals.Delete();
            dbContext.Restaurants.Delete();
            dbContext.Towns.Delete();
            dbContext.Users.Delete();

            dbContext.SaveChanges();
        }
Exemplo n.º 18
0
        public void Edit_ShouldReturn400BadRequestWhenModelIsNull()
        {
            var context = new RestaurantsContext();
            var newTown = new Town()
            {
                Name = "New town"
            };

            context.Towns.Add(newTown);
            context.SaveChanges();

            var newRestaurant = new Restaurant()
            {
                Name = "New restaurant",
                TownId = newTown.Id
            };

            context.Restaurants.Add(newRestaurant);
            context.SaveChanges();

            var newMeal = new Meal()
            {
                Name = "New meal",
                Price = 4.99M,
                RestaurantId = newRestaurant.Id,
                TypeId = 3
            };

            context.Meals.Add(newMeal);
            context.SaveChanges();

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("name", "aaaaa")
            });

            var response = httpClient.PutAsync(string.Format("api/meals/{0}", newMeal.Id), content).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
Exemplo n.º 19
0
        public void RestoreDatabaseFromJson(string fileName, string connectionString)
        {
            var restaurants = LoadFromFile(fileName);

            using (var db = new RestaurantsContext((new DbContextOptionsBuilder().UseSqlServer(connectionString)).Options)) {
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();
                restaurants.ForEach(r => {
                    db.Add(r);
                });
                db.SaveChanges();
            }
        }
Exemplo n.º 20
0
 public static void CleanDatabase()
 {
     using (var dbContext = new RestaurantsContext())
     {
         dbContext.Ratings.Delete();
         dbContext.MealTypes.Delete();
         dbContext.Users.Delete();
         dbContext.Orders.Delete();
         dbContext.Meals.Delete();
         dbContext.Restaurants.Delete();
         dbContext.Towns.Delete();
         dbContext.SaveChanges();
     }
 }
        private List<MealType> SeedMealTypes(RestaurantsContext context)
        {
            var mealTypes = new List<MealType>()
            {
                new MealType() {Name = "Salad", Order = 10},
                new MealType() {Name = "Soup", Order = 20}
            };

            foreach (var mealType in mealTypes)
            {
                context.MealTypes.Add(mealType);
            }

            context.SaveChanges();
            return mealTypes;
        }
Exemplo n.º 22
0
        public void CreateDbTest()
        {
            IQueryable <Restaurant> mesRestaurantsFiltres;

            using (var db = new RestaurantsContext()) {
                db.Database.EnsureCreated();



                var Restau1 = new Restaurant()
                {
                    name    = "Tchouchoura",
                    address = new Address()
                    {
                        street = "rue Ampère", zipcode = "38000"
                    },
                    borough = "Grenoble", cuisine = "Balkans",
                    grades  = new List <Grade> {
                        new Grade()
                        {
                            date = 456, grade = "A", score = 10
                        },
                        new Grade()
                        {
                            date = 789, grade = "A", score = 10
                        }
                    }
                };

                db.Restaurants.Add(Restau1);
                db.SaveChanges();


                var result = db.Restaurants.Include(r => r.address).ToList();

                mesRestaurantsFiltres = db.Restaurants.Where(r => r.ID < 5);


                Restau1.name = "toto";
            }
        }
Exemplo n.º 23
0
        private List <MealType> SeedMealTypes(RestaurantsContext context)
        {
            var mealTypes = new List <MealType>()
            {
                new MealType()
                {
                    Name = "Type #1", Order = 10
                },
                new MealType()
                {
                    Name = "Type #2", Order = 20
                }
            };

            foreach (var mealType in mealTypes)
            {
                context.MealTypes.Add(mealType);
            }

            context.SaveChanges();
            return(mealTypes);
        }
Exemplo n.º 24
0
        public void Setup()
        {
            var optionsBuilder = new DbContextOptionsBuilder();

            // On se sert d'une base en mémoire, car on ne teste que le service. On la remplit avec un restaurant indépendamment du service.
            // J'ai l'impression que cela pose problème pour le tracking des entités : certains tests échouent ou non selon qu'on les lance indépendamment des autres ou tous ensemble...
            optionsBuilder.UseInMemoryDatabase("Restaurants");
            var options = optionsBuilder.Options;

            db = new RestaurantsContext(options);
            db.Restaurants.Add(new Restaurant()
            {
                Nom       = "Test",
                Email     = "*****@*****.**",
                Telephone = "25411225588",
                Adresse   = new Adresse {
                    CodePostal = "63854", Ville = "Grenoble", Rue = "cours test", Numero = "58 bis"
                },
                LastGrade = new Grade {
                    DateDerniereVisite = DateTime.Parse("01/01/20"), Note = 10, Commentaire = "nul"
                }
            });
            db.Restaurants.Add(new Restaurant()
            {
                Nom       = "Test2",
                Email     = "*****@*****.**",
                Telephone = "25411225588",
                Adresse   = new Adresse {
                    CodePostal = "63854", Ville = "Grenoble", Rue = "cours test", Numero = "58 bis"
                },
                LastGrade = new Grade {
                    DateDerniereVisite = DateTime.Parse("01/01/20"), Note = 10, Commentaire = "nul"
                }
            });
            db.SaveChanges();
            srv = new RestaurantsService(db);
        }
        public static void CleanDatabase()
        {
            using (var dbContext = new RestaurantsContext())
            {
                foreach (var meal in dbContext.Meals)
                {
                    dbContext.Meals.Remove(meal);
                }

                foreach (var order in dbContext.Orders)
                {
                    dbContext.Orders.Remove(order);
                }

                foreach (var rating in dbContext.Ratings)
                {
                    dbContext.Ratings.Remove(rating);
                }

                foreach (var restaurant in dbContext.Restaurants)
                {
                    dbContext.Restaurants.Remove(restaurant);
                }
                dbContext.SaveChanges();
            }
        }
Exemplo n.º 26
0
 public ActionResult Create(Cuisine Cuisine)
 {
     _db.Cuisines.Add(Cuisine);
     _db.SaveChanges();
     return(RedirectToAction("Index"));
 }
Exemplo n.º 27
0
 public virtual void CreateRestaurant(Restaurant restaurant)
 {
     _ctx.Restaurants.Add(restaurant);
     _ctx.SaveChanges();
 }
Exemplo n.º 28
0
        public ActionResult Index()
        {
            Random rnd = new Random();

            for (int i = 0; i < 100; i++)
            {
                if (i % 5 == 0)
                {
                    CategoryMeal categoryMeal = new CategoryMeal
                    {
                        Name    = "Yemek Movu " + (i / 5 + 1),
                        Created = DateTime.Now
                    };
                    context.CategoryMeals.Add(categoryMeal);
                    context.SaveChanges();

                    Kitchen kitchen = new Kitchen
                    {
                        Name = "Italyan metbexti " + (i / 5 + 1)
                    };
                    context.Kitchens.Add(kitchen);
                    context.SaveChanges();

                    Place place = new Place
                    {
                        Name = "Baki " + (i / 5 + 1)
                    };
                    context.Places.Add(place);
                    context.SaveChanges();

                    User user = new User
                    {
                        FullName    = "Ad Soyad " + i,
                        PhoneNumber = "05569655" + i.ToString("00"),
                        Created     = DateTime.Now,
                        UserName    = "******",
                        UserType    = UserType.restaurant,
                        IsActived   = true
                    };
                    context.Users.Add(user);
                    context.SaveChanges();

                    Restaurant restaurant = new Restaurant
                    {
                        PlaceId       = place.Id,
                        UserId        = user.Id,
                        HolidayOfWeek = "SunSat",
                        OpenHours     = 9,
                        ClooseHours   = 22,
                        Address       = "Allaha sukur butun metrolarin yanindayix"
                    };
                    context.Restaurants.Add(restaurant);
                    context.SaveChanges();

                    User user1 = new User
                    {
                        FullName    = "Ad Soyad " + i,
                        PhoneNumber = "05569655" + i.ToString("00"),
                        Created     = DateTime.Now,
                        UserName    = "******",
                        UserType    = UserType.customer,
                        IsActived   = true
                    };
                    context.Users.Add(user1);
                    context.SaveChanges();

                    Customer customer = new Customer
                    {
                        UserId = user1.Id
                    };
                    context.Customers.Add(customer);
                    context.SaveChanges();

                    User user2 = new User
                    {
                        FullName    = "Admin " + i,
                        PhoneNumber = "05569655" + i.ToString("00"),
                        Created     = DateTime.Now,
                        UserName    = "******",
                        UserType    = UserType.admin,
                        IsActived   = true
                    };
                    context.Users.Add(user2);
                    context.SaveChanges();
                    Admin admin = new Admin
                    {
                        UserId = user2.Id,
                    };
                    context.Admins.Add(admin);
                    context.SaveChanges();
                }

                Meal meal = new Meal
                {
                    Name           = "Burger " + (i + 1),
                    Price          = rnd.Next(1000, 20000) / 100,
                    Count          = rnd.Next(5, 30),
                    Created        = DateTime.Now,
                    KitchenId      = (i - i % 5) / 5 + 1,
                    CategoryMealId = (i - i % 5) / 5 + 1,
                    RestaurantId   = (i - i % 5) / 5 + 1
                };
                context.Meals.Add(meal);
                context.SaveChanges();

                Review review = new Review
                {
                    Comment    = "Canavardi",
                    Rating     = 4,
                    MealId     = meal.Id,
                    Create     = DateTime.Now,
                    CustomerId = (i - i % 5) / 5 + 1
                };
                context.Reviews.Add(review);
                context.SaveChanges();
            }
            return(View());
        }
Exemplo n.º 29
0
 private static void CleanDatabase()
 {
     using (var context = new RestaurantsContext())
     {
         context.Meals.Delete();
         context.Restaurants.Delete();
         context.Users.Delete();
         context.MealTypes.Delete();
         context.Towns.Delete();
         context.SaveChanges();
     }
 }
Exemplo n.º 30
0
        private void SeedRestaurant(LoginData loginData)
        {
            using (var context = new RestaurantsContext())
            {
                var newRestaurant = new Restaurant()
                {
                    Name = "New restaurant",
                    TownId = townId,
                    OwnerId = context.Users.FirstOrDefault(u => u.UserName == loginData.Username).Id
                };

                context.Restaurants.Add(newRestaurant);
                context.SaveChanges();

                this.restaurantId = newRestaurant.Id;
            }
        }
Exemplo n.º 31
0
        private void SeedTown()
        {
            using (var context = new RestaurantsContext())
            {
                var newTown = new Town()
                {
                    Name = "New town"
                };

                context.Towns.Add(newTown);
                context.SaveChanges();


                this.townId = newTown.Id;
            }
        }
Exemplo n.º 32
0
        private void SeedMealTypes()
        {
            using (var context = new RestaurantsContext())
            {
                var firstNewMealType = new MealType
                {
                    Name = "Salad",
                    Order = 10
                };

                var secondNewMealType = new MealType
                {
                    Name = "Soup",
                    Order = 20
                };

                context.MealTypes.Add(firstNewMealType);
                context.MealTypes.Add(secondNewMealType);
                context.SaveChanges();

                this.firstMealTypeId = firstNewMealType.Id;
                this.secondMealTypeId = secondNewMealType.Id;
            }
        }
Exemplo n.º 33
0
        private void SeedMeal()
        {
            using (var context = new RestaurantsContext())
            {
                var newMeal = new Meal()
                {
                    Name = "New meal",
                    Price = 4.99M,
                    RestaurantId = restaurantId,
                    TypeId = firstMealTypeId
                };

                context.Meals.Add(newMeal);
                context.SaveChanges();

                this.mealId = newMeal.Id;
            }
        }
        private void SeedDatabase()
        {
            var context = new RestaurantsContext();

            var userStore   = new UserStore <ApplicationUser>(context);
            var userManager = new ApplicationUserManager(userStore);

            var user1 = new ApplicationUser()
            {
                UserName = "******",

                Email = "*****@*****.**"
            };

            var user2 = new ApplicationUser()
            {
                UserName = "******",

                Email = "*****@*****.**"
            };

            var result = userManager.CreateAsync(user1, "123456").Result;

            result = userManager.CreateAsync(user2, "123456").Result;

            context.SaveChanges();

            var town = new Town()
            {
                Name = "Test Town"
            };

            context.Towns.Add(town);
            context.SaveChanges();

            var restaurant = new Restaurant()
            {
                Name    = "Test Restaurant",
                Owner   = user1,
                OwnerId = user1.Id,
                Town    = town,
                TownId  = town.Id
            };

            context.Restaurants.Add(restaurant);
            context.SaveChanges();

            var type = new MealType()
            {
                Name  = "Test Type",
                Order = 0
            };

            var typeForUpdate = new MealType()
            {
                Name  = "Updated Type",
                Order = 1
            };

            context.MealTypes.Add(type);
            context.MealTypes.Add(typeForUpdate);
            context.SaveChanges();

            var meal = new Meal()
            {
                Name         = "Test Meal",
                Price        = 10m,
                Restaurant   = restaurant,
                RestaurantId = restaurant.Id,
                Type         = type,
                TypeId       = type.Id
            };

            context.Meals.Add(meal);
            context.SaveChanges();
        }
        public void EditMeal_WithEmptyMealName_ShouldNotAllowMealEdition()
        {
            using (var dbContext = new RestaurantsContext())
            {
                var httpTestServer = TestServer.Create(appBuilder =>
                {
                    var config = new HttpConfiguration();
                    WebApiConfig.Register(config);
                    appBuilder.UseWebApi(config);
                });

                // Arrange

                // Clean Database
                CleanDatabase();

                // Create restaurant

                var restaurant = new Restaurant()
                {
                    Id = 1,
                    Name = "Gurlata",
                    TownId = 1
                };

                // Create meal
                var meal = new Meal()
                {
                    Id = 1,
                    Name = "Gozba",
                    Price = 18.2m,
                    TypeId = 3,
                    RestaurantId = 1
                };

                // Act -> add restaurant and meal to database

                dbContext.Restaurants.Add(restaurant);
                dbContext.Meals.Add(meal);
                dbContext.SaveChanges();

                var mealFromDb = dbContext.Meals.Find(meal.Id);

                mealFromDb.Name = "";
                dbContext.SaveChanges();

                CleanDatabase();
            }
        }
Exemplo n.º 36
0
 public ActionResult Create(Restaurant restaurant)
 {
     _db.Restaurants.Add(restaurant);
     _db.SaveChanges();
     return(RedirectToAction("Index"));
 }