コード例 #1
0
        internal async Task AddOrUpdateLocation()
        {
            var service = ServicesManager.SelfService;

            try
            {
                if (CurrentLocation == null || string.IsNullOrEmpty(CurrentLocation.Name))
                {
                    MainWindow.Instance.AddNotification(new BaseResponse()
                    {
                        Error   = ErrorCode.ValidationError,
                        Message = "Name and details cannot be empty."
                    });
                    return;
                }

                if (AllLocations.Count(x => x.Name == CurrentLocation.Name) > 1)
                {
                    MainWindow.Instance.AddNotification(new BaseResponse()
                    {
                        Error   = ErrorCode.ValidationError,
                        Message = "Client name already exists."
                    });
                    return;
                }

                var locationsResponse = await service.UpdateLocation(Token, CurrentLocation);

                MainWindow.Instance.AddNotification(locationsResponse ?? new BaseResponse()
                {
                    Error = ErrorCode.InternalError, Message = "Failed to receive response from host."
                });
            }
            catch (ServiceException ex)
            {
                //TODO: HIGH Add logging
                MainWindow.Instance.AddNotification(ex);
            }
        }
コード例 #2
0
ファイル: LocationModel.cs プロジェクト: glittle/TallyJ4
        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());
        }