Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks entity #7

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"@nestjs/core": "^10.3.8",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.3.8",
"@nestjs/swagger": "^7.4.2",
"@prisma/client": "^5.13.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"nestjs-prisma": "^0.23.0",
"reflect-metadata": "^0.2.2",
"rimraf": "^5.0.5",
Expand Down
50 changes: 50 additions & 0 deletions apps/backend/prisma/migrations/20241011172602_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- CreateEnum
CREATE TYPE "Role" AS ENUM ('ADMIN', 'USER');

-- AlterTable
ALTER TABLE "User" ADD COLUMN "role" "Role" NOT NULL DEFAULT 'USER';

-- CreateTable
CREATE TABLE "Projekt" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Projekt_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Sprint" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"startDate" TIMESTAMP(3) NOT NULL,
"endDate" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Sprint_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Tasks" (
"id" SERIAL NOT NULL,
"description" TEXT NOT NULL,
"sprintId" INTEGER NOT NULL,
"userId" INTEGER NOT NULL,
"projektId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Tasks_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "Projekt" ADD CONSTRAINT "Projekt_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Tasks" ADD CONSTRAINT "Tasks_sprintId_fkey" FOREIGN KEY ("sprintId") REFERENCES "Sprint"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Tasks" ADD CONSTRAINT "Tasks_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Tasks" ADD CONSTRAINT "Tasks_projektId_fkey" FOREIGN KEY ("projektId") REFERENCES "Projekt"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
22 changes: 22 additions & 0 deletions apps/backend/prisma/migrations/20241011184645_test/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- DropForeignKey
ALTER TABLE "Tasks" DROP CONSTRAINT "Tasks_projektId_fkey";

-- DropForeignKey
ALTER TABLE "Tasks" DROP CONSTRAINT "Tasks_sprintId_fkey";

-- DropForeignKey
ALTER TABLE "Tasks" DROP CONSTRAINT "Tasks_userId_fkey";

-- AlterTable
ALTER TABLE "Tasks" ALTER COLUMN "sprintId" DROP NOT NULL,
ALTER COLUMN "userId" DROP NOT NULL,
ALTER COLUMN "projektId" DROP NOT NULL;

-- AddForeignKey
ALTER TABLE "Tasks" ADD CONSTRAINT "Tasks_sprintId_fkey" FOREIGN KEY ("sprintId") REFERENCES "Sprint"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Tasks" ADD CONSTRAINT "Tasks_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Tasks" ADD CONSTRAINT "Tasks_projektId_fkey" FOREIGN KEY ("projektId") REFERENCES "Projekt"("id") ON DELETE SET NULL ON UPDATE CASCADE;
8 changes: 4 additions & 4 deletions apps/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ model Tasks {
description String
sprintId Int
sprint Sprint @relation(fields: [sprintId], references: [id])
userId Int
user User @relation(fields: [userId], references: [id])
projektId Int
projekt Projekt @relation(fields: [projektId], references: [id])
userId Int?
user User? @relation(fields: [userId], references: [id])
projektId Int?
projekt Projekt? @relation(fields: [projektId], references: [id])
createdAt DateTime @default(now())
}
3 changes: 2 additions & 1 deletion apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { PrismaModule } from 'nestjs-prisma';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SprintModule } from './sprint/sprint.module';
import { TasksModule } from './tasks/tasks.module';
import { UserModule } from './user/user.module';

@Module({
imports: [ConfigModule.forRoot(), PrismaModule.forRoot({ isGlobal: true }), UserModule, SprintModule],
imports: [ConfigModule.forRoot(), PrismaModule.forRoot({ isGlobal: true }), UserModule, SprintModule, TasksModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
5 changes: 5 additions & 0 deletions apps/backend/src/tasks/dto/create-task.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { OmitType } from '@nestjs/mapped-types';

import { Tasks } from '../entities/task.entity';

export class CreateTaskDto extends OmitType(Tasks, ['id'] as const) {}
5 changes: 5 additions & 0 deletions apps/backend/src/tasks/dto/update-task.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PartialType } from '@nestjs/mapped-types';

import { CreateTaskDto } from './create-task.dto';

export class UpdateTaskDto extends PartialType(CreateTaskDto) {}
22 changes: 22 additions & 0 deletions apps/backend/src/tasks/entities/task.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IsDate, IsInt, IsOptional, IsString } from 'class-validator';

export class Tasks {
@IsInt()
id: number;

@IsString()
@IsOptional()
description: string;

@IsInt()
sprintId: number;

@IsInt()
userId: number;

@IsInt()
projectId: number;

@IsDate()
createdAt: Date;
}
35 changes: 35 additions & 0 deletions apps/backend/src/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';

import { CreateTaskDto } from './dto/create-task.dto';
import { UpdateTaskDto } from './dto/update-task.dto';
import { TasksService } from './tasks.service';

@Controller('tasks')
export class TasksController {
constructor(private readonly tasksService: TasksService) {}

@Post()
create(@Body() createTaskDto: CreateTaskDto) {
return this.tasksService.create(createTaskDto);
}

@Get()
findAll() {
return this.tasksService.findAll();
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.tasksService.findOne(Number(id));
}

@Patch(':id')
update(@Param('id') id: string, @Body() updateTaskDto: UpdateTaskDto) {
return this.tasksService.update(Number(id), updateTaskDto);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.tasksService.remove(Number(id));
}
}
10 changes: 10 additions & 0 deletions apps/backend/src/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';

import { TasksController } from './tasks.controller';
import { TasksService } from './tasks.service';

@Module({
controllers: [TasksController],
providers: [TasksService],
})
export class TasksModule {}
53 changes: 53 additions & 0 deletions apps/backend/src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from 'nestjs-prisma';

import { CreateTaskDto } from './dto/create-task.dto';
import { UpdateTaskDto } from './dto/update-task.dto';

@Injectable()
export class TasksService {
constructor(private readonly prisma: PrismaService) {}

async create(data: CreateTaskDto) {
return await this.prisma.tasks.create({ data });
}

async findAll() {
try {
return await this.prisma.tasks.findMany();
} catch {
throw new NotFoundException('Tasks not found');
}
}

async findOne(id: number) {
const task = await this.prisma.tasks.findUnique({
where: { id },
});
if (!task) {
throw new NotFoundException(`Task with ID ${id} not found`);
}
return task;
}

async update(id: number, updateTaskDto: UpdateTaskDto) {
try {
return await this.prisma.tasks.update({
where: { id },
data: updateTaskDto,
});
} catch {
throw new NotFoundException(`Task with ID ${id} not found`);
}
}

async remove(id: number) {
try {
return await this.prisma.tasks.delete({
where: { id },
});
} catch {
throw new NotFoundException(`Task with ID ${id} not found`);
}
}
}
Loading
Loading