Ejemplo n.º 1
0
        public ActionResult Create(Cluster cluster)
        {
            if (String.IsNullOrWhiteSpace(cluster.Name))
            {
                ModelState.AddModelError("Name", "Cluster name has to be set!");
                return(View("Create", cluster));
            }

            var newCluster = new Cluster
            {
                Name = cluster.Name,
            };

            if (cluster.Id == 0)
            {
                _clusterSvc.Create(cluster);
            }
            else
            {
                _clusterSvc.Update(cluster);
            }

            return(View("List", _clusterSvc.GetList()
                        .Select(CreateViewModel)));
        }
Ejemplo n.º 2
0
        public ActionResult Create(Cluster cluster)
        {
            if (ModelState.IsValid)
            {
                bool clustedExists = _clusterSvc.GetList().Any(t => t.Name.Equals(cluster.Name, StringComparison.CurrentCultureIgnoreCase));

                if (!clustedExists)
                {
                    if (cluster.Id == 0)
                    {
                        _clusterSvc.Create(cluster);
                    }
                    else
                    {
                        _clusterSvc.Update(cluster);
                    }

                    return(View("List", _clusterSvc.GetList().Select(CreateViewModel)));
                }
                else
                {
                    ModelState.AddModelError("Name", string.Format("The name '{0}' is already in use.", cluster.Name));
                }
            }

            return(View(cluster));
        }
Ejemplo n.º 3
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));
        }