mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 21:08:12 +00:00
trains, wagons,containers and cargoes schema and API
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Freight } from "@edr/types";
|
||||
import { IsEnum, IsNumber, IsOptional, IsString, Min } from "class-validator";
|
||||
import { IsString, IsNumber, IsOptional, IsUUID, IsDateString, Min, IsEnum } from 'class-validator';
|
||||
import { Freight } from '@edr/types';
|
||||
|
||||
export class CreateTrainDto {
|
||||
@IsString()
|
||||
@@ -11,9 +11,45 @@ export class CreateTrainDto {
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(Freight.TrainStatus)
|
||||
status?: Freight.TrainStatus;
|
||||
status?: Freight.TrainStatus; // ✅ uses enum, not string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
trainNumber?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
trainName?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
routeId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
originStationId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
destinationStationId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
departureTime?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
arrivalTime?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
locomotiveNumber?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
remarks?: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateTrainDto } from './create-train.dto';
|
||||
|
||||
export class UpdateTrainDto extends PartialType(CreateTrainDto) {}
|
||||
@@ -1,23 +1,58 @@
|
||||
import { BaseEntity } from "@edr/api-common";
|
||||
import { Freight } from "@edr/types";
|
||||
import { Column, Entity } from "typeorm";
|
||||
// apps/edr-freight-api/src/modules/trains/entities/train.entity.ts
|
||||
import { BaseEntity } from '@edr/api-common';
|
||||
import { Freight } from '@edr/types';
|
||||
import { Column, Entity, OneToMany } from 'typeorm';
|
||||
import { Wagon } from '../../wagons/entities/wagon.entity';
|
||||
|
||||
@Entity({ schema:"freight",name: "trains" })
|
||||
@Entity({ schema: 'freight', name: 'trains' })
|
||||
export class Train extends BaseEntity {
|
||||
@Column({ name: "code", type: "varchar", length: 32, unique: true })
|
||||
// --- existing fields (keep for backward compatibility) ---
|
||||
@Column({ name: 'code', type: 'varchar', length: 32, unique: true })
|
||||
code!: string;
|
||||
|
||||
@Column({ name: "capacity_tons", type: "numeric", precision: 10, scale: 2 })
|
||||
@Column({ name: 'capacity_tons', type: 'numeric', precision: 10, scale: 2 })
|
||||
capacityTons!: number;
|
||||
|
||||
@Column({
|
||||
name: "status",
|
||||
type: "enum",
|
||||
name: 'status',
|
||||
type: 'enum',
|
||||
enum: Freight.TrainStatus,
|
||||
default: Freight.TrainStatus.Available,
|
||||
})
|
||||
status!: Freight.TrainStatus;
|
||||
|
||||
@Column({ name: "notes", type: "text", nullable: true })
|
||||
@Column({ name: 'notes', type: 'text', nullable: true })
|
||||
notes?: string | null;
|
||||
}
|
||||
|
||||
// --- new required fields ---
|
||||
@Column({ name: 'train_number', type: 'varchar', length: 20, unique: true, nullable: true })
|
||||
trainNumber?: string;
|
||||
|
||||
@Column({ name: 'train_name', type: 'varchar', length: 100, nullable: true })
|
||||
trainName?: string;
|
||||
|
||||
@Column({ name: 'route_id', type: 'uuid', nullable: true })
|
||||
routeId?: string;
|
||||
|
||||
@Column({ name: 'origin_station_id', type: 'uuid', nullable: true })
|
||||
originStationId?: string;
|
||||
|
||||
@Column({ name: 'destination_station_id', type: 'uuid', nullable: true })
|
||||
destinationStationId?: string;
|
||||
|
||||
@Column({ name: 'departure_time', type: 'timestamp', nullable: true })
|
||||
departureTime?: Date;
|
||||
|
||||
@Column({ name: 'arrival_time', type: 'timestamp', nullable: true })
|
||||
arrivalTime?: Date;
|
||||
|
||||
@Column({ name: 'locomotive_number', type: 'varchar', length: 50, nullable: true })
|
||||
locomotiveNumber?: string;
|
||||
|
||||
@Column({ name: 'remarks', type: 'text', nullable: true })
|
||||
remarks?: string;
|
||||
|
||||
// --- relationships ---
|
||||
@OneToMany(() => Wagon, (wagon) => wagon.train)
|
||||
wagons!: Wagon[]; // fixed typo: was 'wagens'
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
|
||||
import { Train } from "./entities/train.entity";
|
||||
import { TrainsController } from "./trains.controller";
|
||||
import { TrainsRepository } from "./trains.repository";
|
||||
import { TrainsService } from "./trains.service";
|
||||
// apps/edr-freight-api/src/modules/trains/trains.module.ts
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Train } from './entities/train.entity';
|
||||
import { TrainsController } from './trains.controller';
|
||||
import { TrainsService } from './trains.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Train])],
|
||||
controllers: [TrainsController],
|
||||
providers: [TrainsService, TrainsRepository],
|
||||
exports: [TrainsService],
|
||||
providers: [TrainsService],
|
||||
exports: [TrainsService], // if other modules need it
|
||||
})
|
||||
export class TrainsModule {}
|
||||
export class TrainsModule {}
|
||||
@@ -1,29 +1,41 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
|
||||
import { CreateTrainDto } from "./dto/create-train.dto";
|
||||
import { Train } from "./entities/train.entity";
|
||||
import { TrainsRepository } from "./trains.repository";
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CreateTrainDto } from './dto/create-train.dto';
|
||||
import { UpdateTrainDto } from './dto/update-train.dto';
|
||||
import { Train } from './entities/train.entity';
|
||||
|
||||
@Injectable()
|
||||
export class TrainsService {
|
||||
constructor(private readonly trainsRepository: TrainsRepository) {}
|
||||
constructor(
|
||||
@InjectRepository(Train)
|
||||
private readonly trainRepo: Repository<Train>,
|
||||
) {}
|
||||
|
||||
/** Register a new train in the fleet. */
|
||||
create(dto: CreateTrainDto): Promise<Train> {
|
||||
return this.trainsRepository.create(dto);
|
||||
const train = this.trainRepo.create(dto);
|
||||
return this.trainRepo.save(train);
|
||||
}
|
||||
|
||||
/** List every active train. */
|
||||
findAll(): Promise<Train[]> {
|
||||
return this.trainsRepository.findAll({ order: { code: "ASC" } });
|
||||
return this.trainRepo.find({ order: { code: 'ASC' } });
|
||||
}
|
||||
|
||||
/** Get a single train by ID. */
|
||||
async findById(id: string): Promise<Train> {
|
||||
const train = await this.trainsRepository.findById(id);
|
||||
if (!train) {
|
||||
throw new NotFoundException(`Train ${id} not found`);
|
||||
}
|
||||
const train = await this.trainRepo.findOne({ where: { id } });
|
||||
if (!train) throw new NotFoundException(`Train ${id} not found`);
|
||||
return train;
|
||||
}
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateTrainDto): Promise<Train> {
|
||||
const train = await this.findById(id);
|
||||
Object.assign(train, dto);
|
||||
// Convert undefined to null for optional fields if needed
|
||||
return this.trainRepo.save(train);
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
const train = await this.findById(id);
|
||||
await this.trainRepo.remove(train);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user