diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-28 12:09:41 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-28 12:09:41 +0000 |
commit | f236eb926bd554cb6494a0184d1070f264c4d03a (patch) | |
tree | c79deb2b5e99dca9019daad6a945d36f4ab24456 /server/src | |
parent | 36bc432fc0f8ae46c7ff0de162ee275d25f12e3a (diff) | |
download | thermoprint-f236eb926bd554cb6494a0184d1070f264c4d03a.tar thermoprint-f236eb926bd554cb6494a0184d1070f264c4d03a.tar.gz thermoprint-f236eb926bd554cb6494a0184d1070f264c4d03a.tar.bz2 thermoprint-f236eb926bd554cb6494a0184d1070f264c4d03a.tar.xz thermoprint-f236eb926bd554cb6494a0184d1070f264c4d03a.zip |
Draft CRUD
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/Thermoprint/Server/API.hs | 74 | ||||
-rw-r--r-- | server/src/Thermoprint/Server/Database.hs | 7 |
2 files changed, 42 insertions, 39 deletions
diff --git a/server/src/Thermoprint/Server/API.hs b/server/src/Thermoprint/Server/API.hs index 3c8fefe..77f33e7 100644 --- a/server/src/Thermoprint/Server/API.hs +++ b/server/src/Thermoprint/Server/API.hs | |||
@@ -10,48 +10,54 @@ module Thermoprint.Server.API | |||
10 | , handlerNat | 10 | , handlerNat |
11 | ) where | 11 | ) where |
12 | 12 | ||
13 | import Thermoprint.API hiding (JobId(..), DraftId(..)) | 13 | import Thermoprint.API hiding (JobId(..), DraftId(..)) |
14 | import qualified Thermoprint.API as API (JobId(..), DraftId(..)) | 14 | import qualified Thermoprint.API as API (JobId(..), DraftId(..)) |
15 | 15 | ||
16 | import Thermoprint.Server.Printer | 16 | import Thermoprint.Server.Printer |
17 | import Thermoprint.Server.Database | 17 | import Thermoprint.Server.Database |
18 | 18 | ||
19 | import Data.Set (Set) | 19 | import Data.Set (Set) |
20 | import qualified Data.Set as Set | 20 | import qualified Data.Set as Set |
21 | import Data.Sequence (Seq) | 21 | import Data.Sequence (Seq) |
22 | import qualified Data.Sequence as Seq | 22 | import qualified Data.Sequence as Seq |
23 | import Data.Map (Map) | 23 | import Data.Map (Map) |
24 | import qualified Data.Map as Map | 24 | import qualified Data.Map as Map |
25 | 25 | ||
26 | import qualified Data.Text as T | 26 | import qualified Data.Text as T |
27 | 27 | ||
28 | import Servant | 28 | import Servant |
29 | import Servant.Server | 29 | import Servant.Server |
30 | import Servant.Server.Internal.Enter | 30 | import Servant.Server.Internal.Enter |
31 | 31 | ||
32 | import Control.Monad.Logger | 32 | import Control.Monad.Logger |
33 | import Control.Monad.Reader | 33 | import Control.Monad.Reader |
34 | import Control.Monad.Trans.Resource | 34 | import Control.Monad.Trans.Resource |
35 | import Control.Monad.Trans.Either | 35 | import Control.Monad.Trans.Either |
36 | import Control.Monad.IO.Class | 36 | import Control.Monad.IO.Class |
37 | 37 | ||
38 | import Control.Concurrent.STM | 38 | import Control.Concurrent.STM |
39 | 39 | ||
40 | import Control.Monad ((<=<), liftM2) | 40 | import Control.Monad ((<=<), liftM2) |
41 | import Prelude hiding ((.), id, mapM) | 41 | import Prelude hiding ((.), id, mapM) |
42 | import Control.Category | 42 | import Control.Category |
43 | 43 | ||
44 | import Control.DeepSeq | 44 | import Control.DeepSeq |
45 | 45 | ||
46 | import Data.Foldable (toList) | 46 | import Data.Foldable (toList) |
47 | import Data.Traversable (mapM) | 47 | import Data.Traversable (mapM) |
48 | import Data.Bifunctor | 48 | import Data.Bifunctor |
49 | import Data.Monoid | 49 | import Data.Monoid |
50 | import Data.Maybe | 50 | import Data.Maybe |
51 | import Data.Function (on) | 51 | import Data.Function (on) |
52 | 52 | ||
53 | import Database.Persist | 53 | import Database.Persist |
54 | import Database.Persist.Sql | 54 | import Database.Persist.Sql |
55 | |||
56 | import Data.Conduit (Source, sourceToList, mapOutput) | ||
57 | |||
58 | import Data.Acquire (with) | ||
59 | |||
60 | import Control.Monad.Catch (handle, catch) | ||
55 | 61 | ||
56 | type ProtoHandler = ReaderT HandlerInput (LoggingT (ResourceT IO)) | 62 | type ProtoHandler = ReaderT HandlerInput (LoggingT (ResourceT IO)) |
57 | type Handler = EitherT ServantErr ProtoHandler | 63 | type Handler = EitherT ServantErr ProtoHandler |
@@ -156,19 +162,21 @@ abortJob jobId = do | |||
156 | when (not found) $ left err404 | 162 | when (not found) $ left err404 |
157 | 163 | ||
158 | listDrafts :: Handler (Map API.DraftId (Maybe DraftTitle)) | 164 | listDrafts :: Handler (Map API.DraftId (Maybe DraftTitle)) |
159 | listDrafts = return undefined | 165 | listDrafts = asks sqlPool >>= runSqlPool (selectSourceRes [] []) >>= flip with toMap |
166 | where | ||
167 | toMap source = fmap Map.fromList . sourceToList $ (\(Entity key (Draft title _)) -> (castId key, title)) `mapOutput` source | ||
160 | 168 | ||
161 | addDraft :: Maybe DraftTitle -> Printout -> Handler API.DraftId | 169 | addDraft :: Maybe DraftTitle -> Printout -> Handler API.DraftId |
162 | addDraft = return undefined | 170 | addDraft title content = fmap castId . runSqlPool (insert $ Draft title content) =<< asks sqlPool |
163 | 171 | ||
164 | updateDraft :: API.DraftId -> Maybe DraftTitle -> Printout -> Handler () | 172 | updateDraft :: API.DraftId -> Maybe DraftTitle -> Printout -> Handler () |
165 | updateDraft = return undefined | 173 | updateDraft draftId title content = (\(KeyNotFound _) -> left $ err404) `handle` (runSqlPool (update (castId draftId) [ DraftTitle =. title, DraftContent =. content ]) =<< asks sqlPool) |
166 | 174 | ||
167 | getDraft :: API.DraftId -> Handler (Maybe DraftTitle, Printout) | 175 | getDraft :: API.DraftId -> Handler (Maybe DraftTitle, Printout) |
168 | getDraft = return undefined | 176 | getDraft draftId = fmap (\(Draft title content) -> (title, content)) . maybe (left err404) return =<< runSqlPool (get $ castId draftId) =<< asks sqlPool |
169 | 177 | ||
170 | deleteDraft :: API.DraftId -> Handler () | 178 | deleteDraft :: API.DraftId -> Handler () |
171 | deleteDraft = return undefined | 179 | deleteDraft draftId = runSqlPool (delete $ (castId draftId :: Key Draft)) =<< asks sqlPool |
172 | 180 | ||
173 | printDraft :: API.DraftId -> Maybe PrinterId -> Handler API.JobId | 181 | printDraft :: API.DraftId -> Maybe PrinterId -> Handler API.JobId |
174 | printDraft = return undefined | 182 | printDraft draftId printerId = (\(Draft _ content) -> queueJob printerId content) =<< maybe (left err404) return =<< runSqlPool (get $ castId draftId) =<< asks sqlPool |
diff --git a/server/src/Thermoprint/Server/Database.hs b/server/src/Thermoprint/Server/Database.hs index 5bd4512..7f21bf9 100644 --- a/server/src/Thermoprint/Server/Database.hs +++ b/server/src/Thermoprint/Server/Database.hs | |||
@@ -7,12 +7,7 @@ | |||
7 | {-# LANGUAGE StandaloneDeriving #-} | 7 | {-# LANGUAGE StandaloneDeriving #-} |
8 | {-# LANGUAGE FlexibleInstances #-} | 8 | {-# LANGUAGE FlexibleInstances #-} |
9 | 9 | ||
10 | module Thermoprint.Server.Database | 10 | module Thermoprint.Server.Database where |
11 | ( Job(..), JobId | ||
12 | , Draft(..), DraftId | ||
13 | , Key(..) | ||
14 | , migrateAll | ||
15 | ) where | ||
16 | 11 | ||
17 | import Control.DeepSeq | 12 | import Control.DeepSeq |
18 | 13 | ||