115 lines
4.2 KiB
TypeScript
115 lines
4.2 KiB
TypeScript
/**
|
|
* @ai-summary Platform API controller
|
|
* @ai-context Request handlers for vehicle data and VIN decoding endpoints
|
|
*/
|
|
import { FastifyRequest, FastifyReply } from 'fastify';
|
|
import { Pool } from 'pg';
|
|
import { VehicleDataService } from '../domain/vehicle-data.service';
|
|
import { PlatformCacheService } from '../domain/platform-cache.service';
|
|
import { cacheService } from '../../../core/config/redis';
|
|
import {
|
|
MakesQuery,
|
|
ModelsQuery,
|
|
TrimsQuery,
|
|
EnginesQuery,
|
|
TransmissionsQuery
|
|
} from '../models/requests';
|
|
import { logger } from '../../../core/logging/logger';
|
|
|
|
export class PlatformController {
|
|
private vehicleDataService: VehicleDataService;
|
|
private pool: Pool;
|
|
|
|
constructor(pool: Pool) {
|
|
this.pool = pool;
|
|
const platformCache = new PlatformCacheService(cacheService);
|
|
this.vehicleDataService = new VehicleDataService(platformCache);
|
|
}
|
|
|
|
/**
|
|
* GET /api/platform/years
|
|
*/
|
|
async getYears(_request: FastifyRequest, reply: FastifyReply): Promise<void> {
|
|
try {
|
|
const years = await this.vehicleDataService.getYears(this.pool);
|
|
reply.code(200).send(years);
|
|
} catch (error) {
|
|
logger.error('Controller error: getYears', { error });
|
|
reply.code(500).send({ error: 'Failed to retrieve years' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/platform/makes?year={year}
|
|
*/
|
|
async getMakes(request: FastifyRequest<{ Querystring: MakesQuery }>, reply: FastifyReply): Promise<void> {
|
|
try {
|
|
const { year } = request.query;
|
|
const makes = await this.vehicleDataService.getMakes(this.pool, year);
|
|
reply.code(200).send({ makes });
|
|
} catch (error) {
|
|
logger.error('Controller error: getMakes', { error, query: request.query });
|
|
reply.code(500).send({ error: 'Failed to retrieve makes' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/platform/models?year={year}&make={make}
|
|
*/
|
|
async getModels(request: FastifyRequest<{ Querystring: ModelsQuery }>, reply: FastifyReply): Promise<void> {
|
|
try {
|
|
const { year, make } = request.query as any;
|
|
const models = await this.vehicleDataService.getModels(this.pool, year, make);
|
|
reply.code(200).send({ models });
|
|
} catch (error) {
|
|
logger.error('Controller error: getModels', { error, query: request.query });
|
|
reply.code(500).send({ error: 'Failed to retrieve models' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/platform/trims?year={year}&make={make}&model={model}
|
|
*/
|
|
async getTrims(request: FastifyRequest<{ Querystring: TrimsQuery }>, reply: FastifyReply): Promise<void> {
|
|
try {
|
|
const { year, make, model } = request.query as any;
|
|
const trims = await this.vehicleDataService.getTrims(this.pool, year, make, model);
|
|
reply.code(200).send({ trims });
|
|
} catch (error) {
|
|
logger.error('Controller error: getTrims', { error, query: request.query });
|
|
reply.code(500).send({ error: 'Failed to retrieve trims' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/platform/engines?year={year}&make={make}&model={model}&trim={trim}
|
|
*/
|
|
async getEngines(request: FastifyRequest<{ Querystring: EnginesQuery }>, reply: FastifyReply): Promise<void> {
|
|
try {
|
|
const { year, make, model, trim } = request.query as any;
|
|
const engines = await this.vehicleDataService.getEngines(this.pool, year, make, model, trim);
|
|
reply.code(200).send({ engines });
|
|
} catch (error) {
|
|
logger.error('Controller error: getEngines', { error, query: request.query });
|
|
reply.code(500).send({ error: 'Failed to retrieve engines' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/platform/transmissions?year={year}&make={make}&model={model}&trim={trim}[&engine={engine}]
|
|
*/
|
|
async getTransmissions(request: FastifyRequest<{ Querystring: TransmissionsQuery }>, reply: FastifyReply): Promise<void> {
|
|
try {
|
|
const { year, make, model, trim, engine } = request.query as any;
|
|
const transmissions = engine
|
|
? await this.vehicleDataService.getTransmissionsForTrimAndEngine(this.pool, year, make, model, trim, engine)
|
|
: await this.vehicleDataService.getTransmissionsForTrim(this.pool, year, make, model, trim);
|
|
reply.code(200).send({ transmissions });
|
|
} catch (error) {
|
|
logger.error('Controller error: getTransmissions', { error, query: request.query });
|
|
reply.code(500).send({ error: 'Failed to retrieve transmissions' });
|
|
}
|
|
}
|
|
|
|
}
|