Esempio n. 1
0
        public IActionResult CreateRole(RoleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(_userRoleView, model));
            }

            try {
                var existingRole = _userRolesService.GetUserRoleByKeyword(model.SystemKeyword);
                if (existingRole != null)
                {
                    ModelState.AddModelError("", $"Unable to create user role with system keyword: {model.SystemKeyword} because the system keyword is not available. Try something else");
                    return(View(_userRoleView, model));
                }

                var role = AutoMapper.Mapper.Map <RoleViewModel, Role>(model);

                _userRolesService.CreateUserRole(role);
                _activityService.InsertActivity(_workContext.CurrentUser, ActivityLogDefaults.CreateRole, $"Created user role: {role.SystemKeyword}");


                model.Title     = "Create Role";
                ViewBag.Status  = "OK";
                ViewBag.Message = "New role has been created successfully.";
            }
            catch (Exception ex) {
                ModelState.AddModelError("", $"Unable to create user role: {ex.Message}");
                _logFactory.InsertLog(LogLevel.Error, $"Unable to create user role: {ex.Message}", ex.ToString());
            }

            return(View(_userRoleView, model));
        }