|
6 | 6 | module Handler.Text where |
7 | 7 |
|
8 | 8 | import Data.ByteString (hGet) |
| 9 | +import qualified Data.Map.Strict as Map |
9 | 10 | import qualified Data.Text as T |
| 11 | +import Database.Persist.Sql (Single (Single), rawSql, toSqlKey) |
10 | 12 | import Import |
11 | 13 | import System.Directory (doesFileExist) |
12 | 14 | import System.Random (randomRIO) |
13 | 15 |
|
14 | | -getRandomParagraph :: Text -> Text -> Int -> Handler (Entity TextParagraph) |
| 16 | +getRandomParagraph :: |
| 17 | + Text -> Text -> Int -> Handler (Entity TextParagraph) |
15 | 18 | getRandomParagraph era source book = do |
16 | | - let filters = |
| 19 | + res <- |
| 20 | + runDB $ |
| 21 | + rawSql |
| 22 | + "SELECT MIN(id), MAX(id) \ |
| 23 | + \FROM text_paragraph \ |
| 24 | + \WHERE era = ? \ |
| 25 | + \ AND source = ? \ |
| 26 | + \ AND book = ?" |
| 27 | + [ toPersistValue era, |
| 28 | + toPersistValue source, |
| 29 | + toPersistValue book |
| 30 | + ] |
| 31 | + pair <- case res of |
| 32 | + [(Single mMinId, Single mMaxId)] -> |
| 33 | + let minId = fromMaybe (error "no rows") mMinId |
| 34 | + maxId = fromMaybe minId mMaxId |
| 35 | + in return (minId, maxId) |
| 36 | + _ -> notFound |
| 37 | + let (minId, maxId) = pair |
| 38 | + rnd <- liftIO $ randomRIO (minId, maxId) |
| 39 | + let baseFilters = |
17 | 40 | [ TextParagraphEra ==. era, |
18 | 41 | TextParagraphSource ==. source, |
19 | 42 | TextParagraphBook ==. book |
20 | 43 | ] |
21 | | - total <- runDB $ count filters |
22 | | - when (total == 0) notFound |
23 | | - |
24 | | - idx <- liftIO $ randomRIO (0, total - 1) |
25 | | - |
26 | | - results <- runDB $ selectList filters [OffsetBy idx, LimitTo 1] |
27 | | - case results of |
28 | | - [ent] -> return ent |
29 | | - _ -> error "No paragraph found for this era." |
| 44 | + mEnt <- |
| 45 | + runDB $ |
| 46 | + selectFirst |
| 47 | + (baseFilters ++ [TextParagraphId >=. toSqlKey rnd]) |
| 48 | + [Asc TextParagraphId] |
| 49 | + case mEnt of |
| 50 | + Just ent -> return ent |
| 51 | + Nothing -> do |
| 52 | + mEnt2 <- |
| 53 | + runDB $ |
| 54 | + selectFirst |
| 55 | + baseFilters |
| 56 | + [Asc TextParagraphId] |
| 57 | + maybe notFound return mEnt2 |
30 | 58 |
|
31 | 59 | getRandomNewText :: Text -> Handler (Entity TextParagraph) |
32 | 60 | getRandomNewText era = do |
33 | | - let filters = [TextParagraphEra ==. era] |
34 | | - total <- runDB $ count filters |
35 | | - when (total == 0) notFound |
36 | | - |
37 | | - idx <- liftIO $ randomRIO (0, total - 1) |
38 | | - |
39 | | - results <- runDB $ selectList filters [OffsetBy idx, LimitTo 1] |
40 | | - case results of |
41 | | - [ent] -> return ent |
42 | | - _ -> error "No paragraph found for this era." |
| 61 | + App {appMinMaxByEra = cacheRef} <- getYesod |
| 62 | + cache <- liftIO $ readIORef cacheRef |
| 63 | + case Map.lookup era cache of |
| 64 | + Nothing -> notFound |
| 65 | + Just (minId, maxId) -> do |
| 66 | + rnd <- liftIO $ randomRIO (minId, maxId) |
| 67 | + mEnt <- |
| 68 | + runDB $ |
| 69 | + selectFirst |
| 70 | + [ TextParagraphEra ==. era, |
| 71 | + TextParagraphId >=. toSqlKey rnd |
| 72 | + ] |
| 73 | + [Asc TextParagraphId] |
| 74 | + case mEnt of |
| 75 | + Just ent -> return ent |
| 76 | + Nothing -> do |
| 77 | + mEnt2 <- |
| 78 | + runDB $ |
| 79 | + selectFirst |
| 80 | + [TextParagraphEra ==. era] |
| 81 | + [Asc TextParagraphId] |
| 82 | + maybe notFound return mEnt2 |
43 | 83 |
|
44 | 84 | getCustomText :: UserId -> Text -> Handler ByteString |
45 | 85 | getCustomText userId filename = do |
|
0 commit comments