Example #1
0
        public async Task <ActionResult> Index(HomeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var courseIds = new List <string>();
                using (var reader = new StreamReader(Request.Files["CoursesDataFile"].InputStream))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = await reader.ReadLineAsync();

                        Regex validator = new Regex(@"^[0-9]+$");

                        var id = line.Split(',')[0];

                        if (!validator.IsMatch(id))
                        {
                            ModelState.AddModelError(nameof(HomeViewModel.CoursesDataFile), "There was a problem while reading the input file.  Please make sure that the file chosen contains a comma delimited list and has an extension of csv.");
                            return(View(viewModel));
                        }

                        courseIds.Add(id);
                    }
                }

                var client        = new CoursesClient();
                var accountClient = new AccountsClient();

                try
                {
                    var account = accountClient.Get <Account>(viewModel.CanvasAccountId);

                    foreach (var id in courseIds)
                    {
                        await client.MoveCourse(id, viewModel.CanvasAccountId);
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(nameof(viewModel.CanvasAccountId), ex);
                    return(View(viewModel));
                }

                viewModel.Notify = true;
            }

            return(View(viewModel));
        }