moved sql from view into controller