Modernization Project Complete. Updated to latest versions of frameworks.
This commit is contained in:
@@ -1,186 +1,219 @@
|
||||
/**
|
||||
* @ai-summary HTTP request handlers for fuel logs
|
||||
* @ai-summary Fastify route handlers for fuel logs API
|
||||
* @ai-context HTTP request/response handling with Fastify reply methods
|
||||
*/
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { FuelLogsService } from '../domain/fuel-logs.service';
|
||||
import { validateCreateFuelLog, validateUpdateFuelLog } from './fuel-logs.validators';
|
||||
import { FuelLogsRepository } from '../data/fuel-logs.repository';
|
||||
import { pool } from '../../../core/config/database';
|
||||
import { logger } from '../../../core/logging/logger';
|
||||
import { CreateFuelLogBody, UpdateFuelLogBody, FuelLogParams, VehicleParams } from '../domain/fuel-logs.types';
|
||||
|
||||
export class FuelLogsController {
|
||||
constructor(private service: FuelLogsService) {}
|
||||
private fuelLogsService: FuelLogsService;
|
||||
|
||||
constructor() {
|
||||
const repository = new FuelLogsRepository(pool);
|
||||
this.fuelLogsService = new FuelLogsService(repository);
|
||||
}
|
||||
|
||||
create = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async createFuelLog(request: FastifyRequest<{ Body: CreateFuelLogBody }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const userId = (request as any).user.sub;
|
||||
const fuelLog = await this.fuelLogsService.createFuelLog(request.body, userId);
|
||||
|
||||
const validation = validateCreateFuelLog(req.body);
|
||||
if (!validation.success) {
|
||||
return res.status(400).json({
|
||||
error: 'Validation failed',
|
||||
details: validation.error.errors
|
||||
return reply.code(201).send(fuelLog);
|
||||
} catch (error: any) {
|
||||
logger.error('Error creating fuel log', { error, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
if (error.message.includes('Unauthorized')) {
|
||||
return reply.code(403).send({
|
||||
error: 'Forbidden',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.service.createFuelLog(validation.data, userId);
|
||||
res.status(201).json(result);
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to create fuel log'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getFuelLogsByVehicle(request: FastifyRequest<{ Params: VehicleParams }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = (request as any).user.sub;
|
||||
const { vehicleId } = request.params;
|
||||
|
||||
const fuelLogs = await this.fuelLogsService.getFuelLogsByVehicle(vehicleId, userId);
|
||||
|
||||
return reply.code(200).send(fuelLogs);
|
||||
} catch (error: any) {
|
||||
logger.error('Error creating fuel log', { error: error.message });
|
||||
logger.error('Error listing fuel logs', { error, vehicleId: request.params.vehicleId, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return res.status(404).json({ error: error.message });
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
if (error.message.includes('Unauthorized')) {
|
||||
return res.status(403).json({ error: error.message });
|
||||
return reply.code(403).send({
|
||||
error: 'Forbidden',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
|
||||
return next(error);
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get fuel logs'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
listByVehicle = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async getUserFuelLogs(request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const userId = (request as any).user.sub;
|
||||
const fuelLogs = await this.fuelLogsService.getUserFuelLogs(userId);
|
||||
|
||||
const { vehicleId } = req.params;
|
||||
const result = await this.service.getFuelLogsByVehicle(vehicleId, userId);
|
||||
res.json(result);
|
||||
return reply.code(200).send(fuelLogs);
|
||||
} catch (error: any) {
|
||||
logger.error('Error listing fuel logs', { error: error.message });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return res.status(404).json({ error: error.message });
|
||||
}
|
||||
if (error.message.includes('Unauthorized')) {
|
||||
return res.status(403).json({ error: error.message });
|
||||
}
|
||||
|
||||
return next(error);
|
||||
logger.error('Error listing all fuel logs', { error, userId: (request as any).user?.sub });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get fuel logs'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
listAll = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async getFuelLog(request: FastifyRequest<{ Params: FuelLogParams }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const userId = (request as any).user.sub;
|
||||
const { id } = request.params;
|
||||
|
||||
const result = await this.service.getUserFuelLogs(userId);
|
||||
res.json(result);
|
||||
} catch (error: any) {
|
||||
logger.error('Error listing all fuel logs', { error: error.message });
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
|
||||
get = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const fuelLog = await this.fuelLogsService.getFuelLog(id, userId);
|
||||
|
||||
const { id } = req.params;
|
||||
const result = await this.service.getFuelLog(id, userId);
|
||||
res.json(result);
|
||||
return reply.code(200).send(fuelLog);
|
||||
} catch (error: any) {
|
||||
logger.error('Error getting fuel log', { error: error.message });
|
||||
logger.error('Error getting fuel log', { error, fuelLogId: request.params.id, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message === 'Fuel log not found') {
|
||||
return res.status(404).json({ error: error.message });
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
if (error.message === 'Unauthorized') {
|
||||
return res.status(403).json({ error: error.message });
|
||||
}
|
||||
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
|
||||
update = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const { id } = req.params;
|
||||
const validation = validateUpdateFuelLog(req.body);
|
||||
if (!validation.success) {
|
||||
return res.status(400).json({
|
||||
error: 'Validation failed',
|
||||
details: validation.error.errors
|
||||
return reply.code(403).send({
|
||||
error: 'Forbidden',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.service.updateFuelLog(id, validation.data, userId);
|
||||
res.json(result);
|
||||
} catch (error: any) {
|
||||
logger.error('Error updating fuel log', { error: error.message });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return res.status(404).json({ error: error.message });
|
||||
}
|
||||
if (error.message === 'Unauthorized') {
|
||||
return res.status(403).json({ error: error.message });
|
||||
}
|
||||
|
||||
return next(error);
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get fuel log'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
delete = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async updateFuelLog(request: FastifyRequest<{ Params: FuelLogParams; Body: UpdateFuelLogBody }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const userId = (request as any).user.sub;
|
||||
const { id } = request.params;
|
||||
|
||||
const { id } = req.params;
|
||||
await this.service.deleteFuelLog(id, userId);
|
||||
res.status(204).send();
|
||||
const fuelLog = await this.fuelLogsService.updateFuelLog(id, request.body, userId);
|
||||
|
||||
return reply.code(200).send(fuelLog);
|
||||
} catch (error: any) {
|
||||
logger.error('Error deleting fuel log', { error: error.message });
|
||||
logger.error('Error updating fuel log', { error, fuelLogId: request.params.id, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return res.status(404).json({ error: error.message });
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
if (error.message === 'Unauthorized') {
|
||||
return res.status(403).json({ error: error.message });
|
||||
return reply.code(403).send({
|
||||
error: 'Forbidden',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
|
||||
return next(error);
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to update fuel log'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getStats = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async deleteFuelLog(request: FastifyRequest<{ Params: FuelLogParams }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const userId = (request as any).user.sub;
|
||||
const { id } = request.params;
|
||||
|
||||
const { vehicleId } = req.params;
|
||||
const result = await this.service.getVehicleStats(vehicleId, userId);
|
||||
res.json(result);
|
||||
await this.fuelLogsService.deleteFuelLog(id, userId);
|
||||
|
||||
return reply.code(204).send();
|
||||
} catch (error: any) {
|
||||
logger.error('Error getting fuel stats', { error: error.message });
|
||||
logger.error('Error deleting fuel log', { error, fuelLogId: request.params.id, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return res.status(404).json({ error: error.message });
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
if (error.message === 'Unauthorized') {
|
||||
return res.status(403).json({ error: error.message });
|
||||
return reply.code(403).send({
|
||||
error: 'Forbidden',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
|
||||
return next(error);
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to delete fuel log'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getFuelStats(request: FastifyRequest<{ Params: VehicleParams }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = (request as any).user.sub;
|
||||
const { vehicleId } = request.params;
|
||||
|
||||
const stats = await this.fuelLogsService.getVehicleStats(vehicleId, userId);
|
||||
|
||||
return reply.code(200).send(stats);
|
||||
} catch (error: any) {
|
||||
logger.error('Error getting fuel stats', { error, vehicleId: request.params.vehicleId, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
if (error.message === 'Unauthorized') {
|
||||
return reply.code(403).send({
|
||||
error: 'Forbidden',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get fuel stats'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,68 @@
|
||||
/**
|
||||
* @ai-summary Route definitions for fuel logs API
|
||||
* @ai-summary Fastify routes for fuel logs API
|
||||
* @ai-context Route definitions with Fastify plugin pattern and authentication
|
||||
*/
|
||||
|
||||
import { Router } from 'express';
|
||||
import { FastifyInstance, FastifyPluginOptions } from 'fastify';
|
||||
import { FastifyPluginAsync } from 'fastify';
|
||||
import {
|
||||
CreateFuelLogBody,
|
||||
UpdateFuelLogBody,
|
||||
FuelLogParams,
|
||||
VehicleParams
|
||||
} from '../domain/fuel-logs.types';
|
||||
import { FuelLogsController } from './fuel-logs.controller';
|
||||
import { FuelLogsService } from '../domain/fuel-logs.service';
|
||||
import { FuelLogsRepository } from '../data/fuel-logs.repository';
|
||||
import { authMiddleware } from '../../../core/security/auth.middleware';
|
||||
import pool from '../../../core/config/database';
|
||||
|
||||
export function registerFuelLogsRoutes(): Router {
|
||||
const router = Router();
|
||||
|
||||
// Initialize layers
|
||||
const repository = new FuelLogsRepository(pool);
|
||||
const service = new FuelLogsService(repository);
|
||||
const controller = new FuelLogsController(service);
|
||||
|
||||
// Define routes
|
||||
router.get('/api/fuel-logs', authMiddleware, controller.listAll);
|
||||
router.get('/api/fuel-logs/:id', authMiddleware, controller.get);
|
||||
router.post('/api/fuel-logs', authMiddleware, controller.create);
|
||||
router.put('/api/fuel-logs/:id', authMiddleware, controller.update);
|
||||
router.delete('/api/fuel-logs/:id', authMiddleware, controller.delete);
|
||||
|
||||
// Vehicle-specific routes
|
||||
router.get('/api/vehicles/:vehicleId/fuel-logs', authMiddleware, controller.listByVehicle);
|
||||
router.get('/api/vehicles/:vehicleId/fuel-stats', authMiddleware, controller.getStats);
|
||||
|
||||
return router;
|
||||
export const fuelLogsRoutes: FastifyPluginAsync = async (
|
||||
fastify: FastifyInstance,
|
||||
_opts: FastifyPluginOptions
|
||||
) => {
|
||||
const fuelLogsController = new FuelLogsController();
|
||||
|
||||
// GET /api/fuel-logs - Get user's fuel logs
|
||||
fastify.get('/fuel-logs', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: fuelLogsController.getUserFuelLogs.bind(fuelLogsController)
|
||||
});
|
||||
|
||||
// POST /api/fuel-logs - Create new fuel log
|
||||
fastify.post<{ Body: CreateFuelLogBody }>('/fuel-logs', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: fuelLogsController.createFuelLog.bind(fuelLogsController)
|
||||
});
|
||||
|
||||
// GET /api/fuel-logs/:id - Get specific fuel log
|
||||
fastify.get<{ Params: FuelLogParams }>('/fuel-logs/:id', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: fuelLogsController.getFuelLog.bind(fuelLogsController)
|
||||
});
|
||||
|
||||
// PUT /api/fuel-logs/:id - Update fuel log
|
||||
fastify.put<{ Params: FuelLogParams; Body: UpdateFuelLogBody }>('/fuel-logs/:id', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: fuelLogsController.updateFuelLog.bind(fuelLogsController)
|
||||
});
|
||||
|
||||
// DELETE /api/fuel-logs/:id - Delete fuel log
|
||||
fastify.delete<{ Params: FuelLogParams }>('/fuel-logs/:id', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: fuelLogsController.deleteFuelLog.bind(fuelLogsController)
|
||||
});
|
||||
|
||||
// GET /api/vehicles/:vehicleId/fuel-logs - Get fuel logs for specific vehicle
|
||||
fastify.get<{ Params: VehicleParams }>('/vehicles/:vehicleId/fuel-logs', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: fuelLogsController.getFuelLogsByVehicle.bind(fuelLogsController)
|
||||
});
|
||||
|
||||
// GET /api/vehicles/:vehicleId/fuel-stats - Get fuel stats for specific vehicle
|
||||
fastify.get<{ Params: VehicleParams }>('/vehicles/:vehicleId/fuel-stats', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: fuelLogsController.getFuelStats.bind(fuelLogsController)
|
||||
});
|
||||
};
|
||||
|
||||
// For backward compatibility during migration
|
||||
export function registerFuelLogsRoutes() {
|
||||
throw new Error('registerFuelLogsRoutes is deprecated - use fuelLogsRoutes Fastify plugin instead');
|
||||
}
|
||||
@@ -67,4 +67,36 @@ export interface FuelStats {
|
||||
averageMPG: number;
|
||||
totalMiles: number;
|
||||
logCount: number;
|
||||
}
|
||||
|
||||
// Fastify-specific types for HTTP handling
|
||||
export interface CreateFuelLogBody {
|
||||
vehicleId: string;
|
||||
date: string;
|
||||
odometer: number;
|
||||
gallons: number;
|
||||
pricePerGallon: number;
|
||||
totalCost: number;
|
||||
station?: string;
|
||||
location?: string;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
export interface UpdateFuelLogBody {
|
||||
date?: string;
|
||||
odometer?: number;
|
||||
gallons?: number;
|
||||
pricePerGallon?: number;
|
||||
totalCost?: number;
|
||||
station?: string;
|
||||
location?: string;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
export interface FuelLogParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface VehicleParams {
|
||||
vehicleId: string;
|
||||
}
|
||||
@@ -14,5 +14,5 @@ export type {
|
||||
FuelStats
|
||||
} from './domain/fuel-logs.types';
|
||||
|
||||
// Internal: Register routes
|
||||
export { registerFuelLogsRoutes } from './api/fuel-logs.routes';
|
||||
// Internal: Register routes with Fastify app
|
||||
export { fuelLogsRoutes, registerFuelLogsRoutes } from './api/fuel-logs.routes';
|
||||
|
||||
@@ -1,105 +1,125 @@
|
||||
/**
|
||||
* @ai-summary HTTP request handlers for stations
|
||||
* @ai-summary Fastify route handlers for stations API
|
||||
* @ai-context HTTP request/response handling with Fastify reply methods
|
||||
*/
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { StationsService } from '../domain/stations.service';
|
||||
import { StationsRepository } from '../data/stations.repository';
|
||||
import { pool } from '../../../core/config/database';
|
||||
import { logger } from '../../../core/logging/logger';
|
||||
import { StationSearchBody, SaveStationBody, StationParams } from '../domain/stations.types';
|
||||
|
||||
export class StationsController {
|
||||
constructor(private service: StationsService) {}
|
||||
private stationsService: StationsService;
|
||||
|
||||
constructor() {
|
||||
const repository = new StationsRepository(pool);
|
||||
this.stationsService = new StationsService(repository);
|
||||
}
|
||||
|
||||
search = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async searchStations(request: FastifyRequest<{ Body: StationSearchBody }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const { latitude, longitude, radius, fuelType } = req.body;
|
||||
const userId = (request as any).user.sub;
|
||||
const { latitude, longitude, radius, fuelType } = request.body;
|
||||
|
||||
if (!latitude || !longitude) {
|
||||
return res.status(400).json({ error: 'Latitude and longitude are required' });
|
||||
return reply.code(400).send({
|
||||
error: 'Bad Request',
|
||||
message: 'Latitude and longitude are required'
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.service.searchNearbyStations({
|
||||
const result = await this.stationsService.searchNearbyStations({
|
||||
latitude,
|
||||
longitude,
|
||||
radius,
|
||||
fuelType
|
||||
}, userId);
|
||||
|
||||
res.json(result);
|
||||
return reply.code(200).send(result);
|
||||
} catch (error: any) {
|
||||
logger.error('Error searching stations', { error: error.message });
|
||||
return next(error);
|
||||
logger.error('Error searching stations', { error, userId: (request as any).user?.sub });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to search stations'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
save = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async saveStation(request: FastifyRequest<{ Body: SaveStationBody }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const { placeId, nickname, notes, isFavorite } = req.body;
|
||||
const userId = (request as any).user.sub;
|
||||
const { placeId, nickname, notes, isFavorite } = request.body;
|
||||
|
||||
if (!placeId) {
|
||||
return res.status(400).json({ error: 'Place ID is required' });
|
||||
return reply.code(400).send({
|
||||
error: 'Bad Request',
|
||||
message: 'Place ID is required'
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.service.saveStation(placeId, userId, {
|
||||
const result = await this.stationsService.saveStation(placeId, userId, {
|
||||
nickname,
|
||||
notes,
|
||||
isFavorite
|
||||
});
|
||||
|
||||
res.status(201).json(result);
|
||||
return reply.code(201).send(result);
|
||||
} catch (error: any) {
|
||||
logger.error('Error saving station', { error: error.message });
|
||||
logger.error('Error saving station', { error, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return res.status(404).json({ error: error.message });
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
|
||||
return next(error);
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to save station'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getSaved = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async getSavedStations(request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const userId = (request as any).user.sub;
|
||||
const result = await this.stationsService.getUserSavedStations(userId);
|
||||
|
||||
const result = await this.service.getUserSavedStations(userId);
|
||||
res.json(result);
|
||||
return reply.code(200).send(result);
|
||||
} catch (error: any) {
|
||||
logger.error('Error getting saved stations', { error: error.message });
|
||||
return next(error);
|
||||
logger.error('Error getting saved stations', { error, userId: (request as any).user?.sub });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get saved stations'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
removeSaved = async (req: Request, res: Response, next: NextFunction) => {
|
||||
async removeSavedStation(request: FastifyRequest<{ Params: StationParams }>, reply: FastifyReply) {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const userId = (request as any).user.sub;
|
||||
const { placeId } = request.params;
|
||||
|
||||
const { placeId } = req.params;
|
||||
await this.service.removeSavedStation(placeId, userId);
|
||||
res.status(204).send();
|
||||
await this.stationsService.removeSavedStation(placeId, userId);
|
||||
|
||||
return reply.code(204).send();
|
||||
} catch (error: any) {
|
||||
logger.error('Error removing saved station', { error: error.message });
|
||||
logger.error('Error removing saved station', { error, placeId: request.params.placeId, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message.includes('not found')) {
|
||||
return res.status(404).json({ error: error.message });
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
|
||||
return next(error);
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to remove saved station'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,49 @@
|
||||
/**
|
||||
* @ai-summary Route definitions for stations API
|
||||
* @ai-summary Fastify routes for stations API
|
||||
* @ai-context Route definitions with Fastify plugin pattern and authentication
|
||||
*/
|
||||
|
||||
import { Router } from 'express';
|
||||
import { FastifyInstance, FastifyPluginOptions } from 'fastify';
|
||||
import { FastifyPluginAsync } from 'fastify';
|
||||
import {
|
||||
StationSearchBody,
|
||||
SaveStationBody,
|
||||
StationParams
|
||||
} from '../domain/stations.types';
|
||||
import { StationsController } from './stations.controller';
|
||||
import { StationsService } from '../domain/stations.service';
|
||||
import { StationsRepository } from '../data/stations.repository';
|
||||
import { authMiddleware } from '../../../core/security/auth.middleware';
|
||||
import pool from '../../../core/config/database';
|
||||
|
||||
export function registerStationsRoutes(): Router {
|
||||
const router = Router();
|
||||
|
||||
// Initialize layers
|
||||
const repository = new StationsRepository(pool);
|
||||
const service = new StationsService(repository);
|
||||
const controller = new StationsController(service);
|
||||
|
||||
// Define routes
|
||||
router.post('/api/stations/search', authMiddleware, controller.search);
|
||||
router.post('/api/stations/save', authMiddleware, controller.save);
|
||||
router.get('/api/stations/saved', authMiddleware, controller.getSaved);
|
||||
router.delete('/api/stations/saved/:placeId', authMiddleware, controller.removeSaved);
|
||||
|
||||
return router;
|
||||
export const stationsRoutes: FastifyPluginAsync = async (
|
||||
fastify: FastifyInstance,
|
||||
_opts: FastifyPluginOptions
|
||||
) => {
|
||||
const stationsController = new StationsController();
|
||||
|
||||
// POST /api/stations/search - Search nearby stations
|
||||
fastify.post<{ Body: StationSearchBody }>('/stations/search', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: stationsController.searchStations.bind(stationsController)
|
||||
});
|
||||
|
||||
// POST /api/stations/save - Save a station to user's favorites
|
||||
fastify.post<{ Body: SaveStationBody }>('/stations/save', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: stationsController.saveStation.bind(stationsController)
|
||||
});
|
||||
|
||||
// GET /api/stations/saved - Get user's saved stations
|
||||
fastify.get('/stations/saved', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: stationsController.getSavedStations.bind(stationsController)
|
||||
});
|
||||
|
||||
// DELETE /api/stations/saved/:placeId - Remove saved station
|
||||
fastify.delete<{ Params: StationParams }>('/stations/saved/:placeId', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: stationsController.removeSavedStation.bind(stationsController)
|
||||
});
|
||||
};
|
||||
|
||||
// For backward compatibility during migration
|
||||
export function registerStationsRoutes() {
|
||||
throw new Error('registerStationsRoutes is deprecated - use stationsRoutes Fastify plugin instead');
|
||||
}
|
||||
@@ -46,4 +46,23 @@ export interface SavedStation {
|
||||
isFavorite: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
// Fastify-specific types for HTTP handling
|
||||
export interface StationSearchBody {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
radius?: number;
|
||||
fuelType?: 'regular' | 'premium' | 'diesel';
|
||||
}
|
||||
|
||||
export interface SaveStationBody {
|
||||
placeId: string;
|
||||
nickname?: string;
|
||||
notes?: string;
|
||||
isFavorite?: boolean;
|
||||
}
|
||||
|
||||
export interface StationParams {
|
||||
placeId: string;
|
||||
}
|
||||
@@ -13,5 +13,5 @@ export type {
|
||||
SavedStation
|
||||
} from './domain/stations.types';
|
||||
|
||||
// Internal: Register routes
|
||||
export { registerStationsRoutes } from './api/stations.routes';
|
||||
// Internal: Register routes with Fastify app
|
||||
export { stationsRoutes, registerStationsRoutes } from './api/stations.routes';
|
||||
|
||||
@@ -1,235 +1,206 @@
|
||||
/**
|
||||
* @ai-summary HTTP request handlers for vehicles API
|
||||
* @ai-context Handles validation, auth, and delegates to service layer
|
||||
* @ai-summary Fastify route handlers for vehicles API
|
||||
* @ai-context HTTP request/response handling with Fastify reply methods
|
||||
*/
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { VehiclesService } from '../domain/vehicles.service';
|
||||
import { VehiclesRepository } from '../data/vehicles.repository';
|
||||
import pool from '../../../core/config/database';
|
||||
import { pool } from '../../../core/config/database';
|
||||
import { logger } from '../../../core/logging/logger';
|
||||
import { ZodError } from 'zod';
|
||||
import {
|
||||
createVehicleSchema,
|
||||
updateVehicleSchema,
|
||||
vehicleIdSchema,
|
||||
CreateVehicleInput,
|
||||
UpdateVehicleInput,
|
||||
} from './vehicles.validation';
|
||||
import { CreateVehicleBody, UpdateVehicleBody, VehicleParams } from '../domain/vehicles.types';
|
||||
|
||||
export class VehiclesController {
|
||||
private service: VehiclesService;
|
||||
private vehiclesService: VehiclesService;
|
||||
|
||||
constructor() {
|
||||
const repository = new VehiclesRepository(pool);
|
||||
this.service = new VehiclesService(repository);
|
||||
this.vehiclesService = new VehiclesService(repository);
|
||||
}
|
||||
|
||||
createVehicle = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
async getUserVehicles(request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
// Validate request body
|
||||
const data = createVehicleSchema.parse(req.body) as CreateVehicleInput;
|
||||
const userId = (request as any).user.sub;
|
||||
const vehicles = await this.vehiclesService.getUserVehicles(userId);
|
||||
|
||||
// Get user ID from JWT token
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
res.status(401).json({ error: 'Unauthorized' });
|
||||
return;
|
||||
}
|
||||
|
||||
const vehicle = await this.service.createVehicle(data, userId);
|
||||
|
||||
logger.info('Vehicle created successfully', { vehicleId: vehicle.id, userId });
|
||||
res.status(201).json(vehicle);
|
||||
} catch (error: any) {
|
||||
if (error instanceof ZodError) {
|
||||
res.status(400).json({ error: error.errors[0].message });
|
||||
return;
|
||||
}
|
||||
if (error.message === 'Invalid VIN format' ||
|
||||
error.message === 'Vehicle with this VIN already exists') {
|
||||
res.status(400).json({ error: error.message });
|
||||
return;
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
getUserVehicles = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
res.status(401).json({ error: 'Unauthorized' });
|
||||
return;
|
||||
}
|
||||
|
||||
const vehicles = await this.service.getUserVehicles(userId);
|
||||
res.json(vehicles);
|
||||
return reply.code(200).send(vehicles);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
logger.error('Error getting user vehicles', { error, userId: (request as any).user?.sub });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get vehicles'
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
getVehicle = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
async createVehicle(request: FastifyRequest<{ Body: CreateVehicleBody }>, reply: FastifyReply) {
|
||||
try {
|
||||
const { id } = vehicleIdSchema.parse(req.params);
|
||||
const userId = req.user?.sub;
|
||||
const userId = (request as any).user.sub;
|
||||
const vehicle = await this.vehiclesService.createVehicle(request.body, userId);
|
||||
|
||||
if (!userId) {
|
||||
res.status(401).json({ error: 'Unauthorized' });
|
||||
return;
|
||||
}
|
||||
|
||||
const vehicle = await this.service.getVehicle(id, userId);
|
||||
res.json(vehicle);
|
||||
return reply.code(201).send(vehicle);
|
||||
} catch (error: any) {
|
||||
if (error instanceof ZodError) {
|
||||
res.status(400).json({ error: error.errors[0].message });
|
||||
return;
|
||||
}
|
||||
if (error.message === 'Vehicle not found') {
|
||||
res.status(404).json({ error: 'Vehicle not found' });
|
||||
return;
|
||||
}
|
||||
if (error.message === 'Unauthorized') {
|
||||
res.status(403).json({ error: 'Access denied' });
|
||||
return;
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
updateVehicle = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
const { id } = vehicleIdSchema.parse(req.params);
|
||||
const data = updateVehicleSchema.parse(req.body) as UpdateVehicleInput;
|
||||
const userId = req.user?.sub;
|
||||
|
||||
if (!userId) {
|
||||
res.status(401).json({ error: 'Unauthorized' });
|
||||
return;
|
||||
}
|
||||
|
||||
const vehicle = await this.service.updateVehicle(id, data, userId);
|
||||
logger.error('Error creating vehicle', { error, userId: (request as any).user?.sub });
|
||||
|
||||
logger.info('Vehicle updated successfully', { vehicleId: id, userId });
|
||||
res.json(vehicle);
|
||||
} catch (error: any) {
|
||||
if (error instanceof ZodError) {
|
||||
res.status(400).json({ error: error.errors[0].message });
|
||||
return;
|
||||
if (error.message === 'Invalid VIN format') {
|
||||
return reply.code(400).send({
|
||||
error: 'Bad Request',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
if (error.message === 'Vehicle not found') {
|
||||
res.status(404).json({ error: 'Vehicle not found' });
|
||||
return;
|
||||
}
|
||||
if (error.message === 'Unauthorized') {
|
||||
res.status(403).json({ error: 'Access denied' });
|
||||
return;
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
deleteVehicle = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
const { id } = vehicleIdSchema.parse(req.params);
|
||||
const userId = req.user?.sub;
|
||||
|
||||
if (!userId) {
|
||||
res.status(401).json({ error: 'Unauthorized' });
|
||||
return;
|
||||
}
|
||||
|
||||
await this.service.deleteVehicle(id, userId);
|
||||
|
||||
logger.info('Vehicle deleted successfully', { vehicleId: id, userId });
|
||||
res.status(204).send();
|
||||
} catch (error: any) {
|
||||
if (error instanceof ZodError) {
|
||||
res.status(400).json({ error: error.errors[0].message });
|
||||
return;
|
||||
if (error.message === 'Vehicle with this VIN already exists') {
|
||||
return reply.code(400).send({
|
||||
error: 'Bad Request',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
if (error.message === 'Vehicle not found') {
|
||||
res.status(404).json({ error: 'Vehicle not found' });
|
||||
return;
|
||||
}
|
||||
if (error.message === 'Unauthorized') {
|
||||
res.status(403).json({ error: 'Access denied' });
|
||||
return;
|
||||
}
|
||||
next(error);
|
||||
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to create vehicle'
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
getDropdownMakes = async (_req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
async getVehicle(request: FastifyRequest<{ Params: VehicleParams }>, reply: FastifyReply) {
|
||||
try {
|
||||
const makes = await this.service.getDropdownMakes();
|
||||
res.json(makes);
|
||||
const userId = (request as any).user.sub;
|
||||
const { id } = request.params;
|
||||
|
||||
const vehicle = await this.vehiclesService.getVehicle(id, userId);
|
||||
|
||||
return reply.code(200).send(vehicle);
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Failed to load makes') {
|
||||
res.status(503).json({ error: 'Unable to load makes data' });
|
||||
return;
|
||||
logger.error('Error getting vehicle', { error, vehicleId: request.params.id, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message === 'Vehicle not found' || error.message === 'Unauthorized') {
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: 'Vehicle not found'
|
||||
});
|
||||
}
|
||||
next(error);
|
||||
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get vehicle'
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
getDropdownModels = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
async updateVehicle(request: FastifyRequest<{ Params: VehicleParams; Body: UpdateVehicleBody }>, reply: FastifyReply) {
|
||||
try {
|
||||
const { make } = req.params;
|
||||
if (!make) {
|
||||
res.status(400).json({ error: 'Make parameter is required' });
|
||||
return;
|
||||
}
|
||||
|
||||
const models = await this.service.getDropdownModels(make);
|
||||
res.json(models);
|
||||
const userId = (request as any).user.sub;
|
||||
const { id } = request.params;
|
||||
|
||||
const vehicle = await this.vehiclesService.updateVehicle(id, request.body, userId);
|
||||
|
||||
return reply.code(200).send(vehicle);
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Failed to load models') {
|
||||
res.status(503).json({ error: 'Unable to load models data' });
|
||||
return;
|
||||
logger.error('Error updating vehicle', { error, vehicleId: request.params.id, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message === 'Vehicle not found' || error.message === 'Unauthorized') {
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: 'Vehicle not found'
|
||||
});
|
||||
}
|
||||
next(error);
|
||||
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to update vehicle'
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
getDropdownTransmissions = async (_req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
async deleteVehicle(request: FastifyRequest<{ Params: VehicleParams }>, reply: FastifyReply) {
|
||||
try {
|
||||
const transmissions = await this.service.getDropdownTransmissions();
|
||||
res.json(transmissions);
|
||||
const userId = (request as any).user.sub;
|
||||
const { id } = request.params;
|
||||
|
||||
await this.vehiclesService.deleteVehicle(id, userId);
|
||||
|
||||
return reply.code(204).send();
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Failed to load transmissions') {
|
||||
res.status(503).json({ error: 'Unable to load transmissions data' });
|
||||
return;
|
||||
logger.error('Error deleting vehicle', { error, vehicleId: request.params.id, userId: (request as any).user?.sub });
|
||||
|
||||
if (error.message === 'Vehicle not found' || error.message === 'Unauthorized') {
|
||||
return reply.code(404).send({
|
||||
error: 'Not Found',
|
||||
message: 'Vehicle not found'
|
||||
});
|
||||
}
|
||||
next(error);
|
||||
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to delete vehicle'
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
getDropdownEngines = async (_req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
async getDropdownMakes(_request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
const engines = await this.service.getDropdownEngines();
|
||||
res.json(engines);
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Failed to load engines') {
|
||||
res.status(503).json({ error: 'Unable to load engines data' });
|
||||
return;
|
||||
}
|
||||
next(error);
|
||||
const makes = await this.vehiclesService.getDropdownMakes();
|
||||
return reply.code(200).send(makes);
|
||||
} catch (error) {
|
||||
logger.error('Error getting dropdown makes', { error });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get makes'
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
getDropdownTrims = async (_req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
async getDropdownModels(request: FastifyRequest<{ Params: { make: string } }>, reply: FastifyReply) {
|
||||
try {
|
||||
const trims = await this.service.getDropdownTrims();
|
||||
res.json(trims);
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Failed to load trims') {
|
||||
res.status(503).json({ error: 'Unable to load trims data' });
|
||||
return;
|
||||
}
|
||||
next(error);
|
||||
const { make } = request.params;
|
||||
const models = await this.vehiclesService.getDropdownModels(make);
|
||||
return reply.code(200).send(models);
|
||||
} catch (error) {
|
||||
logger.error('Error getting dropdown models', { error, make: request.params.make });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get models'
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async getDropdownTransmissions(_request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
const transmissions = await this.vehiclesService.getDropdownTransmissions();
|
||||
return reply.code(200).send(transmissions);
|
||||
} catch (error) {
|
||||
logger.error('Error getting dropdown transmissions', { error });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get transmissions'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getDropdownEngines(_request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
const engines = await this.vehiclesService.getDropdownEngines();
|
||||
return reply.code(200).send(engines);
|
||||
} catch (error) {
|
||||
logger.error('Error getting dropdown engines', { error });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get engines'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getDropdownTrims(_request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
const trims = await this.vehiclesService.getDropdownTrims();
|
||||
return reply.code(200).send(trims);
|
||||
} catch (error) {
|
||||
logger.error('Error getting dropdown trims', { error });
|
||||
return reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to get trims'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,80 @@
|
||||
/**
|
||||
* @ai-summary Express routes for vehicles API
|
||||
* @ai-context Defines REST endpoints with auth middleware
|
||||
* @ai-summary Fastify routes for vehicles API
|
||||
* @ai-context Route definitions with TypeBox validation and authentication
|
||||
*/
|
||||
|
||||
import { Router } from 'express';
|
||||
import { FastifyInstance, FastifyPluginOptions } from 'fastify';
|
||||
import { FastifyPluginAsync } from 'fastify';
|
||||
import {
|
||||
CreateVehicleBody,
|
||||
UpdateVehicleBody,
|
||||
VehicleParams
|
||||
} from '../domain/vehicles.types';
|
||||
import { VehiclesController } from './vehicles.controller';
|
||||
import { authMiddleware } from '../../../core/security/auth.middleware';
|
||||
|
||||
export function registerVehiclesRoutes(): Router {
|
||||
const router = Router();
|
||||
const controller = new VehiclesController();
|
||||
export const vehiclesRoutes: FastifyPluginAsync = async (
|
||||
fastify: FastifyInstance,
|
||||
_opts: FastifyPluginOptions
|
||||
) => {
|
||||
const vehiclesController = new VehiclesController();
|
||||
|
||||
// Dropdown Data Routes (no auth required for form population)
|
||||
router.get('/api/vehicles/dropdown/makes', controller.getDropdownMakes);
|
||||
router.get('/api/vehicles/dropdown/models/:make', controller.getDropdownModels);
|
||||
router.get('/api/vehicles/dropdown/transmissions', controller.getDropdownTransmissions);
|
||||
router.get('/api/vehicles/dropdown/engines', controller.getDropdownEngines);
|
||||
router.get('/api/vehicles/dropdown/trims', controller.getDropdownTrims);
|
||||
// GET /api/vehicles - Get user's vehicles
|
||||
fastify.get('/vehicles', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: vehiclesController.getUserVehicles.bind(vehiclesController)
|
||||
});
|
||||
|
||||
// All other vehicle routes require authentication
|
||||
router.use(authMiddleware);
|
||||
// POST /api/vehicles - Create new vehicle
|
||||
fastify.post<{ Body: CreateVehicleBody }>('/vehicles', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: vehiclesController.createVehicle.bind(vehiclesController)
|
||||
});
|
||||
|
||||
// CRUD Routes
|
||||
router.post('/api/vehicles', controller.createVehicle);
|
||||
router.get('/api/vehicles', controller.getUserVehicles);
|
||||
router.get('/api/vehicles/:id', controller.getVehicle);
|
||||
router.put('/api/vehicles/:id', controller.updateVehicle);
|
||||
router.delete('/api/vehicles/:id', controller.deleteVehicle);
|
||||
// GET /api/vehicles/:id - Get specific vehicle
|
||||
fastify.get<{ Params: VehicleParams }>('/vehicles/:id', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: vehiclesController.getVehicle.bind(vehiclesController)
|
||||
});
|
||||
|
||||
return router;
|
||||
// PUT /api/vehicles/:id - Update vehicle
|
||||
fastify.put<{ Params: VehicleParams; Body: UpdateVehicleBody }>('/vehicles/:id', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: vehiclesController.updateVehicle.bind(vehiclesController)
|
||||
});
|
||||
|
||||
// DELETE /api/vehicles/:id - Delete vehicle
|
||||
fastify.delete<{ Params: VehicleParams }>('/vehicles/:id', {
|
||||
preHandler: fastify.authenticate,
|
||||
handler: vehiclesController.deleteVehicle.bind(vehiclesController)
|
||||
});
|
||||
|
||||
// GET /api/vehicles/dropdown/makes - Get vehicle makes
|
||||
fastify.get('/vehicles/dropdown/makes', {
|
||||
handler: vehiclesController.getDropdownMakes.bind(vehiclesController)
|
||||
});
|
||||
|
||||
// GET /api/vehicles/dropdown/models/:make - Get models for make
|
||||
fastify.get<{ Params: { make: string } }>('/vehicles/dropdown/models/:make', {
|
||||
handler: vehiclesController.getDropdownModels.bind(vehiclesController)
|
||||
});
|
||||
|
||||
// GET /api/vehicles/dropdown/transmissions - Get transmission types
|
||||
fastify.get('/vehicles/dropdown/transmissions', {
|
||||
handler: vehiclesController.getDropdownTransmissions.bind(vehiclesController)
|
||||
});
|
||||
|
||||
// GET /api/vehicles/dropdown/engines - Get engine configurations
|
||||
fastify.get('/vehicles/dropdown/engines', {
|
||||
handler: vehiclesController.getDropdownEngines.bind(vehiclesController)
|
||||
});
|
||||
|
||||
// GET /api/vehicles/dropdown/trims - Get trim levels
|
||||
fastify.get('/vehicles/dropdown/trims', {
|
||||
handler: vehiclesController.getDropdownTrims.bind(vehiclesController)
|
||||
});
|
||||
};
|
||||
|
||||
// For backward compatibility during migration
|
||||
export function registerVehiclesRoutes() {
|
||||
throw new Error('registerVehiclesRoutes is deprecated - use vehiclesRoutes Fastify plugin instead');
|
||||
}
|
||||
@@ -82,4 +82,24 @@ export interface VINDecodeResult {
|
||||
engineType?: string;
|
||||
bodyType?: string;
|
||||
rawData?: any;
|
||||
}
|
||||
|
||||
// Fastify-specific types for HTTP handling
|
||||
export interface CreateVehicleBody {
|
||||
vin: string;
|
||||
nickname?: string;
|
||||
color?: string;
|
||||
licensePlate?: string;
|
||||
odometerReading?: number;
|
||||
}
|
||||
|
||||
export interface UpdateVehicleBody {
|
||||
nickname?: string;
|
||||
color?: string;
|
||||
licensePlate?: string;
|
||||
odometerReading?: number;
|
||||
}
|
||||
|
||||
export interface VehicleParams {
|
||||
id: string;
|
||||
}
|
||||
@@ -14,5 +14,5 @@ export type {
|
||||
VehicleResponse
|
||||
} from './domain/vehicles.types';
|
||||
|
||||
// Internal: Register routes with Express app
|
||||
export { registerVehiclesRoutes } from './api/vehicles.routes';
|
||||
// Internal: Register routes with Fastify app
|
||||
export { vehiclesRoutes, registerVehiclesRoutes } from './api/vehicles.routes';
|
||||
|
||||
Reference in New Issue
Block a user