Ejemplo n.º 1
0
        public ActionResult CreateCorner(CornerImportViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            try
            {
                var currentUserId = Authentication.CurrentUserId;

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

                var importResult = new CornersImportedViewModel();

                foreach (var cornerToImport in data)
                {
                    var existingCorner = _cornerRepository.Find(cornerToImport.Name);

                    if (existingCorner == null)
                    {
                        var corner = new Corner
                        {
                            UserProfileId = currentUserId,
                            Name = cornerToImport.Name,
                            CornerJournals = ParseJournalsFromIssns(cornerToImport),
                            LastVisitedOn = DateTime.Now
                        };

                        _cornerRepository.InsertOrUpdate(corner);
                        importResult.ImportedCorners.Add(corner.Name);
                    }
                    else if (existingCorner.UserProfileId == currentUserId)
                    {
                        var newJournals = ParseJournalsFromIssns(cornerToImport).Where(j => existingCorner.CornerJournals.All(x => x.JournalId != j.JournalId)).ToList();

                        if (!newJournals.Any())
                            continue;

                        foreach (var cornerJournal in newJournals)
                            existingCorner.CornerJournals.Add(cornerJournal);

                       importResult.UpdatedCorners.Add(existingCorner.Name);
                    }
                    else if (existingCorner.UserProfileId != currentUserId)
                    {
                        importResult.ExistingCorners.Add(cornerToImport.Name);
                        continue;
                    }

                    _cornerRepository.Save();
                }

                Session[ImportResultSessionKey] = importResult;

                return RedirectToAction("CornersImported");
            }
            catch (ArgumentException invalidFileException)
            {
                ModelState.AddModelError("generalError", invalidFileException.Message);
                return View(model);
            }
            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;
                return View(model);
            }
        }
Ejemplo n.º 2
0
        void IncreaseNumberOfVisitors(Corner corner)
        {
            if (corner == null)
                return;

            corner.NumberOfVisitors++;
            corner.LastVisitedOn = DateTime.Now;
            _cornerRepository.Save();
        }