public async Task <DomainModel.Entities.Menu> AddAsync(DomainModel.Entities.Menu newMenu) { using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions)) { try { if (newMenu == null) { string msg = $"null input: {nameof(MenuBusinessLogic)}.{nameof(AddAsync)}"; throw new MenuNullInputException(msg); } await _menuValidator.ValidateAsync(newMenu, o => { o.IncludeRuleSets(ValidatorRulesets.AddNew); o.ThrowOnFailures(); }) .ConfigureAwait(false); await ctx.Menus.AddAsync(newMenu).ConfigureAwait(false); await ctx.SaveChangesAsync().ConfigureAwait(false); return(newMenu); } catch (Exception e) { throw new MenuBusinessLogicAddAsyncOperationException(e.Message, e); } } }
public async Task DeleteAsync(DomainModel.Entities.Menu toBeDeleted) { using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions)) { try { if (toBeDeleted == null) { string msg = $"Null input: {nameof(MenuBusinessLogic)}.{nameof(DeleteAsync)}"; throw new MenuNullInputException(msg); } await _menuValidator.ValidateAsync(toBeDeleted, o => { o.IncludeRuleSets(ValidatorRulesets.Delete); o.ThrowOnFailures(); }).ConfigureAwait(false); ctx.Menus.Remove(toBeDeleted); await ctx.SaveChangesAsync().ConfigureAwait(false); } catch (Exception e) { throw new MenuBusinessLogicDeleteAsyncOperationException(e.Message, e); } } }
public async Task <DomainModel.Entities.Menu> FindAsync(DomainModel.Entities.Menu menu) { using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions)) { try { if (menu == null) { string msg = $"Input null: {nameof(MenuBusinessLogic)}.{nameof(FindAsync)}"; throw new MenuNullInputException(msg); } await _menuValidator.ValidateAsync(menu, o => { o.IncludeRuleSets(ValidatorRulesets.Find); o.ThrowOnFailures(); }).ConfigureAwait(false); return(await ctx.Menus.FindAsync(menu.Id).ConfigureAwait(false)); } catch (Exception e) { throw new MenuFindAsyncOperationException(e.Message, e.InnerException); } } }
public TestBase(string testInfo) { string msg = $"{nameof(testInfo)} cannot be empty."; Check.NotNullOrEmptyOrWhitespace(testInfo, msg); string fileName = $"Data Source={testInfo}.sqlite"; MenuValidator menuValidator = new MenuValidator(); ModuleValidator moduleValidator = new ModuleValidator(); DbContextOptions <ControlPanelContext> dbContext = new DbContextOptionsBuilder <ControlPanelContext>() .UseSqlite(fileName) .UseLoggerFactory(LoggerFactory) .EnableDetailedErrors() .EnableSensitiveDataLogging() .Options; MenuBusinessLogic = new MenuBusinessLogic(menuValidator, dbContext); ModuleBusinessLogic = new ModuleBusinessLogic(MenuBusinessLogic, moduleValidator, dbContext); using (ControlPanelContext ctx = new ControlPanelContext(dbContext)) { ctx.Database.EnsureDeleted(); ctx.Database.EnsureCreated(); } }
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); } } }
public static void Populate(ControlPanelContext ctx) { if (ctx != null) { AddModules(ctx); AddMenusToModules(ctx); } }
public async Task <List <DomainModel.Entities.Menu> > GetAllAsync() { using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions)) { try { return(await ctx.Menus.ToListAsync().ConfigureAwait(false)); } catch (Exception e) { throw new MenuGetAllAsyncOperationException(e.Message, e); } } }
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); } } } }
public async Task <List <DomainModel.Entities.Module> > GetAllAsync() { using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions)) { try { return(await ctx.Modules .Include(p => p.Menus) .ToListAsync() .ConfigureAwait(false)); } catch (Exception e) { throw new ModuleGetAllActiveAsyncOperationException(e.Message, e.InnerException); } } }
private static void AddModules(ControlPanelContext ctx) { Module teamManager = new Module { Name = "Team Manager", Description = "Module for managing teams", ModuleRoute = "teammanager", IsActive = 1, }; ctx.Modules.Add(teamManager); ctx.SaveChanges(); Module moneyTracker = new Module { Name = "Money Tracker", Description = "Module for tracking money", ModuleRoute = "money_tracker", IsActive = 1, }; ctx.Modules.Add(moneyTracker); ctx.SaveChanges(); Module masterData = new Module { Name = "Master Data", Description = "Module for managing Master Data", ModuleRoute = "masterdata", IsActive = 1, }; ctx.Modules.Add(masterData); ctx.SaveChanges(); Module controlPanelModule = new Module { Name = "Control Panel", Description = "Control Panel Module", ModuleRoute = "controlpanel", IsActive = 1, }; ctx.Modules.Add(controlPanelModule); ctx.SaveChanges(); }
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); } } } }
public async Task <DomainModel.Entities.Menu> ModifyAsync(DomainModel.Entities.Menu modified) { using (ControlPanelContext ctx = new ControlPanelContext(_dbContextOptions)) { try { if (modified == null) { string msg = $"Null input: {nameof(MenuBusinessLogic)}.{nameof(ModifyAsync)}"; throw new MenuNullInputException(msg); } await _menuValidator.ValidateAndThrowAsync(modified).ConfigureAwait(false); DomainModel.Entities.Menu toBeModified = await ctx.Menus.FindAsync(modified.Id).ConfigureAwait(false); if (toBeModified == null) { string msg = $"There is no menu with id: {modified.Id}"; throw new MenuNoSuchMenuException(msg); } toBeModified.Description = modified.Description; toBeModified.Name = modified.Name; toBeModified.IsActive = modified.IsActive; toBeModified.ModuleId = modified.ModuleId; toBeModified.MenuRoute = modified.MenuRoute; await _menuValidator.ValidateAndThrowAsync(toBeModified).ConfigureAwait(false); ctx.Entry(toBeModified).State = EntityState.Modified; await ctx.SaveChangesAsync().ConfigureAwait(false); return(toBeModified); } catch (Exception e) { throw new MenuModifyOperationException(e.Message, e); } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ControlPanelContext context) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); dbInitializer.Initialize(context); }
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); } } } }
public JuegosController(ControlPanelContext context) { _context = context; }
private static void AddMenusToControlPanel(ControlPanelContext ctx) { Module controlPanel = ctx.Modules.FirstOrDefault(d => d.Name.Equals("Control Panel")); if (controlPanel != null) { Menu modulesListMenu = new Menu { Name = "Modules", Description = "Available modules in the system", IsActive = 1, MenuRoute = "modules", ModuleId = controlPanel.Id, }; ctx.Menus.Add(modulesListMenu); ctx.SaveChanges(); Menu availableMenusList = new Menu { Name = "Menus", Description = "Available menus in the system", IsActive = 1, MenuRoute = "menus", ModuleId = controlPanel.Id, }; ctx.Menus.Add(availableMenusList); ctx.SaveChanges(); } else { throw new NullReferenceException("There is no controlpanel entity"); } Module masterDataModule = ctx.Modules.FirstOrDefault(d => d.Name.Equals("Master Data")); if (masterDataModule != null) { Menu dimension = new Menu { Name = "Dimension", Description = "Dimensions in the system", IsActive = 1, MenuRoute = "dimension", ModuleId = masterDataModule.Id, }; ctx.Menus.Add(dimension); ctx.SaveChanges(); Menu topDimensionStructure = new Menu { Name = "Source formats", Description = "Source formats description", IsActive = 1, MenuRoute = "sourceformats", ModuleId = masterDataModule.Id, }; ctx.Menus.Add(topDimensionStructure); ctx.SaveChanges(); Menu dimensionStructure = new Menu { Name = "Dimension structures", Description = "Dimension structures", IsActive = 1, MenuRoute = "dimensionstructures", ModuleId = masterDataModule.Id, }; ctx.Menus.Add(dimensionStructure); ctx.SaveChanges(); Menu documentBuilder = new Menu { Name = "SourceFormat Builder", Description = "SourceFormat Builder", IsActive = 1, MenuRoute = "sourceformatbuilder", ModuleId = masterDataModule.Id, }; ctx.Menus.Add(documentBuilder); ctx.SaveChanges(); } else { throw new NullReferenceException("There is no master data entity!"); } }
private static void AddMenusToModules(ControlPanelContext ctx) { AddMenusToControlPanel(ctx); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddServerSideBlazor(); services.AddControllers() .AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects; }) .AddApplicationPart( typeof(MenuController).Assembly) .AddApplicationPart( typeof(DimensionController).Assembly) .SetCompatibilityVersion(CompatibilityVersion.Latest); // if (_env.IsStaging()) // { // services.AddDbContext<TeamManagerContext>(options => // { // options.UseSqlite("Data Source=team_manager_test_db.sqlite"); // }); // } // services.AddTransient<IPeopleBusinessLogic, PeopleBusinessLogic>(); // services.AddTransient<ICompanyBusinessLogic, CompanyBusinessLogic>(); // services.AddTransient<IEventBusinessLogic, EventBusinessLogic>(); // services.AddTransient<IPositionBusinessLogic, PositionBusinessLogic>(); // services.AddTransient<ITitleBusinessLogic, TitleBusinessLogic>(); // services.AddTransient<IPeopleEventLogBusinessLogic, PeopleEventLogBusinessLogic>(); // services.AddTransient<PeopleValidator>(); // services.AddTransient<CompanyValidator>(); // services.AddTransient<EventValidator>(); // services.AddTransient<PositionValidator>(); // services.AddTransient<TitleValidator>(); // services.AddTransient<PeopleEventLogValidator>(); // if (_env.IsStaging()) // { services.AddDbContext <ControlPanelContext>(options => { options.UseSqlite("Data Source=control_panel_test_db.sqlite"); }); // } services.AddTransient <IMenuBusinessLogic, MenuBusinessLogic>(); services.AddTransient <MenuValidator>(); services.AddTransient <IModuleBusinessLogic, ModuleBusinessLogic>(); services.AddTransient <ModuleValidator>(); // if (_env.IsStaging()) // { services.AddDbContext <MasterDataContext>(options => { options.UseSqlite("Data Source=master_data_test_db.sqlite"); options.UseLoggerFactory(LoggerFactory); options.EnableSensitiveDataLogging(); }); // } services.AddTransient <IMasterDataBusinessLogic, MasterDataBusinessLogic>(); services.AddTransient <IMasterDataDimensionBusinessLogic, MasterDataDimensionBusinessLogic>(); services.AddTransient <IMasterDataDimensionStructureBusinessLogic, MasterDataDimensionStructureBusinessLogic>(); services.AddTransient <IMasterDataDimensionStructureNodeBusinessLogic, MasterDataDimensionStructureNodeBusinessLogic>(); services.AddTransient <IMasterDataDimensionValueBusinessLogic, MasterDataDimensionValueBusinessLogic>(); services.AddTransient <IMasterDataSourceFormatBusinessLogic, MasterDataSourceFormatBusinessLogic>(); // Validators services.AddTransient <IMasterDataValidators, MasterDataValidators>(); services.AddTransient <DimensionValidator>(); services.AddTransient <MasterDataDimensionValueValidator>(); services.AddTransient <SourceFormatValidator>(); services.AddTransient <DimensionStructureValidator>(); services.AddTransient <DimensionStructureNodeValidator>(); services.AddTransient <DimensionStructureDimensionStructureValidator>(); services.AddTransient <DimensionStructureQueryObjectValidator>(); services.AddTransient <SourceFormatDimensionStructureNodeValidator>(); ServiceProvider serviceProvider = services.BuildServiceProvider(); if (_env.IsStaging()) { using (IServiceScope scope = serviceProvider.CreateScope()) { IServiceProvider scopeServices = scope.ServiceProvider; MasterDataContext masterDataContext = scopeServices.GetRequiredService <MasterDataContext>(); masterDataContext.Database.EnsureDeleted(); masterDataContext.Database.EnsureCreated(); // MasterDataDataSample.Populate(masterDataContext); ControlPanelContext controlPanelContext = scopeServices.GetRequiredService <ControlPanelContext>(); controlPanelContext.Database.EnsureDeleted(); controlPanelContext.Database.EnsureCreated(); ControlPanelDataSample.Populate(controlPanelContext); // TeamManagerContext teamManagerContext = scopeServices.GetRequiredService<TeamManagerContext>(); // teamManagerContext.Database.EnsureDeleted(); // teamManagerContext.Database.EnsureCreated(); } } }