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

واجهات CRUD

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

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

نظرة عامة

الموضوعات

  • Controllers
  • أجسام الطلبات (Request bodies)
  • معاملات المسار والاستعلام
  • الترقيم (Pagination)
  • الترتيب (Sorting)

تعلّم أولًا

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

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

  1. Implement create, read, update (PUT and PATCH), and delete endpoints with correct status codes and Location headers.
  2. Bind path variables, query parameters, and request bodies.
  3. Implement pagination and sorting with limits on page size and allowed sort fields.
  4. Return 201, 204, and 404 where appropriate.

الدروس

  1. 01الإنشاء: POST والرمز 201 وLocation
  2. 02القراءة: عنصر واحد ومجموعة
  3. 03التحديث: PUT مقابل PATCH
  4. 04الحذف
  5. 05الترقيم (Pagination)
  6. 06الترتيب وحدود أمانه
  7. 07عرض عملي: CRUD كامل للفئات، مباشرةً
  8. 08عرض عملي: ترقيم وترتيب آمنان

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

Lab

JNR-019-LAB1 · Catalog CRUD

Outcomes: 1–4 · Time: ~5 h Implement full product and category CRUD over the in-memory repository, following your JNR-018 design. The product list must be paginated and sortable with a maximum page size and an allow-list of sort fields. Write controller tests (@WebMvcTest) for every endpoint, including 404 and invalid-input cases.

Acceptance: matches the JNR-018 design; page size capped; controller tests cover every endpoint.

Exercises (assessment: implement an unseen resource from a spec)

IDOutcomeTask
JNR-019-EX011, 4Implement POST /suppliers returning 201 with a Location header.
JNR-019-EX022, 4Implement GET /suppliers/{id} (path variable) returning 404 when missing, and GET /suppliers?country= (query parameter).
JNR-019-EX031Implement PUT /suppliers/{id} (replace) and explain how it differs from PATCH.
JNR-019-EX041Implement PATCH /suppliers/{id} so omitted fields stay unchanged.
JNR-019-EX051, 4Implement DELETE /suppliers/{id} returning 204, and decide what a missing ID returns.
JNR-019-EX063Add page, size, sort to GET /suppliers with max size 50 and allowed sort fields name and country.
أظهر الحلول
  • EX01 ResponseEntity.created(URI.create("/suppliers/" + s.id())).body(s).
  • EX02 @PathVariable long id; throw ResponseStatusException(NOT_FOUND) when empty; @RequestParam(required = false) String country.
  • EX03 PUT requires all fields and replaces the item (idempotent); PATCH changes only supplied fields.
  • EX04 Patch DTO with nullable fields; set only those that are not null; test that an unsent field keeps its value.
  • EX05 ResponseEntity.noContent().build(); document 404 for missing (or 204, but be consistent).
  • EX06 size = Math.min(size, 50); Set.of("name","country").contains(field) else 400; add id as tie-breaker.

Outcome 2 is also covered by the lab (path, query, body binding in the catalog endpoints).

الاختبار

Outcomes: O1 create, read, update, delete · O2 binding · O3 pagination and sorting · O4 status codes.

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

  1. 1After a successful create, the Location header holds:O1
  2. 2PUT differs from PATCH because PUT:O1
  3. 3A PATCH body has only name. The description should:O1
  4. 4Which annotation binds {id} in the path?O2
  5. 5Which binds the JSON body?O2
  6. 6Why cap the page size?O3
  7. 7Sorting should accept:O3
  8. 8A tie-breaker such as id in sorting is used to:O3
  9. 9A successful delete with no body returns:O4
  10. 10GET /categories/999 where none exists returns:O4

الجزء العملي

Six exercises JNR-019-EX01 to EX06 on an unseen resource plus the catalog lab. Pass: at least five exercises and a lab with capped paging and tests for every endpoint.

أخطاء شائعة

Unbounded list endpoints; PATCH that nulls missing fields; sorting by arbitrary user-supplied columns.

التقييم

Implement an unseen resource from a spec.

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

The daily work of a junior backend engineer.

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