public async Task <IActionResult> GetRoles(RolesParametersModel parameters) { if (parameters.Limit < Configurations.MinLimit || parameters.Limit > Configurations.MaxLimit) { return(await Error(HttpStatusCode.BadRequest, "limit", "Invalid limit parameter")); } if (parameters.Page < Configurations.DefaultPageValue) { return(await Error(HttpStatusCode.BadRequest, "page", "Invalid request parameters")); } IList <RoleDto> rolesDto = _roleApiService.GetRoles( parameters.Ids, parameters.Limit, parameters.Page, parameters.SinceId, parameters.PermissionIds) .Select(role => role.ToDto()).ToList(); var rolesRootObject = new RolesRootObject { Roles = rolesDto }; var json = JsonFieldsSerializer.Serialize(rolesRootObject, parameters.Fields); return(new RawJsonActionResult(json)); }
public async Task <IActionResult> CreateRole([ModelBinder(typeof(JsonModelBinder <RoleDto>))] Delta <RoleDto> roleDelta) { if (!ModelState.IsValid) { return(await Error()); } var newRole = _factory.Initialize(); roleDelta.Merge(newRole); await UserService.InsertRoleAsync(newRole); //permission if (roleDelta.Dto.PermissionIds.Count > 0) { await AddValidPermissions(roleDelta, newRole); await UserService.UpdateRoleAsync(newRole); } await UserActivityService.InsertActivityAsync("AddNewRole", $"Added a new role (ID = {newRole.Id})", newRole); var newRoleDto = newRole.ToDto(); var rootObj = new RolesRootObject(); rootObj.Roles.Add(newRoleDto); var json = JsonFieldsSerializer.Serialize(rootObj, string.Empty); return(new RawJsonActionResult(json)); }
public async Task <IActionResult> GetRoleById(int id, string fields = "") { if (id <= 0) { return(await Error(HttpStatusCode.BadRequest, "id", "invalid id")); } var role = _roleApiService.GetRoleById(id); if (role == null) { return(await Error(HttpStatusCode.NotFound, "role", "not found")); } var rootObj = new RolesRootObject(); rootObj.Roles.Add(role.ToDto()); var json = JsonFieldsSerializer.Serialize(rootObj, fields); return(new RawJsonActionResult(json)); }
public async Task <IActionResult> UpdateRole([ModelBinder(typeof(JsonModelBinder <RoleDto>))] Delta <RoleDto> roleDelta) { if (!ModelState.IsValid) { return(await Error()); } var currentRole = _roleApiService.GetRoleById(roleDelta.Dto.Id); if (currentRole == null) { return(await Error(HttpStatusCode.NotFound, "role", "not found")); } roleDelta.Merge(currentRole); //permission if (roleDelta.Dto.PermissionIds.Count > 0) { await AddValidPermissions(roleDelta, currentRole); } await UserService.UpdateRoleAsync(currentRole); await UserActivityService.InsertActivityAsync("EditRole", $"Edited a role (ID = {currentRole.Id})", currentRole); var roleDto = currentRole.ToDto(); var rootObj = new RolesRootObject(); rootObj.Roles.Add(roleDto); var json = JsonFieldsSerializer.Serialize(rootObj, string.Empty); return(new RawJsonActionResult(json)); }