summaryrefslogtreecommitdiff
path: root/Handler
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2017-03-15 16:56:58 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2017-03-15 16:56:58 +0100
commit37f0dac79707a0de81ec6364d2704007eefd9289 (patch)
tree21c2fd38be8202f5700477360a59a3173057105a /Handler
parentfe5cd6ad6c61eb13ca99acd1b69cd09b84051404 (diff)
downloadbar-37f0dac79707a0de81ec6364d2704007eefd9289.tar
bar-37f0dac79707a0de81ec6364d2704007eefd9289.tar.gz
bar-37f0dac79707a0de81ec6364d2704007eefd9289.tar.bz2
bar-37f0dac79707a0de81ec6364d2704007eefd9289.tar.xz
bar-37f0dac79707a0de81ec6364d2704007eefd9289.zip
Fix sorting
Diffstat (limited to 'Handler')
-rw-r--r--Handler/Common.hs16
-rw-r--r--Handler/InventoryListing.hs2
-rw-r--r--Handler/ReferenceListing.hs2
3 files changed, 17 insertions, 3 deletions
diff --git a/Handler/Common.hs b/Handler/Common.hs
index 1cf63de..65988da 100644
--- a/Handler/Common.hs
+++ b/Handler/Common.hs
@@ -10,13 +10,14 @@ module Handler.Common
10 , ReferenceState(..) 10 , ReferenceState(..)
11 , FormState(..) 11 , FormState(..)
12 , HasFormState(..) 12 , HasFormState(..)
13 , stockSort, referenceSort
13 ) where 14 ) where
14 15
15import Import 16import Import
16 17
17import Data.Unique 18import Data.Unique
18 19
19import qualified Data.Text as Text (pack) 20import qualified Data.Text as Text
20 21
21import Data.Set (Set) 22import Data.Set (Set)
22import qualified Data.Set as Set 23import qualified Data.Set as Set
@@ -165,3 +166,16 @@ kinds = do
165 [ [ itemKind | Entity _ Item{..} <- stock ] 166 [ [ itemKind | Entity _ Item{..} <- stock ]
166 , [ referenceKind | Entity _ Reference{..} <- reference ] 167 , [ referenceKind | Entity _ Reference{..} <- reference ]
167 ] 168 ]
169
170type Sort a = a -> a
171
172stockSort :: Sort [WithType (Entity Item)]
173stockSort = concat . map (sortOn $ entityVal . typedVal) . sortGroups . group
174 where
175 group = groupBy ((==) `on` valType) . sortOn valType
176 sortGroups = sortOn (fmap minimum . fromNullable . map (entityVal . typedVal))
177
178referenceSort :: Sort [WithType (Entity Reference)]
179referenceSort = sortBy referenceOrdering
180 where
181 referenceOrdering (Entity _ refA `WithType` refAType) (Entity _ refB `WithType` refBType) = compare refAType refBType <> compare refA refB
diff --git a/Handler/InventoryListing.hs b/Handler/InventoryListing.hs
index 6d4d8fa..cbf4eab 100644
--- a/Handler/InventoryListing.hs
+++ b/Handler/InventoryListing.hs
@@ -16,7 +16,7 @@ postInventoryListingR = do
16 FormFailure errors -> mapM_ (addMessage "formError" . toHtml) errors 16 FormFailure errors -> mapM_ (addMessage "formError" . toHtml) errors
17 _ -> return () 17 _ -> return ()
18 18
19 (sortOn (entityVal . typedVal) -> stock) <- runDB $ mapM withType =<< selectList [] [] 19 (stockSort -> stock) <- runDB $ mapM withType =<< selectList [] []
20 20
21 selectRep $ do 21 selectRep $ do
22 provideJson (stock :: [WithType (Entity Item)]) 22 provideJson (stock :: [WithType (Entity Item)])
diff --git a/Handler/ReferenceListing.hs b/Handler/ReferenceListing.hs
index 0b89a20..e433429 100644
--- a/Handler/ReferenceListing.hs
+++ b/Handler/ReferenceListing.hs
@@ -17,7 +17,7 @@ postReferenceListingR = do
17 FormFailure errors -> mapM_ (addMessage "formError" . toHtml) errors 17 FormFailure errors -> mapM_ (addMessage "formError" . toHtml) errors
18 _ -> return () 18 _ -> return ()
19 19
20 reference <- runDB $ mapM withType =<< selectList [] [Asc ReferenceKind] 20 (referenceSort -> reference) <- runDB $ mapM withType =<< selectList [] [Asc ReferenceKind]
21 21
22 selectRep $ do 22 selectRep $ do
23 provideJson (reference :: [WithType (Entity Reference)]) 23 provideJson (reference :: [WithType (Entity Reference)])