public async Task <ActionResult> AddStaffModuleLink(string uid, int mid)
        {
            var staff = await _repo.GetStaffAsync(uid);

            var module = await _repo.GetModuleAsync(mid);

            if (staff == null || module == null)
            {
                return(NotFound());
            }
            var moduleStudent = new ModuleStaff
            {
                Module = module,
                Staff  = staff
            };
            await _repo.AddModuleStaffAsync(moduleStudent);

            return(Ok());
        }
示例#2
0
        public async Task <ActionResult> PostStaffFile(String class_code, String year)
        {
            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}"));
            }

            /* Truncate all previous staff data */
            await _repo.EmptyStaffData();

            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 key
                    content.Append(sa[i]);
                }
            }

            /* Key set to avoid duplicates in file */
            List <String> keySet = new List <String>();

            /* Staff entities to add */
            List <Staff> staffToAdd = new List <Staff>();

            /* ModuleStaff assocaition entities to add */
            List <ModuleStaff> moduleStaffToAdd = new List <ModuleStaff>();

            String[]  splitCsv = content.ToString().Split("&#£!*");
            const int codeCell = 0;
            const int staff_idCell = 3;
            const int endCell = 3;
            String    module_code = ""; String staff_id = "";

            for (int i = 4; i < splitCsv.Length - 1; i++)
            {
                if (i % 4 == codeCell)
                {
                    module_code = splitCsv[i];
                }
                else if (i % 4 == staff_idCell)
                {
                    staff_id = splitCsv[i];
                }
                if (i % 4 == endCell)
                {
                    Staff staff = null;
                    if (!keySet.Contains(staff_id))
                    {
                        staff     = new Staff();
                        staff.Uid = staff_id;

                        staffToAdd.Add(staff);
                        keySet.Add(staff_id);
                    }
                    else
                    {
                        staff = staffToAdd.Find(s => s.Uid.Equals(staff_id));
                    }

                    /* Get module to associate student with */
                    var module = await _repo.GetModuleAsync(module_code, year, class_code);

                    if (module != null)
                    {
                        /* Create association entity */
                        ModuleStaff ms = new ModuleStaff();
                        ms.Module = module;
                        ms.Staff  = staff;
                        moduleStaffToAdd.Add(ms);
                    }
                    else
                    {
                        /* Do nothing & log inaction because module is not known in database */
                        object[] logParams = { module_code, year, staff_id };
                        _logger.LogWarning("No module exists with code={0} for the year {1}, skipping CSV entry for staff with UID={2}.", logParams);
                    }

                    module_code = "";
                    staff_id    = "";
                }
            }

            /* Save staff entities */
            await _repo.AddStaffAsync(staffToAdd);

            /* Save module staff entities */
            await _repo.AddModuleStaffAsync(moduleStaffToAdd);

            return(Ok());
        }
 public async Task AddModuleStaffAsync(ModuleStaff moduleStaff)
 {
     _context.ModuleStaff.Add(moduleStaff);
     await _context.SaveChangesAsync();
 }