示例#1
0
    public async Task <AppSrvResult <long> > CreateAsync(RoleCreationDto input)
    {
        var isExists = (await _cacheService.GetAllRolesFromCacheAsync()).Any(x => x.Name == input.Name);

        if (isExists)
        {
            return(Problem(HttpStatusCode.BadRequest, "该角色名称已经存在"));
        }

        var role = Mapper.Map <SysRole>(input);

        role.Id = IdGenerater.GetNextId();
        await _roleRepository.InsertAsync(role);

        return(role.Id);
    }
 public ActionResult <RoleCreatedConfirmationDto> CreateRole([FromBody] RoleCreationDto role)
 {
     try
     {
         Role newRole = _mapper.Map <Role>(role);
         RoleCreatedConfirmation createdRole = _rolesService.CreateRole(newRole);
         var location = _linkGenerator.GetPathByAction("GetRoleById", "Role", new { roleId = createdRole.RoleId });
         return(Created(location, _mapper.Map <RoleCreatedConfirmationDto>(createdRole)));
     }
     catch (Exception ex)
     {
         if (ex.GetBaseException().GetType() == typeof(UniqueValueViolationException))
         {
             return(StatusCode(StatusCodes.Status409Conflict, ex.Message));
         }
         return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
     }
 }
示例#3
0
 public async Task <ActionResult <long> > CreateAsync([FromBody] RoleCreationDto input)
 {
     return(CreatedResult(await _roleService.CreateAsync(input)));
 }