private Category mappingCategory(CategoryServiceViewModel userVm)
        {
            Category category = new Category();

            category.Id           = userVm.Id;
            category.CategoryName = userVm.CategoryName;
            category.Description  = userVm.Description;
            category.ImgPath      = userVm.ImgPath;
            return(category);
        }
Exemplo n.º 2
0
        public async Task <CategoryServiceViewModel> ExecuteAsync(int id)
        {
            var CategoryItem = await _categoryRepository.FindByIdAsync(id);

            CategoryServiceViewModel categoryViewModels = new CategoryServiceViewModel
            {
                Id           = CategoryItem.Id,
                CategoryName = CategoryItem.CategoryName,
                Description  = CategoryItem.Description
            };

            return(categoryViewModels);
        }
        public async Task <IActionResult> EditServices(int id)
        {
            CategoryServiceViewModel model = new CategoryServiceViewModel();

            model.Category          = _context.Categories.Find(id);
            model.ServiceCategories = await _context.Services.Include(i => i.Vehicle).Where(i => i.ServiceCategory.Any(c => c.CategoryId == id)).ToListAsync();

            model.Services = await _context.Services.Include(i => i.Vehicle).ToListAsync();

            model.Services = model.Services.Except(model.ServiceCategories);

            return(View(model));
        }
        public async Task <CommandResult <CategoryServiceViewModel> > ExecuteAsync(CategoryServiceViewModel userVm)
        {
            var userId   = _httpContextAccessor.HttpContext.User.Identity.Name;
            var userName = await _userManager.FindByIdAsync(userId);

            try
            {
                //Check user has permission first
                if (await _checkUserIsAdminQuery.ExecuteAsync(userId) || await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.CATEGORY, ActionSetting.CanUpdate))
                {
                    var categoryUpdate = await _categoryRepository.FindByIdAsync(userVm.Id);

                    if (categoryUpdate != null)
                    {
                        categoryUpdate.CategoryName = userVm.CategoryName;
                        categoryUpdate.Description  = userVm.Description;
                        categoryUpdate.ImgPath      = userVm.ImgPath;
                        _categoryRepository.Update(categoryUpdate);
                        await _categoryRepository.SaveAsync();

                        await Logging <UpdateCategoryServiceCommand> .
                        InformationAsync(ActionCommand.COMMAND_UPDATE, userName.UserName, JsonConvert.SerializeObject(userVm));

                        return(new CommandResult <CategoryServiceViewModel>
                        {
                            isValid = true,
                            myModel = userVm,
                        });
                    }
                    else
                    {
                        await Logging <UpdateCategoryServiceCommand> .
                        WarningAsync(ActionCommand.COMMAND_UPDATE, userName.UserName, ErrorMessageConstant.ERROR_CANNOT_FIND_ID);

                        return(new CommandResult <CategoryServiceViewModel>
                        {
                            isValid = false,
                            myModel = userVm,
                            errorMessage = ErrorMessageConstant.ERROR_CANNOT_FIND_ID
                        });
                    }
                }
                else
                {
                    await Logging <UpdateCategoryServiceCommand> .
                    WarningAsync(ActionCommand.COMMAND_UPDATE, userName.UserName, ErrorMessageConstant.ERROR_UPDATE_PERMISSION);

                    return(new CommandResult <CategoryServiceViewModel>
                    {
                        isValid = false,
                        errorMessage = ErrorMessageConstant.ERROR_UPDATE_PERMISSION
                    });
                }
            }
            catch (Exception ex)
            {
                await Logging <UpdateCategoryServiceCommand> .
                ErrorAsync(ex, ActionCommand.COMMAND_UPDATE, userName.UserName, "Has error: ");

                return(new CommandResult <CategoryServiceViewModel>
                {
                    isValid = false,
                    myModel = userVm,
                    errorMessage = ex.InnerException.ToString()
                });
            }
        }
Exemplo n.º 5
0
        public async Task <CommandResult <CategoryServiceViewModel> > ExecuteAsync(int id)
        {
            var userId   = _httpContextAccessor.HttpContext.User.Identity.Name;
            var userName = await _userManager.FindByIdAsync(userId);

            try
            {
                if (await _checkUserIsAdminQuery.ExecuteAsync(userId) || await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.CATEGORY, ActionSetting.CanDelete))
                {
                    var categoryDel = await _categoryRepository.FindByIdAsync(id);

                    if (categoryDel != null)
                    {
                        _categoryRepository.Remove(categoryDel);
                        await _categoryRepository.SaveAsync();

                        var myModelReturn = new CategoryServiceViewModel
                        {
                            CategoryName = categoryDel.CategoryName,
                            Description  = categoryDel.Description,
                            Id           = categoryDel.Id
                        };
                        await Logging <DeleteCategoryServiceCommand> .
                        InformationAsync(ActionCommand.COMMAND_DELETE, userName.UserName, JsonConvert.SerializeObject(myModelReturn));

                        return(new CommandResult <CategoryServiceViewModel>
                        {
                            isValid = true,
                            myModel = myModelReturn
                        });
                    }
                    else
                    {
                        await Logging <DeleteCategoryServiceCommand>
                        .WarningAsync(ActionCommand.COMMAND_DELETE, userName.UserName, ErrorMessageConstant.ERROR_CANNOT_FIND_ID);

                        return(new CommandResult <CategoryServiceViewModel>
                        {
                            isValid = false,
                            errorMessage = ErrorMessageConstant.ERROR_CANNOT_FIND_ID
                        });
                    }
                }
                else
                {
                    await Logging <DeleteCategoryServiceCommand>
                    .WarningAsync(ActionCommand.COMMAND_DELETE, userName.UserName, ErrorMessageConstant.ERROR_DELETE_PERMISSION);

                    return(new CommandResult <CategoryServiceViewModel>
                    {
                        isValid = false,
                        errorMessage = ErrorMessageConstant.ERROR_DELETE_PERMISSION
                    });
                }
            }
            catch (System.Exception ex)
            {
                await Logging <DeleteCategoryServiceCommand> .ErrorAsync(ex, ActionCommand.COMMAND_DELETE, "Has error");

                return(new CommandResult <CategoryServiceViewModel>
                {
                    isValid = false,
                    errorMessage = ex.InnerException.ToString()
                });
            }
        }
        public async Task <CommandResult <CategoryServiceViewModel> > ExecuteAsync(CategoryServiceViewModel userVm)
        {
            var userId   = _httpContextAccessor.HttpContext.User.Identity.Name;
            var userName = await _userManager.FindByIdAsync(userId);

            try
            {
                //Check category has available
                var availableCategory = await _categoryRepository.FindSingleAsync(x => x.CategoryName.ToLower() == userVm.CategoryName.ToLower());

                if (availableCategory != null)
                {
                    return(new CommandResult <CategoryServiceViewModel>
                    {
                        isValid = false,
                        errorMessage = "Category Name has available"
                    });
                }
                if (await _checkUserIsAdminQuery.ExecuteAsync(userId) || await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.CATEGORY, ActionSetting.CanCreate))
                {
                    var mappingCate = mappingCategory(userVm);
                    await _categoryRepository.Add(mappingCate);

                    await _categoryRepository.SaveAsync();

                    var createReturn = new CommandResult <CategoryServiceViewModel>
                    {
                        isValid = true,
                        myModel = new CategoryServiceViewModel
                        {
                            Id           = mappingCate.Id,
                            CategoryName = mappingCate.CategoryName,
                            Description  = mappingCate.Description,
                            ImgPath      = mappingCate.ImgPath
                        },
                    };
                    await Logging <AddCategoryServiceCommand> .
                    InformationAsync(ActionCommand.COMMAND_ADD, userName.UserName, JsonConvert.SerializeObject(createReturn.myModel));

                    return(createReturn);
                }
                else
                {
                    await Logging <AddCategoryServiceCommand> .WarningAsync(ActionCommand.COMMAND_ADD, userName.UserName, ErrorMessageConstant.ERROR_ADD_PERMISSION);

                    return(new CommandResult <CategoryServiceViewModel>
                    {
                        isValid = false,
                        errorMessage = ErrorMessageConstant.ERROR_ADD_PERMISSION
                    });
                }
            }
            catch (System.Exception ex)
            {
                await Logging <AddCategoryServiceCommand> .ErrorAsync(ex, ActionCommand.COMMAND_ADD, userName.UserName, "You have error");

                return(new CommandResult <CategoryServiceViewModel>
                {
                    isValid = false,
                    myModel = userVm,
                    errorMessage = ex.InnerException.ToString()
                });
            }
        }