コード例 #1
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()));
        }
コード例 #2
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"));
        }
コード例 #3
0
        public async void LunchRepository_UpdateUserAsync_UpdatesUser()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            UserEntity      user    = context.Users.Add(new UserEntity()
            {
                Name  = "Tahra Dactyl",
                Nopes = "[]",
            }).Entity;
            await context.SaveChangesAsync();

            string        name  = "Paul R. Baer";
            List <string> nopes = new List <string> {
                "Chum Bucket", "Jimmy Pesto's Pizzaria"
            };

            // act
            await target.UpdateUserAsync(user.Id, name, nopes, "90210");

            // assert
            UserEntity updatedUser = await context.Users.FirstOrDefaultAsync(u => u.Id == user.Id);

            Assert.Equal(name, updatedUser.Name);
            Assert.Equal(JsonConvert.SerializeObject(nopes), updatedUser.Nopes);
            Assert.Equal("90210", updatedUser.Zip);
        }
コード例 #4
0
        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
        }
コード例 #5
0
        public ActionResult Details(int id)
        {
            if (id == 0)
            {
                // No id specified, return to home page.
                return(this.RedirectToAction("Index", "Home"));
            }

            using (var ctx = new LunchContext())
            {
                var dictatorship = ctx.Dictatorships.SingleOrDefault(d => d.Id == id);

                if (dictatorship == null)
                {
                    // Dictatorship not found! Panic!
                    // Todo: Handle this with some kind of "dictatorship not found" error message
                    return(this.RedirectToAction("Index", "Home"));
                }

                var model = new DictatorshipViewModel
                {
                    Id       = dictatorship.Id,
                    Name     = dictatorship.Name,
                    ImageUrl = dictatorship.ImageUrl
                };

                return(this.View(model));
            }
        }
コード例 #6
0
        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));
        }
コード例 #7
0
        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());
        }
コード例 #8
0
        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);
        }
コード例 #9
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());
        }
コード例 #10
0
        public ActionResult RestaurantEdit(int id)
        {
            using (var lunchContext = new LunchContext())
            {
                ViewBag.Cuisines = lunchContext.Cuisines.Select(c => new SelectListItem
                {
                    Value = c.CuisineId.ToString(),
                    Text  = c.Name
                }).ToList();

                var restaurant = lunchContext.Restaurants.SingleOrDefault(p => p.RestaurantId == id);
                if (restaurant != null)
                {
                    var restaurantViewModel = new RestaurantViewModel
                    {
                        RestaurantId = restaurant.RestaurantId,
                        Name         = restaurant.Name,
                        Cuisine      = new CuisineViewModel
                        {
                            CuisineId = restaurant.CuisineId,
                            Name      = restaurant.Cuisine.Name
                        }
                    };

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

            return(new HttpNotFoundResult());
        }
コード例 #11
0
        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());
        }
コード例 #12
0
        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));
        }
コード例 #13
0
        protected override void Dispose(bool disposing)
        {
            if (disposing && this.context != null)
            {
                this.context.Dispose();
                this.context = null;
            }

            base.Dispose(disposing);
        }
コード例 #14
0
        public void LunchRepository_Ctor_ReturnsRepository()
        {
            // arrange
            LunchContext context = GetContext();

            // act
            LunchRepository repo = new LunchRepository(context);

            // assert
            Assert.NotNull(repo);
        }
コード例 #15
0
        public async void LunchRepository_GetUsersOfTeamAsync_ReturnsNullWhenTeamNotFound()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);

            // act
            IEnumerable <UserDto> result = await target.GetUsersOfTeamAsync(1);

            // assert
            Assert.Null(result);
        }
コード例 #16
0
        public async void LunchRepository_GetUserAsync_ReturnsNullWhenUserNotFound()
        {
            // arrangec
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);

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

            // assert
            Assert.Null(result);
        }
コード例 #17
0
        public async void LunchRepository_GetTeamAsync_ReturnsNullIfTeamDoesNotExist()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);

            // act
            TeamDto result = await target.GetTeamAsync(8888);

            // assert
            Assert.Null(result);
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        public async void LunchRepository_TeamNameExistsAsync_ReturnsFalseWhenTeamDoesNotExist()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            const string    Name    = "bob's team";

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

            // assert
            Assert.False(result);
        }
コード例 #20
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));
            }
        }
コード例 #21
0
        public ActionResult RestaurantAdd()
        {
            using (var lunchContext = new LunchContext())
            {
                ViewBag.Cuisines = lunchContext.Cuisines.Select(c => new SelectListItem
                {
                    Value = c.CuisineId.ToString(),
                    Text  = c.Name
                }).ToList();
            }

            var restaurantViewModel = new RestaurantViewModel();

            return(View("AddEditRestaurant", restaurantViewModel));
        }
コード例 #22
0
        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());
        }
コード例 #23
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());
        }
コード例 #24
0
        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());
        }
コード例 #25
0
        public async void LunchRepository_CreateTeamAsync_CreatesATeam()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            const string    Name    = "bob's team";
            const string    Zip     = "90210";

            // act
            int result = await target.CreateTeamAsync(Name, Zip);

            // assert
            var team = context.Teams.First();

            Assert.Equal(Name, team.Name);
            Assert.Equal(Zip, team.Zip);
            Assert.Equal(team.Id, result);
        }
コード例 #26
0
        public async Task <ActionResult> Activate(AccountActivateViewModel model)
        {
            var user =
                await LunchContext.Users.SingleOrDefaultAsync(u => u.PasswordChangeSecret == model.PasswordChangeSecret);

            if (user == null)
            {
                return(this.View("ActivateSecretInvalid"));
            }

            user.Password             = HashHelper.GetHash(model.Password);
            user.PasswordChangeSecret = null;

            await LunchContext.SaveChangesAsync();

            this.LogInInternal(user.EmailAddress);

            return(this.RedirectToAction("Index", "Home"));
        }
コード例 #27
0
        public async void LunchRepository_CreateUserAsync_ReturnsId()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);

            string googleId = "googleId";
            string email    = "*****@*****.**";
            string name     = "goodname";
            string photoUrl = "photo";

            // act
            var result = await target.CreateUserAsync(googleId, email, name, photoUrl);

            // assert
            UserEntity newUser = context.Users.Where(u => u.Name == name).FirstOrDefault();

            Assert.Equal(newUser.Id, result.Id);
        }
コード例 #28
0
        public async void LunchRepository_GetUserAsync_ReturnsSpecifiedUser()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            UserEntity      user    = context.Users.Add(new UserEntity()
            {
                Name  = "Tahra Dactyl",
                Nopes = "[]",
            }).Entity;
            await context.SaveChangesAsync();

            // act
            UserDto result = await target.GetUserAsync(user.Id);

            // assert
            Assert.Equal(user.Name, result.Name);
            Assert.Equal(user.Nopes, JsonConvert.SerializeObject(result.Nopes));
        }
コード例 #29
0
        public async void LunchRepository_GetTeamAsync_ReturnsSpecifiedTeam()
        {
            // arrange
            LunchContext    context = GetContext();
            LunchRepository target  = new LunchRepository(context);
            TeamEntity      team    = context.Teams.Add(new TeamEntity()
            {
                Name = "Tahra Dactyls",
                Zip  = "90210",
            }).Entity;
            await context.SaveChangesAsync();

            // act
            TeamDto result = await target.GetTeamAsync(team.Id);

            // assert
            Assert.Equal(team.Name, result.Name);
            Assert.Equal(team.Zip, result.Zip);
        }
コード例 #30
0
        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);
        }