/// <summary>
        /// Applies business rules to a partner offer.
        /// </summary>
        /// <param name="partnerOffer">The partner offer to normalize.</param>
        public void Normalize(PartnerOffer partnerOffer)
        {
            partnerOffer.AssertNotNull(nameof(partnerOffer));

            // ensure the Microsoft offer ID and other required properties are set
            Guid offerId;

            if (!Guid.TryParse(partnerOffer.Id, out offerId))
            {
                throw new PartnerDomainException(ErrorCode.InvalidInput, Resources.IdMustBeAValidGUID).AddDetail("Field", "Id");
            }

            if (string.IsNullOrWhiteSpace(partnerOffer.MicrosoftOfferId))
            {
                throw new PartnerDomainException(ErrorCode.InvalidInput, Resources.MicrosoftOfferIdMustBeSet).AddDetail("Field", "MicrosoftOfferId");
            }

            partnerOffer.Title.AssertNotEmpty("Offer title");

            if (partnerOffer.Price <= 0)
            {
                throw new PartnerDomainException(ErrorCode.InvalidInput, Resources.OfferPriceShouldBeMoreThanZero).AddDetail("Field", "Price");
            }

            // flatten the offer price based on locale decimal settings.
            partnerOffer.Price = Math.Round(partnerOffer.Price, Resources.Culture.NumberFormat.CurrencyDecimalDigits, MidpointRounding.AwayFromZero);

            partnerOffer.Features = PartnerOfferNormalizer.CleanupEmptyEntries(partnerOffer.Features);
            partnerOffer.Summary  = PartnerOfferNormalizer.CleanupEmptyEntries(partnerOffer.Summary);
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves all the partner offers from persistence.
        /// </summary>
        /// <returns>The partner offers.</returns>
        public async Task <IEnumerable <PartnerOffer> > RetrieveAsync()
        {
            List <PartnerOffer> partnerOffers = await ApplicationDomain.CachingService
                                                .FetchAsync <List <PartnerOffer> >(PartnerOffersCacheKey).ConfigureAwait(false);

            if (partnerOffers == null)
            {
                CloudBlockBlob partnerOffersBlob = await GetPartnerOffersBlobAsync().ConfigureAwait(false);

                if (await partnerOffersBlob.ExistsAsync().ConfigureAwait(false))
                {
                    // download the partner offer BLOB
                    MemoryStream partnerOffersStream = new MemoryStream();
                    await partnerOffersBlob.DownloadToStreamAsync(partnerOffersStream).ConfigureAwait(false);

                    partnerOffersStream.Seek(0, SeekOrigin.Begin);

                    // deserialize the BLOB into a list of Partner offer objects
                    partnerOffers =
                        JsonConvert.DeserializeObject <List <PartnerOffer> >(await new StreamReader(partnerOffersStream).ReadToEndAsync().ConfigureAwait(false));

                    if (partnerOffers != null && partnerOffers.Count > 0)
                    {
                        // apply business rules to the offers
                        PartnerOfferNormalizer offerNormalizer = new PartnerOfferNormalizer();

                        foreach (PartnerOffer partnerOffer in partnerOffers)
                        {
                            offerNormalizer.Normalize(partnerOffer);
                        }
                    }
                }

                partnerOffers = partnerOffers ?? new List <PartnerOffer>();

                // cache the partner offers
                await ApplicationDomain.CachingService.StoreAsync(
                    PartnerOffersCacheKey,
                    partnerOffers).ConfigureAwait(false);
            }

            return(partnerOffers);
        }