Exemplo n.º 1
0
        public async Task <bool> ModifyAdvertByIdAsync(ModifyAdvertInputModel model)
        {
            var advert = await this.dbContext.Adverts.SingleOrDefaultAsync(x => x.Id == model.Id);

            if (advert != null)
            {
                if (!string.IsNullOrEmpty(model.IssuerName))
                {
                    advert.IssuerName = model.IssuerName;
                }

                if (!string.IsNullOrEmpty(model.Content))
                {
                    advert.Content = model.Content;
                }

                if (model.ViewsLeft >= 0)
                {
                    advert.ViewsLeft = model.ViewsLeft;
                }

                advert.Type = model.Type;
                await this.dbContext.SaveChangesAsync();

                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        public async Task AddAdvertAsync(ModifyAdvertInputModel model)
        {
            if (!string.IsNullOrEmpty(model.IssuerName) && model.ViewsLeft > 0)
            {
                await this.dbContext.Adverts.AddAsync(new Advert()
                {
                    IssuerName = model.IssuerName,
                    Type       = model.Type,
                    Content    = model.Content,
                    ViewsLeft  = model.ViewsLeft
                });

                await this.dbContext.SaveChangesAsync();
            }
        }
Exemplo n.º 3
0
 public async Task <IActionResult> Modify(ModifyAdvertInputModel model)
 {
     if (ModelState.IsValid)
     {
         if (await this.advertService.AdvertExistsAsync(model.Id) || model.Id != null)
         {
             await this.advertService.ModifyAdvertByIdAsync(model);
         }
         else
         {
             await this.advertService.AddAdvertAsync(model);
         }
     }
     return(RedirectToAction("Modify"));
 }
Exemplo n.º 4
0
        public async Task ModifyAdvertByIdAsync_ShouldModify_Succesfully()
        {
            var advert = new Advert()
            {
                Id         = "1234",
                Content    = "Advcontent",
                IssuerName = "Gosho",
                Type       = AdvertType.Group,
                ViewsLeft  = 50
            };
            var advertInputModel = new ModifyAdvertInputModel()
            {
                Id         = "1234",
                Content    = "ModifiedContent",
                IssuerName = "Mitko",
                Type       = AdvertType.User,
                ViewsLeft  = 0
            };

            await this.dbContext.Adverts.AddAsync(advert);

            await this.dbContext.SaveChangesAsync();


            var result = await this.advertService.ModifyAdvertByIdAsync(advertInputModel);

            Assert.True(result);

            var expected = new Advert()
            {
                Id         = "1234",
                Content    = "ModifiedContent",
                IssuerName = "Mitko",
                Type       = AdvertType.User,
                ViewsLeft  = 0
            };
            var actual = await this.dbContext.Adverts.SingleOrDefaultAsync(x => x.Id == expected.Id);

            // Easier to compare all properties
            var expectedString = JsonConvert.SerializeObject(expected);
            var actualString   = JsonConvert.SerializeObject(actual);

            Assert.Equal(expectedString, actualString);
        }
Exemplo n.º 5
0
        public async Task AddAsync_ShouldAddAddon_Succesfully()
        {
            var advert1 = new ModifyAdvertInputModel()
            {
                Content    = "Advcontent",
                IssuerName = "Gosho",
                Type       = AdvertType.Group,
                ViewsLeft  = 50
            };
            var advert2 = new ModifyAdvertInputModel()
            {
                Content    = "ADV CONTENT 2",
                IssuerName = "Pesho",
                Type       = AdvertType.User,
                ViewsLeft  = 1
            };

            await this.advertService.AddAdvertAsync(advert1);

            await this.advertService.AddAdvertAsync(advert2);

            var advertsCount = await this.dbContext.Adverts.CountAsync();

            Assert.Equal(2, advertsCount);

            var advert1Result = await this.dbContext.Adverts.SingleOrDefaultAsync(x => x.IssuerName == advert1.IssuerName);

            var advert2Result = await this.dbContext.Adverts.SingleOrDefaultAsync(x => x.IssuerName == advert2.IssuerName);

            Assert.Equal(advert1.Content, advert1Result.Content);
            Assert.Equal(advert1.IssuerName, advert1Result.IssuerName);
            Assert.Equal(advert1.ViewsLeft, advert1Result.ViewsLeft);
            Assert.Equal(advert1.Type, advert1Result.Type);

            Assert.Equal(advert2.Content, advert2Result.Content);
            Assert.Equal(advert2.IssuerName, advert2Result.IssuerName);
            Assert.Equal(advert2.ViewsLeft, advert2Result.ViewsLeft);
            Assert.Equal(advert2.Type, advert2Result.Type);
        }