refactor code

move db logic into service layer