eidolon/Handler/Search.hs
2017-10-21 21:18:59 +02:00

67 lines
2.5 KiB
Haskell
Executable file

-- eidolon -- A simple gallery in Haskell and Yesod
-- Copyright (C) 2015 Amedeo Molnár
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
module Handler.Search where
import Import
import qualified Data.Text as T
import Data.Maybe (fromJust)
import Data.List (sortBy, nub)
import Text.Fuzzy (test)
import Database.Persist.Sql (rawSql)
import System.FilePath (splitDirectories)
getSearchR :: Handler Html
getSearchR = do
((res, widget), _) <- runFormGet $ renderBootstrap3 BootstrapBasicForm $
searchForm
case res of
FormSuccess query -> do
mediumList <- runDB $ do
a <- rawSql "select ?? from medium where ? % any(string_to_array(title, ' '))"
[PersistText query]
b <- rawSql "select ?? from medium where ? % any(string_to_array(description, ' '))"
[PersistText query]
return $ nub $ sortBy (\sa sb -> compare
(T.unpack $ mediumTitle $ entityVal sa)
(T.unpack $ mediumTitle $ entityVal sb))
$ a ++ b
userList <- runDB $
rawSql "select ?? from \"user\" where name % ?" [PersistText query]
albumList <- runDB $
rawSql "select ?? from album where title % ?" [PersistText query]
tagList <- runDB $ do
media <- selectList [] [Asc MediumTitle]
let tags = sortBy
(\sa sb -> compare sa sb)
(nub (concatMap mediumTags (map entityVal media)))
ret = filter (\a -> query `test` a) tags
return ret
let allEmpty = null mediumList && null userList && null albumList
defaultLayout $ do
setTitle $ toHtml $ "Eidolon :: Search results for " ++ (T.unpack query)
$(widgetFile "result")
_ ->
defaultLayout $ do
setTitle "Eidolon :: Search"
$(widgetFile "search")
searchForm :: AForm Handler T.Text
searchForm = areq (searchField True) "Search" Nothing