배송/화물현황과 리스크/알림(api 활용, 공공데이터 복구시 대체될 가능성 있음)
This commit is contained in:
124
backend-node/src/controllers/riskAlertController.ts
Normal file
124
backend-node/src/controllers/riskAlertController.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* 리스크/알림 컨트롤러
|
||||
*/
|
||||
|
||||
import { Request, Response } from 'express';
|
||||
import { RiskAlertService } from '../services/riskAlertService';
|
||||
import { RiskAlertCacheService } from '../services/riskAlertCacheService';
|
||||
|
||||
const riskAlertService = new RiskAlertService();
|
||||
const cacheService = RiskAlertCacheService.getInstance();
|
||||
|
||||
export class RiskAlertController {
|
||||
/**
|
||||
* 전체 알림 조회 (캐시된 데이터 - 빠름!)
|
||||
* GET /api/risk-alerts
|
||||
*/
|
||||
async getAllAlerts(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { alerts, lastUpdated } = cacheService.getCachedAlerts();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: alerts,
|
||||
count: alerts.length,
|
||||
lastUpdated: lastUpdated,
|
||||
cached: true,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('❌ 전체 알림 조회 오류:', error.message);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 조회 중 오류가 발생했습니다.',
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 전체 알림 강제 갱신 (실시간 조회)
|
||||
* POST /api/risk-alerts/refresh
|
||||
*/
|
||||
async refreshAlerts(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const alerts = await cacheService.forceRefresh();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: alerts,
|
||||
count: alerts.length,
|
||||
message: '알림이 갱신되었습니다.',
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('❌ 알림 갱신 오류:', error.message);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 갱신 중 오류가 발생했습니다.',
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 날씨 특보 조회
|
||||
* GET /api/risk-alerts/weather
|
||||
*/
|
||||
async getWeatherAlerts(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const alerts = await riskAlertService.getWeatherAlerts();
|
||||
|
||||
// 프론트엔드 직접 호출용: alerts 배열만 반환
|
||||
res.json(alerts);
|
||||
} catch (error: any) {
|
||||
console.error('❌ 날씨 특보 조회 오류:', error.message);
|
||||
res.status(500).json([]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 교통사고 조회
|
||||
* GET /api/risk-alerts/accidents
|
||||
*/
|
||||
async getAccidentAlerts(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const alerts = await riskAlertService.getAccidentAlerts();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: alerts,
|
||||
count: alerts.length,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('❌ 교통사고 조회 오류:', error.message);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '교통사고 조회 중 오류가 발생했습니다.',
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 도로공사 조회
|
||||
* GET /api/risk-alerts/roadworks
|
||||
*/
|
||||
async getRoadworkAlerts(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const alerts = await riskAlertService.getRoadworkAlerts();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: alerts,
|
||||
count: alerts.length,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('❌ 도로공사 조회 오류:', error.message);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '도로공사 조회 중 오류가 발생했습니다.',
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user