Skip to content

Commit 0e6f0cf

Browse files
authored
Mn 1 2 create task (#63)
* setup & create task, category * fixed pr review comments * Update data-source.ts
1 parent c45ac39 commit 0e6f0cf

File tree

12 files changed

+277
-100
lines changed

12 files changed

+277
-100
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ Thumbs.db
4141
# Environment file
4242
*.env
4343
*.env.*
44+
!example.env

apps/backend/src/app.module.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,11 @@ import { TypeOrmModule } from '@nestjs/typeorm';
33

44
import { AppController } from './app.controller';
55
import { AppService } from './app.service';
6-
import { UsersModule } from './users/users.module';
7-
import { AuthModule } from './auth/auth.module';
8-
import { PluralNamingStrategy } from './strategies/plural-naming.strategy';
6+
import { TaskModule } from './task/task.module';
7+
import AppDataSource from './data-source';
98

109
@Module({
11-
imports: [
12-
TypeOrmModule.forRoot({
13-
type: 'mongodb',
14-
host: '127.0.0.1',
15-
port: 27017,
16-
database: 'c4cOpsTest',
17-
// username: 'root',
18-
// password: 'root',
19-
autoLoadEntities: true,
20-
// entities: [join(__dirname, '**/**.entity.{ts,js}')],
21-
// Setting synchronize: true shouldn't be used in production - otherwise you can lose production data
22-
synchronize: true,
23-
namingStrategy: new PluralNamingStrategy(),
24-
}),
25-
UsersModule,
26-
AuthModule,
27-
],
10+
imports: [TypeOrmModule.forRoot(AppDataSource.options), TaskModule],
2811
controllers: [AppController],
2912
providers: [AppService],
3013
})

apps/backend/src/data-source.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { DataSource } from 'typeorm';
2+
import { PluralNamingStrategy } from './strategies/plural-naming.strategy';
3+
import { Task } from './task/types/task.entity';
4+
import * as dotenv from 'dotenv';
5+
6+
dotenv.config();
7+
8+
const AppDataSource = new DataSource({
9+
type: 'postgres',
10+
host: process.env.NX_DB_HOST,
11+
port: parseInt(process.env.NX_DB_PORT as string, 10),
12+
username: process.env.NX_DB_USERNAME,
13+
password: process.env.NX_DB_PASSWORD,
14+
database: process.env.NX_DB_DATABASE,
15+
entities: [Task],
16+
migrations: ['apps/backend/src/migrations/*.js'],
17+
// Setting synchronize: true shouldn't be used in production - otherwise you can lose production data
18+
synchronize: false,
19+
namingStrategy: new PluralNamingStrategy(),
20+
});
21+
22+
export default AppDataSource;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class AddTask1754254886189 implements MigrationInterface {
4+
name = 'AddTask1754254886189';
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TYPE "public"."tasks_category_enum" AS ENUM('Draft', 'To Do', 'In Progress', 'Completed')`,
9+
);
10+
await queryRunner.query(
11+
`CREATE TABLE "task" ("id" SERIAL NOT NULL, "title" character varying NOT NULL, "description" character varying, "dateCreated" TIMESTAMP NOT NULL DEFAULT now(), "dueDate" TIMESTAMP, "labels" jsonb NOT NULL DEFAULT '[]', "category" "public"."tasks_category_enum" NOT NULL DEFAULT 'Draft', CONSTRAINT "PK_8d12ff38fcc62aaba2cab748772" PRIMARY KEY ("id"))`,
12+
);
13+
}
14+
15+
public async down(queryRunner: QueryRunner): Promise<void> {
16+
await queryRunner.query(`DROP TABLE "task"`);
17+
await queryRunner.query(`DROP TYPE "public"."tasks_category_enum"`);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
Controller,
3+
Get,
4+
Post,
5+
Body,
6+
Patch,
7+
Param,
8+
Delete,
9+
Query,
10+
} from '@nestjs/common';
11+
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
12+
import { TasksService } from './task.service';
13+
import { Task } from './types/task.entity';
14+
15+
@ApiTags('tasks')
16+
@Controller('tasks')
17+
export class TasksController {
18+
constructor(private readonly tasksService: TasksService) {}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
3+
import { TasksController } from './task.controller';
4+
import { TasksService } from './task.service';
5+
import { Task } from './types/task.entity';
6+
7+
@Module({
8+
imports: [TypeOrmModule.forFeature([Task])],
9+
controllers: [TasksController],
10+
providers: [TasksService],
11+
exports: [TasksService],
12+
})
13+
export class TaskModule {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Task } from './types/task.entity';
2+
import { Injectable } from '@nestjs/common';
3+
import { InjectRepository } from '@nestjs/typeorm';
4+
import { Repository } from 'typeorm';
5+
6+
@Injectable()
7+
export class TasksService {
8+
constructor(
9+
@InjectRepository(Task)
10+
private readonly taskRepository: Repository<Task>,
11+
) {}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export enum TaskCategory {
2+
DRAFT = 'Draft',
3+
TODO = 'To Do',
4+
IN_PROGRESS = 'In Progress',
5+
COMPLETED = 'Completed',
6+
}
7+
8+
export const CATEGORY_DESCRIPTIONS: Record<TaskCategory, string> = {
9+
[TaskCategory.DRAFT]: 'Tasks that are still being planned or outlined',
10+
[TaskCategory.TODO]: 'Tasks that are ready to be worked on',
11+
[TaskCategory.IN_PROGRESS]: 'Tasks that are currently being worked on',
12+
[TaskCategory.COMPLETED]: 'Tasks that have been finished',
13+
};
14+
15+
export const CATEGORY_COLORS: Record<TaskCategory, string> = {
16+
[TaskCategory.DRAFT]: '#6B7280', // Gray
17+
[TaskCategory.TODO]: '#3B82F6', // Blue
18+
[TaskCategory.IN_PROGRESS]: '#F59E0B', // Orange
19+
[TaskCategory.COMPLETED]: '#10B981', // Green
20+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
Entity,
3+
PrimaryGeneratedColumn,
4+
Column,
5+
CreateDateColumn,
6+
} from 'typeorm';
7+
import { TaskCategory } from './category'; // Adjust path as needed
8+
9+
export interface Label {
10+
id: string;
11+
name: string;
12+
color?: string;
13+
}
14+
15+
@Entity('tasks')
16+
export class Task {
17+
@PrimaryGeneratedColumn()
18+
id: number;
19+
20+
@Column()
21+
title: string;
22+
23+
@Column({ nullable: true })
24+
description: string;
25+
26+
@CreateDateColumn()
27+
dateCreated: Date;
28+
29+
@Column({ nullable: true })
30+
dueDate?: Date;
31+
32+
@Column('jsonb', { default: () => "'[]'" })
33+
labels: Label[];
34+
35+
@Column({
36+
type: 'enum',
37+
enum: TaskCategory,
38+
default: TaskCategory.DRAFT,
39+
})
40+
category: TaskCategory;
41+
}

example.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
NX_DB_HOST=localhost,
2+
NX_DB_USERNAME=postgres,
3+
NX_DB_PASSWORD=,
4+
NX_DB_DATABASE=jumpstart,
5+
NX_DB_PORT=5432,

0 commit comments

Comments
 (0)