Improve AvatarR performance by only fetching the avatar from the DB, not the whole User

This commit is contained in:
Danielle McLean 2017-10-13 22:29:22 +11:00
parent 1e7482b30f
commit 660b17f279
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5

View file

@ -1,12 +1,17 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Handler.Avatars where
import Import
import Import hiding ( (==.) )
import Model.Cache ( getCached )
import Database.Esqueleto
import Settings.StaticR ( staticR )
getAvatarR :: UserId -> Handler ()
getAvatarR = redirect <=< fmap (avatarR . entityVal) . getCached
where avatarR user = staticR ["img", userAvatar user]
getAvatarR = responseFrom <=< runDB . select . from . queryAvatar
where responseFrom (a:_) = redirect $ staticR ["img", unValue a]
responseFrom [] = notFound
queryAvatar userId user = do
where_ $ user ^. UserId ==. val userId
return $ user ^. UserAvatar