Exemple #1
0
        public ActionResult DeleteConfirmed(int id)
        {
            Order order = db.Orders.Find(id);

            db.Orders.Remove(order);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "UserName,RestaurantDescrip,CategoryID,Stars,ID,Email,FirstName,LastName,Password,Address1,Address2,Address3,State,RegisterDate,Roles,isActived,VIP,FullAddress")] Restaurant restaurant)
 {
     if (ModelState.IsValid)
     {
         db.Entry(restaurant).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", restaurant.CategoryID);
     return(View(restaurant));
 }
 public void OrderShipped(int OrderID, bool Checked)
 {
     db.Orders
     .Where(o => o.OrderID == OrderID)
     .Single().Shipped = Checked;
     db.SaveChanges();
 }
Exemple #4
0
        public ActionResult Create(DictatorshipViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(viewModel));
            }

            using (var ctx = new LunchContext())
            {
                var user         = ctx.Users.SingleOrDefault(u => u.EmailAddress == User.Identity.Name);
                var dictatorship = new Dictatorship
                {
                    Name     = viewModel.Name,
                    ImageUrl = viewModel.ImageUrl,
                    Users    = new Collection <User> {
                        user
                    }
                };

                ctx.Dictatorships.Add(dictatorship);
                ctx.SaveChanges();
            }

            this.TempData["Message"] = WebCommon.DictatorshipAdded;
            return(this.View(new DictatorshipViewModel()));
        }
Exemple #5
0
        public ActionResult EditRestaurant(RestaurantViewModel restaurantViewModel)
        {
            if (!ModelState.IsValid)
            {
                using (var lunchContext = new LunchContext())
                {
                    ViewBag.Cuisines = lunchContext.Cuisines.Select(c => new SelectListItem
                    {
                        Value = c.CuisineId.ToString(),
                        Text  = c.Name
                    }).ToList();

                    return(View("AddEditRestaurant", restaurantViewModel));
                }
            }

            using (var lunchContext = new LunchContext())
            {
                var restaurant = lunchContext.Restaurants.SingleOrDefault(p => p.RestaurantId == restaurantViewModel.RestaurantId);

                if (restaurant != null)
                {
                    restaurant.Name      = restaurantViewModel.Name;
                    restaurant.CuisineId = restaurantViewModel.Cuisine.CuisineId.Value;
                    lunchContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(new HttpNotFoundResult());
        }
Exemple #6
0
        public ActionResult Create([Bind(Include = "DishID,DishName,DishDisc,Spicy,isMuslim,isGlutenFree,CategoryID,Price")] Dish dish)
        {
            if (ModelState.IsValid)
            {
                var        user       = this.HttpContext.User;
                Restaurant restaurant = db.Restaurants.Where(c => c.Email == user.Identity.Name).FirstOrDefault();
                restaurant.Dishes.Add(dish);
                //db.Dishes.Add(dish);

                db.SaveChanges();
                return(RedirectToAction("Personal_Profile", "Restaurants"));
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", dish.CategoryID);
            return(View(dish));
        }
        public async void LunchRepository_GetNopesAsync_ReturnsUsersNopes()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            UserEntity      user    = context.Users.Add(new UserEntity
            {
                Name  = "bob",
                Nopes = "['thing1','thing2']",
            }).Entity;
            UserEntity user2 = context.Users.Add(new UserEntity
            {
                Name  = "lilTimmy",
                Nopes = "['thing3','thing1']",
            }).Entity;

            context.SaveChanges();

            IEnumerable <int> ids = context.Users.Select(u => u.Id);

            // act
            IEnumerable <string> result = await target.GetNopesAsync(ids);

            // assert
            Assert.Contains("thing1", result);
            Assert.Contains("thing2", result);
            Assert.Contains("thing3", result);
            Assert.Equal(3, result.Count());
        }
Exemple #8
0
        public ActionResult AddRestaurant(RestaurantViewModel restaurantViewModel)
        {
            if (!ModelState.IsValid)
            {
                using (var lunchContext = new LunchContext())
                {
                    ViewBag.Cuisines = lunchContext.Cuisines.Select(c => new SelectListItem
                    {
                        Value = c.CuisineId.ToString(),
                        Text  = c.Name
                    }).ToList();

                    return(View("AddEditRestaurant", restaurantViewModel));
                }
            }

            using (var lunchContext = new LunchContext())
            {
                var restaurant = new Restaurant
                {
                    Name      = restaurantViewModel.Name,
                    CuisineId = restaurantViewModel.Cuisine.CuisineId.Value
                };

                lunchContext.Restaurants.Add(restaurant);
                lunchContext.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
        public async void LunchRepository_AddUserToTeamAsync_AddsUserToTeam()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            TeamEntity      team    = context.Teams.Add(new TeamEntity
            {
                Name = "bob's team"
            }).Entity;
            UserEntity user = context.Users.Add(new UserEntity
            {
                Name = "bob"
            }).Entity;

            context.SaveChanges();


            // act
            await target.AddUserToTeamAsync(user.Id, team.Id);

            // assert
            var userTeam = context.UserTeams.First();

            Assert.Equal(team.Id, userTeam.TeamId);
            Assert.Equal(user.Id, userTeam.UserId);
        }
        public async void LunchRepository_GetUserAsync_GetsUser()
        {
            // arrangec
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            List <string>   nopes   = new List <string> {
                "https://goo.gl/pUu7he"
            };
            var addedUserSettings = context.Users.Add(new UserEntity
            {
                Id       = 1,
                GoogleId = "googleID",
                Name     = "test",
                Nopes    = JsonConvert.SerializeObject(nopes),
                Zip      = "90210"
            }).Entity;

            context.SaveChanges();

            // act
            UserDto result = await target.GetUserAsync(addedUserSettings.GoogleId);

            // assert
            Assert.NotNull(result);
            Assert.Equal(JsonConvert.SerializeObject(nopes), JsonConvert.SerializeObject(result.Nopes));
        }
        public async void LunchRepository_RemoveUserFromTeamAsync_RemovesUserFromTeam()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            TeamEntity      team    = context.Teams.Add(new TeamEntity
            {
                Name = "bob's team"
            }).Entity;
            UserEntity user = context.Users.Add(new UserEntity
            {
                Name = "bob"
            }).Entity;

            context.UserTeams.Add(new UserTeamEntity
            {
                UserId = user.Id,
                TeamId = team.Id
            });
            context.SaveChanges();


            // act
            await target.RemoveUserFromTeamAsync(user.Id, team.Id);

            // assert
            Assert.Equal(0, context.UserTeams.Count());
        }
        public async void LunchRepository_AddUserToTeamAsync_ReturnsWithoutErrorWhenComboAlreadyExists()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            TeamEntity      team    = context.Teams.Add(new TeamEntity
            {
                Name = "bob's team"
            }).Entity;
            UserEntity user = context.Users.Add(new UserEntity
            {
                Name = "bob"
            }).Entity;

            context.UserTeams.Add(new UserTeamEntity
            {
                UserId = user.Id,
                TeamId = team.Id
            });
            context.SaveChanges();


            // act
            await target.AddUserToTeamAsync(user.Id, team.Id);

            // assert
            // no exception thrown
        }
        public async void LunchRepository_GetUserByEmailAsync_GetsUser()
        {
            // arrangec
            LunchContext  context = GetContext();
            List <string> nopes   = new List <string> {
                "https://goo.gl/pUu7he"
            };
            var addedUser = context.Users.Add(new UserEntity
            {
                Id       = 1,
                GoogleId = "googleID",
                Name     = "test",
                Email    = "*****@*****.**",
                Nopes    = JsonConvert.SerializeObject(nopes),
                PhotoUrl = "https://gph.is/NYMue5",
                Zip      = "39955",
            }).Entity;

            context.SaveChanges();

            LunchRepository target = new LunchRepository(context);

            // act
            UserDto result = await target.GetUserByEmailAsync(addedUser.Email);

            // assert
            Assert.Equal(addedUser.Email, result.Email);
            Assert.Equal(addedUser.Id, result.Id);
            Assert.Equal(addedUser.Name, result.Name);
            Assert.Equal(addedUser.Nopes, JsonConvert.SerializeObject(result.Nopes));
        }
Exemple #14
0
        public bool ChangePassword(int userId, string oldPassword, string password)
        {
            try
            {
                var user = _dbContext.Users.FirstOrDefault(x => x.Id == userId);
                if (user == null || user.Password != oldPassword)
                {
                    return(false);
                }
                user.Password = password;

                _dbContext.SaveChanges();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        public async void LunchRepository_GetUserAsync_GetsUserWithTeams()
        {
            // arrangec
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            List <string>   nopes   = new List <string> {
                "https://goo.gl/pUu7he"
            };
            var addedUserSettings = context.Users.Add(new UserEntity
            {
                Id       = 1,
                GoogleId = "googleID",
                Name     = "test",
                Nopes    = JsonConvert.SerializeObject(nopes),
                Zip      = "90210"
            }).Entity;

            TeamEntity team1 = context.Teams.Add(new TeamEntity
            {
                Id   = 1,
                Name = "bob's Team",
                Zip  = "38655"
            }).Entity;
            TeamEntity team2 = context.Teams.Add(new TeamEntity
            {
                Id   = 2,
                Name = "lilTimmy's Team",
                Zip  = "38655"
            }).Entity;

            context.UserTeams.Add(new UserTeamEntity
            {
                UserId = 1,
                TeamId = team1.Id
            });
            context.UserTeams.Add(new UserTeamEntity
            {
                UserId = 1,
                TeamId = team2.Id
            });

            context.SaveChanges();

            // act
            UserWithTeamsDto result = await target.GetUserAsync(addedUserSettings.GoogleId);

            // assert
            Assert.NotNull(result);
            Assert.Equal(2, result.Teams.Count());
            Assert.Equal(team1.Name, result.Teams.ElementAt(0).Name);
            Assert.Equal(team2.Name, result.Teams.ElementAt(1).Name);
        }
Exemple #16
0
        public bool EditCategory(CategoryDto dto)
        {
            if (dto.Id > 0)
            {
                var model = _dbContext.Categorys.FirstOrDefault(x => x.Id == dto.Id);
                if (model == null)
                {
                    return(false);
                }
                model.Name = dto.Name;
            }
            else
            {
                var category = new Category()
                {
                    Name = dto.Name,
                };
                _dbContext.Categorys.Add(category);
            }

            return(_dbContext.SaveChanges() > 0);
        }
Exemple #17
0
        public ActionResult Index()
        {
            using (var ctx = new LunchContext())
            {
                var today = DateTime.Now.Date;
                var user  = ctx.Users.SingleOrDefault(u => u.EmailAddress == User.Identity.Name);

                var model = new HomeIndexViewModel {
                    DictatorshipViewModels = new List <HomeIndexDictatorshipViewModel>()
                };

                foreach (var dictatorship in user.Dictatorships)
                {
                    var places            = ctx.Places.Where(p => p.Dictatorship.Id == dictatorship.Id).ToList();
                    var dictatorshipModel = new HomeIndexDictatorshipViewModel()
                    {
                        Id     = dictatorship.Id,
                        Name   = dictatorship.Name,
                        Places =
                            places.Select(p => new PlaceViewModel {
                            ImageUrl = p.ImageUrl, Name = p.Name, Id = p.Id
                        })
                            .ToList()
                    };

                    if (dictatorshipModel.Places.Any())
                    {
                        var selection = ctx.PlaceSelections.SingleOrDefault(s => s.Date == today && s.Place.Dictatorship.Id == dictatorship.Id);

                        if (selection == null)
                        {
                            var rand = new Random();
                            selection = new PlaceSelection {
                                Date = today, Place = places[rand.Next(places.Count)]
                            };
                            ctx.PlaceSelections.Add(selection);

                            ctx.SaveChanges();
                        }

                        var selectedPlace = dictatorshipModel.Places.Single(p => p.Id == selection.Place.Id);
                        selectedPlace.IsSelected        = true;
                        dictatorshipModel.SelectedPlace = selectedPlace.Name;
                    }

                    model.DictatorshipViewModels.Add(dictatorshipModel);
                }

                return(this.View(model));
            }
        }
Exemple #18
0
        public bool OrderingFood(int userId, decimal totalPrice, List <int> foodIds)
        {
            var order = new Order()
            {
                UserId     = userId,
                Price      = totalPrice,
                Status     = "已付款",
                CreateTime = DateTime.Now
            };

            _dbContext.Orders.Add(order);
            _dbContext.SaveChanges();

            var addedIds = new List <Order_Food>();

            foreach (var id in foodIds)
            {
                var added = addedIds.FirstOrDefault(x => x.FoodId == id);
                if (added == null)
                {
                    var orderFood = new Order_Food()
                    {
                        OrderId = order.Id,
                        FoodId  = id,
                        Count   = 1
                    };
                    _dbContext.Order_Foods.Add(orderFood);
                }
                else
                {
                    added.Count++;
                }
            }

            return(_dbContext.SaveChanges() > 0);
        }
Exemple #19
0
        public int AddFoodMenu(FoodMenuDto food, byte[] image, string imageType)
        {
            if (food.Id > 0)
            {
                var model = _dbContext.FoodMenus.FirstOrDefault(x => x.Id == food.Id);
                if (model == null)
                {
                    return(0);
                }

                model.Image       = image;
                model.ImageType   = imageType;
                model.Name        = food.Name;
                model.Description = food.Description;
                model.Type        = food.Type;
                model.Price       = food.Price;
                model.StockCount  = food.StockCount;
            }
            else
            {
                var foodModel = new FoodMenu()
                {
                    Name        = food.Name,
                    Description = food.Description,
                    Type        = food.Type,
                    Image       = image,
                    ImageType   = imageType,
                    Price       = food.Price,
                    StockCount  = food.StockCount,
                    CreateTime  = DateTime.Now
                };
                _dbContext.FoodMenus.Add(foodModel);
            }

            return(_dbContext.SaveChanges());
        }
Exemple #20
0
        public async Task <ActionResult> Order([Bind(/*Exclude ="Lunch", */ Include = "Lunch, WithoutSoup, IsOrder")] List <OrderViewModel> list)
        {
            var currentUser = db.Users.Where(u => u.Email == HttpContext.User.Identity.Name).ToList().ElementAt(0);

            foreach (OrderViewModel ovm in list)
            {
                if (ovm.IsOrder == true)
                {
                    Order order = new Order();
                    order.UserId  = currentUser.UserId;
                    order.LunchId = ovm.Lunch.LunchId;
                    if (ovm.WithoutSoup == true)
                    {
                        order.WithoutSoup = true;
                    }

                    db.Orders.Add(order);
                    await db.SaveChangesAsync();
                }
                db.SaveChanges();
            }

            return(RedirectToAction("MyOrders"));
        }
        public async void LunchRepository_GetNopesAsync_ReturnsEmptyListWithNoUsers()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);

            context.SaveChanges();

            var ids = new List <int>();

            // act
            IEnumerable <string> result = await target.GetNopesAsync(ids);

            // assert
            Assert.Equal(0, result.Count());
        }
Exemple #22
0
        public ActionResult DeleteRestaurant(RestaurantViewModel restaurantViewModel)
        {
            using (var lunchContext = new LunchContext())
            {
                var restaurant = lunchContext.Restaurants.SingleOrDefault(p => p.RestaurantId == restaurantViewModel.RestaurantId);

                if (restaurant != null)
                {
                    lunchContext.Restaurants.Remove(restaurant);
                    lunchContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult DeletePerson(PersonViewModel personViewModel)
        {
            using (var lunchContext = new LunchContext())
            {
                var person = lunchContext.People.SingleOrDefault(p => p.PersonId == personViewModel.PersonId);

                if (person != null)
                {
                    lunchContext.People.Remove(person);
                    lunchContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(new HttpNotFoundResult());
        }
        public async void LunchRepository_TeamNameExistsAsync_ReturnsTrueWhenTeamDoesExist()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            const string    Name    = "bob's team";

            context.Teams.Add(new TeamEntity
            {
                Name = Name
            });
            context.SaveChanges();

            // act
            bool result = await target.TeamNameExistsAsync(Name);

            // assert
            Assert.True(result);
        }
        public ActionResult AddPerson(PersonViewModel personViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("AddEditPerson", personViewModel));
            }

            using (var lunchContext = new LunchContext())
            {
                var person = new Person
                {
                    LastName  = personViewModel.LastName,
                    FirstName = personViewModel.FirstName
                };

                lunchContext.People.Add(person);
                lunchContext.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
        public async void LunchRepository_GetUsersOfTeamAsync_ReturnsListOfUsersFromTeam()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            TeamEntity      team    = context.Teams.Add(new TeamEntity
            {
                Name = "bob's team"
            }).Entity;
            UserEntity user = context.Users.Add(new UserEntity
            {
                Name = "bob"
            }).Entity;
            UserEntity user2 = context.Users.Add(new UserEntity
            {
                Name = "lilTimmy"
            }).Entity;

            context.UserTeams.Add(new UserTeamEntity
            {
                UserId = user.Id,
                TeamId = team.Id
            });
            context.UserTeams.Add(new UserTeamEntity
            {
                UserId = user2.Id,
                TeamId = team.Id
            });
            context.SaveChanges();


            // act
            List <UserDto> result = (await target.GetUsersOfTeamAsync(team.Id)).ToList();

            // assert
            Assert.Equal(2, result.Count());
            Assert.Equal("bob", result[0].Name);
            Assert.Equal("lilTimmy", result[1].Name);
        }
        public ActionResult EditPerson(PersonViewModel personViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("AddEditPerson", personViewModel));
            }

            using (var lunchContext = new LunchContext())
            {
                var person = lunchContext.People.SingleOrDefault(p => p.PersonId == personViewModel.PersonId);

                if (person != null)
                {
                    person.LastName  = personViewModel.LastName;
                    person.FirstName = personViewModel.FirstName;
                    lunchContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult EditFoodPreferences(PersonViewModel personViewModel)
        {
            using (var lunchContext = new LunchContext())
            {
                var person = lunchContext.People.Include("FoodPreferences").SingleOrDefault(p => p.PersonId == personViewModel.PersonId);

                if (person == null)
                {
                    return(new HttpNotFoundResult());
                }

                foreach (var foodPreference in personViewModel.FoodPreferences)
                {
                    if (foodPreference.Rating != -1)
                    {
                        var existingFoodPreference = person.FoodPreferences.SingleOrDefault(fp => fp.CuisineId == foodPreference.Cuisine.CuisineId);
                        if (existingFoodPreference != null)
                        {
                            existingFoodPreference.Rating = foodPreference.Rating;
                        }
                        else
                        {
                            person.FoodPreferences.Add(new FoodPreference
                            {
                                CuisineId = foodPreference.Cuisine.CuisineId.Value,
                                Rating    = foodPreference.Rating
                            });
                        }
                    }
                }

                lunchContext.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
        private void AddBalanceHistory(AddBalanceToUser addBalanceToUser)
        {
            LunchContext db = new LunchContext();

            Balance balance = new Balance();

            balance.Date   = DateTime.Now;
            balance.UserId = addBalanceToUser.UserId;

            if (addBalanceToUser.AddBalance >= 0)
            {
                balance.Income  = addBalanceToUser.AddBalance;
                balance.Comment = "Пополнение счета администратором.";
            }
            else
            {
                balance.Expence = (-1) * addBalanceToUser.AddBalance;
                balance.Comment = "Выдача со счета администратором.";
            }

            db.Balances.Add(balance);
            db.SaveChanges();
            db.Dispose();
        }
Exemple #30
0
 public void Add(T entity)
 {
     _table.Add(entity);
     _context.SaveChanges();
 }