Note
This is a simplified coding exercise implementation. It is not production-ready code.
A small TypeScript + Express API implementing a multi-layered architecture:
- Routes
- Controllers
- Services
- Repositories (in-memory)
- Models
- Unit tests (Jest)
- Create Book endpoint (
POST /books) - In-memory data storage with:
- ID uniqueness validation
- ISO
createdAttimestamps
- Find Book by ID endpoint (
GET /books/:id) - List/Filter Books endpoint (
GET /books)- Filter by genre (exact match, case-insensitive)
- Filter by title (partial match, case-insensitive)
- Filter by author (partial match, case-insensitive)
- Multiple filters with AND logic
- Update Book by ID endpoint (
PATCH /books/:id) - Delete Book by ID endpoint (
DELETE /books/:id) - Calculate Discounted Price endpoint (
GET /books/discounted-price)- Get total discounted price for all books of a specific genre
- Unit tests for:
- Controller layer
- Service layer
- Repository layer
npm install
npm run devnpm test- ID is provided by the client (per specification).
- Repository uses an in-memory store for this coding exercise.
- Layers follow clean architecture:
- Route → Controller → Service → Repository → Store
- Code is structured to allow easy migration to a real database later.
curl -i -X POST http://localhost:3000/books \
-H "Content-Type: application/json" \
-d '{
"id": 1,
"title": "Dune",
"author": "Frank Herbert",
"genre": "Sci-fi",
"price": 200
}'curl -i -X GET http://localhost:3000/books/1curl -i -X PATCH http://localhost:3000/books/1 \
-H "Content-Type: application/json" \
-d '{
"title": "Dune (Updated Edition)",
"price": 250
}'curl -i -X DELETE http://localhost:3000/books/1curl -i -X GET http://localhost:3000/bookscurl -i -X GET "http://localhost:3000/books?genre=Sci-Fi"curl -i -X GET "http://localhost:3000/books?title=dune"curl -i -X GET "http://localhost:3000/books?author=herbert"curl -i -X GET "http://localhost:3000/books?genre=Sci-Fi&author=asimov"curl -i -X GET "http://localhost:3000/books/discounted-price?genre=Sci-Fi&discount=20"Response:
{
"genre": "Sci-Fi",
"discount_percentage": 20,
"total_discounted_price": 120
}