انتقل إلى المحتوى
أمين أبوزيد
JNR-018

تصميم RESTful

  • 6 درس
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • نمذجة الموارد (Resources)
  • عناوين URL
  • رموز الحالة (Status codes)
  • عقود الطلب والاستجابة

تعلّم أولًا

3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Model resources, sub-resources, and collections from domain requirements.
  2. Design URLs, methods, and status codes for create, read, update, delete, and actions that are not CRUD.
  3. Define request/response contracts before writing code.
  4. Critique an API design against REST conventions.

الدروس

  1. 01موارد لا دوال
  2. 02تصميم عناوين URL
  3. 03اختيار رموز الحالة
  4. 04نمذجة الإجراءات (الدفع، الإلغاء)
  5. 05التصميم بالعقد أولًا (Contract-first)
  6. 06عرض عملي: تصميم عقد Shop API على السبورة

المختبر والتمارين

Lab

JNR-018-LAB1 · Shop API design doc

Outcomes: 1–4 · Time: ~3 h Design endpoints for products, categories, cart, and orders. Produce a table (method, URL, success status, error statuses) plus an example request and response for each endpoint, including checkout and cancel. Then ask a peer to review it against a checklist (nouns, plural names, status codes, no leaked database structure) and record the changes.

Acceptance: the design is reviewed before any implementation starts.

Exercises (assessment: design review using the DES rubric dimension)

IDOutcomeTask
JNR-018-EX011From "a library lends books to members; members can reserve books", list resources, collections, and sub-resources.
JNR-018-EX022Rewrite these as REST: POST /createUser, GET /getUser?id=3, POST /deleteUser.
JNR-018-EX032Give the status code for: created, updated with body, deleted, not found, duplicate, invalid body.
JNR-018-EX042Design the endpoint to cancel an order, including the response when it is already shipped.
JNR-018-EX053Write a contract (request, response, errors) for POST /products before any code.
JNR-018-EX064Critique: GET /products/delete/5, POST /products/5/get, creation returning 200, /api/tbl_products. List the problems.
أظهر الحلول
  • EX01 Resources: book, member, reservation. Collections: /books, /members, /reservations. Sub-resource: /members/{id}/reservations.
  • EX02 POST /users, GET /users/3, DELETE /users/3.
  • EX03 201, 200, 204, 404, 409, 400.
  • EX04 POST /orders/{id}/cancel returns 200; if shipped, 409 with an error body explaining the state.
  • EX05 Request {"name","priceCents","categoryId"}; 201 with Location and the created body; 400 for invalid fields; 404 or 422 for an unknown category (pick one and document it).
  • EX06 Verb in path, GET that deletes (unsafe), verb in path again, creation must be 201, and the database table name leaks into the contract.

Outcome 4 is also exercised during the peer review in the lab.

الاختبار

Outcomes: O1 resources · O2 URLs, methods, status codes · O3 contract-first · O4 critique.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Which is a resource?O1
  2. 2/orders/7/items is:O1
  3. 3Best URL to list products:O2
  4. 4Successful creation returns:O2
  5. 5Delete succeeded, no body to return:O2
  6. 6Cancelling an order that already shipped is best answered with:O2
  7. 7Which request should NOT use GET?O2
  8. 8Contract-first means:O3
  9. 9What is wrong with POST /products/5/delete?O4
  10. 10Exposing table names like /tbl_products is bad because:O4

الجزء العملي

Six exercises JNR-018-EX01 to EX06 plus the design doc. Pass: at least five exercises and a design doc that was reviewed (DES rubric: meets or exceeds on contract clarity and consistency) before implementation.

أخطاء شائعة

Verbs in paths; 200 for creation; exposing database IDs and structure as the contract.

التقييم

Design review using the DES rubric dimension.

الصلة بالمسار الوظيفي

API contracts (SRC-006) and API evolution (MID-062).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.