# REST API Conversion Analysis & Estimates

## Overview
This document outlines the conversion of non-RESTful endpoints to proper REST API standards, including time estimates for each endpoint.

## Current vs. Proposed REST Endpoint Mapping

### 1. `find/{collection}` → `GET /api/{collection}`
**Current:** `GET /api/find/{collection}`  
**Proposed:** `GET /api/{collection}`  
**Complexity:** Low  
**Changes Required:**
- **Backend:** Update route definition, controller method remains similar
- **Frontend:** Update `OnlineCrudService.find()` method to use new endpoint
- **Testing:** Update all service tests

**Estimate:** 2-3 hours
- Backend route update: 30 minutes
- Frontend service update: 30 minutes
- Testing & validation: 1-2 hours

---

### 2. `find-by-id/{collection}/{id}` → `GET /api/{collection}/{id}`
**Current:** `GET /api/find-by-id/{collection}/{id}`  
**Proposed:** `GET /api/{collection}/{id}`  
**Complexity:** Low  
**Changes Required:**
- **Backend:** Update route definition, controller method remains similar
- **Frontend:** Update `OnlineCrudService.findById()` method
- **Testing:** Update all service tests

**Estimate:** 2-3 hours
- Backend route update: 30 minutes
- Frontend service update: 30 minutes
- Testing & validation: 1-2 hours

---

### 3. `find-by-user-id/{collection}/{userId}` → `GET /api/{collection}?user_id={userId}`
**Current:** `GET /api/find-by-user-id/{collection}/{userId}`  
**Proposed:** `GET /api/{collection}?user_id={userId}` (Query parameter approach)  
**Alternative:** `GET /api/{collection}/user/{userId}` (if user_id is a resource identifier)  
**Complexity:** Medium  
**Changes Required:**
- **Backend:** Update route to accept query parameters, modify controller to filter by user_id
- **Frontend:** Update service methods that use this endpoint (e.g., `UserDetailsService.findByUserId()`)
- **Testing:** Test query parameter filtering

**Estimate:** 3-4 hours
- Backend route & controller update: 1 hour
- Frontend service update: 1 hour
- Testing & validation: 1-2 hours

---

### 4. `find-by-value/{collection}/{name}/{value}` → `GET /api/{collection}?{name}={value}`
**Current:** `GET /api/find-by-value/{collection}/{name}/{value}`  
**Proposed:** `GET /api/{collection}?{name}={value}`  
**Complexity:** Medium  
**Changes Required:**
- **Backend:** Update route to accept query parameters, modify controller to handle dynamic field filtering
- **Frontend:** Update `OnlineCrudService.findByValue()` method to build query string
- **Testing:** Test various field/value combinations

**Estimate:** 4-5 hours
- Backend route & controller update: 1.5 hours
- Frontend service update: 1 hour
- Testing & validation: 1.5-2 hours

---

### 5. `find-by-value-set/{collection}` → `POST /api/{collection}/search` or `GET /api/{collection}?{name}[]={value1}&{name}[]={value2}`
**Current:** `POST /api/find-by-value-set/{collection}` (with body containing field name and array of values)  
**Proposed:** 
- Option A: `POST /api/{collection}/search` (with search criteria in body) - **Recommended**
- Option B: `GET /api/{collection}?{name}[]={value1}&{name}[]={value2}` (query string with array)

**Complexity:** Medium-High  
**Changes Required:**
- **Backend:** Create new search endpoint, handle array filtering in controller
- **Frontend:** Update `OnlineCrudService.findByValue()` method when value is List
- **Testing:** Test array filtering with various collections

**Estimate:** 5-6 hours
- Backend route & controller update: 2 hours
- Frontend service update: 1 hour
- Testing & validation: 2-3 hours

---

### 6. `put/{collection}` → `PUT /api/{collection}/{id}`
**Current:** `PUT /api/put/{collection}` (with id in body)  
**Proposed:** `PUT /api/{collection}/{id}` (id in URL path)  
**Complexity:** Medium  
**Changes Required:**
- **Backend:** Update route to include {id} parameter, extract id from URL instead of body
- **Frontend:** Update `OnlineCrudService.save()` method to include id in URL when updating
- **Testing:** Test update operations

**Estimate:** 3-4 hours
- Backend route & controller update: 1 hour
- Frontend service update: 1 hour
- Testing & validation: 1-2 hours

---

### 7. `delete/{collection}/{id}` → `DELETE /api/{collection}/{id}`
**Current:** `DELETE /api/delete/{collection}/{id}`  
**Proposed:** `DELETE /api/{collection}/{id}`  
**Complexity:** Low  
**Changes Required:**
- **Backend:** Update route definition, controller method remains similar
- **Frontend:** Update `OnlineCrudService.delete()` method
- **Testing:** Update all delete operations

**Estimate:** 2-3 hours
- Backend route update: 30 minutes
- Frontend service update: 30 minutes
- Testing & validation: 1-2 hours

---

### 8. `delete-link-table/{collection}` → `DELETE /api/{collection}/links` or `POST /api/{collection}/links/delete`
**Current:** `POST /api/delete-link-table/{collection}` (with link data in body)  
**Proposed:** 
- Option A: `DELETE /api/{collection}/links` (with link data in body) - **Recommended**
- Option B: `POST /api/{collection}/links/delete` (if DELETE with body is not supported)

**Complexity:** Medium  
**Changes Required:**
- **Backend:** Update route, modify controller to handle link deletion
- **Frontend:** Update `OnlineCrudService.deleteFromLinkTable()` method
- **Testing:** Test link table deletion operations

**Estimate:** 3-4 hours
- Backend route & controller update: 1 hour
- Frontend service update: 1 hour
- Testing & validation: 1-2 hours

---

### 9. `post-link-table/{collection}` → `POST /api/{collection}/links`
**Current:** `POST /api/post-link-table/{collection}`  
**Proposed:** `POST /api/{collection}/links`  
**Complexity:** Low-Medium  
**Changes Required:**
- **Backend:** Update route definition, controller method remains similar
- **Frontend:** Update `OnlineCrudService.saveToLinkTable()` method
- **Testing:** Test link table creation operations

**Estimate:** 2-3 hours
- Backend route update: 30 minutes
- Frontend service update: 30 minutes
- Testing & validation: 1-2 hours

---

### 10. `post/{collection}` → `POST /api/{collection}`
**Current:** `POST /api/post/{collection}`  
**Proposed:** `POST /api/{collection}`  
**Complexity:** Low  
**Changes Required:**
- **Backend:** Update route definition, controller method remains similar
- **Frontend:** Update `OnlineCrudService.save()` method (create operation)
- **Testing:** Test create operations

**Estimate:** 2-3 hours
- Backend route update: 30 minutes
- Frontend service update: 30 minutes
- Testing & validation: 1-2 hours

---

### 11. `uploadFile/{key}` → `POST /api/files/upload` or `POST /api/{collection}/files`
**Current:** `POST /api/uploadFile/{key}` (multipart form data)  
**Proposed:** 
- Option A: `POST /api/files/upload` (generic file upload endpoint) - **Recommended**
- Option B: `POST /api/{collection}/files` (collection-specific uploads)

**Complexity:** Medium  
**Changes Required:**
- **Backend:** Update route, ensure file handling logic remains intact
- **Frontend:** Update `HttpService.postFile()` method
- **Testing:** Test file uploads for all file types

**Estimate:** 3-4 hours
- Backend route update: 1 hour
- Frontend service update: 1 hour
- Testing & validation: 1-2 hours

---

## Summary

### Total Estimated Time: **31-40 hours**

### Breakdown by Complexity:
- **Low Complexity (5 endpoints):** 10-15 hours
- **Medium Complexity (5 endpoints):** 18-23 hours
- **Medium-High Complexity (1 endpoint):** 5-6 hours

### Implementation Phases:

**Phase 1: Simple CRUD Operations (8-10 hours)**
- `find/{collection}` → `GET /api/{collection}`
- `find-by-id/{collection}/{id}` → `GET /api/{collection}/{id}`
- `delete/{collection}/{id}` → `DELETE /api/{collection}/{id}`
- `post/{collection}` → `POST /api/{collection}`
- `post-link-table/{collection}` → `POST /api/{collection}/links`

**Phase 2: Update & Query Operations (10-12 hours)**
- `put/{collection}` → `PUT /api/{collection}/{id}`
- `find-by-user-id/{collection}/{userId}` → `GET /api/{collection}?user_id={userId}`
- `find-by-value/{collection}/{name}/{value}` → `GET /api/{collection}?{name}={value}`
- `delete-link-table/{collection}` → `DELETE /api/{collection}/links`

**Phase 3: Advanced Operations (8-10 hours)**
- `find-by-value-set/{collection}` → `POST /api/{collection}/search`
- `uploadFile/{key}` → `POST /api/files/upload`

### Additional Considerations:

1. **Backward Compatibility:** Consider maintaining old endpoints during transition period (add deprecation warnings)
2. **API Versioning:** May want to implement `/api/v1/{collection}` for future-proofing
3. **Error Handling:** Ensure consistent error response format across all endpoints
4. **Rate Limiting:** Verify rate limiting works with new endpoint structure
5. **Documentation:** Update API documentation for all endpoints
6. **Testing:** Comprehensive testing required for all collections using these endpoints

### Files to Modify:

**Backend (PHP/CodeIgniter):**
- Route configuration files
- Controller files handling these endpoints
- Any middleware/validation logic

**Frontend (Flutter/Dart):**
- `lib/services/http_service.dart` - Base HTTP methods
- `lib/services/online_crud_service.dart` - Main CRUD service
- All service files extending `OnlineCrudService` or `LocalCrudService`
- `lib/services/file_service.dart` - File upload service

### Risk Factors:
- Breaking changes for existing clients (if any)
- Need to coordinate frontend and backend deployments
- Testing across all collections (could be 50+ collections)
- Potential issues with query parameter encoding for special characters

