コード例 #1
0
        public async Task <IActionResult> RegisterConfirmed(int?variantId, IList <int> sIds)
        {
            if (variantId == null || !sIds.Any())
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                var variant = (await _variantRepository.GetVariantById(variantId.Value));
                if (variant == null)
                {
                    return(NotFound());
                }
                var students = await Task.WhenAll(sIds.Select(id =>
                                                              _studentRepository.GetStudentById(id)
                                                              ));

                if (students != null)
                {
                    var studentsPostDto = students.Select(StudentViewModel.CreateStudentPostDto).ToList();
                    var registrationDto = VariantViewModel.CreateRegistrationDto(variant);
                    var registeredIds   = await WisClient.RegisterStudents(studentsPostDto, registrationDto);

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

                        await _studentRepository.SaveChanges();
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        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());
        }