Archived
issue entity + remove template code
This commit is contained in:
14 files changed
+4919
-103
No files matched your search
@@ -17,9 +17,15 @@
|
||||
"dependencies": {
|
||||
"@nestjs/common": "^9.0.0",
|
||||
"@nestjs/core": "^9.0.0",
|
||||
"@nestjs/mapped-types": "*",
|
||||
"@nestjs/platform-express": "^9.0.0",
|
||||
"@nestjs/typeorm": "^9.0.1",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.0",
|
||||
"pg": "^8.11.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rxjs": "^7.2.0"
|
||||
"rxjs": "^7.2.0",
|
||||
"typeorm": "^0.3.16"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^9.0.0",
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,20 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { IssuesModule } from './issues/issues.module';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
imports: [
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'postgres',
|
||||
host: 'postgres',
|
||||
port: 5432,
|
||||
username: 'admin',
|
||||
password: 'admin!',
|
||||
database: 'city_tracker_db',
|
||||
autoLoadEntities: true,
|
||||
synchronize: process.env.NODE_ENV !== 'production',
|
||||
}),
|
||||
IssuesModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { IsNumber, IsString, MaxLength, MinLength } from 'class-validator';
|
||||
|
||||
export class CreateIssueDto {
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
@MaxLength(32)
|
||||
title: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(10)
|
||||
@MaxLength(255)
|
||||
description: string;
|
||||
|
||||
@IsNumber()
|
||||
latitude: number;
|
||||
|
||||
@IsNumber()
|
||||
longitude: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateIssueDto } from './create-issue.dto';
|
||||
|
||||
export class UpdateIssueDto extends PartialType(CreateIssueDto) {}
|
||||
@@ -0,0 +1,5 @@
|
||||
export enum IssueStatus {
|
||||
new = 'NEW',
|
||||
inProgress = 'IN_PROGRESS',
|
||||
done = 'DONE',
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
import { IssueStatus } from './issue-status.enum';
|
||||
|
||||
@Entity('issues')
|
||||
export class Issue {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@Column()
|
||||
description: string;
|
||||
|
||||
@Column()
|
||||
latitude: number;
|
||||
|
||||
@Column()
|
||||
longitude: string;
|
||||
|
||||
@Column({ type: 'enum', enum: IssueStatus, default: IssueStatus.new })
|
||||
status: IssueStatus;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
toDto() {
|
||||
return {
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
status: this.status,
|
||||
description: this.description,
|
||||
latitude: this.latitude,
|
||||
longitude: this.longitude,
|
||||
createdAt: this.createdAt,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { IssuesService } from './issues.service';
|
||||
import { CreateIssueDto } from './dto/create-issue.dto';
|
||||
import { UpdateIssueDto } from './dto/update-issue.dto';
|
||||
|
||||
@Controller('issues')
|
||||
export class IssuesController {
|
||||
constructor(private readonly issuesService: IssuesService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createIssueDto: CreateIssueDto) {
|
||||
return this.issuesService.create(createIssueDto).then((i) => i.toDto());
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Param('direction') direction: string) {
|
||||
return this.issuesService
|
||||
.findAll(
|
||||
direction.toLowerCase() === 'desc' ||
|
||||
direction.toLowerCase() === 'descending',
|
||||
)
|
||||
.then((r) => r.map((i) => i.toDto()));
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.issuesService.findOne(id).then((i) => i.toDto());
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateIssueDto: UpdateIssueDto) {
|
||||
return this.issuesService.update(id, updateIssueDto).then((i) => i.toDto());
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
remove(@Param('id') id: string) {
|
||||
return this.issuesService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { IssuesService } from './issues.service';
|
||||
import { IssuesController } from './issues.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Issue } from './entities/issue.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Issue])],
|
||||
controllers: [IssuesController],
|
||||
providers: [IssuesService],
|
||||
exports: [IssuesService],
|
||||
})
|
||||
export class IssuesModule {}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateIssueDto } from './dto/create-issue.dto';
|
||||
import { UpdateIssueDto } from './dto/update-issue.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Issue } from './entities/issue.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class IssuesService {
|
||||
constructor(
|
||||
@InjectRepository(Issue) private readonly repository: Repository<Issue>,
|
||||
) {}
|
||||
|
||||
async create(createIssueDto: CreateIssueDto) {
|
||||
const issue = this.repository.create({ ...createIssueDto });
|
||||
return this.repository.save(issue);
|
||||
}
|
||||
|
||||
async findAll(descending: boolean) {
|
||||
return this.repository.find({
|
||||
order: { createdAt: descending ? 'DESC' : 'ASC' },
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const ent = this.repository.findOneBy({ id });
|
||||
if (ent === null) {
|
||||
throw new NotFoundException('Issue not found');
|
||||
}
|
||||
return ent;
|
||||
}
|
||||
|
||||
async update(id: string, updateIssueDto: UpdateIssueDto) {
|
||||
const ent = await this.findOne(id);
|
||||
this.repository.merge(ent, updateIssueDto);
|
||||
return this.repository.save(ent);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const ent = await this.findOne(id);
|
||||
return this.repository.remove(ent);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,16 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
transformOptions: { enableImplicitConversion: false },
|
||||
whitelist: true,
|
||||
}),
|
||||
);
|
||||
await app.listen(3000);
|
||||
}
|
||||
bootstrap();
|
||||
Reference in new issue
Block a user