Exemple #1
0
        public void PostAthlete(string raceId, string athleteId, AthleteDto incomingAthlete)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }
            if (string.IsNullOrEmpty(athleteId))
            {
                throw new ArgumentNullException("Missing AthleteId");
            }
            var  repositorySet  = repositorySetProvider.GetRepositorySet(raceId);
            bool requireProfile = false;

            var athleteModel = repositorySet.Athletes.FindAthlete(athleteId);

            if (athleteModel == null)
            {
                athleteModel               = new Athlete();
                athleteModel.AthleteId     = athleteId;
                athleteModel.Announcements = new List <Announcement>();
                athleteModel.ActualResults = new List <ActualResult>();
                requireProfile             = true;
            }

            if (incomingAthlete.Profile == null)
            {
                if (requireProfile)
                {
                    throw new ArgumentNullException("Missing Profile");
                }
            }
            else
            {
                if (string.IsNullOrEmpty(incomingAthlete.Profile.FirstName))
                {
                    throw new ArgumentNullException("Missing FirstName");
                }
                if (string.IsNullOrEmpty(incomingAthlete.Profile.Surname))
                {
                    throw new ArgumentNullException("Missing Surname");
                }
                athleteModel.FirstName        = incomingAthlete.Profile.FirstName;
                athleteModel.Surname          = incomingAthlete.Profile.Surname;
                athleteModel.Club             = incomingAthlete.Profile.Club;
                athleteModel.CountryName      = incomingAthlete.Profile.CountryName;
                athleteModel.ProfilePhotoName = incomingAthlete.Profile.ProfilePhotoName;
                athleteModel.Sex            = Sex.Parse(incomingAthlete.Profile.Sex);
                athleteModel.Category       = incomingAthlete.Profile.Category;
                athleteModel.ModeratorNotes = incomingAthlete.Profile.ModeratorNotes;
            }

            if (incomingAthlete.Announcements != null)
            {
                foreach (var incomingAnnouncement in incomingAthlete.Announcements)
                {
                    string disciplineId = incomingAnnouncement.DisciplineId;
                    if (string.IsNullOrEmpty(disciplineId))
                    {
                        throw new ArgumentNullException("Missing Announcement.DisciplineId");
                    }
                    Discipline discipline = repositorySet.Disciplines.FindDiscipline(disciplineId);
                    if (discipline == null)
                    {
                        throw new ArgumentOutOfRangeException("Unknown Announcement.DisciplineId " + disciplineId);
                    }
                    IRules disciplineRules = rulesRepository.Get(discipline.Rules);

                    if (discipline.AnnouncementsClosed)
                    {
                        throw new ArgumentOutOfRangeException("Discipline " + disciplineId + " already closed announcements");
                    }

                    if (incomingAnnouncement.Performance == null)
                    {
                        athleteModel.Announcements.RemoveAll(a => a.DisciplineId == disciplineId);
                    }
                    else
                    {
                        var newAnnouncement   = false;
                        var announcementModel = athleteModel.Announcements.FirstOrDefault(a => a.DisciplineId == disciplineId);
                        if (announcementModel == null)
                        {
                            announcementModel = new Announcement();
                            announcementModel.DisciplineId = incomingAnnouncement.DisciplineId;
                            newAnnouncement = true;
                        }

                        if (disciplineRules.HasDuration && incomingAnnouncement.Performance.Duration == null)
                        {
                            throw new ArgumentNullException("Missing Announcement.Duration for " + disciplineId);
                        }
                        if (disciplineRules.HasDepth && incomingAnnouncement.Performance.Depth == null)
                        {
                            throw new ArgumentNullException("Missing Announcement.Depth for " + disciplineId);
                        }
                        if (disciplineRules.HasDistance && incomingAnnouncement.Performance.Distance == null)
                        {
                            throw new ArgumentNullException("Missing Announcement.Distance for " + disciplineId);
                        }

                        announcementModel.ModeratorNotes = incomingAnnouncement.ModeratorNotes;
                        announcementModel.Performance    = ExtractPerformance(incomingAnnouncement.Performance);
                        if (newAnnouncement)
                        {
                            athleteModel.Announcements.Add(announcementModel);
                        }
                    }
                }
            }

            repositorySet.Athletes.SaveAthlete(athleteModel);
        }