예제 #1
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"));
        }
        public IHttpActionResult Get(int institutionId)
        {
            try
            {
                HttpRequires.IsTrue(institutionId > 0, "A valid institution identifier is required");

                var response = _institutionRepo.Find(institutionId);

                HttpAssert.Success(response);
                HttpAssert.NotNull(response, String.Format("Unable to find an institution with id [{0}]", institutionId));

                return(Ok(response.Result));
            }
            catch (Exception ex)
            {
                if (_logger != null)
                {
                    _logger.Write(ex);
                }
                return(InternalServerError());
            }
        }
예제 #3
0
        public ActionResult BulkImportInstitutionalPrices(InstitutionalPricesViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                int imported = 0, deleted = 0, updated = 0;

                var data = _bulkImporter.Execute(model.File.InputStream);

                var institutionJournals = (from u in data
                                           let institution = institutionRepository.Find(u.Domain)
                                                             where institution != null
                                                             from info in u.Licenses
                                                             let journal = journalRepository.FindByIssn(info.ISSN)
                                                                           where journal != null
                                                                           select new InstitutionJournal
                {
                    DateAdded = DateTime.Now,
                    Link = info.Text,
                    JournalId = journal.Id,
                    UserProfileId = Authentication.CurrentUserId,
                    InstitutionId = institution.Id
                }).ToList();

                // This is gonna be quite an expensive operation... Rethink!
                foreach (var institutionJournal in institutionJournals.Distinct())
                {
                    var existing = institutionJournalRepository.Find(institutionJournal.JournalId, institutionJournal.InstitutionId);

                    if (existing != null)
                    {
                        if (string.IsNullOrWhiteSpace(institutionJournal.Link))
                        {
                            institutionJournalRepository.Delete(existing);
                            deleted++;
                        }
                        else
                        {
                            existing.DateAdded     = DateTime.Now;
                            existing.Link          = institutionJournal.Link;
                            existing.UserProfileId = institutionJournal.UserProfileId;

                            institutionJournalRepository.InsertOrUpdate(existing);

                            updated++;
                        }
                    }
                    else if (!string.IsNullOrWhiteSpace(institutionJournal.Link))
                    {
                        institutionJournalRepository.InsertOrUpdate(institutionJournal);
                        imported++;
                    }
                }

                institutionJournalRepository.Save();

                return(RedirectToAction("BulkImportSuccessful", new { amountImported = imported, amountDeleted = deleted, amountUpdated = updated }));
            }
            catch (ArgumentException invalidFileException)
            {
                ModelState.AddModelError("generalError", invalidFileException.Message);
            }
            catch (DbEntityValidationException)
            {
                //foreach (var eve in e.EntityValidationErrors)
                //{
                //    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",eve.Entry.Entity.GetType().Name, eve.Entry.State);
                //    foreach (var ve in eve.ValidationErrors)
                //    {
                //        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                //    }
                //}
                //throw;
            }
            catch (Exception exception)
            {
                while (exception.InnerException != null)
                {
                    exception = exception.InnerException;
                }

                ModelState.AddModelError("generalError", $"An error has ocurred: {exception.Message}");
            }

            return(View(model));
        }