public async Task <IActionResult> GetCompetitionMarketTypeFetchRoot(int dataProviderId, string competitionId, string marketType)
        {
            CompetitionMarketType competitionMarketType = await _competitionMarketTypeReader.Read(Tuple.Create(dataProviderId, competitionId, marketType));

            if (competitionMarketType == null)
            {
                return(NotFound($"Unable to find fetch root with dataProviderId of {dataProviderId}, competitionId of {competitionId} and marketType of {marketType}"));
            }

            return(Ok(competitionMarketType));
        }
        public async Task <IActionResult> CreateCompetitionMarketTypeFetchRoot([FromRoute] int dataProviderId, [FromRoute] string competitionId,
                                                                               [FromBody] CompetitionMarketTypeCreationDto competitionMarketType)
        {
            DataProvider dataProvider = await _dataProviderReader.Read(dataProviderId);

            if (dataProvider == null)
            {
                return(NotFound($"Unable to find dataProvider with id {dataProviderId}"));
            }

            Competition competition = await _competitionReader.Read(competitionId);

            if (competition == null)
            {
                return(NotFound($"Unable to find competition with id {competitionId}"));
            }

            IExternalMarketTypesRepository marketTypesRepository = _marketTypesRepositoryFactory?.Invoke(dataProviderId);
            IEnumerable <MarketType>       marketTypes           = await marketTypesRepository.GetMarketTypesByCompetitionId(competitionId);

            if (!marketTypes.Any(mtr => mtr.Name == competitionMarketType.MarketType))
            {
                return(NotFound($"There are no markets with type {competitionMarketType.MarketType} in the competition with id {competitionId} in the Betfair system"));
            }

            if (await _competitionMarketTypeReader.Read(Tuple.Create(dataProviderId, competitionId, competitionMarketType.MarketType)) != null)
            {
                return(Conflict($"The fetch root object with competitionId {competitionId} and marketType {competitionMarketType.MarketType} has already been saved."));
            }

            var fetchRoot = new CompetitionMarketType()
            {
                DataProviderId = dataProviderId,
                CompetitionId  = competitionId,
                MarketType     = competitionMarketType.MarketType
            };
            await _competitionMarketTypeSaver.Save(fetchRoot);

            return(CreatedAtRoute(
                       "GetCompetitionMarketTypeFetchRoot",
                       new
            {
                dataProviderId = dataProviderId,
                competitionId = competitionId,
                marketType = competitionMarketType.MarketType
            },
                       fetchRoot));
        }
        public async Task <IActionResult> DeleteCompetitionMarketTypeFetchRoot(int dataProviderId, string competitionId, string marketType)
        {
            if (await _competitionMarketTypeReader.Read(Tuple.Create(dataProviderId, competitionId, marketType)) == null)
            {
                return(NotFound($"The fetch root object with dataProviderId {dataProviderId}, competitionId {competitionId} and marketType {marketType} cannot be found."));
            }

            var competitionMarketType = new CompetitionMarketType()
            {
                DataProviderId = dataProviderId,
                CompetitionId  = competitionId,
                MarketType     = marketType
            };

            await _competitionMarketTypeDeleter.Delete(competitionMarketType);

            return(NoContent());
        }
Ejemplo n.º 4
0
        public async Task DoWork(CompetitionMarketType competitionMarketType)
        {
            DataProvider externalDataProvider = await _externalDataProviderReader.Read(competitionMarketType.DataProviderId);

            if (externalDataProvider == null)
            {
                throw new Exception($"No data provider found with id of {competitionMarketType.DataProviderId}");
            }
            Domain.Internal.DataProvider dataProvider = _mapper.Map <Domain.Internal.DataProvider>(externalDataProvider);
            await _upserter.Upsert(dataProvider);

            IExternalCompetitionsRepository competitionsRepository = _externalCompetitionsRepositoryFactory?.Invoke(competitionMarketType.DataProviderId);
            Competition externalCompetition = await competitionsRepository.GetCompetition(competitionMarketType.CompetitionId);

            if (externalCompetition == null)
            {
                throw new Exception($"No competition found with id of {competitionMarketType.CompetitionId}");
            }

            IExternalMarketTypesRepository marketTypesRepository = _externalMarketTypesRepositoryFactory?.Invoke(competitionMarketType.DataProviderId);
            MarketType marketTypeForCompetition = await marketTypesRepository.GetMarketTypeForCompetition(
                competitionMarketType.CompetitionId,
                competitionMarketType.MarketType);

            if (marketTypeForCompetition == null)
            {
                throw new Exception($"No {competitionMarketType.MarketType} market type found for competition with id {competitionMarketType.CompetitionId}");
            }

            IExternalEventTypesRepository eventTypesRepository = _externalEventTypesRepositoryFactory?.Invoke(competitionMarketType.DataProviderId);
            EventType externalEventType = await eventTypesRepository.GetEventTypeForCompetition(competitionMarketType.CompetitionId);

            if (externalEventType != null)
            {
                Domain.Internal.EventType eventType = _mapper.Map <Domain.Internal.EventType>(externalEventType);
                await _upserter.Upsert(eventType);
            }

            Domain.Internal.Competition competition = _mapper.Map <Domain.Internal.Competition>(externalCompetition);
            if (competition != null)
            {
                competition.EventTypeId = externalEventType?.Id;
                await _upserter.Upsert(competition);
            }
            IExternalEventsRepository eventsRepository = _externalEventsRepositoryFactory?.Invoke(competitionMarketType.DataProviderId);
            IEnumerable <Event>       events           = await eventsRepository.GetEventsByCompetitionIdAndMarketType(
                competitionMarketType.CompetitionId,
                competitionMarketType.MarketType);

            foreach (var externalEvent in events ?? new Event[0])
            {
                Domain.Internal.Event internalEvent = _mapper.Map <Domain.Internal.Event>(externalEvent);
                if (internalEvent != null)
                {
                    internalEvent.CompetitionId = competition?.Id;
                    await _upserter.Upsert(internalEvent);
                }

                IExternalMarketsRepository marketsRepository = _externalMarketsRepositoryFactory?.Invoke(competitionMarketType.DataProviderId);
                Market externalMarket = await marketsRepository.GetMarketForEventAndMarketType(
                    externalEvent.Id,
                    competitionMarketType.MarketType);

                Domain.Internal.Market market = _mapper.Map <Domain.Internal.Market>(externalMarket);
                if (market != null)
                {
                    market.EventId = internalEvent?.Id;
                    await _upserter.Upsert(market);
                }
                foreach (var externalRunner in externalMarket.Runners)
                {
                    Domain.Internal.Selection selection = _mapper.Map <Domain.Internal.Selection>(externalRunner);
                    if (selection != null)
                    {
                        selection.MarketId = market?.Id;
                        await _upserter.Upsert(selection);
                    }
                }
            }
        }