Factor out the database query into separate entity

step 2