Skip to content
Snippets Groups Projects
Commit 6f0685e9 authored by Sarthak Shrivastava's avatar Sarthak Shrivastava
Browse files

Added auto cascading removal of organizations if no members left.

parent c3de8878
No related branches found
No related tags found
No related merge requests found
......@@ -10,10 +10,7 @@ import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
@Repository
public class MyOrgRosterRepository implements OrgRosterRepository{
......@@ -174,9 +171,9 @@ public class MyOrgRosterRepository implements OrgRosterRepository{
@Transactional
public Map<String, Object> deleteMember(Integer orgId, String memberEmail)
{
public Map<String, Object> deleteMember(Integer orgId, String memberEmail) {
Map<String, Object> result = new HashMap<>();
try {
// Delete the member from ORGANIZATION_ROSTER
String deleteQuery = "DELETE FROM organization_roster WHERE organization_id = :orgId AND user_email = :memberEmail";
......@@ -193,10 +190,22 @@ public class MyOrgRosterRepository implements OrgRosterRepository{
updateCountNativeQuery.setParameter("orgId", orgId);
int updatedCountRows = updateCountNativeQuery.executeUpdate();
// List<Object> resultList = updateCountNativeQuery.getResultList();
// System.out.println(Arrays.toString(resultList.toArray()));
result.put("result", "success");
result.put("deletedRows", deletedRows);
result.put("updatedCountRows", updatedCountRows);
// Check if the query effected a row, which means the user was present and subsequently removed
if (updatedCountRows == 1) {
String nativeQuery = "DELETE FROM organization WHERE organization_id = :orgId AND NOT EXISTS (SELECT 1 FROM organization_roster WHERE organization_id = :orgId)";
Query deleteOrganizationNativeQuery = entityManager.createNativeQuery(nativeQuery);
deleteOrganizationNativeQuery.setParameter("orgId", orgId);
int deletedOrganizationRows = deleteOrganizationNativeQuery.executeUpdate();
if (deletedOrganizationRows != 0)
result.put("deletedOrganization", "deleted the organization " +orgId+ " since last member was removed");
}
} else {
result.put("result", "failure");
result.put("error", "Member not found in the organization roster");
......@@ -205,9 +214,11 @@ public class MyOrgRosterRepository implements OrgRosterRepository{
result.put("result", "failure");
result.put("error", e.getMessage());
}
return result;
}
@Override
public <S extends OrganizationRoster> S save(S entity) {
return null;
......
......@@ -134,10 +134,8 @@ public class OrgRosterController {
orgId = Integer.parseInt((String)json.get("orgId"));
}
String userEmail = (String)map.get("userEmail");
System.out.println(json.entrySet());
if ("self".equals((String)(json.get("memberEmail"))) && json.get("newtype").equals("DELETE"))
{
System.out.println("In the self removal case");
//basically if the user tries to remove themselves from the organization
result.put("data", myOrgRosterRepository.deleteMember(orgId, userEmail));
//above, remove the user
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment