No title

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
module Handler.Company where

import Data.Monoid
import Data.ByteString (ByteString)
import qualified Database.Redis as R
import Control.Monad.Except

type Radius = Int

type Category = Char

instance R.RedisCtx m f => R.RedisCtx (ExceptT e m) f where
  returnDecode = lift . R.returnDecode

instance R.MonadRedis m => R.MonadRedis (ExceptT e m) where
  liftRedis = lift . R.liftRedis

getCompanies :: R.Connection -> (Maybe Radius, Maybe Category) -> IO (Either R.Reply [Maybe ByteString])
getCompanies p (Just r, Just c) = undefined
getCompanies p (Just r, _) = undefined
getCompanies p (_, Just c) = undefined
getCompanies p _ = do
  R.runRedis p $
    runExceptT $ do
      lastDates <- ExceptT $ R.zrevrangebyscoreLimit "dates" 9999999 0 0 1
      case lastDates of
        [] -> error "oops2"
        (d:_) -> do
          ids <- ExceptT $ R.zrangebyscore ("companies:date:" <> d) 0 99999999
          ExceptT $ R.mget $ map ((<>) "companies:id:") ids