public async Task <IActionResult> DigiMaster()
        {
            //ClassroomDigiMasterVM model = await repository.FindClassroom(id);
            ClassroomDigiMasterVM model = await repository.FindClassroom(HttpContext.User);

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register(AccountRegisterVM viewModel)
        {
            if (!ModelState.IsValid)
            {
                string user = repository.GetUsername(HttpContext.User);
                string id   = repository.GetUserId(HttpContext.User);

                var model = new HomeIndexVM
                {
                    LoggedIn    = User.Identity.IsAuthenticated,
                    DigiStudent = new HomeIndexFindClassroomVM {
                        Classrooms = await repository.GetAllClassrooms()
                    },                                                                                               //classrooms = repository.FindAllClassrooms();
                    CreateClassroom = new HomeIndexCreateClassroomVM(),
                    DigiMaster      = new HomeIndexLoginVM {
                        Username = user
                    },
                    Register = new AccountRegisterVM {
                        OldClassroomName = repository.GetClassroomNameByAspNetId(id), OldClassroomId = repository.GetClassroomId(id)
                    }
                };
                TempData["username"]      = viewModel.RegUsername;
                TempData["password"]      = viewModel.RegPassword;
                TempData["classroomname"] = viewModel.ClassroomName;
                TempData["message"]       = "Fyll i alla fält.";
                return(RedirectToAction(nameof(Index), model));
            }

            if (!await repository.IsClassroomNameAvailable(viewModel.ClassroomName))
            {
                string user = repository.GetUsername(HttpContext.User);
                string id   = repository.GetUserId(HttpContext.User);

                var model = new HomeIndexVM
                {
                    LoggedIn    = User.Identity.IsAuthenticated,
                    DigiStudent = new HomeIndexFindClassroomVM {
                        Classrooms = await repository.GetAllClassrooms()
                    },                                                                                               //classrooms = repository.FindAllClassrooms();
                    CreateClassroom = new HomeIndexCreateClassroomVM(),
                    DigiMaster      = new HomeIndexLoginVM {
                        Username = user
                    },
                    Register = new AccountRegisterVM {
                        OldClassroomName = repository.GetClassroomNameByAspNetId(id), OldClassroomId = repository.GetClassroomId(id)
                    }
                };
                TempData["username"]      = viewModel.RegUsername;
                TempData["password"]      = viewModel.RegPassword;
                TempData["classroomname"] = viewModel.ClassroomName;
                TempData["message"]       = "Klassrumsnamn är upptaget.";
                return(RedirectToAction(nameof(Index), model));
            }

            var result = await repository.CreateUser(viewModel.RegUsername, viewModel.RegPassword);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    if (error.Code.Contains("User"))
                    {
                        ModelState.AddModelError(nameof(AccountRegisterVM.RegUsername), error.Description);
                    }
                    else
                    {
                        ModelState.AddModelError(nameof(AccountRegisterVM.RegPassword), error.Description);
                    }
                }
                string user = repository.GetUsername(HttpContext.User);
                string id   = repository.GetUserId(HttpContext.User);

                var model = new HomeIndexVM
                {
                    LoggedIn    = User.Identity.IsAuthenticated,
                    DigiStudent = new HomeIndexFindClassroomVM {
                        Classrooms = await repository.GetAllClassrooms()
                    },                                                                                               //classrooms = repository.FindAllClassrooms();
                    CreateClassroom = new HomeIndexCreateClassroomVM(),
                    DigiMaster      = new HomeIndexLoginVM {
                        Username = user
                    },
                    Register = new AccountRegisterVM {
                        OldClassroomName = repository.GetClassroomNameByAspNetId(id), OldClassroomId = repository.GetClassroomId(id)
                    }
                };
                TempData["username"]      = viewModel.RegUsername;
                TempData["password"]      = viewModel.RegPassword;
                TempData["classroomname"] = viewModel.ClassroomName;
                TempData["message"]       = "Användarnamn eller lösenord är inte tillgänglig.";
                return(RedirectToAction(nameof(Index), model));
            }

            ClassroomDigiMasterVM modell = await repository.CreateClassroom(UtilClass.ParseHtml(viewModel.ClassroomName), await repository.GetUserAsync(viewModel.RegUsername));

            return(RedirectToAction("DigiMaster", "Classroom", new { id = await repository.GetClassroomIdByName(viewModel.OldClassroomName) }));
        }