Thuta Learning
Next.js
AdvancedWeb Developmentအလယ်အလတ်

Final Project — Myanmar Notes ကို Production-ready လုပ်ခြင်း

Next.jsသင်ခန်းစာ 2890 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Myanmar Notes ရဲ့ route၊ data layer၊ Server Action၊ permission ကို system တစ်ခုအဖြစ် ပုံဆွဲပြနိုင်ဖို့
  • Feature တစ်ခုစီကို UI ကနေ database နဲ့ test ထိ vertical slice အဖြစ် အဆုံးထိ ပြီးအောင် လုပ်နိုင်ဖို့
  • User မြင်တဲ့ behaviour နဲ့ server rule ပါတဲ့ acceptance criteria ရေးနိုင်ဖို့
  • Server Action တိုင်းမှာ session၊ validation၊ ownership ကို အစီအစဉ်တကျ စစ်နိုင်ဖို့
  • Architecture ဆုံးဖြတ်ချက်၊ setup၊ env၊ test၊ deploy ပါတဲ့ README ရေးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

ကားမောင်းသင်တန်းမှာ နည်းပြက ဘေးမှာ ထိုင်ပြီး ဘရိတ်ကို ကူနင်းပေးနိုင်ပါတယ်။ လိုင်စင်ရပြီး ပထမဆုံး တစ်ယောက်တည်း မောင်းတဲ့ နေ့ကျမှ တကယ် မောင်းတတ်ပြီလား သိရပါတယ်။ Tutorial ပြီးဆုံးတာလည်း ဒီလိုပါပဲ၊ ကိုးကားစရာ မကြည့်ဘဲ ကိုယ့်ဆုံးဖြတ်ချက်နဲ့ feature တစ်ခုကို အစကနေ အဆုံးထိ တည်ဆောက်နိုင်မှ ပညာက ကိုယ့်ဟာ ဖြစ်လာပါတယ်။

ဒီ final project မှာ အရင် lesson တွေကို အပိုင်းအစ သီးသီးအဖြစ် မဟုတ်ဘဲ system တစ်ခုအဖြစ် ပြန်ချိတ်ပါမယ်။ Routing က user ကို page ဆီ ပို့ပြီး data layer က note တွေကို ယူ၊ Server Action က ပြောင်းလဲမှုကို လုံခြုံစွာ သိမ်း၊ test နဲ့ deploy က အားလုံး ဆက်အလုပ်လုပ်နေကြောင်း အာမခံပါတယ်။

Problem → Why → Solution

ပြဿနာ: အားလုံး စပြီး ဘာမှ မပြီး

သုံးပတ်ကြာပြီးနောက် Myanmar Notes ရဲ့ အခြေအနေကို ကြည့်လိုက်တော့ feature တိုင်းက တဝက်တပျက်ပဲ ရှိပါတယ်။ Note list page က ကြည့်လို့ လှပေမယ့် data အတု ပြနေတယ်၊ create form က database ကို ချိတ်ထားပေမယ့် validation မရှိဘူး၊ login page ရှိပေမယ့် dashboard က ဘယ်သူ့ note ကိုမဆို ပြင်ခွင့်ပေးနေတယ်။

Demo ပြဖို့ တောင်းတဲ့အခါ အစကနေ အဆုံးထိ အလုပ်လုပ်တဲ့ flow တစ်ခုမှ မရှိပါဘူး။ ဘာက ပြီးပြီလဲ မေးရင်လည်း "90% လောက်" ဆိုတဲ့ အဖြေပဲ ရပါတယ်။

status.md
Notes list      UI done        data: hard-coded array
Note detail     UI done        404 page: missing
Create note     form done      validation: none, tests: none
Edit note       started        permission check: none
Login           page done      dashboard: not protected
Deploy          not started    env vars: unknown

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

အလုပ်ကို layer အလိုက် (UI အားလုံး အရင်၊ ပြီးမှ database အားလုံး၊ ပြီးမှ security) ခွဲလုပ်လို့ပါ။ Layer တိုင်းက 90% ရောက်နေပေမယ့် user တစ်ယောက် သုံးလို့ရတဲ့ feature က 0 ပါ။ Feature တစ်ခုကို UI၊ validation၊ database၊ permission၊ test အကုန် ပြီးအောင် လုပ်ပြီးမှ နောက်တစ်ခုကို ကူးရင် အချိန်မရွေး ပြလို့ရတဲ့ app တစ်ခု အမြဲ ရှိနေပါတယ်။

Core concept: vertical slice နဲ့ system တစ်ခုလုံး

Vertical slice ဆိုတာ ကိတ်မုန့်ကို အလွှာလိုက် မဟုတ်ဘဲ ထောင်လိုက် လှီးထားတဲ့ အပိုင်းလိုပါ။ အပိုင်းတိုင်းမှာ အလွှာ အားလုံး ပါပါတယ်။ "Note ပြင်ခြင်း" slice တစ်ခုမှာ edit page (route)၊ form (Client Component)၊ Server Action၊ zod validation၊ ownership စစ်တာ၊ database update၊ revalidatePath နဲ့ redirect၊ unit test နဲ့ E2E test တစ်ခု အကုန် ပါပါတယ်။ Slice တစ်ခု ပြီးတိုင်း deploy လုပ်လို့ ရတဲ့ app ပိုကောင်းလာပါတယ်။

Slice တိုင်းက system တစ်ခုတည်းရဲ့ အစိတ်အပိုင်း ဖြစ်တာမို့ architecture ကို အရင် ရှင်းထားဖို့ လိုပါတယ်။ Myanmar Notes မှာ data ကို ဖတ်တဲ့ လမ်းကြောင်းနဲ့ ပြောင်းလဲတဲ့ လမ်းကြောင်း နှစ်ခုပဲ ရှိပါတယ်။ ဖတ်တဲ့အခါ Server Component က lib/notes ထဲက function ကို ခေါ်ပြီး အဲဒီ function က database ကို query လုပ်ပါတယ်။ Public note တွေကို cache လုပ်ထားပြီး dashboard data ကိုတော့ user အလိုက် dynamic ဖြစ်ပါတယ်။ ပြောင်းလဲတဲ့အခါ form က Server Action ကို ခေါ်ပြီး action က အစီအစဉ် တိတိကျကျနဲ့ စစ်ပါတယ်: session ရှိလား၊ input မှန်လား၊ ဒီ user က ဒီ note ကို ပြင်ခွင့် ရှိလား၊ ပြီးမှ database ကို ပြောင်းကာ cache ကို revalidate လုပ်ပါတယ်။

Slice တစ်ခု "ပြီးပြီ" လို့ ဘယ်တော့ ပြောလို့ရလဲ ဆိုတာကို acceptance criteria က သတ်မှတ်ပါတယ်။ ကောင်းတဲ့ criteria က စစ်လို့ရပြီး ထင်မြင်ချက် မပါပါဘူး: "ကိုယ်ပိုင်မဟုတ်တဲ့ note ကို ပြင်ဖို့ ကြိုးစားရင် 403 ရပြီး database မပြောင်းဘူး" က စစ်လို့ရပေမယ့် "edit က ကောင်းကောင်း အလုပ်လုပ်တယ်" ကတော့ စစ်လို့ မရပါဘူး။ Criteria တစ်ခုစီကို test တစ်ခုနဲ့ ချိတ်ထားရင် နောက်ပိုင်း ပြောင်းလဲမှုတွေက ဘာကိုမှ မဖျက်ကြောင်း အလိုလို သိနိုင်ပါတယ်။

Slice တွေကို risk နဲ့ တန်ဖိုးအလိုက် အစီအစဉ် ချပါ။ တခြား slice အားလုံးက data အစစ် ပေါ်လာဖို့ မှီခိုနေလို့ read path ကို အရင်ဆောက်ပါ။ User ဘယ်သူလဲ မသိဘဲ edit နဲ့ delete က အဓိပ္ပာယ်မရှိလို့ authentication ကို ဒုတိယ ထားပါ။ Search၊ Open Graph ပုံ၊ animation လို အလှဆင်တဲ့ အပိုင်းတွေကို နောက်မှ ထားပါ: အလုပ်လုပ်နေတဲ့ app ကို ပိုကောင်းအောင် လုပ်ပေးနိုင်ပေမယ့် ပျက်နေတဲ့ app ကိုတော့ မကယ်နိုင်ပါဘူး။

text
Myanmar Notes: the read path and the write path
-----------------------------------------------
READ                                   WRITE
browser ─> /notes/[slug]               form submit (Client Component)
             Server Component                 │
                 │                             ▼
                 ▼                       Server Action
           lib/notes.getNote()        1. session?      no  -> 401
                 │                    2. zod valid?    no  -> 400
                 ▼                    3. owner/admin?  no  -> 403
              database  <───────────  4. db update
                 │                    5. revalidatePath + redirect
                 ▼
     HTML (cached for public notes)
Mental model: ကိတ်မုန့်ကို ထောင်လိုက် လှီးပါ
  1. Feature တစ်ခုရဲ့ criteria ကို အရင်ရေး
  2. UI မှ DB ထိ တစ်ခါတည်း ဖြတ်ဆောက်
  3. Server rule တိုင်းကို test နဲ့ ချုပ်
  4. Deploy ပြီးမှ နောက် slice ကို ကူး

အချိန်မရွေး "အခု deploy လုပ်ရင် user တွေ ဘာသုံးလို့ ရမလဲ" လို့ မေးပါ။ အဖြေက တိုးလာနေရပါမယ်။

အခြေခံ ပုံစံ: slice တစ်ခုရဲ့ acceptance criteria

text
Feature: <name>
As a <role>, I can <action> so that <benefit>.

Done when:
  - <what the user sees on success>
  - <what the user sees on invalid input, empty data or loading>
  - <the rule the server enforces, with its status code>
  - <the test that proves each rule>
  - <it works on the preview deployment>

Criteria တစ်ကြောင်းစီက စစ်လို့ရတဲ့ ဝါကျ ဖြစ်ရပါမယ်။ UI ပိုင်း၊ server ပိုင်း၊ test ပိုင်း သုံးမျိုးလုံး ပါမှ slice က တကယ် အဆုံးထိ ရောက်ပါတယ်။

ဥပမာ: Myanmar Notes ရဲ့ Definition of Done

အောက်က checklist က project တစ်ခုလုံး ပြီးဆုံးကြောင်း ပြတဲ့ Definition of Done ပါ။ အပေါ်ကနေ အောက်ကို တစ်ကြောင်းချင်း လုပ်ဖို့ မဟုတ်ပါဘူး။ Slice တစ်ခုစီက လိုင်းအများအပြားကို နည်းနည်းစီ ထိသွားပါတယ်: ဥပမာ "create note" slice က validation၊ server-side authorization၊ loading/error state နဲ့ test လိုင်းတွေကို တစ်ပြိုင်နက် ရှေ့တိုးစေပါတယ်။ လိုင်းတိုင်း ပြီးတဲ့အခါမှ app ကို production-ready လို့ ခေါ်နိုင်ပါတယ်။

text
Final Project Definition of Done

✓ Public notes list + detail routes
✓ Login-protected dashboard
✓ Create / edit / delete with validation
✓ Server-side authorization
✓ Loading / error / not-found states
✓ Search, metadata, sitemap and OG image
✓ Responsive and keyboard accessible UI
✓ Tests and successful production build
✓ Preview + production deployment
✓ Logs, Web Vitals and rollback plan

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Public read path✓ Public notes list + detail routes

    /notes နဲ့ /notes/[slug] က login မလိုဘဲ ဖတ်လို့ရတဲ့ public page တွေပါ။ Static ဒါမှမဟုတ် cache လုပ်ထားလို့ မြန်ပြီး metadata နဲ့ sitemap ထဲမှာလည်း ပါရပါမယ်။ မရှိတဲ့ slug အတွက် notFound() နဲ့ 404 page ပြပါ။

  2. Rules live on the server✓ Server-side authorization

    Edit button ကို ဖျောက်ထားတာက UI အဆင်ပြေဖို့ပဲ ဖြစ်ပြီး security မဟုတ်ပါဘူး။ Server Action တွေကို ဘယ်သူမဆို တိုက်ရိုက် ခေါ်နိုင်လို့ action တိုင်းက session နဲ့ ownership ကို ကိုယ်တိုင် စစ်ရပါမယ်။

  3. Every state, not just success✓ Loading / error / not-found states

    loading.tsx၊ error.tsx၊ not-found.tsx ကို အရေးကြီး route တိုင်းမှာ ထားပါ။ Note မရှိသေးတဲ့ user အသစ်အတွက် empty state လည်း လိုပါတယ်၊ user အသစ်တိုင်း ပထမဆုံး မြင်ရမယ့် screen ဖြစ်လို့ပါ။

  4. Proof, not hope✓ Tests and successful production build

    Validation နဲ့ permission rule တွေအတွက် unit test တွေ၊ note ဖန်တီးတဲ့ flow အတွက် E2E test တစ်ခု ရှိပြီး pnpm build က warning အသစ် မရှိဘဲ အောင်ရပါမယ်။

  5. Life after launch✓ Logs, Web Vitals and rollback plan

    Deploy ပြီးတာနဲ့ project မပြီးသေးပါဘူး။ Structured log၊ Web Vitals dashboard နဲ့ ပြန်ဆုတ်မယ့် version ရှိမှ ပြဿနာကို user မပြောခင် သိပြီး မိနစ်ပိုင်းအတွင်း ပြန်ဆုတ်နိုင်ပါတယ်။

You should see
အခြေခံမှ deployment အထိ ကိုယ်တိုင်ရှင်းပြပြီး ထိန်းသိမ်းနိုင်သော production-ready Myanmar Notes app တစ်ခု ရရှိမည်။

Folder ပုံစံ

ဒီ layout က အရင် lesson တွေမှာ သုံးခဲ့တဲ့ route group၊ dynamic route၊ data layer ခွဲထုတ်ပုံတွေကို ပေါင်းထားတာပါ။ စည်းမျဉ်း တစ်ခုတည်းက app/ ထဲမှာ route နဲ့ UI ပဲ ထားပြီး ဆုံးဖြတ်ချက် ချတဲ့ code (validation၊ permission၊ query) တွေကို lib/ ထဲ ထားတာပါ၊ ဒါမှ test လုပ်ရ လွယ်ပါတယ်။

text
app/
  (public)/notes/page.tsx            list, ?q= search, ?page= pagination
  (public)/notes/[slug]/page.tsx     detail + generateMetadata
  dashboard/layout.tsx               redirects signed-out users to /login
  dashboard/notes/new/page.tsx       create form
  dashboard/notes/[id]/edit/         edit form + actions.ts ("use server")
  api/health/route.ts                liveness / readiness
  sitemap.ts, robots.ts
lib/
  db.ts            database client (server-only)
  notes.ts         queries: getPublicNotes, getNote, ...
  validation.ts    zod schemas shared by actions and tests
  permissions.ts   canEdit(session, note)
e2e/create-note.spec.ts

Milestone အစီအစဉ်

Milestoneပြီးကြောင်း သက်သေ
1. Read path: list + detail + 404Preview URL မှာ database ထဲက note အစစ်တွေ ပြတယ်
2. Auth: login + ကာကွယ်ထားတဲ့ dashboardLogin မဝင်ဘဲ /dashboard ဖွင့်ရင် /login ကို redirect ဖြစ်တယ်
3. Create sliceE2E test က note ဖန်တီးပြီး detail page ကို ရောက်တယ်
4. Edit + delete sliceသူများ note အတွက် 403 ကို unit test က အတည်ပြုတယ်
5. Search၊ metadata၊ sitemap/sitemap.xml မှာ public note တွေ ပါတယ်
6. Production + monitoringSmoke test အောင်ပြီး dashboard မှာ release version မြင်ရတယ်

Server Action တိုင်းအတွက် security စစ်ဆေးချက်

၁။ Session ကို action ထဲမှာ ယူပါ

User ID ကို form ရဲ့ hidden field ကနေ ဘယ်တော့မှ မယူပါနဲ့၊ browser က ဘာမဆို ပို့နိုင်ပါတယ်။ Action ထဲမှာ cookie ကနေ session ကို ကိုယ်တိုင် ဖတ်ပြီး မရှိရင် ချက်ချင်း ရပ်ပါ။

၂။ Input အားလုံးကို schema နဲ့ စစ်ပါ

FormData ထဲက value တိုင်းက string ဒါမှမဟုတ် File ဖြစ်ပြီး ဘာမဆို ဖြစ်နိုင်ပါတယ်။ zod schema နဲ့ trim၊ အရှည်၊ ခွင့်ပြုတဲ့ value တွေကို စစ်ပြီး schema မှာ မပါတဲ့ field တွေကို လျစ်လျူရှုပါ။

၃။ Resource ကို database ကနေ ယူပြီး ownership စစ်ပါ

Note ကို ID နဲ့ database ကနေ ယူပြီး မရှိရင် 404၊ ပိုင်ရှင် ဒါမှမဟုတ် admin မဟုတ်ရင် 403 ပြန်ပေးပါ။ ဒီစစ်ဆေးချက်ကို lib/permissions ထဲမှာ ထားရင် action တိုင်း တူညီတဲ့ rule ကို သုံးပါတယ်။

၄။ ပြောင်းပြီးမှ cache ကို refresh လုပ်ပါ

Database update အောင်မှ revalidatePath ဒါမှမဟုတ် revalidateTag ကို ခေါ်ပြီး redirect လုပ်ပါ။ မဟုတ်ရင် public list က note အဟောင်းကို ဆက်ပြနေပါမယ်။

README မှာ ပါရမယ့် အချက်တွေ

  • Project က ဘာလဲ၊ live URL နဲ့ screenshot
  • Architecture ဆုံးဖြတ်ချက်တွေနဲ့ အကြောင်းရင်း (ဥပမာ "public note တွေကို cache လုပ်ပြီး dashboard ကို dynamic ထားတယ်")
  • Local setup: Node version၊ install command၊ database ကို စတင်ပုံ၊ seed data
  • Environment variable စာရင်း: နာမည်၊ ရည်ရွယ်ချက်၊ secret ဟုတ်မဟုတ် (တန်ဖိုး အစစ် မထည့်ပါ)
  • Test run ပုံ၊ deploy ပုံ နဲ့ rollback ပုံ

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ

Product team တွေက feature အကြီးတွေကို ဒီနည်းနဲ့ပဲ ခွဲလုပ်ကြပါတယ်။ Sprint တစ်ခု အဆုံးမှာ "UI 80% ပြီးပြီ" လို့ မပြောဘဲ "user တွေ note ပြင်လို့ ရပြီ၊ preview link ဒီမှာပါ" လို့ demo ပြပါတယ်။ အလုပ်ကို ticket ခွဲတဲ့အခါလည်း slice တစ်ခုစီကို acceptance criteria နဲ့ ရေးပြီး criteria တိုင်း test နဲ့ ပြည့်မှ ticket ကို ပိတ်ပါတယ်။

ဒါ့အပြင် ဒီ project က portfolio အတွက်လည်း အရေးကြီးပါတယ်။ Hiring လုပ်တဲ့ engineer တွေက ဒီဇိုင်း လှတာထက် README ကို ဖတ်ပြီး ဘာကြောင့် ဒီလို ဆုံးဖြတ်ခဲ့လဲ၊ test ရှိလား၊ deploy လုပ်ထားလား၊ permission ကို server ပေါ်မှာ စစ်လား ဆိုတာကို ကြည့်လေ့ရှိပါတယ်။ Live URL၊ ရှင်းလင်းတဲ့ commit history နဲ့ "ဒါကို နောက်တစ်ခါ ဆိုရင် ဘယ်လို ပြောင်းလုပ်မလဲ" ဆိုတဲ့ အပိုင်း ပါတဲ့ README က တကယ့် project ကို ကိုင်တွယ်ခဲ့ဖူးကြောင်း ပြပါတယ်။

အမှားများ

အမှားများတဲ့ အချက်တွေ

  • Feature အားလုံးကို တပြိုင်နက် စပြီး တစ်ခုမှ အဆုံးထိ မပြီးတာ။ UI အားလုံး ပြီးမှ backend ကို စရင် ချိတ်ဆက်တဲ့ အချိန်ကျမှ ပြဿနာ အကြီးကြီးတွေ တပြိုင်နက် ပေါ်လာပါတယ်။
  • Edit/Delete button ကို ပိုင်ရှင်မဟုတ်တဲ့ user ဆီ မပြတာနဲ့ လုံခြုံပြီလို့ ယူဆတာ။ Server Action ID ကို သိရင် browser ကနေ တိုက်ရိုက် ခေါ်လို့ ရလို့ action ထဲမှာ ownership မစစ်ရင် ဘယ်သူမဆို ပြင်နိုင်ပါတယ်။
  • Happy path ပဲ တည်ဆောက်ပြီး empty၊ loading၊ error၊ 404 state တွေကို မေ့တာ။ User အသစ် ပထမဆုံး မြင်ရမယ့် screen က note မရှိတဲ့ empty state ဖြစ်ပါတယ်။
  • Deploy ပြီးတာနဲ့ log၊ metric၊ dependency update တွေကို စစ်ဆေးတာ ရပ်လိုက်တာ။ Security patch တွေ နောက်ကျပြီး performance ကျလာတာကို user ပြောမှ သိရပါတယ်။

Mini challenge

ကိုယ်တိုင် လုပ်ကြည့်ရအောင်: "note ပြင်ခြင်း" slice ရဲ့ server ဘက်

UI မပါဘဲ "note ပြင်ခြင်း" slice ရဲ့ server ဘက် logic ကို ရေးပါ: zod schema၊ canEdit permission function နဲ့ updateNote action။ Database အစစ်အစား Map တစ်ခုကို သုံးပြီး acceptance criteria တစ်ခုစီကို case တစ်ခုအဖြစ် run ကာ ရလဒ်ကို print ထုတ်ပါ။ Project အစစ်မှာ session ကို action ထဲမှာ cookie ကနေ ဖတ်ပေမယ့် ဒီမှာ test လုပ်လို့ရအောင် parameter အဖြစ် ပေးထားပါတယ်။

  • Session မရှိရင် 401၊ title က trim ပြီး 3 လုံးအောက် ဒါမှမဟုတ် 80 ကျော်ရင် 400 ပြန်ပေးပါ
  • Note မရှိရင် 404၊ ပိုင်ရှင်လည်း admin လည်း မဟုတ်ရင် 403 ပြန်ပေးပါ
  • အောင်ရင် trim လုပ်ထားတဲ့ title နဲ့ သိမ်းပြီး checkbox မပါရင် isPublic က false ဖြစ်ရပါမယ်
  • စစ်ဆေးမှု အစီအစဉ်: session၊ validation၊ note ရှိမရှိ၊ ownership၊ ပြီးမှ save
edit-note-slice.ts
import { z } from "zod";

type Session = { userId: string; role: "member" | "admin" } | null;
type Note = {
  id: string;
  ownerId: string;
  title: string;
  body: string;
  isPublic: boolean;
};
type ActionResult =
  | { ok: true; note: Note }
  | { ok: false; status: 400 | 401 | 403 | 404; message: string };

// TODO: an in-memory db with findNote(id) and saveNote(note)
// TODO: EditNoteSchema: id, title (trim, 3-80), body (max 5000), isPublic ("on" | "off")

export function canEdit(session: Session, note: Note): boolean {
  // TODO: owner or admin
  return false;
}

export async function updateNote(
  session: Session,
  form: FormData,
): Promise<ActionResult> {
  // TODO: session -> validation -> find note -> canEdit -> save
  return { ok: false, status: 400, message: "not implemented" };
}

// TODO: run five cases (signed out, stranger, short title, missing note, owner edit)
// and print one line per case

ရလဒ်: Case ငါးခုအတွက် "signed out -> 401"၊ "stranger -> 403"၊ "short title -> 400"၊ "missing note -> 404" နဲ့ "owner edit -> saved \"Mohinga, my way\" (public: false)" ဆိုတဲ့ စာကြောင်း ငါးကြောင်း ပေါ်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

Object.fromEntries(form) က FormData ကို object ပြောင်းပေးလို့ schema.safeParse() ကို တိုက်ရိုက် ပေးနိုင်ပါတယ်။ z.string().trim().min(3) က length ကို trim ပြီးမှ စစ်ပါတယ်။ Checkbox က check မလုပ်ရင် form ထဲ လုံးဝ မပါလို့ isPublic ကို .default("off") နဲ့ ကိုင်တွယ်ပါ။

အဖြေ ကြည့်ရန်
edit-note-slice.ts
import { z } from "zod";

// One vertical slice of Myanmar Notes: "edit a note", without the UI.
type Session = { userId: string; role: "member" | "admin" } | null;
type Note = {
  id: string;
  ownerId: string;
  title: string;
  body: string;
  isPublic: boolean;
};
type ActionResult =
  | { ok: true; note: Note }
  | { ok: false; status: 400 | 401 | 403 | 404; message: string };

// lib/db.ts (an in-memory stand-in for the real database layer)
const notes = new Map<string, Note>([
  ["n1", { id: "n1", ownerId: "u1", title: "Mohinga recipe", body: "", isPublic: true }],
]);
const db = {
  findNote: async (id: string) => notes.get(id) ?? null,
  saveNote: async (note: Note) => {
    notes.set(note.id, note);
    return note;
  },
};

// lib/validation.ts
const EditNoteSchema = z.object({
  id: z.string().min(1),
  title: z.string().trim().min(3, "Title is too short").max(80, "Title is too long"),
  body: z.string().max(5000),
  isPublic: z.enum(["on", "off"]).default("off"),
});

// lib/permissions.ts
export function canEdit(session: Session, note: Note): boolean {
  return !!session && (session.role === "admin" || session.userId === note.ownerId);
}

// app/dashboard/notes/[id]/edit/actions.ts ("use server" in the real file,
// where the session comes from the cookie inside the action, never from the form)
export async function updateNote(
  session: Session,
  form: FormData,
): Promise<ActionResult> {
  if (!session) return { ok: false, status: 401, message: "Please sign in" };

  const parsed = EditNoteSchema.safeParse(Object.fromEntries(form));
  if (!parsed.success) {
    return { ok: false, status: 400, message: parsed.error.issues[0].message };
  }
  const note = await db.findNote(parsed.data.id);
  if (!note) return { ok: false, status: 404, message: "Note not found" };
  if (!canEdit(session, note)) {
    return { ok: false, status: 403, message: "Not your note" };
  }

  const saved = await db.saveNote({
    ...note,
    title: parsed.data.title,
    body: parsed.data.body,
    isPublic: parsed.data.isPublic === "on",
  });
  // The real action would now call revalidatePath("/notes") and redirect.
  return { ok: true, note: saved };
}

// Acceptance checks for the slice
function form(fields: Record<string, string>) {
  const fd = new FormData();
  for (const [key, value] of Object.entries(fields)) fd.set(key, value);
  return fd;
}

async function demo() {
  const owner: Session = { userId: "u1", role: "member" };
  const stranger: Session = { userId: "u2", role: "member" };
  const cases: [string, Session, Record<string, string>][] = [
    ["signed out", null, { id: "n1", title: "Hacked", body: "" }],
    ["stranger", stranger, { id: "n1", title: "Hacked", body: "" }],
    ["short title", owner, { id: "n1", title: "Hi", body: "" }],
    ["missing note", owner, { id: "n9", title: "Shan noodles", body: "" }],
    ["owner edit", owner, { id: "n1", title: "  Mohinga, my way ", body: "Fish broth" }],
  ];
  for (const [name, session, fields] of cases) {
    const result = await updateNote(session, form(fields));
    const detail = result.ok
      ? `saved "${result.note.title}" (public: ${result.note.isPublic})`
      : `${result.status} ${result.message}`;
    console.log(`${name.padEnd(12)} -> ${detail}`);
  }
}

demo();

Quick Quiz 1 / 3

Myanmar Notes ကို vertical slice အလိုက် တည်ဆောက်တယ်ဆိုတာ ဘာကို ဆိုလိုလဲ။

Quick Quiz 2 / 3

Note ကို ပိုင်ရှင်ပဲ ပြင်ခွင့်ရှိရမယ်။ ဒီ rule ကို ဘယ်မှာ မဖြစ်မနေ စစ်ရမလဲ။

Quick Quiz 3 / 3

Challenge solution မှာ isPublic checkbox ကို မပို့ဘဲ owner က title " Mohinga, my way " နဲ့ ပြင်ရင် ဘာ print ထုတ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Feature တွေကို layer အလိုက် မဟုတ်ဘဲ UI မှ database နဲ့ test ထိ vertical slice အလိုက် တစ်ခုချင်း အဆုံးထိ ပြီးအောင် လုပ်ပါ
  • Slice တိုင်းအတွက် စစ်လို့ရတဲ့ acceptance criteria ရေးပြီး criteria တစ်ခုစီကို test နဲ့ ချိတ်ပါ
  • Server Action တိုင်းမှာ session၊ validation၊ resource ရှိမရှိ၊ ownership ကို အစီအစဉ်အတိုင်း server ပေါ်မှာ စစ်ပါ
  • Empty၊ loading၊ error၊ 404 state တွေနဲ့ deploy ပြီးနောက် monitoring ကိုလည်း "done" ထဲ ထည့်ပါ
  • README မှာ architecture ဆုံးဖြတ်ချက်၊ setup၊ env၊ test၊ deploy နဲ့ rollback ကို ရေးထားပါ

Next.js — Production Checklist — Next.js

Next.js — Production Checklist — Next.js

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Final Project — Myanmar Notes ကို Production-ready လုပ်ခြင်း | Thuta Learning