diff options
-rw-r--r-- | server/src/Thermoprint/Server/API.hs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/server/src/Thermoprint/Server/API.hs b/server/src/Thermoprint/Server/API.hs index 77f33e7..4d036ce 100644 --- a/server/src/Thermoprint/Server/API.hs +++ b/server/src/Thermoprint/Server/API.hs | |||
@@ -67,6 +67,9 @@ data HandlerInput = HandlerInput { sqlPool :: ConnectionPool -- ^ How to intera | |||
67 | , printers :: Map PrinterId Printer | 67 | , printers :: Map PrinterId Printer |
68 | } | 68 | } |
69 | 69 | ||
70 | instance MonadLogger m => MonadLogger (EitherT a m) where | ||
71 | monadLoggerLog loc src lvl = lift . monadLoggerLog loc src lvl | ||
72 | |||
70 | handlerNat :: ( MonadReader ConnectionPool m | 73 | handlerNat :: ( MonadReader ConnectionPool m |
71 | , MonadLoggerIO m | 74 | , MonadLoggerIO m |
72 | ) => Map PrinterId Printer -> m (Handler :~> EitherT ServantErr IO) | 75 | ) => Map PrinterId Printer -> m (Handler :~> EitherT ServantErr IO) |
@@ -157,7 +160,7 @@ abortJob jobId = do | |||
157 | let filtered = Seq.filter (/= castId jobId) pending | 160 | let filtered = Seq.filter (/= castId jobId) pending |
158 | writeTVar (queue p) $ current { pending = filtered } | 161 | writeTVar (queue p) $ current { pending = filtered } |
159 | return . not $ ((==) `on` length) pending filtered | 162 | return . not $ ((==) `on` length) pending filtered |
160 | when found . lift . $(logInfo) $ "Removed " <> (T.pack $ show (castId jobId :: Integer)) <> " from " <> (T.pack . show $ pId') | 163 | when found . $(logInfo) $ "Removed job #" <> (T.pack $ show (castId jobId :: Integer)) <> " from " <> (T.pack . show $ pId') |
161 | return found | 164 | return found |
162 | when (not found) $ left err404 | 165 | when (not found) $ left err404 |
163 | 166 | ||
@@ -167,16 +170,23 @@ listDrafts = asks sqlPool >>= runSqlPool (selectSourceRes [] []) >>= flip with t | |||
167 | toMap source = fmap Map.fromList . sourceToList $ (\(Entity key (Draft title _)) -> (castId key, title)) `mapOutput` source | 170 | toMap source = fmap Map.fromList . sourceToList $ (\(Entity key (Draft title _)) -> (castId key, title)) `mapOutput` source |
168 | 171 | ||
169 | addDraft :: Maybe DraftTitle -> Printout -> Handler API.DraftId | 172 | addDraft :: Maybe DraftTitle -> Printout -> Handler API.DraftId |
170 | addDraft title content = fmap castId . runSqlPool (insert $ Draft title content) =<< asks sqlPool | 173 | addDraft title content = do |
174 | id <- fmap castId . runSqlPool (insert $ Draft title content) =<< asks sqlPool | ||
175 | $(logInfo) $ "Added draft #" <> (T.pack $ show (castId id :: Integer)) <> " (" <> (T.pack $ show title) <> ")" | ||
176 | return id | ||
171 | 177 | ||
172 | updateDraft :: API.DraftId -> Maybe DraftTitle -> Printout -> Handler () | 178 | updateDraft :: API.DraftId -> Maybe DraftTitle -> Printout -> Handler () |
173 | updateDraft draftId title content = (\(KeyNotFound _) -> left $ err404) `handle` (runSqlPool (update (castId draftId) [ DraftTitle =. title, DraftContent =. content ]) =<< asks sqlPool) | 179 | updateDraft draftId title content = handle (\(KeyNotFound _) -> left $ err404) $ do |
180 | runSqlPool (update (castId draftId) [ DraftTitle =. title, DraftContent =. content ]) =<< asks sqlPool | ||
181 | $(logInfo) $ "Updated draft #" <> (T.pack $ show (castId draftId :: Integer)) | ||
174 | 182 | ||
175 | getDraft :: API.DraftId -> Handler (Maybe DraftTitle, Printout) | 183 | getDraft :: API.DraftId -> Handler (Maybe DraftTitle, Printout) |
176 | getDraft draftId = fmap (\(Draft title content) -> (title, content)) . maybe (left err404) return =<< runSqlPool (get $ castId draftId) =<< asks sqlPool | 184 | getDraft draftId = fmap (\(Draft title content) -> (title, content)) . maybe (left err404) return =<< runSqlPool (get $ castId draftId) =<< asks sqlPool |
177 | 185 | ||
178 | deleteDraft :: API.DraftId -> Handler () | 186 | deleteDraft :: API.DraftId -> Handler () |
179 | deleteDraft draftId = runSqlPool (delete $ (castId draftId :: Key Draft)) =<< asks sqlPool | 187 | deleteDraft draftId = do |
188 | runSqlPool (delete $ (castId draftId :: Key Draft)) =<< asks sqlPool | ||
189 | $(logInfo) $ "Made sure draft #" <> (T.pack $ show (castId draftId :: Integer)) <> " is Deleted" | ||
180 | 190 | ||
181 | printDraft :: API.DraftId -> Maybe PrinterId -> Handler API.JobId | 191 | printDraft :: API.DraftId -> Maybe PrinterId -> Handler API.JobId |
182 | printDraft draftId printerId = (\(Draft _ content) -> queueJob printerId content) =<< maybe (left err404) return =<< runSqlPool (get $ castId draftId) =<< asks sqlPool | 192 | printDraft draftId printerId = (\(Draft _ content) -> queueJob printerId content) =<< maybe (left err404) return =<< runSqlPool (get $ castId draftId) =<< asks sqlPool |