Exemplo n.º 1
0
        public async Task CreateUpdateAdAsync_WithInvalidAdId_ShouldThrowAndInvalidArgumentException()
        {
            //Arrange
            var expectedErrorMessage = "Ad with the given id doesn't exist!";

            var context = InitializeContext.CreateContextForInMemory();

            updatesService = new UpdatesService(context);

            //Act and assert
            var ex = await Assert.ThrowsAsync <ArgumentException>(() => updatesService.CreateUpdateAdAsync(1));

            Assert.Equal(expectedErrorMessage, ex.Message);
        }
Exemplo n.º 2
0
        public async Task CreateUpdateAdAsync_WithValidData_ShouldCreateUpdateAd()
        {
            //Arrange
            var expectedUpdateAdCount = 1;

            var context = InitializeContext.CreateContextForInMemory();

            updatesService = new UpdatesService(context);

            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
                {
                    Country      = "Bulgaria",
                    City         = "Sofia",
                    Street       = "Ivan Vazov",
                    District     = "Student city",
                    ZipCode      = 1000,
                    PhoneNumber  = "0895335532",
                    EmailAddress = "*****@*****.**"
                }
            };

            await context.Ads.AddAsync(testingAd);

            await context.SaveChangesAsync();

            //Act
            await updatesService.CreateUpdateAdAsync(1);

            //Assert
            Assert.Equal(expectedUpdateAdCount, context.UpdateAds.Count());
        }
Exemplo n.º 3
0
        public async Task <bool> UpdateAdByIdAsync(int adId)
        {
            var adFromDb = await GetAdByIdAsync(adId);

            if (adFromDb.Updates > 0)
            {
                adFromDb.ActiveFrom = DateTime.UtcNow;
                adFromDb.ActiveTo   = DateTime.UtcNow.AddDays(GlobalConstants.AdDuration);
                adFromDb.Updates--;

                await updatesService.CreateUpdateAdAsync(adId);

                context.Update(adFromDb);
                await context.SaveChangesAsync();

                return(true);
            }

            return(false);
        }