mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 22:18:12 +00:00
Project Initialization
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { Passenger } from '@edr/types';
|
||||
import { IsDateString, IsEnum, IsNumber, IsOptional, IsString, IsUUID, Min } from 'class-validator';
|
||||
|
||||
export class CreateScheduleDto {
|
||||
@IsString()
|
||||
trainCode!: string;
|
||||
|
||||
@IsUUID()
|
||||
originStationId!: string;
|
||||
|
||||
@IsUUID()
|
||||
destinationStationId!: string;
|
||||
|
||||
@IsDateString()
|
||||
departureTime!: string;
|
||||
|
||||
@IsDateString()
|
||||
arrivalTime!: string;
|
||||
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
basePrice!: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(Passenger.ScheduleStatus)
|
||||
status?: Passenger.ScheduleStatus;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { BaseEntity } from '@edr/api-common';
|
||||
import { Passenger } from '@edr/types';
|
||||
import { Column, Entity } from 'typeorm';
|
||||
|
||||
@Entity({ name: 'schedules' })
|
||||
export class Schedule extends BaseEntity {
|
||||
@Column({ name: 'train_code', type: 'varchar', length: 32 })
|
||||
trainCode!: string;
|
||||
|
||||
@Column({ name: 'origin_station_id', type: 'uuid' })
|
||||
originStationId!: string;
|
||||
|
||||
@Column({ name: 'destination_station_id', type: 'uuid' })
|
||||
destinationStationId!: string;
|
||||
|
||||
@Column({ name: 'departure_time', type: 'timestamptz' })
|
||||
departureTime!: Date;
|
||||
|
||||
@Column({ name: 'arrival_time', type: 'timestamptz' })
|
||||
arrivalTime!: Date;
|
||||
|
||||
@Column({
|
||||
name: 'status',
|
||||
type: 'enum',
|
||||
enum: Passenger.ScheduleStatus,
|
||||
default: Passenger.ScheduleStatus.Scheduled,
|
||||
})
|
||||
status!: Passenger.ScheduleStatus;
|
||||
|
||||
@Column({ name: 'base_price', type: 'numeric', precision: 10, scale: 2 })
|
||||
basePrice!: number;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Body, Controller, Get, Param, ParseUUIDPipe, Post } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import { CreateScheduleDto } from './dto/create-schedule.dto';
|
||||
import { SchedulesService } from './schedules.service';
|
||||
|
||||
@ApiTags('schedules')
|
||||
// @UseGuards(JwtAuthGuard) — TODO: integrate @edr/auth
|
||||
@Controller('schedules')
|
||||
export class SchedulesController {
|
||||
constructor(private readonly schedulesService: SchedulesService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Publish a new train schedule' })
|
||||
create(@Body() dto: CreateScheduleDto) {
|
||||
return this.schedulesService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all schedules' })
|
||||
findAll() {
|
||||
return this.schedulesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get a schedule by ID' })
|
||||
findOne(@Param('id', ParseUUIDPipe) id: string) {
|
||||
return this.schedulesService.findById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { Schedule } from './entities/schedule.entity';
|
||||
import { SchedulesController } from './schedules.controller';
|
||||
import { SchedulesRepository } from './schedules.repository';
|
||||
import { SchedulesService } from './schedules.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Schedule])],
|
||||
controllers: [SchedulesController],
|
||||
providers: [SchedulesService, SchedulesRepository],
|
||||
exports: [SchedulesService],
|
||||
})
|
||||
export class SchedulesModule {}
|
||||
@@ -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 { Schedule } from './entities/schedule.entity';
|
||||
|
||||
@Injectable()
|
||||
export class SchedulesRepository extends BaseRepository<Schedule> {
|
||||
constructor(
|
||||
@InjectRepository(Schedule)
|
||||
repository: Repository<Schedule>,
|
||||
) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
|
||||
import { CreateScheduleDto } from './dto/create-schedule.dto';
|
||||
import { Schedule } from './entities/schedule.entity';
|
||||
import { SchedulesRepository } from './schedules.repository';
|
||||
|
||||
@Injectable()
|
||||
export class SchedulesService {
|
||||
constructor(private readonly schedulesRepository: SchedulesRepository) {}
|
||||
|
||||
/** Publish a new train schedule. */
|
||||
create(dto: CreateScheduleDto): Promise<Schedule> {
|
||||
return this.schedulesRepository.create({
|
||||
...dto,
|
||||
departureTime: new Date(dto.departureTime),
|
||||
arrivalTime: new Date(dto.arrivalTime),
|
||||
});
|
||||
}
|
||||
|
||||
/** List every published schedule. */
|
||||
findAll(): Promise<Schedule[]> {
|
||||
return this.schedulesRepository.findAll({ order: { departureTime: 'ASC' } });
|
||||
}
|
||||
|
||||
/** Get a single schedule by ID. */
|
||||
async findById(id: string): Promise<Schedule> {
|
||||
const schedule = await this.schedulesRepository.findById(id);
|
||||
if (!schedule) {
|
||||
throw new NotFoundException(`Schedule ${id} not found`);
|
||||
}
|
||||
return schedule;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user