public async Task <IActionResult> EditConfirmed(int?variantId, IList <int> sIds)
        {
            if (!sIds.Any())
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                var variant = (variantId == null)
                                        ? null : (await _variantRepository.GetVariantById(variantId.Value));
                await Task.WhenAll(sIds.Select(id =>
                                               _studentRepository.UpdateStudent(id, variant)
                                               ));

                await _studentRepository.SaveChanges();
            }
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> IndexPost(int?activeVariantId)
        {
            var variants = await _variantRepository.GetAllVariants();

            var variantsVm = variants.Select(VariantViewModel.CreateVariantVm);

            if (activeVariantId != null)
            {
                var activeVariant = await _variantRepository.GetVariantById(activeVariantId.Value);

                if (activeVariant == null)
                {
                    return(NotFound());
                }
                VariantViewModel.ActiveVariantId = activeVariantId;
                WisClient.ActiveCourseId         = activeVariant.WisCourseId;
            }
            ViewBag.activeVariantId = VariantViewModel.ActiveVariantId;
            ViewBag.autoRegister    = WisClient.AutoRegister;
            return(View(variantsVm));
        }
        public async Task <ActionResult <string> > CreateStudent([FromForm] string uid)
        {
            if (uid != null)
            {
                var auth = WisClient.AuthBase64;
                if (string.IsNullOrEmpty(auth))
                {
                    return(NotFound());
                }
                Debug.WriteLine("Incoming ID: " + uid);
                var studentRfidDto = await WisClient.GetStudentByRfidUid(uid);

                int?activeVariantId = VariantViewModel.ActiveVariantId;
                if (activeVariantId == null)
                {
                    return("Variant!");
                }
                int variantId = activeVariantId.Value;
                var variant   = await _variantRepository.GetVariantById(variantId);

                var studentsDto = await WisClient.GetStudents(variant.WisCourseId);

                var studentDto = studentsDto?.Single(s => s.Login == studentRfidDto.Login);
                if (studentDto == null)
                {
                    return("Not found");
                }
                if (_studentRepository.StudentExists(studentDto.Id))
                {
                    return("Registered");
                }
                try
                {
                    var student        = StudentViewModel.CreateStudent(studentDto);
                    var studentPostDto = StudentViewModel.CreateStudentPostDto(student);
                    await _studentRepository.InsertStudent(student);

                    await _studentRepository.AddStudentToVariant(student.Id, variantId);

                    await _studentRepository.SaveChanges();

                    if (WisClient.AutoRegister)
                    {
                        var registerStudentsDto = new List <StudentPostDto> {
                            studentPostDto
                        };
                        var registrationDto = VariantViewModel.CreateRegistrationDto(variant);
                        var registeredIds   = await WisClient.RegisterStudents(registerStudentsDto, registrationDto);

                        foreach (var registeredId in registeredIds)
                        {
                            await _studentRepository.RegisterStudent(registeredId);

                            await _studentRepository.SaveChanges();
                        }
                    }
                }
                catch (DbUpdateConcurrencyException) { }

                return(studentRfidDto.Login);
            }

            return(NotFound());
        }