Trains management CRUD issues solved

This commit is contained in:
hagiye
2026-06-05 21:07:04 +03:00
parent 9676a6bb56
commit dc42838a43
41 changed files with 2192 additions and 1830 deletions

View File

@@ -7,6 +7,7 @@ import {
ParseUUIDPipe,
Patch,
Post,
Query,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreateCargoDto } from './dto/create-cargo.dto';
@@ -28,8 +29,8 @@ export class CargoesController {
@Get()
@ApiOperation({ summary: 'List all cargoes' })
findAll() {
return this.cargoesService.findAll();
findAll(@Query() query: Record<string, string | undefined>) {
return this.cargoesService.findAll(query);
}
@Get(':id')
@@ -67,4 +68,4 @@ export class CargoesController {
deliver(@Param('id', ParseUUIDPipe) id: string, @Body() dto?: DeliverCargoDto) {
return this.cargoesService.deliverCargo(id, dto);
}
}
}

View File

@@ -1,6 +1,6 @@
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FindOptionsOrder, FindOptionsWhere, ILike, Repository } from 'typeorm';
import { CreateCargoDto } from './dto/create-cargo.dto';
import { UpdateCargoDto } from './dto/update-cargo.dto';
import { LoadCargoDto } from './dto/load-cargo.dto';
@@ -22,8 +22,36 @@ export class CargoesService {
return this.cargoRepo.save(cargo);
}
async findAll(): Promise<Cargo[]> {
return this.cargoRepo.find({ order: { cargoReference: 'ASC' } });
async findAll(query: Record<string, string | undefined> = {}): Promise<Cargo[]> {
const where: FindOptionsWhere<Cargo>[] | FindOptionsWhere<Cargo> = [];
const search = query.search?.trim();
const status = query.status?.trim();
const containerId = query.containerId?.trim();
if (search) {
where.push({
cargoReference: ILike(`%${search}%`),
...(status ? { status } : {}),
...(containerId ? { containerId } : {}),
});
where.push({
description: ILike(`%${search}%`),
...(status ? { status } : {}),
...(containerId ? { containerId } : {}),
});
}
const sortBy = ['cargoReference', 'quantity', 'weight', 'volume', 'status'].includes(query.sortBy ?? '')
? (query.sortBy as keyof Cargo)
: 'cargoReference';
const sortOrder = query.sortOrder?.toUpperCase() === 'DESC' ? 'DESC' : 'ASC';
return this.cargoRepo.find({
where: search ? where : { ...(status ? { status } : {}), ...(containerId ? { containerId } : {}) },
order: { [sortBy]: sortOrder } as FindOptionsOrder<Cargo>,
skip: query.page && query.limit ? (Number(query.page) - 1) * Number(query.limit) : undefined,
take: query.limit ? Number(query.limit) : undefined,
});
}
async findById(id: string): Promise<Cargo> {
@@ -101,4 +129,4 @@ export class CargoesService {
return this.cargoRepo.save(cargo);
}
}
}