How to delete ID in another table if delete api was called in another table

Viewed 40

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
  };
1 Answers

From your model, it seems that the User model and Role model are not config the relationship using EF core, right? If that is the case, after delete the role from the Role table, you have to call the relates service method to find the relates Users based on the role id, and then update their Role property/column. Refer to the following steps:

[HttpDelete("role/{id}")]
public async Task<ServiceResponse> DeleteRole(Guid id)
{
    //1. based on the role id to query the user table 
    //2. update the user's role.
    //3. call the following method to delete the role from the role table.
    return await _service.DeleteRoleAsync(id);
}

Beside, if you have already config the relationship using EF core, you can use the Cascade Delete to delete the related entity/property. Refer to the following tutorials:

Entity Framework Core Cascade Delete

Cascade Delete

Related