示例#1
0
        public async Task <DomainModel.Entities.Module> FindAsync(DomainModel.Entities.Module module)
        {
            using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions))
            {
                try
                {
                    if (module == null)
                    {
                        string msg = $"Null input: {nameof(ModuleBusinessLogic)}.{nameof(FindAsync)}";
                        throw new ModuleNullInputException(msg);
                    }

                    await _moduleValidator.ValidateAndThrowAsync(module).ConfigureAwait(false);

                    return(await ctx.Modules
                           .Include(p => p.Menus)
                           .FirstOrDefaultAsync(p => p.Id == module.Id)
                           .ConfigureAwait(false));
                }
                catch (Exception e)
                {
                    throw new ModuleFindAsyncOperationException(e.Message, e.InnerException);
                }
            }
        }
示例#2
0
        public async Task Add_Menu()
        {
            // Arrange
            DomainModel.Entities.Module module = new DomainModel.Entities.Module
            {
                Name        = "asd",
                Description = "desc",
                IsActive    = 1,
                ModuleRoute = "asd",
            };
            DomainModel.Entities.Module moduleResult = await ModuleBusinessLogic.AddAsync(module).ConfigureAwait(false);

            DomainModel.Entities.Menu menu = new DomainModel.Entities.Menu
            {
                Name        = "name",
                Description = "desc",
                ModuleId    = moduleResult.Id,
                IsActive    = 1,
                MenuRoute   = "asd",
            };

            // Act
            DomainModel.Entities.Menu menuResult = await MenuBusinessLogic.AddAsync(menu).ConfigureAwait(false);

            // Assert
            menuResult.Id.Should().BeGreaterThan(0);
            menuResult.Description.Should().Be(menu.Description);
            menuResult.Name.Should().Be(menu.Name);
            menuResult.IsActive.Should().Be(menu.IsActive);
            menuResult.ModuleId.Should().Be(menu.ModuleId);
            menuResult.MenuRoute.Should().Be(menu.MenuRoute);
        }
示例#3
0
        public async Task DeleteAsync(DomainModel.Entities.Module toBeDelete)
        {
            using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions))
            {
                using (var transaction = ctx.Database.BeginTransaction())
                {
                    try
                    {
                        if (toBeDelete == null)
                        {
                            string msg = $"Null input: {nameof(ModuleBusinessLogic)}.{nameof(DeleteAsync)}";
                            throw new ModuleNullInputException(msg);
                        }

                        await _moduleValidator.ValidateAsync(toBeDelete, o =>
                        {
                            o.IncludeRuleSets(ValidatorRulesets.Delete);
                            o.ThrowOnFailures();
                        }).ConfigureAwait(false);

                        List <DomainModel.Entities.Menu> menusToBeDelete = await ctx.Menus
                                                                           .Where(p => p.ModuleId == toBeDelete.Id)
                                                                           .ToListAsync()
                                                                           .ConfigureAwait(false);

                        if (menusToBeDelete.Any())
                        {
                            foreach (DomainModel.Entities.Menu menu in menusToBeDelete)
                            {
                                await _menuBusinessLogic.DeleteAsync(menu).ConfigureAwait(false);
                            }
                        }

                        ctx.Modules.Remove(toBeDelete);
                        await ctx.SaveChangesAsync().ConfigureAwait(false);

                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        throw new ModuleDeleteOperationException(e.Message, e.InnerException);
                    }
                }
            }
        }
示例#4
0
        public async Task <DomainModel.Entities.Module> AddAsync(DomainModel.Entities.Module module)
        {
            using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions))
            {
                using (IDbContextTransaction transaction = ctx.Database.BeginTransaction())
                {
                    try
                    {
                        if (module == null)
                        {
                            string msg = $"Null input: {nameof(ModuleBusinessLogic)}.{nameof(AddAsync)}";
                            throw new ModuleNullInputException(msg);
                        }

                        await _moduleValidator.ValidateAsync(module, o =>
                        {
                            o.IncludeRuleSets(ValidatorRulesets.AddNew);
                            o.ThrowOnFailures();
                        }).ConfigureAwait(false);

                        await ctx.Modules.AddAsync(module).ConfigureAwait(false);

                        await ctx.SaveChangesAsync().ConfigureAwait(false);

                        if (module.Menus.Any())
                        {
                            foreach (DomainModel.Entities.Menu moduleMenu in module.Menus)
                            {
                                await _menuBusinessLogic.AddAsync(moduleMenu).ConfigureAwait(false);
                            }
                        }

                        transaction.Commit();

                        return(module);
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        throw new ModuleAddOperationException(e.Message, e.InnerException);
                    }
                }
            }
        }
示例#5
0
        public async Task Delete_AnItem()
        {
            // Arrange
            DomainModel.Entities.Module module = new DomainModel.Entities.Module
            {
                Name = "asd", Description = "desc", IsActive = 1, ModuleRoute = "asd",
            };
            DomainModel.Entities.Module moduleResult = await ModuleBusinessLogic.AddAsync(module).ConfigureAwait(false);

            DomainModel.Entities.Menu menuActive = new DomainModel.Entities.Menu
            {
                Name        = "name",
                Description = "desc",
                IsActive    = 1,
                ModuleId    = 1,
                MenuRoute   = "asd",
            };
            DomainModel.Entities.Menu menuActiveResult =
                await MenuBusinessLogic.AddAsync(menuActive).ConfigureAwait(false);

            DomainModel.Entities.Menu menuInactive = new DomainModel.Entities.Menu
            {
                Name        = "name",
                Description = "desc",
                IsActive    = 0,
                ModuleId    = 1,
                MenuRoute   = "asd",
            };
            DomainModel.Entities.Menu menuInactiveResult =
                await MenuBusinessLogic.AddAsync(menuInactive).ConfigureAwait(false);

            // Act
            await MenuBusinessLogic.DeleteAsync(menuInactiveResult).ConfigureAwait(false);

            List <DomainModel.Entities.Menu> result = await MenuBusinessLogic.GetAllAsync().ConfigureAwait(false);

            // Assert
            result.Count.Should().Be(1);
        }
        public async Task <DomainModel.Entities.Module> ModifyAsync(DomainModel.Entities.Module modify)
        {
            using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions))
            {
                using (IDbContextTransaction transaction = ctx.Database.BeginTransaction())
                {
                    try
                    {
                        if (modify == null)
                        {
                            string msg = $"Null input: {nameof(ModuleBusinessLogic)}.{nameof(ModifyAsync)}";
                            throw new ModuleNullInputException(msg);
                        }

                        await _moduleValidator.ValidateAsync(modify, o =>
                        {
                            o.IncludeRuleSets(ValidatorRulesets.Modify);
                            o.ThrowOnFailures();
                        }).ConfigureAwait(false);

                        DomainModel.Entities.Module module = await ctx.Modules
                                                             .Include(p => p.Menus)
                                                             .FirstOrDefaultAsync(p => p.Id == modify.Id)
                                                             .ConfigureAwait(false);

                        if (module == null)
                        {
                            string msg = $"Module does not exists with id: {nameof(modify.Id)}";
                            throw new ModuleDoesNotExistsException(msg);
                        }

                        module.Description = modify.Description;
                        module.Name        = modify.Name;
                        module.IsActive    = modify.IsActive;
                        module.ModuleRoute = modify.ModuleRoute;

                        await _moduleValidator.ValidateAsync(module, o =>
                        {
                            o.IncludeProperties(ValidatorRulesets.Modify);
                            o.ThrowOnFailures();
                        }).ConfigureAwait(false);

                        ctx.Entry(module).State = EntityState.Modified;
                        await ctx.SaveChangesAsync().ConfigureAwait(false);

                        List <long> alreadyAttachedMenuIds      = module.Menus.Select(p => p.Id).ToList();
                        List <long> modifiedListOfShouldBeAdded = modify.Menus.Select(p => p.Id).ToList();

                        List <long> diff = alreadyAttachedMenuIds.Except(modifiedListOfShouldBeAdded).ToList();

                        if (diff.Count > 0)
                        {
                            foreach (long l in diff)
                            {
                                DomainModel.Entities.Menu toBeDeleted = new DomainModel.Entities.Menu
                                {
                                    Id = l,
                                };
                                await _menuBusinessLogic.DeleteAsync(toBeDeleted).ConfigureAwait(false);
                            }
                        }

                        transaction.Commit();

                        return(module);
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        throw new ModuleModifyAsyncOperationException(e.Message, e.InnerException);
                    }
                }
            }
        }