mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 02:30:55 +00:00
Project Initialization
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { Freight } from '@edr/types';
|
||||
import { IsEnum, IsNumber, IsOptional, IsString, Min } from 'class-validator';
|
||||
|
||||
export class CreateTrainDto {
|
||||
@IsString()
|
||||
code!: string;
|
||||
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
capacityTons!: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(Freight.TrainStatus)
|
||||
status?: Freight.TrainStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
notes?: string;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { BaseEntity } from '@edr/api-common';
|
||||
import { Freight } from '@edr/types';
|
||||
import { Column, Entity } from 'typeorm';
|
||||
|
||||
@Entity({ name: 'trains' })
|
||||
export class Train extends BaseEntity {
|
||||
@Column({ name: 'code', type: 'varchar', length: 32, unique: true })
|
||||
code!: string;
|
||||
|
||||
@Column({ name: 'capacity_tons', type: 'numeric', precision: 10, scale: 2 })
|
||||
capacityTons!: number;
|
||||
|
||||
@Column({
|
||||
name: 'status',
|
||||
type: 'enum',
|
||||
enum: Freight.TrainStatus,
|
||||
default: Freight.TrainStatus.Available,
|
||||
})
|
||||
status!: Freight.TrainStatus;
|
||||
|
||||
@Column({ name: 'notes', type: 'text', nullable: true })
|
||||
notes?: string | null;
|
||||
}
|
||||
30
apps/edr-freight-api/src/modules/trains/trains.controller.ts
Normal file
30
apps/edr-freight-api/src/modules/trains/trains.controller.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Body, Controller, Get, Param, ParseUUIDPipe, Post } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import { CreateTrainDto } from './dto/create-train.dto';
|
||||
import { TrainsService } from './trains.service';
|
||||
|
||||
@ApiTags('trains')
|
||||
// @UseGuards(JwtAuthGuard) — TODO: integrate @edr/auth
|
||||
@Controller('trains')
|
||||
export class TrainsController {
|
||||
constructor(private readonly trainsService: TrainsService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Register a new train' })
|
||||
create(@Body() dto: CreateTrainDto) {
|
||||
return this.trainsService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all trains' })
|
||||
findAll() {
|
||||
return this.trainsService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get a train by ID' })
|
||||
findOne(@Param('id', ParseUUIDPipe) id: string) {
|
||||
return this.trainsService.findById(id);
|
||||
}
|
||||
}
|
||||
15
apps/edr-freight-api/src/modules/trains/trains.module.ts
Normal file
15
apps/edr-freight-api/src/modules/trains/trains.module.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
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';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Train])],
|
||||
controllers: [TrainsController],
|
||||
providers: [TrainsService, TrainsRepository],
|
||||
exports: [TrainsService],
|
||||
})
|
||||
export class TrainsModule {}
|
||||
16
apps/edr-freight-api/src/modules/trains/trains.repository.ts
Normal file
16
apps/edr-freight-api/src/modules/trains/trains.repository.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { BaseRepository } from '@edr/api-common';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { Train } from './entities/train.entity';
|
||||
|
||||
@Injectable()
|
||||
export class TrainsRepository extends BaseRepository<Train> {
|
||||
constructor(
|
||||
@InjectRepository(Train)
|
||||
repository: Repository<Train>,
|
||||
) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
29
apps/edr-freight-api/src/modules/trains/trains.service.ts
Normal file
29
apps/edr-freight-api/src/modules/trains/trains.service.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
|
||||
import { CreateTrainDto } from './dto/create-train.dto';
|
||||
import { Train } from './entities/train.entity';
|
||||
import { TrainsRepository } from './trains.repository';
|
||||
|
||||
@Injectable()
|
||||
export class TrainsService {
|
||||
constructor(private readonly trainsRepository: TrainsRepository) {}
|
||||
|
||||
/** Register a new train in the fleet. */
|
||||
create(dto: CreateTrainDto): Promise<Train> {
|
||||
return this.trainsRepository.create(dto);
|
||||
}
|
||||
|
||||
/** List every active train. */
|
||||
findAll(): Promise<Train[]> {
|
||||
return this.trainsRepository.findAll({ 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`);
|
||||
}
|
||||
return train;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user