Updated controller to write to database