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 | |
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')
-rw-r--r-- | server/src/Thermoprint/Server/API.hs | 74 | ||||
-rw-r--r-- | server/src/Thermoprint/Server/Database.hs | 7 | ||||
-rw-r--r-- | server/thermoprint-server.cabal | 2 | ||||
-rw-r--r-- | server/thermoprint-server.nix | 17 |
4 files changed, 53 insertions, 47 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 | ||
diff --git a/server/thermoprint-server.cabal b/server/thermoprint-server.cabal index dd495c0..181bd9a 100644 --- a/server/thermoprint-server.cabal +++ b/server/thermoprint-server.cabal | |||
@@ -43,6 +43,8 @@ library | |||
43 | , stm >=2.4.4 && <3 | 43 | , stm >=2.4.4 && <3 |
44 | , deepseq >=1.4.1 && <2 | 44 | , deepseq >=1.4.1 && <2 |
45 | , monad-control >=1.0.0 && <2 | 45 | , monad-control >=1.0.0 && <2 |
46 | , conduit >=1.2.6 && <2 | ||
47 | , exceptions >=0.8.0 && <1 | ||
46 | hs-source-dirs: src | 48 | hs-source-dirs: src |
47 | default-language: Haskell2010 | 49 | default-language: Haskell2010 |
48 | 50 | ||
diff --git a/server/thermoprint-server.nix b/server/thermoprint-server.nix index 46859e2..8ac5456 100644 --- a/server/thermoprint-server.nix +++ b/server/thermoprint-server.nix | |||
@@ -1,7 +1,8 @@ | |||
1 | { mkDerivation, base, containers, data-default-class, deepseq, dyre | 1 | { mkDerivation, base, conduit, containers, data-default-class |
2 | , either, monad-control, monad-logger, mtl, persistent | 2 | , deepseq, dyre, either, exceptions, monad-control, monad-logger |
3 | , persistent-sqlite, persistent-template, resourcet, servant-server | 3 | , mtl, persistent, persistent-sqlite, persistent-template |
4 | , stdenv, stm, text, thermoprint-spec, transformers, wai, warp | 4 | , resourcet, servant-server, stdenv, stm, text, thermoprint-spec |
5 | , transformers, wai, warp | ||
5 | }: | 6 | }: |
6 | mkDerivation { | 7 | mkDerivation { |
7 | pname = "thermoprint-server"; | 8 | pname = "thermoprint-server"; |
@@ -10,10 +11,10 @@ mkDerivation { | |||
10 | isLibrary = true; | 11 | isLibrary = true; |
11 | isExecutable = true; | 12 | isExecutable = true; |
12 | libraryHaskellDepends = [ | 13 | libraryHaskellDepends = [ |
13 | base containers data-default-class deepseq dyre either | 14 | base conduit containers data-default-class deepseq dyre either |
14 | monad-control monad-logger mtl persistent persistent-template | 15 | exceptions monad-control monad-logger mtl persistent |
15 | resourcet servant-server stm text thermoprint-spec transformers wai | 16 | persistent-template resourcet servant-server stm text |
16 | warp | 17 | thermoprint-spec transformers wai warp |
17 | ]; | 18 | ]; |
18 | executableHaskellDepends = [ | 19 | executableHaskellDepends = [ |
19 | base monad-logger mtl persistent-sqlite resourcet | 20 | base monad-logger mtl persistent-sqlite resourcet |