public async Task <ActionResult> AddStudentModuleLink(string uid, int mid) { var student = await _repo.GetStudentAsync(uid); var module = await _repo.GetModuleAsync(mid); if (student == null || module == null) { return(NotFound()); } var moduleStudent = new ModuleStudent { Module = module, Student = student }; await _repo.AddModuleStudentAsync(moduleStudent); return(Ok()); }
public async Task <ActionResult> PostStudentFile(String class_code) { if (!class_code.Equals("AB0") && !class_code.Equals("MU0") && !class_code.Equals("EX1")) { return(BadRequest("Campus class code must be one of {AB0,MU0,EX1}")); } /**/ var file = Request.Form.Files.First(); StringBuilder content = new StringBuilder(); StreamReader sr = new StreamReader(file.OpenReadStream()); while (!sr.EndOfStream) { String s = sr.ReadLine(); string[] sa = Regex.Split(s, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"); for (int i = 0; i < sa.Length; i++) { sa[i] = sa[i] + "&#£!*"; //Unique enough delimeter content.Append(sa[i]); } } /* Key set to avoid duplicates in file */ List <String> keySet = new List <String>(); /* students list to build up */ List <Student> studentsToAdd = new List <Student>(); /* Association list to build up */ List <ModuleStudent> moduleStudentToAdd = new List <ModuleStudent>(); String[] splitCsv = content.ToString().Split("&#£!*"); const int codeCell = 0; const int yearCell = 2; const int emailCell = 7; const int nameCell = 10; const int endCell = 10; String module_code = ""; String year = ""; String user_id = ""; String name = ""; for (int i = 11; i < splitCsv.Length - 1; i++) { if (i % 11 == codeCell) { module_code = splitCsv[i]; } else if (i % 11 == yearCell) { year = splitCsv[i]; } else if (i % 11 == emailCell) { user_id = splitCsv[i]; } else if (i % 11 == nameCell) { name = splitCsv[i]; } if (i % 11 == endCell) { name = Regex.Replace(name, @"(\[|""|\])", ""); String[] nameSplit = name.Split(','); nameSplit[nameSplit.Length - 1] = nameSplit[nameSplit.Length - 1].Remove(0, 1); Student student = new Student(); student.Uid = user_id; student.Forename = nameSplit[nameSplit.Length - 1]; student.Surname = nameSplit[0]; if (!_repo.StudentExists(user_id)) { if (!keySet.Contains(user_id)) { studentsToAdd.Add(student); keySet.Add(user_id); /* Get module to associate student with */ var module = await _repo.GetModuleAsync(module_code, year, class_code); if (module != null) { ModuleStudent ms = new ModuleStudent(); ms.Module = module; ms.Student = student; moduleStudentToAdd.Add(ms); } else { /* Do nothing & log inaction because module is not known in database */ object[] logParams = { module_code, year, user_id }; _logger.LogWarning("No module exists with code={0} for the year {1}, skipping CSV entry for student with UID={2}.", logParams); } } } else { var module = await _repo.GetModuleAsync(module_code, year, class_code); if (module != null) { ModuleStudent ms = new ModuleStudent(); ms.Module = module; ms.Student = await _repo.GetStudentAsync(user_id); if (await _repo.GetModuleStudentAsync(module.Id, student.Uid) == null) { moduleStudentToAdd.Add(ms); } } } module_code = ""; year = ""; user_id = ""; name = ""; } } /* Save student entities */ await _repo.AddStudentsAsync(studentsToAdd); /* Save module student entities */ await _repo.AddModuleStudentAsync(moduleStudentToAdd); return(Ok()); }
public async Task AddModuleStudentAsync(ModuleStudent moduleStudents) { _context.ModuleStudents.Add(moduleStudents); await _context.SaveChangesAsync(); }