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;
        }
 partial void DeleteInti_Athlete(Inti_Athlete instance);
 partial void UpdateInti_Athlete(Inti_Athlete instance);
 partial void InsertInti_Athlete(Inti_Athlete instance);
Example #5
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);
        }
        private ImportAthleteAction GetOrCreateAthleteImportAction(Guid selectedTournament, PlayerDetails playerDetails)
        {
            var importAthleteAction = new ImportAthleteAction();

            try
            {
                //verify and get club
                var club = new ClubManagement(Global.ConnectionString, SessionProps).GetClubByCode(selectedTournament, playerDetails.ClubCode);

                //verify and get position
                Inti_Position position = null;
                if (!String.IsNullOrEmpty(playerDetails.PosCode))
                    position = new CommonDataFetches(Global.ConnectionString, SessionProps).GetPosition(playerDetails.PosCode);

                //try to get athlete
                var athlete = new AthleteManagement(Global.ConnectionString, SessionProps).GetAthleteByName(playerDetails.FirstName, playerDetails.LastName);

                importAthleteAction.AthleteIsStored = (athlete != null);

                if (athlete == null)
                {
                    //athlete is added with full name?
                    var searchString = String.IsNullOrEmpty(playerDetails.FirstName)
                                           ? playerDetails.LastName
                                           : String.Format("{0}, {1}", playerDetails.FirstName, playerDetails.LastName);
                    IList<Inti_Athlete> matchingAthletes;
                    using (var db = Global.GetConnection())
                    {
                        matchingAthletes = Searching.SearchForAthletes(db, searchString);
                    }

                    if (matchingAthletes.Count == 0)
                    {
                        //do we have position?
                        if (position != null)
                        {
                            //create new athlete
                            athlete = new Inti_Athlete();
                            athlete.FirstName = playerDetails.FirstName;
                            athlete.LastName = playerDetails.LastName;
                        }
                        else
                        {
                            importAthleteAction.Exception = new Exception("Ny spelare men position saknas. Ange med position");
                        }

                    }
                    else
                    {
                        if (matchingAthletes.Count ==1)
                        {
                            athlete = matchingAthletes[0];
                            importAthleteAction.ImportAction += "OBS, kontrollera namnet. ";
                        }
                        else
                        {
                            var message = "Flera spelare matchar namnet:";
                            foreach (var matchingAthlete in matchingAthletes)
                            {
                                message += (matchingAthlete.FirstName + " " + matchingAthlete.LastName).Trim() + ", ";
                            }
                            importAthleteAction.Exception = new Exception(message);
                        }

                    }

                }

                if (athlete != null)
                {
                    //is the athlete already in tournament?
                    var athleteClub = new AthleteManagement(Global.ConnectionString, SessionProps).GetAthleteClubByTournament(athlete.GUID,
                                                                                                     selectedTournament);

                    importAthleteAction.AthleteClubIsStored = (athleteClub != null);
                    importAthleteAction.AthleteClubIsUpToDate = true;
                    if (athleteClub == null)
                    {
                        if(position != null)
                        {
                            athleteClub = new Inti_AthleteClub();

                            athleteClub.Inti_Athlete = athlete;
                            athleteClub.Inti_Club = club;
                            athleteClub.Inti_Position = position;
                            athleteClub.Price = playerDetails.Price;
                        }
                        else
                        {
                            importAthleteAction.Exception = new Exception("Befintlig spelare men position saknas. Ange med position");
                        }
                    }
                    else
                    {
                        importAthleteAction.AthleteClubIsUpToDate = (athleteClub.Inti_Club.GUID == club.GUID);

                        //new club
                        if (athleteClub.Inti_Club.GUID != club.GUID)
                        {
                            athleteClub.Inti_Club = club;
                        }

                        if (position != null && athleteClub.Inti_Position.GUID != position.GUID)
                        {
                            importAthleteAction.Exception = new Exception(String.Format("Changed position from {0} to {1}, is it the correct player?", athleteClub.Inti_Position.ShortName, position.ShortName));
                        }

                        if (athleteClub.Price != playerDetails.Price)
                        {
                            importAthleteAction.Exception =
                                new Exception(String.Format("Changed price from {0} to {1}, is it the correct player?",
                                    athleteClub.Price, playerDetails.Price));
                        }
                    }

                    //set import action
                    importAthleteAction.ImportAction += GetImportAction(importAthleteAction);

                    importAthleteAction.AthleteClub = athleteClub;
                }

            }
            catch (Exception exception)
            {
                importAthleteAction.Exception = exception;
            }

            return importAthleteAction;
        }