Skip to content

Commit 9c9c927

Browse files
committed
📖 docs: Add comprehensive CRUD operations documentation - Complete @crud annotation guide with examples - Configuration options and best practices - Testing commands and API examples - Integration with existing docs structure - Quick start guide for zero-boilerplate development
1 parent 3b6a157 commit 9c9c927

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

docs-site/docs/crud.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# 🚀 CRUD Operations
2+
3+
Get full REST API with **zero boilerplate code**! The `@Crud` annotation automatically generates all standard CRUD endpoints for your entities.
4+
5+
## ⚡ Quick Start
6+
7+
### 1. Create Your Entity
8+
```java
9+
@Entity
10+
@Table(name = "products")
11+
public class Product {
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
private Long id;
15+
16+
private String name;
17+
private String description;
18+
private Double price;
19+
private Integer stock;
20+
21+
// Constructors, getters, setters...
22+
}
23+
```
24+
25+
### 2. Create Repository
26+
```java
27+
@Component
28+
public interface ProductRepository extends BaseRepository<Product, Long> {
29+
// Framework handles all basic operations automatically
30+
}
31+
```
32+
33+
### 3. Add @Crud Controller
34+
```java
35+
@Component
36+
@Crud(
37+
entity = Product.class,
38+
endpoint = "/api/products",
39+
enablePagination = true,
40+
enableSearch = true,
41+
searchableFields = {"name", "description"}
42+
)
43+
public class ProductController {
44+
// That's it! All CRUD endpoints are auto-generated
45+
}
46+
```
47+
48+
### 4. Start Using Your API
49+
50+
**🎉 You instantly get these endpoints:**
51+
52+
| Method | Endpoint | Description |
53+
|--------|----------|-------------|
54+
| `GET` | `/api/products/{id}` | Get product by ID |
55+
| `POST` | `/api/products` | Create new product |
56+
| `PUT` | `/api/products/{id}` | Update product |
57+
| `DELETE` | `/api/products/{id}` | Delete product |
58+
| `GET` | `/api/products/search?name=laptop` | Search products |
59+
60+
## 📋 @Crud Configuration
61+
62+
```java
63+
@Crud(
64+
entity = Product.class, // Your JPA entity
65+
endpoint = "/api/products", // Base URL path
66+
enablePagination = true, // Adds ?page=1&size=10 support
67+
enableSearch = true, // Adds /search endpoint
68+
searchableFields = {"name", "description"}, // Fields to search in
69+
enableExists = true // Adds /exists/{id} endpoint
70+
)
71+
```
72+
73+
### Configuration Options
74+
75+
| Option | Default | Description |
76+
|--------|---------|-------------|
77+
| `entity` | **Required** | The JPA entity class |
78+
| `endpoint` | **Required** | Base URL path for endpoints |
79+
| `enablePagination` | `false` | Enable pagination support |
80+
| `enableSearch` | `false` | Enable search functionality |
81+
| `searchableFields` | `{}` | Fields to search in (when search enabled) |
82+
| `enableExists` | `false` | Enable exists check endpoint |
83+
84+
## 🎨 Customizing Methods
85+
86+
Need custom logic? Simply override any method:
87+
88+
```java
89+
@Component
90+
@Crud(entity = Product.class, endpoint = "/api/products")
91+
public class ProductController {
92+
93+
private final ProductRepository productRepository;
94+
95+
public ProductController(ProductRepository productRepository) {
96+
this.productRepository = productRepository;
97+
}
98+
99+
// Override findAll to show only in-stock products
100+
@CrudOverride(value = "Returns only in-stock products")
101+
public Response findAll(Request request) {
102+
var inStockProducts = productRepository.findAll().stream()
103+
.filter(product -> product.getStock() > 0)
104+
.toList();
105+
return Response.json(ApiResponse.success("In-stock products retrieved", inStockProducts));
106+
}
107+
108+
// All other methods (findById, create, update, delete) remain auto-generated
109+
}
110+
```
111+
112+
### Manual Route Registration for Overrides
113+
114+
When you override a method, register its route manually:
115+
116+
```java
117+
public static void main(String[] args) {
118+
Router router = new Router();
119+
120+
// Manual registration for overridden methods
121+
router.GET("/api/products", "findAll", ProductController.class);
122+
123+
Server server = new Server(router, config);
124+
server.start(8080);
125+
}
126+
```
127+
128+
## 🧪 Testing Your API
129+
130+
### Create Product
131+
```bash
132+
curl -X POST -H "Content-Type: application/json" \
133+
-d '{"name":"Gaming Laptop","description":"High-performance laptop","price":1299.99,"stock":5}' \
134+
"http://localhost:8080/api/products"
135+
```
136+
137+
### Get All Products
138+
```bash
139+
curl "http://localhost:8080/api/products"
140+
```
141+
142+
### Get Product by ID
143+
```bash
144+
curl "http://localhost:8080/api/products/1"
145+
```
146+
147+
### Search Products
148+
```bash
149+
curl "http://localhost:8080/api/products/search?name=laptop"
150+
```
151+
152+
### Update Product
153+
```bash
154+
curl -X PUT -H "Content-Type: application/json" \
155+
-d '{"name":"Updated Laptop","description":"Updated description","price":1199.99,"stock":3}' \
156+
"http://localhost:8080/api/products/1"
157+
```
158+
159+
### Delete Product
160+
```bash
161+
curl -X DELETE "http://localhost:8080/api/products/1"
162+
```
163+
164+
## 📊 Response Format
165+
166+
All CRUD endpoints return standardized responses:
167+
168+
```json
169+
{
170+
"success": true,
171+
"message": "Product retrieved successfully",
172+
"data": {
173+
"id": 1,
174+
"name": "Gaming Laptop",
175+
"description": "High-performance laptop",
176+
"price": 1299.99,
177+
"stock": 5
178+
},
179+
"timestamp": "2023-12-07T10:30:00"
180+
}
181+
```
182+
183+
## 🔧 Requirements
184+
185+
Before using `@Crud`, ensure you have:
186+
187+
1. **Entity Class**: JPA annotated entity
188+
2. **Repository**: Interface extending `BaseRepository<Entity, ID>`
189+
3. **Component Registration**: Repository marked with `@Component`
190+
4. **Database Configuration**: Hibernate/JPA properly configured
191+
192+
## 💡 Best Practices
193+
194+
### ✅ Do's
195+
- Use meaningful entity and endpoint names
196+
- Enable search for user-facing entities
197+
- Override methods only when you need custom logic
198+
- Use `@CrudOverride` for documentation
199+
200+
### ❌ Don'ts
201+
- Don't forget manual route registration for overrides
202+
- Don't enable unnecessary features (keeps API clean)
203+
- Don't override methods unless you need custom behavior
204+
205+
## 🔍 Complete Example
206+
207+
Check out the [Simple CRUD Example](https://github.com/your-repo/jazzy-framework/tree/main/src/main/java/examples/simple_crud) for a working implementation with:
208+
209+
- ✅ Product entity with validation
210+
- ✅ Repository implementation
211+
- ✅ Controller with one custom method
212+
- ✅ Complete test commands
213+
- ✅ Real-world usage patterns
214+
215+
---
216+
217+
**🎯 Result**: With just a few annotations, you get a complete REST API with all CRUD operations, search, pagination, and standardized responses!

docs-site/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const sidebars = {
2020
// Getting Started
2121
'index',
2222
'getting-started',
23+
'crud', // ⚡ CRUD Operations - Quick Start Feature
2324

2425
// Core Concepts
2526
{

0 commit comments

Comments
 (0)