Well, I'm building a Audit log for my spring boot app. I want to track every POST request to my web server(even failures) in DB. My code for auditing has cluttered my controller. Can someone please suggest a appropriate design pattern to make it clean, generic & maintainable..
@JsonView(View.Student.class)
@PostMapping("/updateStudent")
public Student updateStudent(@Valid @RequestBody final Student
studentConfig) {
Student oldStudentDefn =
repository.findById(studentConfig.getStudentId())
.map(Function.identity()).orElse(null);
final AuditBuilder builder = aAuditBuilder().withAction("UPDATE")
.withSource("GUI")
.withBeforeObj(oldStudentDefn)
.withAfterObj(studentConfig);
try{
if(repository.save(studentConfig)) {
builder.withStatus("Success");
} else {
throw new ApplicationException(" Student Save failed");
}
}catch(ApplicationException e) {
builder.withStatus("Failure");
throw e;
}
finally {
auditService.save(builder.build());
}
}
I want to refactor it such a way that this repetative try-catch builder for every endpoint goes to a common service and which is generic enough for accepting any kind of object so that even Teacher,Section,ProgressReport etc endpoints can also be audited by caling same service but it shouldn't have any code duplication.
Aucun commentaire:
Enregistrer un commentaire