Пример #1
0
        public ActionResult CheckinResource(VolunteerViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Index", GetDefaultViewModel(model)));
            }

            try
            {
                var person = _volunteerSvc.FindByUserId(_webSecurity.CurrentUserId);
                if (person == null)
                {
                    throw new ArgumentException(
                              "The logged in user is either the administrator or does not have a valid account for joining a crisis.");
                }
                _disasterSvc.AddResourceCheckIn(person.Organization, person, model.SelectedDisasterId, model.Description,
                                                model.Qty, model.SelectedResourceTypeId, model.ResourceStartDate, model.ResourceEndDate, model.Location);

                return(Redirect("/Home"));
            }
            catch (ArgumentException ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            var modelToReturn = GetDefaultViewModel();

            modelToReturn.SelectedDisasterId = model.SelectedDisasterId;
            modelToReturn.SelectedStartDate  = model.SelectedStartDate;
            modelToReturn.SelectedEndDate    = model.SelectedEndDate;
            modelToReturn.SelectedClusterId  = model.SelectedClusterId;
            modelToReturn.Description        = model.Description;
            modelToReturn.Qty = model.Qty;
            modelToReturn.SelectedResourceTypeId = model.SelectedResourceTypeId;
            modelToReturn.ResourceStartDate      = model.ResourceStartDate;
            modelToReturn.ResourceEndDate        = model.ResourceEndDate;
            modelToReturn.Location = model.Location;

            if (model.SelectedDisasterId != 0)
            {
                modelToReturn.DisasterClusters
                    = _disasterClusterSvc.GetClustersForADisaster(model.SelectedDisasterId);
            }

            return(View("Index", modelToReturn));
        }
Пример #2
0
        public ActionResult Create(DisasterViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (!model.SelectedDisasterClusters.Exists(x => x.Selected == true))
                    {
                        ModelState.AddModelError("Name", "You have to include at least one Cluster!");
                        return(View("Create", model));
                    }

                    var disaster = new Disaster
                    {
                        Id       = model.Id,
                        Name     = model.Name,
                        IsActive = model.IsActive
                    };

                    var newClusterList = (from selectedList in model.SelectedDisasterClusters
                                          join clusterList in _clusterSvc.GetList()
                                          on selectedList.Id equals clusterList.Id into outerList
                                          from clusterList in outerList.DefaultIfEmpty()
                                          where clusterList == null
                                          select new Cluster {
                        Name = selectedList.Name, Id = selectedList.Id
                    }).ToList();

                    foreach (var item in newClusterList)
                    {
                        _clusterSvc.Create(item);
                    }

                    if (model.Id == -1)
                    {
                        _disasterSvc.Create(disaster);

                        var id = _disasterSvc.GetList().Where(x => x.Name == model.Name).FirstOrDefault().Id;

                        foreach (var item in model.SelectedDisasterClusters)
                        {
                            if (item.Selected == true)
                            {
                                if (newClusterList.Exists(x => x.Name == item.Name))
                                {
                                    _disasterClusterSvc.Create(new DisasterCluster {
                                        Id = -1, DisasterId = id, Cluster = newClusterList.Find(x => x.Name == item.Name)
                                    });
                                }
                                else
                                {
                                    _disasterClusterSvc.Create(new DisasterCluster {
                                        Id = -1, DisasterId = id, ClusterId = item.Id
                                    });
                                }
                            }
                        }
                    }
                    else
                    {
                        _disasterSvc.Update(disaster);

                        var disasterClusterList = _disasterClusterSvc.GetClustersForADisaster(model.Id);


                        foreach (var item in model.SelectedDisasterClusters)
                        {
                            if (item.Selected == true)
                            {
                                if (newClusterList.Exists(x => x.Name == item.Name))
                                {
                                    _disasterClusterSvc.Create(new DisasterCluster {
                                        Id = -1, DisasterId = model.Id, Cluster = newClusterList.Find(x => x.Name == item.Name)
                                    });
                                }
                                else
                                {
                                    _disasterClusterSvc.Create(new DisasterCluster {
                                        Id = -1, DisasterId = model.Id, ClusterId = item.Id
                                    });
                                }
                            }
                            else
                            {
                                if (disasterClusterList.Exists(x => x.ClusterId == item.Id))
                                {
                                    _disasterClusterSvc.Remove(disasterClusterList.Find(x => x.ClusterId == item.Id));
                                }
                            }
                        }
                    }
                }
                catch (DisasterAlreadyExistsException)
                {
                    ModelState.AddModelError("Name", "A Disaster already exists with that Name!");
                    return(View("Create", model));
                }

                return(Redirect("/Disaster/List"));
            }
            return(View("Create", model));
        }