init
This commit is contained in:
22
backend/src/app.controller.spec.ts
Normal file
22
backend/src/app.controller.spec.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
});
|
||||
12
backend/src/app.controller.ts
Normal file
12
backend/src/app.controller.ts
Normal file
@ -0,0 +1,12 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
32
backend/src/app.module.ts
Normal file
32
backend/src/app.module.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { IdeasModule } from './ideas/ideas.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
}),
|
||||
TypeOrmModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
type: 'postgres',
|
||||
host: configService.get('DB_HOST', 'localhost'),
|
||||
port: configService.get<number>('DB_PORT', 5432),
|
||||
username: configService.get('DB_USERNAME', 'teamplanner'),
|
||||
password: configService.get('DB_PASSWORD', 'teamplanner'),
|
||||
database: configService.get('DB_DATABASE', 'teamplanner'),
|
||||
entities: [__dirname + '/**/*.entity{.ts,.js}'],
|
||||
synchronize: true, // Only for development!
|
||||
}),
|
||||
}),
|
||||
IdeasModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
8
backend/src/app.service.ts
Normal file
8
backend/src/app.service.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
52
backend/src/ideas/dto/create-idea.dto.ts
Normal file
52
backend/src/ideas/dto/create-idea.dto.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsEnum,
|
||||
MaxLength,
|
||||
} from 'class-validator';
|
||||
import { IdeaStatus, IdeaPriority } from '../entities/idea.entity';
|
||||
|
||||
export class CreateIdeaDto {
|
||||
@IsString()
|
||||
@MaxLength(255)
|
||||
title: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(IdeaStatus)
|
||||
status?: IdeaStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(IdeaPriority)
|
||||
priority?: IdeaPriority;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(100)
|
||||
module?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(255)
|
||||
targetAudience?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
pain?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
aiRole?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
verificationMethod?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(20)
|
||||
color?: string;
|
||||
}
|
||||
3
backend/src/ideas/dto/index.ts
Normal file
3
backend/src/ideas/dto/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './create-idea.dto';
|
||||
export * from './update-idea.dto';
|
||||
export * from './query-ideas.dto';
|
||||
39
backend/src/ideas/dto/query-ideas.dto.ts
Normal file
39
backend/src/ideas/dto/query-ideas.dto.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { IsOptional, IsEnum, IsString, IsInt, Min, Max } from 'class-validator';
|
||||
import { IdeaStatus, IdeaPriority } from '../entities/idea.entity';
|
||||
|
||||
export class QueryIdeasDto {
|
||||
@IsOptional()
|
||||
@IsEnum(IdeaStatus)
|
||||
status?: IdeaStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(IdeaPriority)
|
||||
priority?: IdeaPriority;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
module?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
search?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
sortBy?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(['ASC', 'DESC'])
|
||||
sortOrder?: 'ASC' | 'DESC';
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
page?: number = 1;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@Max(100)
|
||||
limit?: number = 20;
|
||||
}
|
||||
10
backend/src/ideas/dto/update-idea.dto.ts
Normal file
10
backend/src/ideas/dto/update-idea.dto.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { IsOptional, IsInt, Min } from 'class-validator';
|
||||
import { CreateIdeaDto } from './create-idea.dto';
|
||||
|
||||
export class UpdateIdeaDto extends PartialType(CreateIdeaDto) {
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
order?: number;
|
||||
}
|
||||
75
backend/src/ideas/entities/idea.entity.ts
Normal file
75
backend/src/ideas/entities/idea.entity.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
export enum IdeaStatus {
|
||||
BACKLOG = 'backlog',
|
||||
TODO = 'todo',
|
||||
IN_PROGRESS = 'in_progress',
|
||||
DONE = 'done',
|
||||
CANCELLED = 'cancelled',
|
||||
}
|
||||
|
||||
export enum IdeaPriority {
|
||||
LOW = 'low',
|
||||
MEDIUM = 'medium',
|
||||
HIGH = 'high',
|
||||
CRITICAL = 'critical',
|
||||
}
|
||||
|
||||
@Entity('ideas')
|
||||
export class Idea {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@Column({ type: 'text', nullable: true })
|
||||
description: string | null;
|
||||
|
||||
@Column({
|
||||
type: 'enum',
|
||||
enum: IdeaStatus,
|
||||
default: IdeaStatus.BACKLOG,
|
||||
})
|
||||
status: IdeaStatus;
|
||||
|
||||
@Column({
|
||||
type: 'enum',
|
||||
enum: IdeaPriority,
|
||||
default: IdeaPriority.MEDIUM,
|
||||
})
|
||||
priority: IdeaPriority;
|
||||
|
||||
@Column({ type: 'varchar', length: 100, nullable: true })
|
||||
module: string | null;
|
||||
|
||||
@Column({ name: 'target_audience', type: 'varchar', length: 255, nullable: true })
|
||||
targetAudience: string | null;
|
||||
|
||||
@Column({ type: 'text', nullable: true })
|
||||
pain: string | null;
|
||||
|
||||
@Column({ name: 'ai_role', type: 'text', nullable: true })
|
||||
aiRole: string | null;
|
||||
|
||||
@Column({ name: 'verification_method', type: 'text', nullable: true })
|
||||
verificationMethod: string | null;
|
||||
|
||||
@Column({ type: 'varchar', length: 20, nullable: true })
|
||||
color: string | null;
|
||||
|
||||
@Column({ type: 'int', default: 0 })
|
||||
order: number;
|
||||
|
||||
@CreateDateColumn({ name: 'created_at' })
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn({ name: 'updated_at' })
|
||||
updatedAt: Date;
|
||||
}
|
||||
51
backend/src/ideas/ideas.controller.ts
Normal file
51
backend/src/ideas/ideas.controller.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
Query,
|
||||
ParseUUIDPipe,
|
||||
} from '@nestjs/common';
|
||||
import { IdeasService } from './ideas.service';
|
||||
import { CreateIdeaDto, UpdateIdeaDto, QueryIdeasDto } from './dto';
|
||||
|
||||
@Controller('ideas')
|
||||
export class IdeasController {
|
||||
constructor(private readonly ideasService: IdeasService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createIdeaDto: CreateIdeaDto) {
|
||||
return this.ideasService.create(createIdeaDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Query() query: QueryIdeasDto) {
|
||||
return this.ideasService.findAll(query);
|
||||
}
|
||||
|
||||
@Get('modules')
|
||||
getModules() {
|
||||
return this.ideasService.getModules();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id', ParseUUIDPipe) id: string) {
|
||||
return this.ideasService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(
|
||||
@Param('id', ParseUUIDPipe) id: string,
|
||||
@Body() updateIdeaDto: UpdateIdeaDto,
|
||||
) {
|
||||
return this.ideasService.update(id, updateIdeaDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id', ParseUUIDPipe) id: string) {
|
||||
return this.ideasService.remove(id);
|
||||
}
|
||||
}
|
||||
13
backend/src/ideas/ideas.module.ts
Normal file
13
backend/src/ideas/ideas.module.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { IdeasService } from './ideas.service';
|
||||
import { IdeasController } from './ideas.controller';
|
||||
import { Idea } from './entities/idea.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Idea])],
|
||||
controllers: [IdeasController],
|
||||
providers: [IdeasService],
|
||||
exports: [IdeasService],
|
||||
})
|
||||
export class IdeasModule {}
|
||||
126
backend/src/ideas/ideas.service.ts
Normal file
126
backend/src/ideas/ideas.service.ts
Normal file
@ -0,0 +1,126 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, Like, FindOptionsWhere } from 'typeorm';
|
||||
import { Idea } from './entities/idea.entity';
|
||||
import { CreateIdeaDto, UpdateIdeaDto, QueryIdeasDto } from './dto';
|
||||
|
||||
@Injectable()
|
||||
export class IdeasService {
|
||||
constructor(
|
||||
@InjectRepository(Idea)
|
||||
private readonly ideasRepository: Repository<Idea>,
|
||||
) {}
|
||||
|
||||
async create(createIdeaDto: CreateIdeaDto): Promise<Idea> {
|
||||
const maxOrder = await this.ideasRepository.maximum('order');
|
||||
const idea = this.ideasRepository.create({
|
||||
...createIdeaDto,
|
||||
order: (maxOrder ?? -1) + 1,
|
||||
});
|
||||
return this.ideasRepository.save(idea);
|
||||
}
|
||||
|
||||
async findAll(query: QueryIdeasDto) {
|
||||
const {
|
||||
status,
|
||||
priority,
|
||||
module,
|
||||
search,
|
||||
sortBy = 'order',
|
||||
sortOrder = 'ASC',
|
||||
page = 1,
|
||||
limit = 20,
|
||||
} = query;
|
||||
|
||||
const where: FindOptionsWhere<Idea> = {};
|
||||
|
||||
if (status) {
|
||||
where.status = status;
|
||||
}
|
||||
|
||||
if (priority) {
|
||||
where.priority = priority;
|
||||
}
|
||||
|
||||
if (module) {
|
||||
where.module = module;
|
||||
}
|
||||
|
||||
const queryBuilder = this.ideasRepository.createQueryBuilder('idea');
|
||||
|
||||
if (status) {
|
||||
queryBuilder.andWhere('idea.status = :status', { status });
|
||||
}
|
||||
|
||||
if (priority) {
|
||||
queryBuilder.andWhere('idea.priority = :priority', { priority });
|
||||
}
|
||||
|
||||
if (module) {
|
||||
queryBuilder.andWhere('idea.module = :module', { module });
|
||||
}
|
||||
|
||||
if (search) {
|
||||
queryBuilder.andWhere(
|
||||
'(idea.title ILIKE :search OR idea.description ILIKE :search)',
|
||||
{ search: `%${search}%` },
|
||||
);
|
||||
}
|
||||
|
||||
const validSortFields = [
|
||||
'order',
|
||||
'title',
|
||||
'status',
|
||||
'priority',
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
];
|
||||
const sortField = validSortFields.includes(sortBy) ? sortBy : 'order';
|
||||
|
||||
queryBuilder
|
||||
.orderBy(`idea.${sortField}`, sortOrder)
|
||||
.skip((page - 1) * limit)
|
||||
.take(limit);
|
||||
|
||||
const [data, total] = await queryBuilder.getManyAndCount();
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<Idea> {
|
||||
const idea = await this.ideasRepository.findOne({ where: { id } });
|
||||
if (!idea) {
|
||||
throw new NotFoundException(`Idea with ID "${id}" not found`);
|
||||
}
|
||||
return idea;
|
||||
}
|
||||
|
||||
async update(id: string, updateIdeaDto: UpdateIdeaDto): Promise<Idea> {
|
||||
const idea = await this.findOne(id);
|
||||
Object.assign(idea, updateIdeaDto);
|
||||
return this.ideasRepository.save(idea);
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
const idea = await this.findOne(id);
|
||||
await this.ideasRepository.remove(idea);
|
||||
}
|
||||
|
||||
async getModules(): Promise<string[]> {
|
||||
const result = await this.ideasRepository
|
||||
.createQueryBuilder('idea')
|
||||
.select('DISTINCT idea.module', 'module')
|
||||
.where('idea.module IS NOT NULL')
|
||||
.getRawMany();
|
||||
|
||||
return result.map((r) => r.module).filter(Boolean);
|
||||
}
|
||||
}
|
||||
32
backend/src/main.ts
Normal file
32
backend/src/main.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
// Global prefix
|
||||
app.setGlobalPrefix('api');
|
||||
|
||||
// CORS
|
||||
app.enableCors({
|
||||
origin: 'http://localhost:4000',
|
||||
credentials: true,
|
||||
});
|
||||
|
||||
// Global validation pipe
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
transform: true,
|
||||
transformOptions: {
|
||||
enableImplicitConversion: true,
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
const port = process.env.PORT ?? 4001;
|
||||
await app.listen(port);
|
||||
console.log(`Backend running on http://localhost:${port}`);
|
||||
}
|
||||
bootstrap();
|
||||
Reference in New Issue
Block a user