Esempio n. 1
0
        public void ClearTeamPoints(Guid matchId)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                var teamPointEvents = from tpe in db.Inti_TeamPointEvents
                                      where tpe.Inti_MatchPointEvent.MatchGUID == matchId
                                      select tpe;

                db.Inti_TeamPointEvents.DeleteAllOnSubmit(teamPointEvents.ToList());

                if (teamPointEvents.ToList().Count > 0)
                    LogEvent(matchId, typeof(Inti_Match), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Update, "ClearTeamPoints");

                db.SubmitChanges();
            }
        }
        internal void LogEvent(Guid objectId, Type objectType, Guid userGuid, string client, EventType action, string message)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                var logEvent = new Ext_ChangeLog();
                logEvent.ObjectGUID = objectId;
                logEvent.ObjectType = objectType.Name;
                logEvent.UserGUID = userGuid;
                logEvent.Client = client;
                logEvent.Action = action.ToString();
                logEvent.Message = message;
                logEvent.LogDate = DateTime.Now;

                db.Ext_ChangeLog.InsertOnSubmit(logEvent);
                db.SubmitChanges();
            }
        }
        public void ActivateTeam(Guid teamGUID)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                try
                {
                    var team = db.Inti_Team.Single(t => t.GUID == teamGUID);

                    team.IsActive = !team.IsActive;

                    db.SubmitChanges();

                }
                catch (System.InvalidOperationException)
                {
                    //team was not to be found
                }
            }
        }
Esempio n. 4
0
        public void ClearPointEvents(Guid matchId)
        {
            //always clear teamPoints first
            ClearTeamPoints(matchId);

            using (var db = new IntiDataContext(_connectionString))
            {
                var matchPointEvents = from mpe in db.Inti_MatchPointEvent
                                       where mpe.MatchGUID == matchId
                                       select mpe;

                db.Inti_MatchPointEvent.DeleteAllOnSubmit(matchPointEvents.ToList());

                var match = db.Inti_Match.Single(m => m.GUID == matchId);
                match.IsUpdated = false;

                LogEvent(matchId, typeof(Inti_Match), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Update, "ClearPointEvents");

                db.SubmitChanges();
            }
        }
Esempio n. 5
0
        public void SaveUser(Guid userGuid, string userName, string firstName, string lastName, List<Guid> permissionGuids)
        {
            using(var db = new IntiDataContext(_connectionString))
            {
                var user = db.Sys_User.Single(u => u.GUID == userGuid);

                //is user name still unique?
                if (userName != user.UserName)
                {
                    var users = from u in db.Sys_User
                                where u.UserName == userName &&
                                      u.GUID != userGuid
                                select u;

                    if(users.ToList().Count > 0)
                    {
                        throw new Exception(String.Format("The Username {0} not unique", userName));
                    }
                }

                var message = "";
                if (user.UserName != userName)
                {
                    message += String.Format("Changed {0} from {1} to {2}. ", "UserName", user.UserName, userName);
                    user.UserName = userName;
                }

                if (user.FirstName!= firstName)
                {
                    message += String.Format("Changed {0} from {1} to {2}. ", "FirstName", user.FirstName, firstName);
                    user.FirstName = firstName;
                }

                if (user.LastName != lastName)
                {
                    message += String.Format("Changed {0} from {1} to {2}. ", "LastName", user.LastName, lastName);
                    user.LastName = lastName;
                }

                if(!String.IsNullOrEmpty(message))
                    LogEvent(userGuid, user.GetType(), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Change, message);

                //clear permissions
                var userPermissions = from up in db.Sys_UserPermission
                                      where up.UserGUID == userGuid
                                      select up;

                foreach (Sys_UserPermission userPermission in userPermissions.ToList())
                {
                    if (permissionGuids.Contains(userPermission.PermissionGUID))
                    {
                        permissionGuids.Remove(userPermission.PermissionGUID);
                    }
                    else
                    {
                        db.Sys_UserPermission.DeleteOnSubmit(userPermission);
                        LogEvent(userGuid, user.GetType(), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Change, "Removed permission " + userPermission.Sys_Permission.Name);
                    }
                }

                foreach (Guid permissionGuid in permissionGuids)
                {
                    var userPermission = new Sys_UserPermission();
                    userPermission.UserGUID = userGuid;
                    userPermission.PermissionGUID = permissionGuid;

                    Guid guid = permissionGuid;
                    var permission = db.Sys_Permission.Single(p => p.GUID == guid);
                    LogEvent(userGuid, user.GetType(), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Change, "Added permission " + permission.Name);

                    db.Sys_UserPermission.InsertOnSubmit(userPermission);
                }

                db.SubmitChanges();

            }
        }
        public void UpdateTeamImage(Guid teamGUID, string imageName)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                var team = db.Inti_Team.Single(t => t.GUID == teamGUID);

                team.Picture = imageName;

                db.SubmitChanges();

                LogEvent(teamGUID, typeof(Inti_Team), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Update, "Ändrat bild");
            }
        }
        public void ToggleUserFavoriteTeam(Guid teamGUID)
        {
            if (SessionProperties.UserGuid == Guid.Empty) return;

            using(var db = new IntiDataContext(_connectionString))
            {
                var userFavoriteTeam = from uft in db.Ext_UserFavoriteTeam
                                       where uft.UserGUID == SessionProperties.UserGuid &&
                                       uft.TeamGUID == teamGUID
                                       select uft;

                if (userFavoriteTeam.ToList().Count > 0)
                    db.Ext_UserFavoriteTeam.DeleteAllOnSubmit(userFavoriteTeam);
                else
                {
                    var newUserFavoriteTeam = new Ext_UserFavoriteTeam();
                    newUserFavoriteTeam.UserGUID = SessionProperties.UserGuid;
                    newUserFavoriteTeam.TeamGUID = teamGUID;

                    db.Ext_UserFavoriteTeam.InsertOnSubmit(newUserFavoriteTeam);
                }

                db.SubmitChanges();
            }
        }
        public void ToggleBonus(Guid teamGUID)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                try
                {
                    var team = db.Inti_Team.Single(t => t.GUID == teamGUID);

                    team.BonusPoints = team.BonusPoints.HasValue && team.BonusPoints.Value == 2 ? 0 : 2;

                    db.SubmitChanges();

                }
                catch (System.InvalidOperationException)
                {
                    //team was not to be found
                }
            }
        }
        public void ReOpenTeamForTransfers(IntiDataContext db, Inti_TransferPeriod transferPeriod, Inti_TeamVersion transferVersion, Inti_TeamVersion currentVersion, Guid teamId)
        {
            //remove transfers
            var transfers =
                db.Inti_TeamTransfer.Where(
                    tf => tf.TeamGUID == teamId && tf.TransferDate == transferPeriod.EndDate).ToList();
            if (transfers.Count > 0)
                db.Inti_TeamTransfer.DeleteAllOnSubmit(transfers);

            //update validTo and ValidFroms
            transferVersion.ValidFrom = null;
            currentVersion.ValidTo = null;

            db.SubmitChanges();

            LogEvent(teamId, typeof(Inti_Team), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Update, "Ångrat byten");
        }
        public void RemoveDuplicateTransfers(IntiDataContext db, Guid teamId)
        {
            var summary = from tf in db.Inti_TeamTransfer
                where tf.TeamGUID == teamId
                group tf by new {tf.TransferDate, tf.AthleteInGUID, tf.AthleteOutGUID}
                into dup
                select new {dup.Key.TransferDate,
                    dup.Key.AthleteInGUID,
                    dup.Key.AthleteOutGUID,
                    Count = dup.Count()
                };

            var duplicates = summary.Where(s => s.Count > 1).ToList();

            foreach (var duplicate in duplicates)
            {
                //remove one of the duplicates
                var toDelete = db.Inti_TeamTransfer.FirstOrDefault(
                    tf =>
                        tf.TeamGUID == teamId && tf.TransferDate == duplicate.TransferDate &&
                        tf.AthleteInGUID == duplicate.AthleteInGUID && tf.AthleteOutGUID == duplicate.AthleteOutGUID);

                if (toDelete == null) continue;

                db.Inti_TeamTransfer.DeleteOnSubmit(toDelete);
            }

            db.SubmitChanges();
        }
Esempio n. 11
0
        public void SetUpdater(Guid matchId)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                var match = db.Inti_Match.Single(m => m.GUID == matchId);

                match.Updater = SessionProperties.UserGuid;
                match.StartUpdateDate = DateTime.Now;

                db.SubmitChanges();
            }
        }
Esempio n. 12
0
        public void UpdateNews(Guid guid, string header, string body, string picture, DateTime? validFrom, DateTime? validTo)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                var news = db.Ext_News.Single(n => n.GUID == guid);

                news.Header = header;
                news.Body = body;
                news.Picture = picture;
                news.ValidFrom = validFrom;
                news.ValidTo = validTo;

                LogEvent(news.GUID, news.GetType(), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Update, "Update news");

                db.SubmitChanges();
            }
        }
        public void CommitTransfers(IntiDataContext db, Inti_TransferPeriod transferPeriod, Inti_TeamVersion currentVersion, Inti_TeamVersion transferVersion, Guid teamId)
        {
            //add transfers
            IList<Inti_TeamTransfer> transfers = new List<Inti_TeamTransfer>();
            foreach (var teamAthlete in currentVersion.Inti_TeamAthlete.ToList())
            {
                //is this athlete removed in the new version?
                if (transferVersion.Inti_TeamAthlete.Where(ta => ta.AthleteGUID == teamAthlete.AthleteGUID).ToList().Count == 0)
                {
                    var transfer = new Inti_TeamTransfer();
                    transfer.TeamGUID = teamId;
                    transfer.TransferDate = transferPeriod.EndDate;
                    transfer.AthleteOutGUID = teamAthlete.AthleteGUID;
                    transfer.AthleteInGUID = Guid.Empty;
                    transfers.Add(transfer);
                }

            }

            foreach (var teamAthlete in transferVersion.Inti_TeamAthlete.ToList())
            {
                //is this athlete present in the old version?
                if (currentVersion.Inti_TeamAthlete.Where(ta => ta.AthleteGUID == teamAthlete.AthleteGUID).ToList().Count == 0)
                {
                    foreach (var tr in transfers)
                    {
                        if (tr.AthleteInGUID == Guid.Empty)
                        {
                            tr.AthleteInGUID = teamAthlete.AthleteGUID;
                            db.Inti_TeamTransfer.InsertOnSubmit(tr);
                            break;
                        }
                    }
                }

            }

            //update validTo and ValidFroms
            transferVersion.ValidFrom = transferPeriod.EndDate;
            currentVersion.ValidTo = transferPeriod.EndDate.AddDays(-1);

            db.SubmitChanges();

            LogEvent(teamId, typeof(Inti_Team), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Update, "Genomfört byten");
        }
Esempio n. 14
0
        public void RegisterUser(string firstName, string lastName, string email, string password)
        {
            if (String.IsNullOrEmpty(firstName))
                throw new IntiGeneralException("Du måste ange ett förnamn");

            if (String.IsNullOrEmpty(lastName))
                throw new IntiGeneralException("Du måste ange ett efternamn");

            if (String.IsNullOrEmpty(email))
                throw new IntiGeneralException("Du måste ange en epostadress");

            if (String.IsNullOrEmpty(password))
                throw new IntiGeneralException("Du måste ange ett lösenord");

            using (var db = new IntiDataContext(_connectionString))
            {
                //username/email must be unique
                var users = from u in db.Sys_User
                            where u.UserName.ToLower() == email.ToLower()
                            select u;

                if (users.ToList().Count > 0)
                    throw new IntiGeneralException("Det finns redan en användare med den epostadressen");

                var user = new Sys_User { FirstName = firstName, LastName = lastName, UserName = email, Password = password };

                db.Sys_User.InsertOnSubmit(user);

                //default permissions
                var defaultPermissions = from p in db.Sys_Permission
                                         where p.Name == "USER" || p.Name == "USER_PVTTOUR"
                                         select p;

                foreach (var perm in defaultPermissions.ToList())
                {
                    var userPerm = new Sys_UserPermission();
                    userPerm.UserGUID = user.GUID;
                    userPerm.PermissionGUID = perm.GUID;
                    db.Sys_UserPermission.InsertOnSubmit(userPerm);
                }

                db.SubmitChanges();
            }
        }
Esempio n. 15
0
        public string ForgotPassword(string email)
        {
            if (String.IsNullOrEmpty(email))
                throw new IntiGeneralException("Du måste ange en epostadress");

            using (var db = new IntiDataContext(_connectionString))
            {
                var users = from u in db.Sys_User
                            where u.UserName == email
                            select u;

                if (users.Count() != 1)
                {
                    throw new IntiGeneralException("Hittar ingen användare med den epostadressen");
                }

                var user = users.ToList()[0];

                user.Password = GetRandomPassword();
                db.SubmitChanges();

                //send mail
                MailAndLog.SendMessage(Parameters.Instance.ForgotPasswordSubject,
                    String.Format(Parameters.Instance.ForgotPasswordBody, user.Password),
                    Parameters.Instance.MailSender, email);

                return user.Password;

            }
        }
Esempio n. 16
0
        private void UpdateParameter(string parameterName, string value)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                var parameter = db.Sys_Parameter.SingleOrDefault(p => p.Name == parameterName);
                if (parameter == null) return;

                parameter.Value = value;

                db.SubmitChanges();
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Adds a new parameter if it doesn't exist
        /// </summary>
        /// <param name="db"></param>
        /// <param name="parameterName"></param>
        private static void AddParameter(IntiDataContext db, string parameterName)
        {
            if (!db.Sys_Parameter.Any(p => p.Name == parameterName))
            {
                var parameter = new Sys_Parameter();
                parameter.GUID = Guid.NewGuid();
                parameter.Name = parameterName;
                parameter.Description = parameterName;

                db.Sys_Parameter.InsertOnSubmit(parameter);

                db.SubmitChanges();
            }
        }
Esempio n. 18
0
        public void DistributePoints(Guid matchId)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                var match = db.Inti_Match.Single(m => m.GUID == matchId);

                var mpes = from mpe in db.Inti_MatchPointEvent
                           where mpe.Inti_Match.GUID == matchId
                           orderby mpe.Inti_AthleteClub.Inti_Club.Name
                           orderby mpe.Inti_AthleteClub.Inti_Position.Name
                           orderby mpe.Inti_PointEvent.Name
                           select mpe;

                foreach (var mpe in mpes)
                {
                    //loop the teams that has this athlete
                    var teamAthletes = from ta in db.Inti_TeamAthlete
                                       where ta.Inti_TeamVersion.ValidFrom < match.MatchDate
                                             && (ta.Inti_TeamVersion.ValidTo ?? match.MatchDate) >= match.MatchDate
                                             && (ta.Inti_TeamVersion.Inti_Team.IsActive ?? false)
                                             && (ta.Inti_TeamVersion.Inti_Team.IsPaid ?? false)
                                             && ta.AthleteGUID == mpe.AthleteClubGUID
                                       select new
                                       {
                                           ta.Inti_TeamVersion.TeamGUID
                                       };

                    foreach (var teamAthlete in teamAthletes)
                    {
                        var tpe = new Inti_TeamPointEvents();
                        tpe.MatchPointEventGUID = mpe.GUID;
                        tpe.TeamGUID = teamAthlete.TeamGUID;

                        db.Inti_TeamPointEvents.InsertOnSubmit(tpe);
                    }
                }

                match.IsUpdated = true;

                db.SubmitChanges();

                LogEvent(matchId, typeof(Inti_Match), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Update, "DistributePoints" );
            }
        }
Esempio n. 19
0
        public Guid StoreNewNews(string header, string body, string picture, DateTime? validFrom, DateTime? validTo)
        {
            using (var db = new IntiDataContext(_connectionString))
            {
                var news = new Ext_News();
                news.Header = header;
                news.Body = body;
                news.Picture = picture;
                news.ValidFrom = validFrom;
                news.ValidTo = validTo;

                db.Ext_News.InsertOnSubmit(news);

                LogEvent(news.GUID, news.GetType(), SessionProperties.UserGuid, SessionProperties.ClientInfo, EventType.Create, "New news");

                db.SubmitChanges();

                return news.GUID;
            }
        }
        public void ReloadTransferVersion(IntiDataContext db, Inti_TeamVersion transferVersion, Inti_TeamVersion currentVersion, Guid teamId)
        {
            //delete the ones in transferversion
            var teamAthletesToDelete = transferVersion.Inti_TeamAthlete.ToList();
            db.Inti_TeamAthlete.DeleteAllOnSubmit(teamAthletesToDelete);
            db.SubmitChanges();

            //get players from current version
            foreach (var ta in currentVersion.Inti_TeamAthlete.ToList())
            {
                var newTa = new Inti_TeamAthlete();
                newTa.AthleteGUID = ta.AthleteGUID;
                newTa.TeamVersionGUID = transferVersion.GUID;
                db.Inti_TeamAthlete.InsertOnSubmit(newTa);

                db.SubmitChanges();
            }
        }
Esempio n. 21
0
        public Guid SaveAthlete(string firstName, string lastName, Guid playerGuid, Guid tournamentGuid, Guid clubGuid, Guid positionGuid, int price, bool isActive, string clubCode, Action<string, Guid> notifyCommunityAction)
        {
            using (var db = new IntiDataContext(_connectionString))
            {

                Inti_Athlete athlete;

                if (playerGuid != Guid.Empty)
                {
                    try
                    {
                        athlete = db.Inti_Athlete.Single(a => a.GUID == new Guid(playerGuid.ToString()));
                    }
                    catch (Exception)
                    {
                        //not saved athlete
                        athlete = null;
                    }
                }
                else
                {
                    athlete = null;
                }

                var newAthlete = athlete == null;
                var newInTournament = true;

                //check if athlete with same name already exists
                if (AthleteHasNameDuplicate(db, playerGuid, firstName, lastName))
                    throw new DuplicateNameException(String.Format("There is already a player with the name {0} {1}", firstName, lastName));

                if(athlete == null)
                {
                    athlete = new Inti_Athlete();
                    db.Inti_Athlete.InsertOnSubmit(athlete);
                }

                athlete.FirstName = firstName;
                athlete.LastName = lastName;

                var fired = false;
                var isManager = false;
                var changedClub = false;
                var athleteClubGuid = Guid.Empty;
                Inti_AthleteClub athleteClub;

                //add clubs/tournaments
                if (clubGuid != Guid.Empty && positionGuid != Guid.Empty)
                {
                    bool newAthleteClub = false;

                    //save club/tournament setting
                    try
                    {
                        athleteClub = db.Inti_AthleteClub.Single(ac => ac.AthleteGUID == athlete.GUID && ac.Inti_Club.TournamentGUID == tournamentGuid);
                    }
                    catch (Exception)
                    {
                        //no athleteClub for this athlete/tournament
                        //new settings
                        newAthleteClub = true;
                        athleteClub = new Inti_AthleteClub();
                        athleteClub.AthleteGUID = athlete.GUID;
                    }

                    fired = !isActive && (athleteClub.IsActive ?? true);

                    changedClub = clubGuid != athleteClub.ClubGUID;
                    athleteClub.ClubGUID = clubGuid;
                    athleteClub.PositionGUID = positionGuid;

                    athleteClub.Price = price;
                    athleteClub.IsActive = isActive;

                    if (newAthleteClub)
                        db.Inti_AthleteClub.InsertOnSubmit(athleteClub);

                    newInTournament = newAthleteClub;

                    athleteClubGuid = athleteClub.GUID;
                }

                db.SubmitChanges();

                //refresh object
                athleteClub = db.Inti_AthleteClub.Single(ac => ac.AthleteGUID == athlete.GUID && ac.Inti_Club.TournamentGUID == tournamentGuid);
                isManager = athleteClub.Inti_Position.ShortName == "MGR";

                //notify community
                if (newAthlete || newInTournament)
                {
                    notifyCommunityAction(String.Format("{0} {1} ({2}) har lagts till", firstName, lastName, clubCode), athleteClubGuid);
                }
                else
                {
                    //fired?
                    if (fired)
                    {
                        if (isManager)
                        {
                            notifyCommunityAction(String.Format("{0} {1} har fått sparken.", firstName, lastName),
                                athleteClubGuid);
                        }
                        else
                        {
                            //notifyCommunityAction(String.Format("{0} {1} har fått sparken.", firstName, lastName),
                            //    athleteClubGuid);
                        }

                    }
                    else
                    {
                        if (changedClub)
                        {
                            notifyCommunityAction(String.Format("{0} {1} har gått över till {2}.", firstName, lastName, clubCode),
                                athleteClubGuid);
                        }
                    }
                }

                playerGuid = athlete.GUID;
            }

            return playerGuid;
        }
Esempio n. 22
0
        private void GetCurrentAndTransferVersion(IntiDataContext db, out Inti_TeamVersion currentVersion, out Inti_TeamVersion transferVersion)
        {
            currentVersion = null;
            transferVersion = null;
            var currentDate = CurrentDate;

            var teamVersions = db.Inti_TeamVersion.Where(tv => tv.TeamGUID == TeamId);
            foreach (var teamVersion in teamVersions.OrderByDescending(tv => tv.Version))
            {
                if ((teamVersion.ValidFrom == null || teamVersion.ValidFrom >= currentDate) && teamVersion.ValidTo == null)
                {
                    //we have our version
                    transferVersion = teamVersion;
                }
                if (teamVersion.ValidFrom < currentDate
                    && (teamVersion.ValidTo ?? currentDate).AddDays(1) >= currentDate)
                {
                    currentVersion = teamVersion;
                }
            }

            //create a new team version
            if (transferVersion == null && currentVersion != null)
            {
                transferVersion = new Inti_TeamVersion();
                transferVersion.TeamGUID = TeamId;
                transferVersion.Version = currentVersion.Version + 1;

                db.Inti_TeamVersion.InsertOnSubmit(transferVersion);

                db.SubmitChanges();
            }

            //in tournament startoff, current version and transfer is the same
            currentVersion = currentVersion ?? transferVersion;
        }