Complete CRUD operations with validation:
- ✅
getAllDepartments()- List all departments - ✅
showCreateForm()- Display create form - ✅
createDepartment()- Create new department with validation - ✅
showEditForm()- Display edit form - ✅
updateDepartment()- Update existing department - ✅
deleteDepartment()- Delete department (with employee check) - ✅
getDepartmentAPI()- JSON API endpoint
Features:
- Input validation (name, code length checks)
- Duplicate detection (name and code must be unique)
- Employee count check before deletion
- Error handling and user feedback
Advanced CRUD with pagination, search, and filters:
- ✅
getAllEmployees()- List with pagination, search, filters - ✅
showCreateForm()- Display create form with dropdowns - ✅
createEmployee()- Create new employee with validation - ✅
getEmployeeDetails()- Show detailed employee view - ✅
showEditForm()- Display edit form - ✅
updateEmployee()- Update existing employee - ✅
deleteEmployee()- Delete employee (with supervisor check) - ✅
getEmployeesByDepartmentAPI()- JSON API endpoint
Features:
- Pagination: Configurable page size and navigation
- Search: By first name, last name, or email (case-insensitive)
- Filters: By department or job title
- Relationships: Department and supervisor population
- Validation: Email format, required fields, phone format
- Business Logic: Cannot delete supervisor with subordinates
- Self-reference Prevention: Employee cannot supervise themselves
RESTful API endpoints:
GET /departments - List all
GET /departments/new - Show create form
POST /departments - Create new
GET /departments/:id/edit - Show edit form
PUT /departments/:id - Update
DELETE /departments/:id - Delete
GET /departments/:id/api - Get JSON data
RESTful API endpoints with query parameters:
GET /employees - List with pagination/search/filter
GET /employees/new - Show create form
POST /employees - Create new
GET /employees/api/by-department/:id - Get by department (JSON)
GET /employees/:id - Show details
GET /employees/:id/edit - Show edit form
PUT /employees/:id - Update
DELETE /employees/:id - Delete
Query Parameters:
page- Page numberlimit- Items per pagesearch- Search termdepartment- Department filterjobTitle- Job title filter
External API integration for location data:
GET /api/countries - Get all countries
GET /api/states/:country - Get states by country
GET /api/cities/:country/:state - Get cities by country and state
Integration: CountriesNow API via apiHelper.js
- Separation of Concerns: Business logic separated from routes
- Error Handling: Try-catch blocks with user-friendly messages
- Validation: Server-side validation before database operations
- Mongoose Operations: Using async/await with proper error handling
- Population: Related data (department, supervisor) automatically loaded
- Virtual Fields: Full name computed, subordinates relationship
- RESTful Design: Following REST principles
- Method Override: Support for PUT/DELETE via POST
- API Endpoints: Separate JSON endpoints for AJAX calls
- Query Parameters: Support for pagination, search, filters
- CRUD: Full Create, Read, Update, Delete operations
- Queries: Complex filtering with $or, $regex operators
- Pagination: Skip and limit for efficient data loading
- Sorting: Ordered results (by name, date, etc.)
- Counting: Total counts for pagination
- References: ObjectId references with population
- Models ↔ Controllers: Controllers use Mongoose models
- Controllers ↔ Routes: Routes call controller functions
- Routes ↔ App.js: Main app imports and uses all routes
- External API: apiHelper abstracts external API calls
- Views: Controllers render EJS templates (Phase 4)
✅ Server Running: http://localhost:3000 ✅ MongoDB Connected: 127.0.0.1:27017 ✅ Sample Data: 5 departments, 6 employees ✅ Routes Registered: All endpoints active
Next: Build frontend views to interact with these endpoints
Complete API documentation available in: API_DOCUMENTATION.md
Includes:
- All endpoint descriptions
- Request/response examples
- Query parameter details
- cURL examples
- Method override instructions
Department Management:
- ✅ Full CRUD operations
- ✅ Duplicate prevention
- ✅ Employee count validation
- ✅ JSON API endpoint
Employee Management:
- ✅ Full CRUD operations
- ✅ Server-side pagination
- ✅ Multi-field search
- ✅ Department and job title filters
- ✅ Supervisor relationships (self-referencing)
- ✅ Location integration ready
- ✅ Subordinate management
- ✅ JSON API endpoints
External API:
- ✅ Country list
- ✅ State list by country
- ✅ City list by country and state
- ✅ Error handling
- ✅ Ready for frontend integration
Next Phase: Frontend Development
- Create EJS views for departments
- Create EJS views for employees
- Implement dynamic location dropdowns
- Add pagination UI
- Add search and filter forms
- Style with Tailwind CSS
Server is running at: http://localhost:3000 MongoDB is connected and has sample data All backend routes are functional and ready for views