|
| 1 | +{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 2 | +-- The idea behind this module and doing orphan instances is that |
| 3 | +-- if we ever want to switch to another implementation, we should |
| 4 | +-- just remove this module, not rewrite all the types and change instances. |
| 5 | +-- This way, we have real separation. |
| 6 | + |
| 7 | +module DataSource.DB |
| 8 | + ( withDatabase |
| 9 | + , withProdDatabase |
| 10 | + , cachedZendeskLayer |
| 11 | + ) where |
| 12 | + |
| 13 | +import Universum |
| 14 | + |
| 15 | +import Data.Text (split) |
| 16 | + |
| 17 | +import Database.SQLite.Simple (FromRow (..), NamedParam (..), SQLData (..), close, field, |
| 18 | + open, queryNamed, query_) |
| 19 | +import Database.SQLite.Simple.FromField (FromField (..), ResultError (..), returnError) |
| 20 | +import Database.SQLite.Simple.Internal (Connection, Field (..)) |
| 21 | +import Database.SQLite.Simple.Ok (Ok (..)) |
| 22 | +import Database.SQLite.Simple.ToField (ToField (..)) |
| 23 | + |
| 24 | +import DataSource.Types (Attachment (..), AttachmentContent (..), AttachmentId (..), |
| 25 | + Comment (..), CommentBody (..), CommentId (..), Config, |
| 26 | + TicketId (..), TicketInfo (..), TicketStatus (..), |
| 27 | + TicketTags (..), TicketURL (..), UserId (..), ZendeskLayer (..)) |
| 28 | + |
| 29 | + |
| 30 | +-- https://ocharles.org.uk/blog/posts/2014-08-07-postgresql-simple-generic-sop.html |
| 31 | + |
| 32 | +-- | The cached Zendesk layer. We are using a simple SQLite DB behind the scenes |
| 33 | +-- that we need to sync occasionaly. |
| 34 | +cachedZendeskLayer :: (MonadIO m, MonadReader Config m) => ZendeskLayer m |
| 35 | +cachedZendeskLayer = ZendeskLayer |
| 36 | + { zlGetTicketInfo = liftIO . getTicketInfoByTicketId |
| 37 | + , zlListAssignedTickets = liftIO . getAllAssignedTicketsByUser |
| 38 | + , zlListRequestedTickets = liftIO . getAllRequestedTicketsByUser |
| 39 | + , zlGetAttachment = liftIO . DataSource.DB.getAttachmentContent |
| 40 | + , zlGetTicketComments = liftIO . getTicketComments |
| 41 | + , zlPostTicketComment = error "We won't use this for now!" |
| 42 | + } |
| 43 | + |
| 44 | +-- Database instances |
| 45 | +-- FROM |
| 46 | + |
| 47 | +instance FromField TicketId where |
| 48 | + fromField (Field (SQLInteger tId) _) = Ok . TicketId . fromIntegral $ tId |
| 49 | + fromField f = returnError ConversionFailed f "need an integer, ticket id" |
| 50 | + |
| 51 | +instance FromField UserId where |
| 52 | + fromField (Field (SQLInteger uId) _) = Ok . UserId . fromIntegral $ uId |
| 53 | + fromField f = returnError ConversionFailed f "need an integer, user id" |
| 54 | + |
| 55 | +instance FromField TicketURL where |
| 56 | + fromField (Field (SQLText tURL) _) = Ok . TicketURL $ tURL |
| 57 | + fromField f = returnError ConversionFailed f "need a text, ticket url" |
| 58 | + |
| 59 | +-- | TODO(ks): Yes, yes, normal form... |
| 60 | +instance FromField TicketTags where |
| 61 | + fromField (Field (SQLText tTags) _) = Ok . TicketTags . split (==',') $ tTags |
| 62 | + fromField f = returnError ConversionFailed f "need a text, ticket tags" |
| 63 | + |
| 64 | +instance FromField TicketStatus where |
| 65 | + fromField (Field (SQLText tStat) _) = Ok . TicketStatus $ tStat |
| 66 | + fromField f = returnError ConversionFailed f "need a text, ticket status" |
| 67 | + |
| 68 | +instance FromRow TicketInfo where |
| 69 | + fromRow = TicketInfo <$> field <*> field <*> field <*> field <*> field <*> field |
| 70 | + |
| 71 | +instance FromField CommentId where |
| 72 | + fromField (Field (SQLInteger commId) _) = Ok . CommentId . fromIntegral $ commId |
| 73 | + fromField f = returnError ConversionFailed f "need an integer, comment id" |
| 74 | + |
| 75 | +instance FromField CommentBody where |
| 76 | + fromField (Field (SQLText cBody) _) = Ok . CommentBody $ cBody |
| 77 | + fromField f = returnError ConversionFailed f "need a text, comment body" |
| 78 | + |
| 79 | +instance FromField AttachmentId where |
| 80 | + fromField (Field (SQLInteger attId) _) = Ok . AttachmentId . fromIntegral $ attId |
| 81 | + fromField f = returnError ConversionFailed f "need an integer, attachment id" |
| 82 | + |
| 83 | + |
| 84 | +-- TO |
| 85 | + |
| 86 | +instance FromRow Attachment where |
| 87 | + fromRow = Attachment <$> field <*> field <*> field <*> field |
| 88 | + |
| 89 | +instance FromRow AttachmentContent where |
| 90 | + fromRow = AttachmentContent <$> field |
| 91 | + |
| 92 | +instance ToField TicketId where |
| 93 | + toField (TicketId tId) = SQLInteger . fromIntegral $ tId |
| 94 | + |
| 95 | +instance ToField UserId where |
| 96 | + toField (UserId userId) = SQLInteger . fromIntegral $ userId |
| 97 | + |
| 98 | +instance ToField CommentId where |
| 99 | + toField (CommentId commentId) = SQLInteger . fromIntegral $ commentId |
| 100 | + |
| 101 | +instance ToField AttachmentId where |
| 102 | + toField (AttachmentId attachmentId) = SQLInteger . fromIntegral $ attachmentId |
| 103 | + |
| 104 | + |
| 105 | +-- | A general resource closing function. |
| 106 | +withDatabase :: forall a. String -> (Connection -> IO a) -> IO a |
| 107 | +withDatabase dbName dbOperation = |
| 108 | + bracket |
| 109 | + (open dbName) |
| 110 | + (close) |
| 111 | + dbOperation |
| 112 | + |
| 113 | +-- | A production resource closing function. |
| 114 | +withProdDatabase :: forall a. (Connection -> IO a) -> IO a |
| 115 | +withProdDatabase = withDatabase "./prod.db" |
| 116 | + |
| 117 | +_getTicketsInfo :: IO [TicketInfo] |
| 118 | +_getTicketsInfo = withProdDatabase $ \conn -> |
| 119 | + query_ conn "SELECT * FROM ticket_info" |
| 120 | + |
| 121 | +getTicketInfoByTicketId :: TicketId -> IO (Maybe TicketInfo) |
| 122 | +getTicketInfoByTicketId ticketId = withProdDatabase $ \conn -> |
| 123 | + safeHead <$> queryNamed conn "SELECT * FROM ticket_info WHERE ticket_id = :id" [":id" := ticketId] |
| 124 | + |
| 125 | +getAllAssignedTicketsByUser :: UserId -> IO [TicketInfo] |
| 126 | +getAllAssignedTicketsByUser userId = withProdDatabase $ \conn -> |
| 127 | + queryNamed conn "SELECT * FROM ticket_info WHERE assignee_id = :id" [":id" := userId] |
| 128 | + |
| 129 | +getAllRequestedTicketsByUser :: UserId -> IO [TicketInfo] |
| 130 | +getAllRequestedTicketsByUser userId = withProdDatabase $ \conn -> |
| 131 | + queryNamed conn "SELECT * FROM ticket_info WHERE requester_id = :id" [":id" := userId] |
| 132 | + |
| 133 | + |
| 134 | +-- | A join would be more performance, but KISS for now. |
| 135 | +getTicketComments :: TicketId -> IO [Comment] |
| 136 | +getTicketComments ticketId = do |
| 137 | + commentsInfo <- getTicketIdComments ticketId |
| 138 | + |
| 139 | + forM commentsInfo $ \(commentId, commentBody, commentIsPublic, commentAuthorId) -> do |
| 140 | + |
| 141 | + commentAttachments <- getCommentAttachments commentId |
| 142 | + |
| 143 | + pure Comment |
| 144 | + { cId = commentId |
| 145 | + , cBody = commentBody |
| 146 | + , cAttachments = commentAttachments |
| 147 | + , cPublic = commentIsPublic |
| 148 | + , cAuthor = commentAuthorId |
| 149 | + } |
| 150 | + where |
| 151 | + getTicketIdComments :: TicketId -> IO [(CommentId, CommentBody, Bool, Integer)] |
| 152 | + getTicketIdComments ticketId' = withProdDatabase $ \conn -> |
| 153 | + queryNamed conn "SELECT tc.id, tc.body, tc.is_public, tc.author_id FROM ticket_comment tc WHERE tc.ticket_id = :id" [":id" := ticketId'] |
| 154 | + |
| 155 | + getCommentAttachments :: CommentId -> IO [Attachment] |
| 156 | + getCommentAttachments commentId = withProdDatabase $ \conn -> |
| 157 | + queryNamed conn "SELECT * FROM comment_attachments WHERE comment_id = :id" [":id" := commentId] |
| 158 | + |
| 159 | + |
| 160 | +getAttachmentContent :: Attachment -> IO (Maybe AttachmentContent) |
| 161 | +getAttachmentContent Attachment{..} = withProdDatabase $ \conn -> |
| 162 | + safeHead <$> queryNamed conn "SELECT * FROM attachment_content WHERE attachment_id = :id" [":id" := aId] |
| 163 | + |
0 commit comments