Changes to db layer and API layer

  - Change the way request parameters were being parsed
  - Move some db related code from API to db