diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-15 16:56:58 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-15 16:56:58 +0100 |
commit | 37f0dac79707a0de81ec6364d2704007eefd9289 (patch) | |
tree | 21c2fd38be8202f5700477360a59a3173057105a /Handler | |
parent | fe5cd6ad6c61eb13ca99acd1b69cd09b84051404 (diff) | |
download | bar-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.hs | 16 | ||||
-rw-r--r-- | Handler/InventoryListing.hs | 2 | ||||
-rw-r--r-- | Handler/ReferenceListing.hs | 2 |
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 | ||
15 | import Import | 16 | import Import |
16 | 17 | ||
17 | import Data.Unique | 18 | import Data.Unique |
18 | 19 | ||
19 | import qualified Data.Text as Text (pack) | 20 | import qualified Data.Text as Text |
20 | 21 | ||
21 | import Data.Set (Set) | 22 | import Data.Set (Set) |
22 | import qualified Data.Set as Set | 23 | import 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 | |||
170 | type Sort a = a -> a | ||
171 | |||
172 | stockSort :: Sort [WithType (Entity Item)] | ||
173 | stockSort = 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 | |||
178 | referenceSort :: Sort [WithType (Entity Reference)] | ||
179 | referenceSort = 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)]) |