mirror of
https://github.com/aelve/guide.git
synced 2024-12-22 12:21:31 +03:00
Fix Postgres TODOs (#379)
* Use $(fields) in Guide.Database.Queries.Select * Checking schema hash is hard and not really necessary * Explain why we store deleted objects
This commit is contained in:
parent
c1c815b3ce
commit
29a2fb718c
@ -114,9 +114,9 @@ selectCategoryRowByItemMaybe itemId = do
|
||||
|
||||
-- | Get a list of available categories' IDs.
|
||||
--
|
||||
-- Includes categories marked as deleted.
|
||||
--
|
||||
-- TODO explain why we store deleted categories at all.
|
||||
-- Includes categories marked as deleted. See
|
||||
-- <https://www.notion.so/aelve/Handling-of-deleted-objects-aa21056f7bdb43d2b635a8f6f93541a3>
|
||||
-- for an explanation of why we store deleted objects at all.
|
||||
selectCategoryIds :: ExceptT DatabaseError Transaction [Uid Category]
|
||||
selectCategoryIds = do
|
||||
let statement :: Statement () [Uid Category]
|
||||
@ -206,7 +206,7 @@ selectDeletedItemRowsByCategory catId = do
|
||||
|]
|
||||
lift $ HT.statement catId statement
|
||||
|
||||
-- Get available ItemRows belonging to a category.
|
||||
-- | Get available ItemRows belonging to a category.
|
||||
--
|
||||
-- Returns item rows sorted by order.
|
||||
selectItemRowsByCategory :: Uid Category -> ExceptT DatabaseError Transaction [ItemRow]
|
||||
|
@ -36,8 +36,6 @@ migrations =
|
||||
-- not create a database if it does not exist yet. You should create the
|
||||
-- database manually by doing @CREATE DATABASE guide;@ or run Postgres with
|
||||
-- @POSTGRES_DB=guide@ when running when running the app for the first time.
|
||||
--
|
||||
-- TODO: check schema hash as well, not just schema version?
|
||||
setupDatabase :: IO ()
|
||||
setupDatabase = do
|
||||
conn <- connect
|
||||
|
@ -116,12 +116,9 @@ instance FromPostgresRow TraitRow
|
||||
|
||||
-- | Convert CategoryRow to Category.
|
||||
--
|
||||
-- | To fetch items (they have an order) use 'getItemRowsByCategory' from 'Get' module.
|
||||
-- | To fetch deleted items use 'getDeletedItemRowsByCategory' from 'Get' module
|
||||
--
|
||||
-- TODO: somehow handle the case when item IDs don't match the @itemsOrder@?
|
||||
--
|
||||
-- TODO: use 'fields' for pattern-matching.
|
||||
-- To fetch items, use @selectItemRowsByCategory@ from
|
||||
-- "Guide.Database.Queries.Select". To fetch deleted items, use
|
||||
-- @selectDeletedItemRowsByCategory@.
|
||||
categoryRowToCategory
|
||||
:: "items" :! [Item]
|
||||
-> "itemsDeleted" :! [Item]
|
||||
@ -130,7 +127,7 @@ categoryRowToCategory
|
||||
categoryRowToCategory
|
||||
(arg #items -> items)
|
||||
(arg #itemsDeleted -> itemsDeleted)
|
||||
CategoryRow{..}
|
||||
$(fields 'CategoryRow)
|
||||
=
|
||||
Category
|
||||
{ categoryUid = categoryRowUid
|
||||
@ -143,28 +140,37 @@ categoryRowToCategory
|
||||
, categoryItemsDeleted = itemsDeleted
|
||||
, categoryEnabledSections = categoryRowEnabledSections
|
||||
}
|
||||
where
|
||||
-- Ignored fields
|
||||
_ = categoryRowDeleted
|
||||
_ = categoryRowItemsOrder
|
||||
|
||||
-- | Convert Category to CategoryRow.
|
||||
categoryToRowCategory :: Category -> "deleted" :! Bool -> CategoryRow
|
||||
categoryToRowCategory $(fields 'Category) (arg #deleted -> deleted) = CategoryRow
|
||||
{ categoryRowUid = categoryUid
|
||||
, categoryRowTitle = categoryTitle
|
||||
, categoryRowCreated = categoryCreated
|
||||
, categoryRowGroup = categoryGroup
|
||||
, categoryRowStatus = categoryStatus
|
||||
, categoryRowNotes = markdownBlockSource categoryNotes
|
||||
, categoryRowEnabledSections = categoryEnabledSections
|
||||
, categoryRowItemsOrder = map itemUid categoryItems
|
||||
, categoryRowDeleted = deleted
|
||||
}
|
||||
categoryToRowCategory
|
||||
:: Category
|
||||
-> "deleted" :! Bool
|
||||
-> CategoryRow
|
||||
categoryToRowCategory $(fields 'Category) (arg #deleted -> deleted) =
|
||||
CategoryRow
|
||||
{ categoryRowUid = categoryUid
|
||||
, categoryRowTitle = categoryTitle
|
||||
, categoryRowCreated = categoryCreated
|
||||
, categoryRowGroup = categoryGroup
|
||||
, categoryRowStatus = categoryStatus
|
||||
, categoryRowNotes = markdownBlockSource categoryNotes
|
||||
, categoryRowEnabledSections = categoryEnabledSections
|
||||
, categoryRowItemsOrder = map itemUid categoryItems
|
||||
, categoryRowDeleted = deleted
|
||||
}
|
||||
where
|
||||
-- Ignored fields
|
||||
_ = categoryItemsDeleted
|
||||
|
||||
-- | Convert ItemRow to Item.
|
||||
--
|
||||
-- | To fetch traits (they have an order) use 'getTraitRowsByItem' from 'Get' module.
|
||||
-- | To fetch deleted traits use 'getDeletedTraitRowsByItem' from 'Get' module
|
||||
-- To fetch traits, use @getTraitRowsByItem@ from
|
||||
-- "Guide.Database.Queries.Select". To fetch deleted traits, use
|
||||
-- @getDeletedTraitRowsByItem@.
|
||||
itemRowToItem
|
||||
:: "proTraits" :! [Trait]
|
||||
-> "proDeletedTraits" :! [Trait]
|
||||
@ -177,7 +183,7 @@ itemRowToItem
|
||||
(arg #proDeletedTraits -> proDeletedTraits)
|
||||
(arg #conTraits -> conTraits)
|
||||
(arg #conDeletedTraits -> conDeletedTraits)
|
||||
ItemRow{..}
|
||||
$(fields 'ItemRow)
|
||||
=
|
||||
Item
|
||||
{ itemUid = itemRowUid
|
||||
@ -195,35 +201,46 @@ itemRowToItem
|
||||
}
|
||||
where
|
||||
prefix = "item-notes-" <> uidToText itemRowUid <> "-"
|
||||
-- Ignored fields
|
||||
_ = (itemRowConsOrder, itemRowProsOrder)
|
||||
_ = itemRowCategoryUid
|
||||
_ = itemRowDeleted
|
||||
|
||||
-- | Convert Item to ItemRow.
|
||||
itemToRowItem :: Uid Category -> "deleted" :! Bool -> Item -> ItemRow
|
||||
itemToRowItem catId (arg #deleted -> deleted) $(fields 'Item) = ItemRow
|
||||
{ itemRowUid = itemUid
|
||||
, itemRowName = itemName
|
||||
, itemRowCreated = itemCreated
|
||||
, itemRowLink = itemLink
|
||||
, itemRowHackage = itemHackage
|
||||
, itemRowSummary = markdownBlockSource itemSummary
|
||||
, itemRowEcosystem = markdownBlockSource itemEcosystem
|
||||
, itemRowNotes = markdownTreeSource itemNotes
|
||||
, itemRowDeleted = deleted
|
||||
, itemRowCategoryUid = catId
|
||||
, itemRowProsOrder = map traitUid itemPros
|
||||
, itemRowConsOrder = map traitUid itemCons
|
||||
}
|
||||
itemToRowItem catId (arg #deleted -> deleted) $(fields 'Item) =
|
||||
ItemRow
|
||||
{ itemRowUid = itemUid
|
||||
, itemRowName = itemName
|
||||
, itemRowCreated = itemCreated
|
||||
, itemRowLink = itemLink
|
||||
, itemRowHackage = itemHackage
|
||||
, itemRowSummary = markdownBlockSource itemSummary
|
||||
, itemRowEcosystem = markdownBlockSource itemEcosystem
|
||||
, itemRowNotes = markdownTreeSource itemNotes
|
||||
, itemRowDeleted = deleted
|
||||
, itemRowCategoryUid = catId
|
||||
, itemRowProsOrder = map traitUid itemPros
|
||||
, itemRowConsOrder = map traitUid itemCons
|
||||
}
|
||||
where
|
||||
-- Ignored fields
|
||||
_ = (itemConsDeleted, itemProsDeleted)
|
||||
|
||||
-- | Convert TraitRow to Trait.
|
||||
traitRowToTrait :: TraitRow -> Trait
|
||||
traitRowToTrait TraitRow{..} = Trait
|
||||
{ traitUid = traitRowUid
|
||||
, traitContent = toMarkdownInline traitRowContent
|
||||
}
|
||||
traitRowToTrait $(fields 'TraitRow) =
|
||||
Trait
|
||||
{ traitUid = traitRowUid
|
||||
, traitContent = toMarkdownInline traitRowContent
|
||||
}
|
||||
where
|
||||
-- Ignored fields
|
||||
_ = traitRowItemUid
|
||||
_ = traitRowType
|
||||
_ = traitRowDeleted
|
||||
|
||||
-- Convert Trait to TraitRow
|
||||
-- | Convert Trait to TraitRow.
|
||||
traitToTraitRow
|
||||
:: Uid Item
|
||||
-> "deleted" :! Bool
|
||||
|
Loading…
Reference in New Issue
Block a user