Exemple #1
0
        public ActionResult AddInstitution(UpsertViewModel model)
        {
            ModelState["File"].Errors.Clear();

            if (institutionRepository.Exists(model.Name))
                ModelState.AddModelError("Name", $"There is already an institution with the name \"{model.Name}\".");

            if (institutionRepository.DomainExists(model.ShortName))
                ModelState.AddModelError("ShortName", $"There is already an institution with the domain \"{model.ShortName}\".");

            if (!ModelState.IsValid)
                return View(model);

            institutionRepository.InsertOrUpdate(model.ToInstitution());
            institutionRepository.Save();

            return View("AddedInstitution", new InstitutionsAddedViewModel
            {
                AmountImported = 1
            });
        }
Exemple #2
0
        ActionResult FetchUpsertViewModel(int id)
        {
            var institution = institutionRepository.Find(id);

            if(institution == null)
                return new HttpNotFoundResult();

            var model = new UpsertViewModel
            {
                Id = id,
                Name = institution.Name,
                ShortName = institution.ShortName
            };

            return View(model);
        }
Exemple #3
0
        public ActionResult DeleteInstitution(UpsertViewModel model)
        {
            if (!model.Id.HasValue)
                return new HttpNotFoundResult();

            var institution = institutionRepository.Find(model.Id.Value);

            if (institution == null)
                return new HttpNotFoundResult();

            if (institution.UserProfiles.Any())
            {
                ModelState["File"].Errors.Clear();
                ModelState.AddModelError("noetempty", $"There are {institution.UserProfiles.Count} users registered under this domain. Institution {institution.Name} cannot be deleted.");
                return View("DeleteInstitution", model);
            }

            institutionRepository.Delete(institution);
            institutionRepository.Save();

            return RedirectToAction("InstitutionDeleted");
        }
Exemple #4
0
        public ActionResult EditInstitution(UpsertViewModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Name) || string.IsNullOrWhiteSpace(model.ShortName))
                return View(model);

            institutionRepository.InsertOrUpdate(model.ToInstitution());
            institutionRepository.Save();

            return RedirectToAction("Index", "Institutions");
        }
Exemple #5
0
        public ActionResult BulkAddInstitution(UpsertViewModel model)
        {
            if (model.File == null)
                return View("AddInstitution", model);

            try
            {
                var data = _institutionImporter.Execute(model.File.InputStream);

                List<Institution> invalidRecords = new List<Institution>(), existingNames = new List<Institution>(), existingDomains = new List<Institution>();
                var imported = 0;

                foreach (var institution in data)
                {
                    if (string.IsNullOrWhiteSpace(institution.Name) || string.IsNullOrWhiteSpace(institution.ShortName))
                    {
                        invalidRecords.Add(institution);
                        continue;
                    }

                    if (institutionRepository.Exists(institution.Name))
                    {
                        existingNames.Add(institution);
                        continue;
                    }

                    if (institutionRepository.DomainExists(institution.ShortName))
                    {
                        existingDomains.Add(institution);
                        continue;
                    }

                    institutionRepository.InsertOrUpdate(institution);
                    institutionRepository.Save();

                    imported++;
                }

                return View("AddedInstitution", new InstitutionsAddedViewModel
                {
                    AmountImported = imported,
                    Invalid = invalidRecords,
                    ExistingNames = existingNames,
                    ExistingDomains = existingDomains
                });
            }
            catch (ArgumentException invalidFileException)
            {
                ModelState.AddModelError("generalError", invalidFileException.Message);
                return View("AddInstitution", model);
            }
        }