private void LoadElectionInfo() { var electionNode = _xmlRoot.SelectSingleNode("t:election", _nsm) as XmlElement; _election = new Election(); electionNode.CopyAttributeValuesTo(_election); // reset Guid to a new guid _election.ElectionGuid = _electionGuid = Guid.NewGuid(); var newName = _election.Name; var matchingNums = Db.Election .Where(e => e.Name == newName || e.Name.StartsWith(newName)) .ToList() .Select(e => e.Name.Replace(newName, "").Split(' ').Last().AsInt()) .ToList(); if (matchingNums.Any()) { if (matchingNums.All(n => n != 0)) { // don't need to rename } else { var last = matchingNums.Max(n => n); _election.Name = $"{newName} - {last + 1}"; } } Db.BulkInsert(new[] { _election }); _hub.StatusUpdate("Loading <b>{0}</b>".FilledWith(_election.Name)); // set current person as owner var join = new JoinElectionUser { ElectionGuid = _electionGuid, UserId = UserSession.UserGuid }; //Db.JoinElectionUser.Add(join); Db.BulkInsert(new[] { join }); }
//public JsonResult Copy(Guid guidOfElectionToCopy) //{ // if (UserSession.IsGuestTeller) // { // return new // { // Success = false, // Message = "Not authorized" // }.AsJsonResult(); // } // var election = Db.Election.SingleOrDefault(e => e.ElectionGuid == guidOfElectionToCopy); // if (election == null) // { // return new // { // Success = false, // Message = "Not found" // }.AsJsonResult(); // } // // copy in SQL // var result = Db.CloneElection(election.ElectionGuid, UserSession.LoginId).SingleOrDefault(); // if (result == null) // { // return new // { // Success = false, // Message = "Unable to copy" // }.AsJsonResult(); // } // if (!result.Success.AsBoolean()) // { // return new // { // Success = false, // Message = "Sorry: " + result.Message // }.AsJsonResult(); // } // election = Db.Election.SingleOrDefault(e => e.ElectionGuid == result.NewElectionGuid); // if (election == null) // { // return new // { // Success = false, // Message = "New election not found" // }.AsJsonResult(); // } // UserSession.CurrentElection = election; // return new // { // Success = true, // election.ElectionGuid // }.AsJsonResult(); //} public JsonResult Create() { if (UserSession.IsGuestTeller) { return(new { Success = false, Message = "Not authorized" }.AsJsonResult()); } // create an election for this ID // create a default Location // assign all of these to this person and computer UserSession.ResetWhenSwitchingElections(); var election = new Election(); // { // Convenor = "[Convener]", // correct spelling is Convener. DB field name is wrong. // ElectionGuid = Guid.NewGuid(), // Name = "[New Election]", // ElectionType = "LSA", // ElectionMode = ElectionMode.Normal, // TallyStatus = ElectionTallyStatusEnum.NotStarted, // NumberToElect = 9, // NumberExtra = 0, // CanVote = CanVoteOrReceive.All, // CanReceive = CanVoteOrReceive.All // }; Db.Election.Add(election); Db.SaveChanges(); UserSession.CurrentElectionGuid = election.ElectionGuid; // new ElectionStatusSharer().SetStateFor(election); var join = new JoinElectionUser { ElectionGuid = election.ElectionGuid, UserId = UserSession.UserGuid }; Db.JoinElectionUser.Add(join); var mainLocation = new Location { Name = "Main Location", LocationGuid = Guid.NewGuid(), ElectionGuid = election.ElectionGuid, SortOrder = 1 }; Db.Location.Add(mainLocation); Db.SaveChanges(); new LocationCacher(Db).UpdateItemAndSaveCache(mainLocation); // var computerModel = new ComputerModel(); // computerModel.CreateComputerForMe(); return(new { Success = true }.AsJsonResult()); }