import { Router, Request, Response } from 'express';
import { CitiesRepository } from '../elasticsearch/cities-repository.js';
import { CreateCityRequest, UpdateCityRequest } from '../types/city.js';
import { CityResponse, CitiesResponse, DeleteResponse } from '../types/api-responses.js';
const router = Router();
const citiesRepository = new CitiesRepository();
/**
* GET /api/cities
* Get all cities
*/
router.get('/cities', async (req: Request, res: Response<CitiesResponse>) => {
try {
const cities = await citiesRepository.getAllCities();
res.json({
success: true,
data: cities,
});
} catch (error: any) {
console.error('Error fetching cities:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch cities',
});
}
});
/**
* GET /api/cities/search/:name
* Search cities by name
*/
router.get('/cities/search/:name', async (req: Request, res: Response<CitiesResponse>) => {
try {
const { name } = req.params;
const cities = await citiesRepository.searchCitiesByName(name);
res.json({
success: true,
data: cities,
});
} catch (error: any) {
console.error('Error searching cities:', error);
res.status(500).json({
success: false,
error: 'Failed to search cities',
});
}
});
/**
* GET /api/cities/:id
* Get a city by ID
*/
router.get('/cities/:id', async (req: Request, res: Response<CityResponse>) => {
try {
const { id } = req.params;
const city = await citiesRepository.getCityById(id);
if (!city) {
res.status(404).json({
success: false,
error: 'City not found',
});
return;
}
res.json({
success: true,
data: city,
});
} catch (error: any) {
console.error('Error fetching city:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch city',
});
}
});
/**
* POST /api/cities
* Create a new city
*/
router.post('/cities', async (req: Request<{}, {}, CreateCityRequest>, res: Response<CityResponse>) => {
try {
const { nome } = req.body;
if (!nome || typeof nome !== 'string' || nome.trim() === '') {
res.status(400).json({
success: false,
error: 'City name is required and must be a non-empty string',
});
return;
}
const city = await citiesRepository.createCity({ nome: nome.trim() });
res.status(201).json({
success: true,
data: city,
message: 'City created successfully',
});
} catch (error: any) {
console.error('Error creating city:', error);
res.status(500).json({
success: false,
error: 'Failed to create city',
});
}
});
/**
* PUT /api/cities/:id
* Update a city
*/
router.put('/cities/:id', async (req: Request<{ id: string }, {}, UpdateCityRequest>, res: Response<CityResponse>) => {
try {
const { id } = req.params;
const { nome } = req.body;
if (!nome || typeof nome !== 'string' || nome.trim() === '') {
res.status(400).json({
success: false,
error: 'City name is required and must be a non-empty string',
});
return;
}
const city = await citiesRepository.updateCity(id, { nome: nome.trim() });
if (!city) {
res.status(404).json({
success: false,
error: 'City not found',
});
return;
}
res.json({
success: true,
data: city,
message: 'City updated successfully',
});
} catch (error: any) {
console.error('Error updating city:', error);
res.status(500).json({
success: false,
error: 'Failed to update city',
});
}
});
/**
* DELETE /api/cities/:id
* Delete a city
*/
router.delete('/cities/:id', async (req: Request, res: Response<DeleteResponse>) => {
try {
const { id } = req.params;
const deleted = await citiesRepository.deleteCity(id);
if (!deleted) {
res.status(404).json({
success: false,
message: 'City not found',
});
return;
}
res.json({
success: true,
message: 'City deleted successfully',
});
} catch (error: any) {
console.error('Error deleting city:', error);
res.status(500).json({
success: false,
message: 'Failed to delete city',
});
}
});
export default router;