189 lines
6.0 KiB
TypeScript

// src/controllers/DicomStudyController.ts
import { Request, Response, NextFunction } from 'express';
import { DicomStudyService, CommonService} from '../services';
import logger from '../utils/logger';
import { Pool } from 'mysql2/promise';
export class DicomStudyController {
private service: DicomStudyService;
private commonService: CommonService;
constructor(pool:Pool) {
this.service = new DicomStudyService(pool);
this.commonService = new CommonService();
}
getAllStudies = async (req: Request, res: Response, next: NextFunction) => {
try {
const studies = await this.service.getAllStudies();
res.json(studies);
} catch (error) {
const apiError = this.commonService.handleError(error, 'Failed to fetch studies');
res.status(500).json({ error: apiError });
}
};
getStudyById = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id);
if (isNaN(id)) {
return res.status(400).json({ error: 'Invalid study ID' });
}
const study = await this.service.getStudyById(id);
if (!study) {
return res.status(404).json({ error: 'Study not found' });
}
res.json(study);
} catch (error) {
const apiError = this.commonService.handleError(error, `Failed to fetch study ${req.params.id}`);
res.status(500).json({ error: apiError });
}
};
getStudiesByRouterId = async (req: Request, res: Response, next: NextFunction) => {
try {
const routerId = req.params.routerId;
if (!routerId || typeof routerId !== 'string') {
return res.status(400).json({ error: 'Invalid router ID' });
}
const studies = await this.service.getStudiesByRouterId(routerId);
res.json(studies);
} catch (error) {
const apiError = this.commonService.handleError(error, `Failed to fetch studies for router ${req.params.routerId}`);
// If router not found, return 404
if (error instanceof Error && error.message.includes('Invalid router_id')) {
return res.status(404).json({ error: 'Router not found' });
}
res.status(500).json({ error: apiError });
}
};
createStudy = async (req: Request, res: Response, next: NextFunction) => {
try {
const requiredFields = [
'router_id',
'study_instance_uid',
'patient_id',
'patient_name',
'accession_number',
'study_date',
'modality',
'series_instance_uid',
'study_status_code',
'association_id'
];
const missingFields = requiredFields.filter(field => !req.body[field]);
if (missingFields.length > 0) {
return res.status(400).json({
error: 'Missing required fields',
missingFields
});
}
// Validate router_id format if needed
if (typeof req.body.router_id !== 'string') {
return res.status(400).json({
error: 'Invalid router_id format',
details: 'router_id must be a string'
});
}
const study = await this.service.createStudy(req.body);
res.status(201).json(study);
} catch (error) {
const apiError = this.commonService.handleError(error, 'Failed to create study');
// Handle specific error cases
if (error instanceof Error) {
if (error.message.includes('Missing required field')) {
return res.status(400).json({ error: apiError });
}
if (error.message.includes('Invalid router_id')) {
return res.status(404).json({ error: 'Router not found' });
}
if (error.message.includes('Invalid study status code')) {
return res.status(400).json({ error: apiError });
}
if (error.message.includes('Invalid study date format')) {
return res.status(400).json({ error: apiError });
}
}
res.status(500).json({ error: apiError });
}
};
updateStudy = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id);
if (isNaN(id)) {
return res.status(400).json({ error: 'Invalid study ID' });
}
const study = await this.service.updateStudy(id, req.body);
if (!study) {
return res.status(404).json({ error: 'Study not found' });
}
res.json(study);
} catch (error) {
const apiError = this.commonService.handleError(error, `Failed to update study ${req.params.id}`);
res.status(500).json({ error: apiError });
}
};
deleteStudy = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id);
if (isNaN(id)) {
return res.status(400).json({ error: 'Invalid study ID' });
}
const success = await this.service.deleteStudy(id);
if (!success) {
return res.status(404).json({ error: 'Study not found' });
}
res.status(204).send();
} catch (error) {
const apiError = this.commonService.handleError(error, `Failed to delete study ${req.params.id}`);
res.status(500).json({ error: apiError });
}
};
searchStudies = async (req: Request, res: Response, next: NextFunction) => {
try {
const { startDate, endDate, modality, patientName } = req.query;
if (startDate && typeof startDate !== 'string') {
return res.status(400).json({
error: 'Invalid startDate format',
expected: 'YYYY-MM-DD'
});
}
if (endDate && typeof endDate !== 'string') {
return res.status(400).json({
error: 'Invalid endDate format',
expected: 'YYYY-MM-DD'
});
}
const studies = await this.service.searchStudies({
startDate: startDate as string,
endDate: endDate as string,
modality: modality as string,
patientName: patientName as string
});
res.json(studies);
} catch (error) {
const apiError = this.commonService.handleError(error, 'Failed to search studies');
res.status(500).json({ error: apiError });
}
};
}