mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 02:58:11 +00:00
Initial commit of edr-passenger-api alpha version
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
import { IsNumber, IsOptional, IsString } from "class-validator";
|
||||
|
||||
export class CreateStationDto {
|
||||
@IsString()
|
||||
code!: string;
|
||||
|
||||
@IsString()
|
||||
name!: string;
|
||||
|
||||
@IsString()
|
||||
city!: string;
|
||||
|
||||
@IsString()
|
||||
country!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
latitude?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
longitude?: number;
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { BaseEntity } from "@edr/api-common";
|
||||
import { Column, Entity } from "typeorm";
|
||||
|
||||
@Entity({ name: "stations" })
|
||||
export class Station extends BaseEntity {
|
||||
@Column({ name: "code", type: "varchar", length: 16, unique: true })
|
||||
code!: string;
|
||||
|
||||
@Column({ name: "name", type: "varchar", length: 128 })
|
||||
name!: string;
|
||||
|
||||
@Column({ name: "city", type: "varchar", length: 128 })
|
||||
city!: string;
|
||||
|
||||
@Column({ name: "country", type: "varchar", length: 64 })
|
||||
country!: string;
|
||||
|
||||
@Column({
|
||||
name: "latitude",
|
||||
type: "numeric",
|
||||
precision: 9,
|
||||
scale: 6,
|
||||
nullable: true,
|
||||
})
|
||||
latitude?: number | null;
|
||||
|
||||
@Column({
|
||||
name: "longitude",
|
||||
type: "numeric",
|
||||
precision: 9,
|
||||
scale: 6,
|
||||
nullable: true,
|
||||
})
|
||||
longitude?: number | null;
|
||||
}
|
||||
@@ -1,37 +1,15 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
ParseUUIDPipe,
|
||||
Post,
|
||||
} from "@nestjs/common";
|
||||
import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
||||
import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { StationsService } from './stations.service';
|
||||
import { CreateStationDto } from './stations.dto';
|
||||
import { JwtGuard } from '../../common/jwt.guard';
|
||||
|
||||
import { CreateStationDto } from "./dto/create-station.dto";
|
||||
import { StationsService } from "./stations.service";
|
||||
|
||||
@ApiTags("stations")
|
||||
// @UseGuards(JwtAuthGuard) — TODO: integrate @edr/auth
|
||||
@Controller("stations")
|
||||
@ApiTags('Stations')
|
||||
@Controller('stations')
|
||||
export class StationsController {
|
||||
constructor(private readonly stationsService: StationsService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: "Register a new station" })
|
||||
create(@Body() dto: CreateStationDto) {
|
||||
return this.stationsService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: "List all stations" })
|
||||
findAll() {
|
||||
return this.stationsService.findAll();
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
@ApiOperation({ summary: "Get a station by ID" })
|
||||
findOne(@Param("id", ParseUUIDPipe) id: string) {
|
||||
return this.stationsService.findById(id);
|
||||
}
|
||||
constructor(private service: StationsService) {}
|
||||
@Get() @ApiOperation({ summary: 'List all stations' }) findAll() { return this.service.findAll(); }
|
||||
@Get(':id') @ApiOperation({ summary: 'Get station by ID' }) findOne(@Param('id') id: string) { return this.service.findOne(id); }
|
||||
@Post() @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') @ApiOperation({ summary: 'Create station' })
|
||||
create(@Body() dto: CreateStationDto) { return this.service.create(dto); }
|
||||
}
|
||||
|
||||
11
apps/edr-passenger-api/src/modules/stations/stations.dto.ts
Normal file
11
apps/edr-passenger-api/src/modules/stations/stations.dto.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { IsString, IsNumber, IsOptional } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class CreateStationDto {
|
||||
@ApiProperty({ example: 'ADD' }) @IsString() code: string;
|
||||
@ApiProperty({ example: 'Addis Ababa' }) @IsString() name: string;
|
||||
@ApiProperty({ example: 'Addis Ababa' }) @IsString() city: string;
|
||||
@ApiPropertyOptional() @IsOptional() @IsString() timezone?: string;
|
||||
@ApiProperty({ example: 9.0054 }) @IsNumber() lat: number;
|
||||
@ApiProperty({ example: 38.7636 }) @IsNumber() lng: number;
|
||||
}
|
||||
@@ -1,14 +1,6 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { Module } from '@nestjs/common';
|
||||
import { StationsController } from './stations.controller';
|
||||
import { StationsService } from './stations.service';
|
||||
|
||||
import { Station } from "./entities/station.entity";
|
||||
import { StationsController } from "./stations.controller";
|
||||
import { StationsService } from "./stations.service";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Station])],
|
||||
controllers: [StationsController],
|
||||
providers: [StationsService],
|
||||
exports: [StationsService],
|
||||
})
|
||||
@Module({ controllers: [StationsController], providers: [StationsService], exports: [StationsService] })
|
||||
export class StationsModule {}
|
||||
|
||||
@@ -1,34 +1,15 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
|
||||
import { CreateStationDto } from "./dto/create-station.dto";
|
||||
import { Station } from "./entities/station.entity";
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../../common/prisma.service';
|
||||
import { CreateStationDto } from './stations.dto';
|
||||
|
||||
@Injectable()
|
||||
export class StationsService {
|
||||
constructor(
|
||||
@InjectRepository(Station)
|
||||
private readonly stationsRepository: Repository<Station>,
|
||||
) {}
|
||||
|
||||
/** Register a new station. */
|
||||
create(dto: CreateStationDto): Promise<Station> {
|
||||
const entity = this.stationsRepository.create(dto);
|
||||
return this.stationsRepository.save(entity);
|
||||
}
|
||||
|
||||
/** List every station (alphabetical). */
|
||||
findAll(): Promise<Station[]> {
|
||||
return this.stationsRepository.find({ order: { name: "ASC" } });
|
||||
}
|
||||
|
||||
/** Get a single station by ID. */
|
||||
async findById(id: string): Promise<Station> {
|
||||
const station = await this.stationsRepository.findOne({ where: { id } });
|
||||
if (!station) {
|
||||
throw new NotFoundException(`Station ${id} not found`);
|
||||
}
|
||||
return station;
|
||||
constructor(private prisma: PrismaService) {}
|
||||
findAll() { return this.prisma.station.findMany({ orderBy: { name: 'asc' } }); }
|
||||
async findOne(id: string) {
|
||||
const s = await this.prisma.station.findUnique({ where: { id } });
|
||||
if (!s) throw new NotFoundException('Station not found');
|
||||
return s;
|
||||
}
|
||||
create(dto: CreateStationDto) { return this.prisma.station.create({ data: dto }); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user