refactor and simplify into api to accept requests and insert into tables