Changes between Version 2 and Version 3 of P6


Ignore:
Timestamp:
04/25/26 21:42:40 (7 days ago)
Author:
211099
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • P6

    v2 v3  
    124124      AND story_id = 1
    125125),
    126 inserted AS (
    127     INSERT INTO reading_list_items (list_id, story_id, added_at)
    128     SELECT ul.list_id, ps.story_id, CURRENT_TIMESTAMP
    129     FROM user_list ul
    130     CROSS JOIN published_story ps
    131     WHERE NOT EXISTS (SELECT 1 FROM already_added)
    132     RETURNING list_id, story_id, added_at
     126list_items AS (
     127    SELECT
     128        rli.list_id,
     129        COUNT(rli.story_id) AS total_items,
     130        MAX(rli.added_at)   AS last_added_at
     131    FROM reading_list_items rli
     132    WHERE rli.list_id = 1
     133    GROUP BY rli.list_id
    133134)
    134135SELECT
     
    136137    rl.list_name,
    137138    rl.is_public,
    138     u.username                  AS owner,
    139     COUNT(rli.story_id)         AS total_items,
    140     MAX(rli.added_at)           AS last_added_at,
     139    u.username                              AS owner,
     140    ps.story_id                             AS story_to_add,
     141    ps.short_description                    AS story_description,
     142    COALESCE(li.total_items, 0)             AS total_items,
     143    li.last_added_at,
    141144    CASE
    142         WHEN ins.story_id IS NOT NULL         THEN 'Story successfully added'
    143         WHEN EXISTS (SELECT 1 FROM already_added) THEN 'Story already in list'
    144         ELSE                                       'Failed: story not published or list not found'
    145     END                         AS result_message
    146 FROM reading_list       rl
    147 JOIN users              u   ON rl.user_id  = u.user_id
    148 LEFT JOIN reading_list_items rli ON rl.list_id = rli.list_id
    149 LEFT JOIN inserted      ins ON ins.list_id  = rl.list_id
    150 WHERE rl.list_id = 1
    151 GROUP BY rl.list_id, rl.list_name, rl.is_public, u.username, ins.story_id;
     145        WHEN NOT EXISTS (SELECT 1 FROM published_story) THEN 'Failed: story not published or not found'
     146        WHEN NOT EXISTS (SELECT 1 FROM user_list)       THEN 'Failed: reading list not found or not owned by you'
     147        WHEN EXISTS     (SELECT 1 FROM already_added)   THEN 'Story already in list'
     148        ELSE                                                 'Ready to add story'
     149    END                                     AS status
     150FROM reading_list               rl
     151JOIN users                      u   ON rl.user_id  = u.user_id
     152LEFT JOIN published_story       ps  ON ps.story_id = 1
     153LEFT JOIN list_items            li  ON li.list_id  = rl.list_id
     154WHERE rl.list_id = 1;
    152155}}}
    153156
    154157=== Relational Algebra
    155158{{{
    156 
    157 PublishedStory <-
     159ValidateAndInsertStoryIntoUserReadingListWithConfirmation
     160
     161
     162PublishedStory ←
    158163π story_id, short_description
    159164(
    160   σ s.story_id = :story_id ∧ st.status = 'published'
     165  σ s.story_id = 1 ∧ st.status = 'published'
    161166  (
    162167    story s
     
    165170)
    166171
    167 UserList <-
    168 σ list_id = :list_id ∧ user_id = :user_id
    169 (
    170   reading_list
    171 )
    172 
    173 AlreadyAdded <-
    174 σ list_id = :list_id ∧ story_id = :story_id
     172UserList ←
     173π list_id, list_name, is_public
     174(
     175  σ list_id = 1 ∧ user_id = 4
     176  (
     177    reading_list
     178  )
     179)
     180
     181AlreadyAdded ←
     182σ list_id = 1 ∧ story_id = 1
    175183(
    176184  reading_list_items
    177185)
    178186
    179 NewItem <-
    180 { (list_id := :list_id, story_id := :story_id, added_at := CURRENT_TIMESTAMP) }
    181 
    182 ToInsert <-
    183 (UserList ⨯ PublishedStory)
    184 − π list_id, story_id, added_at (AlreadyAdded)
    185 
    186 reading_list_items <- reading_list_items ∪ ToInsert
    187 
    188 UpdatedCount <-
     187ListItems ←
    189188γ list_id;
    190189  total_items  := COUNT(story_id),
    191   last_added   := MAX(added_at)
    192 (
    193   σ list_id = :list_id (reading_list_items)
    194 )
    195 
    196 Result <-
     190  last_added_at := MAX(added_at)
     191(
     192  σ list_id = 1
     193  (
     194    reading_list_items
     195  )
     196)
     197
     198Result ←
    197199π
    198200  rl.list_id,
     
    200202  rl.is_public,
    201203  u.username                                        → owner,
    202   uc.total_items,
    203   uc.last_added,
     204  ps.story_id                                       → story_to_add,
     205  ps.short_description                              → story_description,
     206  COALESCE(li.total_items, 0)                       → total_items,
     207  li.last_added_at,
    204208  CASE
    205     WHEN ins.story_id IS NOT NULL     → 'Story successfully added'
    206     WHEN AlreadyAdded ≠ ∅             → 'Story already in list'
    207     ELSE                               'Failed: story not published or list not found'
    208   END                                               → result_message
    209 (
    210   (
    211     reading_list rl
    212     ⨝ (rl.user_id = u.user_id) users u
    213   )
    214   ⨝  (rl.list_id = uc.list_id) UpdatedCount uc
    215   ⟕  (rl.list_id = ins.list_id) ToInsert ins
    216 )
    217 }}}
     209    WHEN PublishedStory = ∅   → 'Failed: story not published or not found'
     210    WHEN UserList = ∅         → 'Failed: reading list not found or not owned by you'
     211    WHEN AlreadyAdded ≠ ∅     → 'Story already in list'
     212    ELSE                      → 'Ready to add story'
     213  END                                               → status
     214(
     215  (
     216    (
     217      σ rl.list_id = 1 (reading_list rl)
     218      ⨝ (rl.user_id = u.user_id) users u
     219    )
     220    ⟕ (ps.story_id = 1) PublishedStory ps
     221  )
     222  ⟕ (li.list_id = rl.list_id) ListItems li
     223)
     224}}}