예제 #1
0
        public async Task <IActionResult> Register(RegisterInputModel registerInputModel)
        {
            if (ModelState.IsValid)
            {
                if (registerInputModel.Password == registerInputModel.ConfirmPassword)
                {
                    var user = new GameInfoUser()
                    {
                        UserName = registerInputModel.Username,
                        Email    = registerInputModel.Email
                    };
                    var result = await _userManager.CreateAsync(user, registerInputModel.Password);

                    if (result.Succeeded)
                    {
                        IdentityResult resultRole;
                        if (_userManager.Users.Count() == 1)
                        {
                            resultRole = await _userManager.AddToRolesAsync(user, new string [] { "Admin", "User" });
                        }
                        else
                        {
                            resultRole = await _userManager.AddToRoleAsync(user, "User");
                        }
                        if (resultRole.Succeeded)
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                }
            }
            return(View(registerInputModel));
        }
예제 #2
0
        public void Add_SavesToDatabase()
        {
            var options = new DbContextOptionsBuilder <GameInfoContext>()
                          .UseInMemoryDatabase(databaseName: "AddGuide_ToDb")
                          .Options;

            using (var context = new GameInfoContext(options))
            {
                var guideToAdd = new AddGuideInputModel()
                {
                    GuideTitle = "Guide", GuideContent = "Content"
                };

                var user = new GameInfoUser()
                {
                    UserName = "******"
                };

                var service = new GuidesService(context);
                service.Add(guideToAdd, user);

                var expectedGuide = new Guide()
                {
                    Title = guideToAdd.GuideTitle, Content = guideToAdd.GuideContent
                };

                Assert.NotEmpty(context.Guides);
                Assert.Equal(expectedGuide.Title, context.Guides.First().Title);
                Assert.Equal(expectedGuide.Content, context.Guides.First().Content);
            }
        }
예제 #3
0
        public void Add(AddGuideInputModel model, GameInfoUser currentUser)
        {
            var guide = new Guide
            {
                Title   = model.GuideTitle,
                Content = model.GuideContent
            };

            guide.Creator = currentUser;

            this._db.Guides.Add(guide);
            this._db.SaveChanges();
        }