Пример #1
0
        public ActionResult <PerformanceDto> CreatePerformance(Guid directorId,
                                                               [FromBody] PerformanceForCreationDto performance)
        {
            if (!_unitOfWork.Directors.DirectorExists(directorId))
            {
                return(NotFound());
            }

            var performanceEntity = _mapper.Map <Performance>(performance);

            if (_unitOfWork.Performances.PerformanceExists(directorId, performanceEntity))
            {
                return(Conflict(new { message = $"This Performance already exists in the database!" }));
            }

            SavePerformanceInDb(directorId, performanceEntity);

            var performanceToReturn = _mapper.Map <PerformanceDto>(
                _unitOfWork.Performances.GetPerformance(performanceEntity.Guid));

            var linkedResourceToReturn = performanceToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", _dependentLinksService.
                                       CreateLinksForDependentEntity(directorId, performanceToReturn.Id, null));

            return(CreatedAtRoute("GetPerformanceForDirector",
                                  new { directorId,
                                        performanceId = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
        private static PerformanceForCreationDto ExtractPerformance(HtmlNode node, TraceWriter log)
        {
            try
            {
                var perf = new PerformanceForCreationDto();

                var data = node.SelectNodes("td");

                // Column 1 is row index

                // Column 2 is event
                perf.Event = data[1].InnerText;

                // Column 3 is performance
                perf.PerformanceValue = data[2].InnerText;

                // column 9 is age
                perf.Age = Convert.ToInt16(data[8].InnerText);

                // Column 10 is position
                perf.Position = data[9].InnerText;

                // Column 11 is round
                perf.Round = data[10].InnerText;

                // Column 14 is venue
                var uri = new Uri(new Uri("http://test.com"), data[13].FirstChild.Attributes["href"].Value);

                var parameters = HttpUtility.ParseQueryString(uri.Query);

                perf.MeetingId = parameters.Get("meetingId");
                perf.Venue     = data[13].FirstChild.InnerText;

                // Column 15 is meeting
                perf.MeetingName = data[14].InnerText;

                // Column 16 is date
                perf.Date = Convert.ToDateTime(data[15].InnerText);

                return(perf);
            }
            catch (Exception ex)
            {
                log.Error("error extracting performance data", ex);
                return(null);
            }
        }
        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"));
            }
        }