Refactor getRepository and findOr404 methods