Design a User CRUD REST Service
viaLeetCode
Problem Design and code a User CRUD REST service: add, update, get, and delete users, with proper layering.
Requirements
- Endpoints: POST /users, GET /users/{id}, GET /users (paginated), PUT/PATCH /users/{id}, DELETE /users/{id}.
- Correct status codes (201 on create, 404 on missing, 400 on validation failure) and idempotency semantics (PUT idempotent, POST not).
- Validation (email format, required fields, uniqueness) with clear error responses.
Core design
- Three layers: Controller (HTTP mapping, request/response DTOs) → Service (business rules, transactions) → Repository (persistence). Entities never leak raw to the API — map entity ↔ DTO.
- Model: User(id, name, email, createdAt, …); repository interface with an in-memory or JPA/SQL implementation behind it.
Discussion points
- Why DTOs and layering: independent evolution, testability (mock the repository), validation at the boundary.
- Partial update semantics (PATCH) vs full replace (PUT); optimistic locking/version field for concurrent updates.
- Soft delete vs hard delete; pagination and filtering contracts; how you'd unit-test the service layer.
asked …