MVP Build
This commit is contained in:
186
backend/src/features/fuel-logs/api/fuel-logs.controller.ts
Normal file
186
backend/src/features/fuel-logs/api/fuel-logs.controller.ts
Normal file
@@ -0,0 +1,186 @@
|
||||
/**
|
||||
* @ai-summary HTTP request handlers for fuel logs
|
||||
*/
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { FuelLogsService } from '../domain/fuel-logs.service';
|
||||
import { validateCreateFuelLog, validateUpdateFuelLog } from './fuel-logs.validators';
|
||||
import { logger } from '../../../core/logging/logger';
|
||||
|
||||
export class FuelLogsController {
|
||||
constructor(private service: FuelLogsService) {}
|
||||
|
||||
create = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const validation = validateCreateFuelLog(req.body);
|
||||
if (!validation.success) {
|
||||
return res.status(400).json({
|
||||
error: 'Validation failed',
|
||||
details: validation.error.errors
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.service.createFuelLog(validation.data, userId);
|
||||
res.status(201).json(result);
|
||||
} catch (error: any) {
|
||||
logger.error('Error creating fuel log', { 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);
|
||||
}
|
||||
}
|
||||
|
||||
listByVehicle = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const { vehicleId } = req.params;
|
||||
const result = await this.service.getFuelLogsByVehicle(vehicleId, userId);
|
||||
res.json(result);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
listAll = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
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 { id } = req.params;
|
||||
const result = await this.service.getFuelLog(id, userId);
|
||||
res.json(result);
|
||||
} catch (error: any) {
|
||||
logger.error('Error getting fuel log', { error: error.message });
|
||||
|
||||
if (error.message === 'Fuel log 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);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
delete = 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;
|
||||
await this.service.deleteFuelLog(id, userId);
|
||||
res.status(204).send();
|
||||
} catch (error: any) {
|
||||
logger.error('Error deleting 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);
|
||||
}
|
||||
}
|
||||
|
||||
getStats = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userId = req.user?.sub;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const { vehicleId } = req.params;
|
||||
const result = await this.service.getVehicleStats(vehicleId, userId);
|
||||
res.json(result);
|
||||
} catch (error: any) {
|
||||
logger.error('Error getting fuel stats', { 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user