Exemple #1
0
        public async Task <IActionResult> Create([Bind("JuegoID,Nombre,Categoria,Descripcion,Estado")] Juego juego)
        {
            if (ModelState.IsValid)
            {
                _context.Add(juego);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(juego));
        }
Exemple #2
0
        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);
                }
            }
        }
Exemple #3
0
        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);
                }
            }
        }
Exemple #4
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);
                    }
                }
            }
        }
        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);
                    }
                }
            }
        }
Exemple #6
0
        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);
                }
            }
        }
        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);
                    }
                }
            }
        }