CMS-4087 Introduce Repository

Added test, fixed query