Skip to content

Commit

Permalink
Updated DELETE contact methods in School, District and Authority serv…
Browse files Browse the repository at this point in the history
…ices to be less verbose.
  • Loading branch information
chris.ditcher authored and chris.ditcher committed May 13, 2024
1 parent e7ed33f commit 7b7bcd6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ public DistrictContact updateDistrictContact(UUID districtId, UUID contactId, Di

@Override
public ResponseEntity<Void> deleteDistrictContact(UUID districtId, UUID contactId) throws JsonProcessingException {
InstituteEvent instituteEvent = this.districtService.deleteDistrictContact(districtId, contactId);
if(instituteEvent != null){
publisher.dispatchChoreographyEvent(instituteEvent);
}
publisher.dispatchChoreographyEvent(this.districtService.deleteDistrictContact(contactId));
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,7 @@ public AuthorityContact updateIndependentAuthorityContact(UUID independentAuthor

@Override
public ResponseEntity<Void> deleteIndependentAuthorityContact(UUID independentAuthorityId, UUID contactId) throws JsonProcessingException {
InstituteEvent instituteEvent = this.independentAuthorityService.deleteIndependentAuthorityContact(independentAuthorityId, contactId);
if(instituteEvent != null){
publisher.dispatchChoreographyEvent(instituteEvent);
}
publisher.dispatchChoreographyEvent(this.independentAuthorityService.deleteIndependentAuthorityContact(contactId));
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,7 @@ public SchoolContact updateSchoolContact(UUID schoolId, UUID contactId, SchoolCo

@Override
public ResponseEntity<Void> deleteSchoolContact(UUID schoolId, UUID contactId) throws JsonProcessingException {
InstituteEvent instituteEvent = this.schoolService.deleteSchoolContact(schoolId, contactId);
if(instituteEvent != null){
publisher.dispatchChoreographyEvent(instituteEvent);
}
publisher.dispatchChoreographyEvent(this.schoolService.deleteSchoolContact(contactId));
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,11 @@ public Pair<DistrictContactEntity, InstituteEvent> updateDistrictContact(Distric
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public InstituteEvent deleteDistrictContact(UUID districtId, UUID contactId) throws JsonProcessingException {
Optional<DistrictEntity> curDistrictEntityOptional = districtRepository.findById(districtId);

if (curDistrictEntityOptional.isPresent()) {
final DistrictEntity currentDistrictEntity = curDistrictEntityOptional.get();
final Optional<DistrictContactEntity> districtContactEntityOptional = currentDistrictEntity.getContacts()
.stream()
.filter(e -> e.getDistrictContactId().equals(contactId))
.findFirst();
public InstituteEvent deleteDistrictContact(UUID contactId) throws JsonProcessingException {
Optional<DistrictContactEntity> districtContactEntityOptional = districtContactRepository.findById(contactId);
if (districtContactEntityOptional.isPresent()){
districtContactRepository.deleteByDistrictContactIdAndDistrictEntity(contactId, currentDistrictEntity);
DistrictContactEntity districtContactEntity = districtContactEntityOptional.get();
districtContactRepository.delete(districtContactEntity);
final InstituteEvent instituteEvent = EventUtil.createInstituteEvent(
districtContactEntity.getCreateUser(),
districtContactEntity.getUpdateUser(),
Expand All @@ -240,12 +233,9 @@ public InstituteEvent deleteDistrictContact(UUID districtId, UUID contactId) thr
instituteEventRepository.save(instituteEvent);
return instituteEvent;
}

} else {
throw new EntityNotFoundException(DistrictEntity.class, DISTRICT_ID_ATTR, String.valueOf(districtId));
else {
throw new EntityNotFoundException(DistrictContactEntity.class, CONTACT_ID_ATTR, String.valueOf(contactId));
}
// Contact not found, so return null, no event. Intention is to DELETE.
return null;
}

public Optional<NoteEntity> getDistrictNote(UUID districtId, UUID noteId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,11 @@ public Pair<AuthorityContactEntity, InstituteEvent> updateIndependentAuthorityCo
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public InstituteEvent deleteIndependentAuthorityContact(UUID independentAuthorityId, UUID contactId) throws JsonProcessingException {
Optional<IndependentAuthorityEntity> curIndependentAuthorityEntityOptional = independentAuthorityRepository.findById(independentAuthorityId);

if (curIndependentAuthorityEntityOptional.isPresent()) {
final IndependentAuthorityEntity currentIndependentAuthorityEntity = curIndependentAuthorityEntityOptional.get();
final Optional<AuthorityContactEntity> authorityContactEntityOptional = currentIndependentAuthorityEntity.getContacts()
.stream()
.filter(e -> e.getAuthorityContactId().equals(contactId))
.findFirst();
public InstituteEvent deleteIndependentAuthorityContact(UUID contactId) throws JsonProcessingException {
Optional<AuthorityContactEntity> authorityContactEntityOptional = authorityContactRepository.findById(contactId);
if(authorityContactEntityOptional.isPresent()){
authorityContactRepository.deleteByAuthorityContactIdAndIndependentAuthorityEntity(contactId, currentIndependentAuthorityEntity);
AuthorityContactEntity authorityContactEntity = authorityContactEntityOptional.get();
authorityContactRepository.delete(authorityContactEntity);
final InstituteEvent instituteEvent = EventUtil.createInstituteEvent(
authorityContactEntity.getCreateUser(),
authorityContactEntity.getUpdateUser(),
Expand All @@ -237,11 +230,9 @@ public InstituteEvent deleteIndependentAuthorityContact(UUID independentAuthorit
instituteEventRepository.save(instituteEvent);
return instituteEvent;
}

} else {
throw new EntityNotFoundException(IndependentAuthorityEntity.class, INDEPENDENT_AUTHORITY_ID_ATTR, String.valueOf(independentAuthorityId));
else {
throw new EntityNotFoundException(AuthorityContactEntity.class, CONTACT_ID_ATTR, String.valueOf(contactId));
}
return null;
}

public Optional<NoteEntity> getIndependentAuthorityNote(UUID independentAuthorityId, UUID noteId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,11 @@ public Pair<SchoolContactEntity, InstituteEvent> updateSchoolContact(SchoolConta
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public InstituteEvent deleteSchoolContact(UUID schoolId, UUID contactId) throws JsonProcessingException {
Optional<SchoolEntity> curSchoolEntityOptional = schoolRepository.findById(schoolId);
if (curSchoolEntityOptional.isPresent()) {
final SchoolEntity currentSchoolEntity = curSchoolEntityOptional.get();
final Optional<SchoolContactEntity> schoolContactEntityOptional = currentSchoolEntity.getContacts()
.stream()
.filter(e -> e.getSchoolContactId().equals(contactId))
.findFirst();
public InstituteEvent deleteSchoolContact(UUID contactId) throws JsonProcessingException {
Optional<SchoolContactEntity> schoolContactEntityOptional = schoolContactRepository.findById(contactId);
if(schoolContactEntityOptional.isPresent()){
schoolContactRepository.deleteBySchoolContactIdAndSchoolEntity(contactId, currentSchoolEntity);
SchoolContactEntity schoolContactEntity = schoolContactEntityOptional.get();
schoolContactRepository.delete(schoolContactEntity);
final InstituteEvent instituteEvent = EventUtil.createInstituteEvent(
schoolContactEntity.getCreateUser(),
schoolContactEntity.getUpdateUser(),
Expand All @@ -341,12 +335,9 @@ public InstituteEvent deleteSchoolContact(UUID schoolId, UUID contactId) throws
instituteEventRepository.save(instituteEvent);
return instituteEvent;
}

} else {
throw new EntityNotFoundException(SchoolEntity.class, SCHOOL_ID_ATTR,
String.valueOf(schoolId));
else {
throw new EntityNotFoundException(SchoolContactEntity.class, CONTACT_ID_ATTR, String.valueOf(contactId));
}
return null;
}

public Optional<NoteEntity> getSchoolNote(UUID schoolId, UUID noteId) {
Expand Down

0 comments on commit 7b7bcd6

Please sign in to comment.