Пример #1
0
        public async Task <IActionResult> Buy(PromotionBindingModel bindingModel)
        {
            await promotionsService.CreatePromotionOrderAsync(bindingModel.PromotionInputModel.AdId,
                                                              bindingModel.PromotionInputModel.PromotionId);

            TempData["SuccessfullyPromotedMessage"] = SuccessfullyPromotedMessage;

            return(RedirectToAction("Index", new{ adId = bindingModel.PromotionInputModel.AdId }));
        }
Пример #2
0
        public async Task <PromotionBindingModel> GetPromotionBindingModelByAdIdAsync(int adId)
        {
            if (!await context.Ads.AnyAsync(x => x.Id == adId))
            {
                throw new ArgumentException(GlobalConstants.InvalidAdIdErrorMessage);
            }

            var adFromDb = await adsService.GetAdByIdAsync(adId);

            var promotionViewModels = await context
                                      .Promotions
                                      .To <PromotionViewModel>()
                                      .ToListAsync();

            var promotionBindingModel = new PromotionBindingModel
            {
                AdId                = adFromDb.Id,
                AdTitle             = adFromDb.Title,
                PromotionViewModels = promotionViewModels
            };

            return(promotionBindingModel);
        }
Пример #3
0
        public async Task GetPromotionBindingModelByAdIdAsync_WithValidData_ShouldReturnCorrectPromotionBindingModel()
        {
            //Arrange
            var expected = new PromotionBindingModel
            {
                AdId                = 1,
                AdTitle             = "Iphone 6s",
                PromotionViewModels = new List <PromotionViewModel>
                {
                    new PromotionViewModel
                    {
                        ActiveDays = 10,
                        Price      = 3.50M,
                        Type       = "Silver",
                        Updates    = 3
                    },
                    new PromotionViewModel
                    {
                        ActiveDays = 30,
                        Price      = 8.00M,
                        Type       = "Gold",
                        Updates    = 10
                    }
                }
            };

            var moqAdsService = new Mock <IAdsService>();

            moqAdsService.Setup(x => x.GetAdByIdAsync(1))
            .ReturnsAsync(new Ad
            {
                Id                = 1,
                Title             = "Iphone 6s",
                Description       = "PerfectCondition",
                ActiveFrom        = DateTime.UtcNow,
                ActiveTo          = DateTime.UtcNow.AddDays(30),
                AvailabilityCount = 1,
                Price             = 120,
                Condition         = new Condition {
                    Name = "Brand New"
                },
                Address = new Address
                {
                    Country      = "Bulgaria",
                    City         = "Sofia",
                    Street       = "Ivan Vazov",
                    District     = "Student city",
                    ZipCode      = 1000,
                    PhoneNumber  = "0895335532",
                    EmailAddress = "*****@*****.**"
                }
            });

            var context = InitializeContext.CreateContextForInMemory();

            promotionsService = new PromotionsService(context, moqAdsService.Object);
            var testingAd = new Ad
            {
                Id                = 1,
                Title             = "Iphone 6s",
                Description       = "PerfectCondition",
                ActiveFrom        = DateTime.UtcNow,
                ActiveTo          = DateTime.UtcNow.AddDays(30),
                AvailabilityCount = 1,
                Price             = 120,
                Condition         = new Condition {
                    Name = "Brand New"
                },
                Address = new Address
                {
                    Id           = 1,
                    Country      = "Bulgaria",
                    City         = "Sofia",
                    Street       = "Ivan Vazov",
                    District     = "Student city",
                    ZipCode      = 1000,
                    PhoneNumber  = "0895335532",
                    EmailAddress = "*****@*****.**"
                }
            };

            var testingPromotions = new List <Promotion>
            {
                new Promotion
                {
                    ActiveDays = 10,
                    Price      = 3.50M,
                    Type       = "silver",
                    Updates    = 3
                },
                new Promotion
                {
                    ActiveDays = 30,
                    Price      = 8.00M,
                    Type       = "gold",
                    Updates    = 10
                }
            };

            await context.Ads.AddAsync(testingAd);

            await context.Promotions.AddRangeAsync(testingPromotions);

            await context.SaveChangesAsync();

            //Act
            var actual = await promotionsService.GetPromotionBindingModelByAdIdAsync(1);

            Assert.Equal(expected.AdId, actual.AdId);
            Assert.Equal(expected.AdTitle, actual.AdTitle);
            Assert.Collection(actual.PromotionViewModels,
                              elem1 =>
            {
                Assert.Equal(expected.PromotionViewModels[0].ActiveDays, elem1.ActiveDays);
                Assert.Equal(expected.PromotionViewModels[0].Price, elem1.Price);
                Assert.Equal(expected.PromotionViewModels[0].Type, elem1.Type);
                Assert.Equal(expected.PromotionViewModels[0].Updates, elem1.Updates);
            },
                              elem2 =>
            {
                Assert.Equal(expected.PromotionViewModels[1].ActiveDays, elem2.ActiveDays);
                Assert.Equal(expected.PromotionViewModels[1].Price, elem2.Price);
                Assert.Equal(expected.PromotionViewModels[1].Type, elem2.Type);
                Assert.Equal(expected.PromotionViewModels[1].Updates, elem2.Updates);
            });

            Assert.Equal(expected.PromotionViewModels.Count, actual.PromotionViewModels.Count);
        }