Ejemplo n.º 1
0
        async public void Ignore_Category_Relation_Has_Other()
        {
            // Assign
            var result   = true;
            var category = new Category
            {
                Faqs = new List <Faq>
                {
                    new Faq(),
                    new Faq(),
                },
            };

            DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(category));

            using (var context = DbContextFake.GetDbContext <IntranetApiContext>())
            {
                // Act
                result = await CategoryHelpers.HasNoRelatedEntities(context.Categories.SingleOrDefault(), context, category.Faqs.First());
            }

            using (var context = DbContextFake.GetDbContext <IntranetApiContext>())
            {
                // Assert
                Assert.False(result);
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var faq = await _context.Faqs
                      .Include(f => f.FaqTags)
                      .ThenInclude(ft => ft.Tag)
                      .Include(f => f.Category)
                      .ThenInclude(c => c.Faqs)
                      .SingleOrDefaultAsync(f => f.Id == id);

            if (faq.IsNull())
            {
                return(NotFound());
            }

            _context.Remove(faq);

            // If the category has no more related entities then delete the category as well
            if (await CategoryHelpers.HasNoRelatedEntities(faq.Category, _context, ignore: faq))
            {
                _context.Remove(faq.Category);
            }

            // If the tags has no more related entities then delete the category as well
            foreach (var tag in faq.FaqTags.Select(ft => ft.Tag))
            {
                if (await TagHelpers.HasNoRelatedEntities(tag, _context, ignore: faq.FaqTags))
                {
                    _context.Remove(tag);
                }
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Ejemplo n.º 3
0
        async public void No_Category_Relations()
        {
            // Assign
            var result = false;

            DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(new Category()));

            using (var context = DbContextFake.GetDbContext <IntranetApiContext>())
            {
                // Act
                result = await CategoryHelpers.HasNoRelatedEntities(context.Categories.SingleOrDefault(), context);
            }

            using (var context = DbContextFake.GetDbContext <IntranetApiContext>())
            {
                // Assert
                Assert.True(result);
            }
        }
Ejemplo n.º 4
0
        async public void Ignore_Category_Relation_Has_No_Other()
        {
            // Assign
            var result = false;

            DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(new Category()));
            DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(new Faq()), ensureDeleted: false);
            DbContextFake.SeedDb <IntranetApiContext>(c => c.Faqs.SingleOrDefault().Category = c.Categories.SingleOrDefault(), ensureDeleted: false);

            using (var context = DbContextFake.GetDbContext <IntranetApiContext>())
            {
                // Act
                result = await CategoryHelpers.HasNoRelatedEntities(context.Categories.SingleOrDefault(), context, context.Faqs.SingleOrDefault());
            }

            using (var context = DbContextFake.GetDbContext <IntranetApiContext>())
            {
                // Assert
                Assert.True(result);
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var policy = await _context.Policies
                         .Include(p => p.PolicyTags)
                         .ThenInclude(pt => pt.Tag)
                         .Include(p => p.Category)
                         .ThenInclude(c => c.Policies)
                         .SingleOrDefaultAsync(p => p.Id == id);

            if (policy.IsNull())
            {
                return(NotFound());
            }

            // TODO: Delete blob as well

            _context.Remove(policy);

            // If the category has no more related entities then delete the category as well
            if (await CategoryHelpers.HasNoRelatedEntities(policy.Category, _context, ignore: policy))
            {
                _context.Remove(policy.Category);
            }

            // If the tags has no more related entities then delete the tag as well
            foreach (var tag in policy.PolicyTags.Select(ft => ft.Tag))
            {
                if (await TagHelpers.HasNoRelatedEntities(tag, _context, ignore: policy.PolicyTags))
                {
                    _context.Remove(tag);
                }
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }