Exemplo n.º 1
0
        public JsonResult EditLocation(int id, string text, bool allowModifyOnline = false)
        {
            if (text == OnlineLocationName && !allowModifyOnline)
            {
                return(new
                {
                    Success = false,
                    Status = $"Cannot name a location as \"{OnlineLocationName}\""
                }.AsJsonResult());
            }

            if (text == ImportedLocationName)
            {
                return(new
                {
                    Success = false,
                    Status = $"Cannot name a location as \"{ImportedLocationName}\""
                }.AsJsonResult());
            }

            var locationCacher = new LocationCacher(Db);

            var location = locationCacher.AllForThisElection.SingleOrDefault(l => l.C_RowId == id);
            var changed  = false;

            if (location == null)
            {
                location = new Location
                {
                    ElectionGuid = UserSession.CurrentElectionGuid,
                    LocationGuid = Guid.NewGuid()
                };
                Db.Location.Add(location);
                changed = true;
            }
            else
            {
                if (location.IsVirtual && !allowModifyOnline)
                {
                    return(new
                    {
                        Success = false,
                        Status = "Cannot edit Online location"
                    }.AsJsonResult());
                }

                Db.Location.Attach(location);
            }

            int    locationId;
            string locationText;
            string status;
            var    success = false;

            if (text.HasNoContent() && location.C_RowId > 0)
            {
                // deleting this location

                // don't delete last location
                if (location.IsTheOnlineLocation && allowModifyOnline ||
                    GetLocations_Physical().Count > 1)
                {
                    // delete existing if we can
                    if (!IsLocationInUse(location.LocationGuid))
                    {
                        Db.Location.Remove(location);
                        Db.SaveChanges();
                        locationCacher.RemoveItemAndSaveCache(location);

                        status       = "Deleted";
                        success      = true;
                        locationId   = 0;
                        locationText = "";
                    }
                    else
                    {
                        status       = "Cannot deleted this location because it has Ballots recorded in it";
                        locationId   = location.C_RowId;
                        locationText = location.Name;
                    }
                }
                else
                {
                    // only one
                    status       = "At least one location is required";
                    locationId   = location.C_RowId;
                    locationText = location.Name;
                }
            }
            else if (text.HasContent())
            {
                locationText = location.Name = text;
                locationId   = location.C_RowId; // may be 0 if new

                changed = true;
                status  = "Saved";
            }
            else
            {
                status       = "Nothing to save";
                locationId   = 0;
                locationText = "";
                success      = true;
                changed      = false;
            }

            if (changed)
            {
                Db.SaveChanges();

                locationId = location.C_RowId;
                locationCacher.UpdateItemAndSaveCache(location);
                success = true;
            }

            return(new
            {
                // returns 0 if deleted or not created
                Id = locationId,
                Text = locationText,
                Success = success,
                Status = status
            }.AsJsonResult());
        }
Exemplo n.º 2
0
        public JsonResult EditLocation(int id, string text)
        {
            var locationCacher = new LocationCacher(SharedDbContext);

            var location = locationCacher.AllForThisElection.SingleOrDefault(l => l.Id == id);
            var changed  = false;

            if (location == null)
            {
                location = new Location
                {
                    ElectionGuid = UserSession.CurrentElectionGuid,
                    LocationGuid = Guid.NewGuid()
                };
                SharedDbContext.Location.Add(location);
                changed = true;
            }
            else
            {
                SharedDbContext.Location.Attach(location);
            }

            int    locationId;
            string locationText;
            string status;
            var    success = false;

            if (text.HasNoContent() && location.Id > 0)
            {
                // don't delete last location
                if (AllLocations.Count() > 1)
                {
                    // delete existing if we can
                    var used = new BallotCacher(SharedDbContext).AllForThisElection.Any(b => b.LocationGuid == location.LocationGuid);
                    if (!used)
                    {
                        SharedDbContext.Location.Remove(location);
                        SharedDbContext.SaveChanges();
                        locationCacher.RemoveItemAndSaveCache(location);

                        status       = "Deleted";
                        success      = true;
                        locationId   = 0;
                        locationText = "";
                    }
                    else
                    {
                        status       = "Cannot deleted this location because it has Ballots recorded in it";
                        locationId   = location.Id;
                        locationText = location.Name;
                    }
                }
                else
                {
                    // only one
                    status       = "At least one location is required";
                    locationId   = location.Id;
                    locationText = location.Name;
                }
            }
            else if (text.HasContent())
            {
                locationText = location.Name = text;
                locationId   = location.Id; // may be 0 if new

                changed = true;
                status  = "Saved";
            }
            else
            {
                status       = "Nothing to save";
                locationId   = 0;
                locationText = "";
                success      = true;
                changed      = false;
            }

            if (changed)
            {
                SharedDbContext.SaveChanges();

                locationId = location.Id;
                locationCacher.UpdateItemAndSaveCache(location);
                success = true;
            }

            return(new
            {
                // returns 0 if deleted or not created
                Id = locationId,
                Text = locationText,
                Success = success,
                Status = status
            }.AsJsonResult());
        }