예제 #1
0
        /// <summary>
        /// Indexes offers with their respective logos.
        /// </summary>
        /// <returns>A task.</returns>
        private async Task IndexOffersAsync()
        {
            // Need to manage this based on the partner's country locale to retrieve localized offers for the store front.
            IPartner localeSpecificPartnerCenterClient = ApplicationDomain.PartnerCenterClient.With(RequestContextFactory.Instance.Create(ApplicationDomain.PortalLocalization.OfferLocale));

            // retrieve the offers for this country
            Microsoft.Store.PartnerCenter.Models.ResourceCollection <Offer> localizedOffers = await localeSpecificPartnerCenterClient.Offers.ByCountry(ApplicationDomain.PortalLocalization.CountryIso2Code).GetAsync().ConfigureAwait(false);

            foreach (Offer offer in localizedOffers.Items)
            {
                if (offer?.Product?.Id != null && offerLogosIndex.ContainsKey(offer.Product.Id))
                {
                    // this offer product has already been indexed, skip it
                    continue;
                }

                foreach (IOfferLogoMatcher offerLogoMatcher in offerLogoMatchers)
                {
                    string logo = offerLogoMatcher.Match(offer);

                    if (!string.IsNullOrWhiteSpace(logo))
                    {
                        // logo matched, add it to the index
                        offerLogosIndex.Add(offer.Product.Id, logo);
                        break;
                    }
                }
            }

            isIndexed       = true;
            lastIndexedTime = DateTime.Now;
        }
        /// <summary>
        /// Checks if the partner offers had been configured or not.
        /// </summary>
        /// <returns>True if the offers were configured and stored, false otherwise.</returns>
        //public async Task<bool> IsConfiguredAsync()
        //{
        //    return (await RetrieveAsync().ConfigureAwait(false)).Any(offer => !offer.IsInactive);
        //}

        /// <summary>
        /// Fetches all Microsoft CSP offers.
        /// </summary>
        /// <returns>A list of all Microsoft CSP offers.</returns>
        public async Task <IEnumerable <MicrosoftOffer> > RetrieveMicrosoftOffersAsync()
        {
            //List<MicrosoftOffer> microsoftOffers = await ApplicationDomain.CachingService
            //    .FetchAsync<List<MicrosoftOffer>>(MicrosoftOffersCacheKey).ConfigureAwait(false);

            //if (microsoftOffers == null)
            //{
            // Need to manage this based on the offer locale supported by the Offer API. Either its english or using one of the supported offer locale to retrieve localized offers for the store front.
            IPartner localeSpecificPartnerCenterClient = ApplicationDomain.PartnerCenterClient.With(RequestContextFactory.Instance.Create(ApplicationDomain.PortalLocalization.OfferLocale));

            // Offers.ByCountry is required to pull country / region specific offers.
            Microsoft.Store.PartnerCenter.Models.ResourceCollection <Microsoft.Store.PartnerCenter.Models.Offers.Offer> partnerCenterOffers = await localeSpecificPartnerCenterClient.Offers.ByCountry(ApplicationDomain.PortalLocalization.CountryIso2Code).GetAsync().ConfigureAwait(false);

            IEnumerable <Microsoft.Store.PartnerCenter.Models.Offers.Offer> eligibleOffers = partnerCenterOffers?.Items.Where(offer =>
                                                                                                                              !offer.IsAddOn &&
                                                                                                                              (offer.PrerequisiteOffers == null || !offer.PrerequisiteOffers.Any()) &&
                                                                                                                              offer.IsAvailableForPurchase);

            List <MicrosoftOffer> microsoftOffers = new List <MicrosoftOffer>();

            if (eligibleOffers != null)
            {
                foreach (Microsoft.Store.PartnerCenter.Models.Offers.Offer partnerCenterOffer in eligibleOffers)
                {
                    microsoftOffers.Add(new MicrosoftOffer()
                    {
                        Offer = partnerCenterOffer,
                        // ThumbnailUri = new Uri(await ApplicationDomain.MicrosoftOfferLogoIndexer.GetOfferLogoUriAsync(partnerCenterOffer).ConfigureAwait(false), UriKind.Relative)
                    });
                }
            }

            // cache the Microsoft offers for one day
            //await ApplicationDomain.CachingService.StoreAsync(
            //    MicrosoftOffersCacheKey,
            //    microsoftOffers,
            //    TimeSpan.FromDays(1)).ConfigureAwait(false);
            //}

            return(microsoftOffers);
        }