public async Task DeleteMenuAsync(DomainModel.Entities.Menu menu)
        {
            try
            {
                if (menu == null)
                {
                    string msg = $"Null input: {nameof(ControlPanelWebClient)}.{nameof(DeleteMenuAsync)}";
                    throw new ControlPanelWebApiClientArgumentNullException(msg);
                }

                string             url     = $"{ControlPanelWebApi.Menu.Base}/{ControlPanelWebApi.Menu.Delete}";
                HttpRequestMessage request = new HttpRequestMessage(
                    HttpMethod.Delete,
                    url);

                string        json          = JsonConvert.SerializeObject(menu);
                StringContent stringContent = new StringContent(json, Encoding.UTF8);
                stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                request.Content = stringContent;

                HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(request)
                                                          .ConfigureAwait(false);

                httpResponseMessage.EnsureSuccessStatusCode();
            }
            catch (Exception e)
            {
                throw new ControlPanelWebApiClientDeleteMenuAsyncOperationException(e.Message, e);
            }
        }
Exemplo n.º 2
0
        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);
                }
            }
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void Throw_ValidationException_WhenInputIsInvalid(
            int id,
            string name,
            string desc,
            int moduleId,
            int isActive,
            string menuRoute)
        {
            // Arrange
            DomainModel.Entities.Menu menu = new DomainModel.Entities.Menu
            {
                Id          = id,
                Name        = name,
                Description = desc,
                ModuleId    = moduleId,
                IsActive    = isActive,
                MenuRoute   = menuRoute,
            };

            // Act
            Func <Task> action = async() => { await MenuBusinessLogic.AddAsync(menu).ConfigureAwait(false); };

            // Assert
            action.Should().ThrowExactly <MenuBusinessLogicAddAsyncOperationException>()
            .WithInnerException <ValidationException>();
        }
Exemplo n.º 6
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);
                }
            }
        }
Exemplo n.º 7
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);
                }
            }
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        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);
                    }
                }
            }
        }