Move db logic from view functions to models.