public async Task <IActionResult> Post([FromBody] MeetingWrapper wrapper)
 {
     try
     {
         if (ModelState.IsValid)
         {
             _repository.AddMeeting(wrapper.Meeting);
             if (await _repository.SaveChangesAsync())
             {
                 _logger.LogInformation("Saved data in database");
                 return(Created($"api/meetings", wrapper));
             }
             else
             {
                 _logger.LogError("Failed to save data in database");
                 return(BadRequest("Failed to save data to database"));
             }
         }
     }
     catch (Exception ex)
     {
         _logger.LogError("Failed to save data: {0}", ex);
     }
     return(BadRequest("Failed to post data"));
 }
        public IActionResult CreatePerformance([FromBody] PerformanceForCreationDto performance)
        {
            try
            {
                // Check the input has been passed in
                if (performance == null)
                {
                    return(BadRequest());
                }

                // Validate using the fluid validation rules
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                // Check if the event exists
                var eventEntity = _eventRepository.GetEvent(performance.Event);

                if (eventEntity == null)
                {
                    return(StatusCode(501, "Event Not Supported"));
                }

                // check if the athlete exists
                var athleteEntity = _athleteRepository.GetAthlete(performance.AthleteId);

                if (athleteEntity == null)
                {
                    return(NotFound());
                }

                // Check if the meeting exists already
                var meeting = _meetingRepository.GetMeeting(performance.MeetingId);

                if (meeting == null)
                {
                    // Create the meeting
                    meeting = new Meeting()
                    {
                        Name        = performance.MeetingName,
                        PowerOf10Id = performance.MeetingId
                    };

                    _meetingRepository.AddMeeting(meeting);

                    if (!_meetingRepository.Save())
                    {
                        return(StatusCode(500, "A problem happened while handling your request"));
                    }
                }

                // Check if the performance exists already
                // combo of the event id and the meeting id
                if (_performanceRepository.PerformanceExists(athleteEntity.Id, meeting.Id, eventEntity.Id, performance.Round))
                {
                    return(StatusCode(409, "Performance already registered with server"));
                }

                // Map the dto to a domain entity
                var finalPerformance = _mapper.Map <Performance>(performance);
                finalPerformance.EventId   = eventEntity.Id;
                finalPerformance.MeetingId = meeting.Id;

                var ageGroup = string.Empty;

                // from the age of the athlete at performance work out which age group they were in
                switch (performance.Age)
                {
                case 7:
                case 8:
                case 9:
                case 10:
                    ageGroup = "U11";
                    break;

                case 11:
                case 12:
                    ageGroup = "U13";
                    break;

                case 13:
                case 14:
                    ageGroup = "U15";
                    break;

                case 15:
                case 16:
                    ageGroup = "U17";
                    break;

                case 17:
                case 18:
                case 19:
                    ageGroup = "U20";
                    break;

                default:
                    ageGroup = "SEN";
                    break;
                }

                // Get all standards applicable for the athlete and performance
                var eventStandards = _standardRepository.GetStandards(performance.Date.Year, eventEntity.Id, ageGroup, athleteEntity.Gender);

                // Try converting the performance into a double
                if (double.TryParse(performance.PerformanceValue, out var result))
                {
                    var standardsMet = eventStandards.Where(x =>
                                                            (result <= x.Value && x.Operator == "<") ||
                                                            (result >= x.Value && x.Operator == ">")
                                                            ).Select(x => x.Standard).OrderBy(x => x.Priority);

                    if (standardsMet.Any())
                    {
                        finalPerformance.StandardId = standardsMet.First().Id;
                    }
                }

                // Create the performance in the repository
                _performanceRepository.AddPerformance(finalPerformance);

                if (!_performanceRepository.Save())
                {
                    return(StatusCode(500, "A problem happened while handling your request"));
                }

                var createdPerformanceToReturn = _mapper.Map <PerformanceDto>(finalPerformance);

                return(CreatedAtRoute("GetPerformance", new { id = createdPerformanceToReturn.Id },
                                      createdPerformanceToReturn));
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Exception while creating performance.", ex);
                return(StatusCode(500, "A problem happened while handling your request"));
            }
        }