Esempio n. 1
0
        public ActionResult Delete()
        {
            var target = Db.Election.SingleOrDefault(e => e.ElectionGuid == _electionGuid);

            if (target == null)
            {
                return(new
                {
                    Deleted = false,
                    Error = "Cannot find specified election."
                }.AsJsonResult());
            }
            var electionName = target.Name;

            var user = Db.Users.SingleOrDefault(u => u.UserName == UserSession.LoginId);

            if (user == null || !Db.JoinElectionUser.Any(j => j.ElectionGuid == _electionGuid && j.UserId == user.UserId))
            {
                return(new
                {
                    Deleted = false,
                    Error = "Specified election not accessible to you."
                }.AsJsonResult());
            }

            // delete everything...
            // don't rely on stored procedures
            using (var transaction = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(10)))
            {
                //var electionGuidName = ReflectionHelper.GetName(() => default(Election).ElectionGuid);

                try
                {
                    //DeleteFrom<Vote, Ballot, Location>(electionGuidName,
                    //                                   ReflectionHelper.GetName(() => default(Ballot).BallotGuid),
                    //                                   ReflectionHelper.GetName(() => default(Location).LocationGuid)
                    //  );
                    //DeleteFrom<Ballot, Location>(electionGuidName,
                    //                             ReflectionHelper.GetName(() => default(Location).LocationGuid));

                    //DeleteFrom<Result>(electionGuidName);
                    //DeleteFrom<Person>(electionGuidName);
                    //DeleteFrom<ResultTie>(electionGuidName);

                    //DeleteFrom<Computer>(electionGuidName);
                    //DeleteFrom<Location>(electionGuidName);
                    //DeleteFrom<Teller>(electionGuidName);
                    //DeleteFrom<ResultSummary>(electionGuidName);
                    //DeleteFrom<JoinElectionUser>(electionGuidName);
                    //DeleteFrom<ImportFile>(electionGuidName);
                    //DeleteFrom<Message>(electionGuidName);
                    //DeleteFrom<Election>(electionGuidName);

                    Election.EraseBallotsAndResults(_electionGuid);

                    //          Db.Computer.Where(x => x.ElectionGuid == _electionGuid);
                    Db.Location.Where(x => x.ElectionGuid == _electionGuid).Delete();
                    Db.Person.Where(x => x.ElectionGuid == _electionGuid).Delete();
                    Db.Teller.Where(x => x.ElectionGuid == _electionGuid).Delete();
                    Db.JoinElectionUser.Where(x => x.ElectionGuid == _electionGuid).Delete();
                    Db.ImportFile.Where(x => x.ElectionGuid == _electionGuid).Delete();
                    Db.Message.Where(x => x.ElectionGuid == _electionGuid).Delete();
                    Db.Election.Where(x => x.ElectionGuid == _electionGuid).Delete();

                    new LogHelper(_electionGuid).Add("Deleted election '{0}'".FilledWith(electionName));

                    transaction.Complete();

                    if (_electionGuid == UserSession.CurrentElectionGuid)
                    {
                        new CacherHelper().DropAllCachesForThisElection();
                    }

                    return(new
                    {
                        Deleted = true
                    }.AsJsonResult());
                }
                catch (Exception ex)
                {
                    return(new
                    {
                        Deleted = false,
                        Message = ex.GetAllMsgs("<br>")
                    }.AsJsonResult());
                }
            }
        }