Database integration and code refactoring for backend api