I have 2 table in database, lets say I delete a row (Role) inside of Role Table,
How can i also delete away the particular role ID related to the deleted role inside the User Table, which contains the user information (with a column which contains a List with all the Role ID)? Should I do it in the backend (.NET) or should I do it in the frontend (Javascript - by using update API with the delete axios API call in javascript)
(Backend ASP.NET) RoleController and the delete API (Extra Note: A role contains many permission):
[HttpDelete("role/{id}")]
public async Task<ServiceResponse> DeleteRole(Guid id)
{
return await _service.DeleteRoleAsync(id);
}
(Backend ASP.NET) The other entity (UserRole Entity) which contains the ID of the deleted permission:
{
public class UserRole : Entity
{
public string Email { get; set; }
public List<Guid> RoleIds { get; set; }
public List<string> ProjectIds { get; set; }
}
}
(Backend ASP.NET) UserRoleController with PUT api to update roles for the user:
[HttpPut("user-role")]
public async Task<ServiceResponse> UpdateRolesForUser(UserRoleRequest request)
{
return await _service.UpdateUserRoles(request);
}
(Backend ASP.NET) UserRequest PUT api for Update:
{
public class UserRoleRequest
{
[EmailAddress]
public string Email { get; set; }
public List<Guid> RoleIds { get; set; }
public List<string> ProjectIds { get; set; }
}
}
(Frontend) axios async function used to delete the Role:
async function deleteRole(id) {
return await axios.delete(`/RolePermission/role/${id}`);
}
(Frontend) The prompt message which will call handleDeleteItem to delete a role:
const onDeleteRole = (record) => {
Modal.confirm({
title: "Are you sure you want to delete this permission?",
okText: "Yes",
okType: "danger",
onOk: () => {
handleDeleteItem([record.id]);
},
});
};
(Frontend) handleDeleteItem - which deletes the role but not the roleIds in the user table:
const handleDeleteItem = async (id = null) => {
await deleteRole(id);//delete the role
await getDataRole();//refresh table
};