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

Feature/event validation #19

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
"dependencies": {
"@nestjs/common": "^10.3.8",
"@nestjs/core": "^10.3.8",
"@nestjs/mapped-types": "*",
"@nestjs/mapped-types": "2.0.5",
"@nestjs/platform-express": "^10.3.8",
"@nestjs/swagger": "^7.3.1",
"@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
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ ALTER TABLE IF EXISTS "Event"
ADD CONSTRAINT only_one_owner
CHECK (
("ownerUserId" IS NOT NULL AND "ownerGroupId" IS NULL) OR
("ownerUserId" IS NULL AND "ownerGroupId" IS NOT NULL)
("ownerUserId" IS NULL AND "ownerGroupId" IS NOT NULL) OR
("ownerUserId" IS NULL AND "ownerGroupId" IS NULL)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- DropForeignKey
ALTER TABLE "Event" DROP CONSTRAINT "Event_categoryId_fkey";

-- AlterTable
ALTER TABLE "Event" ALTER COLUMN "categoryId" DROP NOT NULL;

-- AddForeignKey
ALTER TABLE "Event" ADD CONSTRAINT "Event_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "Category"("id") ON DELETE SET NULL ON UPDATE CASCADE;
6 changes: 3 additions & 3 deletions apps/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ generator client {

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
url = "postgresql://postgres:postgres@localhost:5432/mydb?schema=public"
}

enum Priority {
Expand Down Expand Up @@ -71,8 +71,8 @@ model Event {
endTime DateTime? @db.Time
priority Priority @default(MEDIUM)
status Status @default(NOT_STARTED)
categoryId Int
category Category @relation(fields: [categoryId], references: [id])
categoryId Int?
category Category? @relation(fields: [categoryId], references: [id])
ownerUserId Int?
ownerUser User? @relation(fields: [ownerUserId], references: [id], onDelete: Cascade)
ownerGroupId Int?
Expand Down
5 changes: 5 additions & 0 deletions apps/backend/src/event/dto/create-event.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { OmitType } from '@nestjs/swagger';

import { Event } from '../entities/event.entity';

export class CreateEventDto extends OmitType(Event, ['id']) {}
5 changes: 5 additions & 0 deletions apps/backend/src/event/dto/update-event.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PartialType } from '@nestjs/swagger';

import { CreateEventDto } from './create-event.dto';

export class UpdateEventDto extends PartialType(CreateEventDto) {}
52 changes: 52 additions & 0 deletions apps/backend/src/event/entities/event.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Priority, Status } from '@prisma/client';
import { IsDate, IsDateString, IsEnum, IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';

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

@IsString()
@IsNotEmpty()
name: string;

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

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

@IsDate()
startDate: Date;

@IsDate()
@IsOptional()
endDate: Date;

@IsDateString()
@IsOptional()
startTime: Date;

@IsDateString()
@IsOptional()
endTime: Date;

@IsEnum(Priority)
priority: Priority;

@IsEnum(Status)
status: Status;

@IsInt()
@IsOptional()
categoryId: number;

@IsInt()
@IsOptional()
ownerUserId: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional


@IsInt()
@IsOptional()
ownerGroupId: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional

}
35 changes: 35 additions & 0 deletions apps/backend/src/event/event.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 { CreateEventDto } from './dto/create-event.dto';
import { UpdateEventDto } from './dto/update-event.dto';
import { EventService } from './event.service';

@Controller('event')
export class EventController {
constructor(private readonly eventService: EventService) {}

@Post()
create(@Body() data: CreateEventDto) {
return this.eventService.create(data);
}

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

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

@Patch(':id')
update(@Param('id') id: string, @Body() data: UpdateEventDto) {
return this.eventService.update(Number(id), data);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.eventService.remove(Number(id));
}
}
51 changes: 51 additions & 0 deletions apps/backend/src/event/event.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from 'nestjs-prisma';

import { CreateEventDto } from './dto/create-event.dto';
import { UpdateEventDto } from './dto/update-event.dto';
import { Event } from './entities/event.entity';

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

async create(data: CreateEventDto): Promise<Event> {
return await this.prisma.event.create({ data });
}

async findAll(): Promise<Event[]> {
try {
const res = await this.prisma.event.findMany();
if (!res) throw new Error();
return res;
} catch (error) {
throw new NotFoundException('Events not found');
}
}

async findOne(id: number): Promise<Event> {
try {
const res = await this.prisma.event.findUnique({ where: { id: id } });
if (!res) throw new Error();
return res;
} catch (e) {
throw new NotFoundException(`Event with ID ${id} not found`);
}
}

async update(id: number, data: UpdateEventDto): Promise<Event> {
try {
return await this.prisma.event.update({ where: { id: id }, data });
} catch (e) {
throw new NotFoundException(`Event with ID ${id} not found`);
}
}

async remove(id: number): Promise<Event> {
try {
return await this.prisma.event.delete({ where: { id: id } });
} catch (e) {
throw new NotFoundException(`Event with ID ${id} not found`);
}
}
}
60 changes: 51 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,11 @@ ansi-styles@^4.0.0, ansi-styles@^4.1.0:
dependencies:
color-convert "^2.0.1"

ansi-styles@^6.1.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.2.1.tgz#0e62320cf99c21afff3b3012192546aacbfb05c5"
integrity sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==

any-promise@^1.0.0:
version "1.3.0"
resolved "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz"
Expand Down Expand Up @@ -1668,12 +1673,12 @@ chrome-trace-event@^1.0.2:

class-transformer@^0.5.1:
version "0.5.1"
resolved "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz"
resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.5.1.tgz#24147d5dffd2a6cea930a3250a677addf96ab336"
integrity sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==

class-validator@^0.14.1:
version "0.14.1"
resolved "https://registry.npmjs.org/class-validator/-/class-validator-0.14.1.tgz"
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.14.1.tgz#ff2411ed8134e9d76acfeb14872884448be98110"
integrity sha512-2VEG9JICxIqTpoK1eMzZqaV+u/EiwEJkMGzTrZf6sU/fwsnOITVgYJ8yojSy6CaXtO9V0Cc6ZQZ8h8m4UBuLwQ==
dependencies:
"@types/validator" "^13.11.8"
Expand Down Expand Up @@ -2044,6 +2049,11 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

eastasianwidth@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==

[email protected]:
version "1.1.1"
resolved "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz"
Expand Down Expand Up @@ -3565,7 +3575,7 @@ levn@^0.4.1:

libphonenumber-js@^1.10.53:
version "1.11.2"
resolved "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.11.2.tgz"
resolved "https://registry.yarnpkg.com/libphonenumber-js/-/libphonenumber-js-1.11.2.tgz#9ddd7d1a1e1be0e7c596c7e09487c362b4f1210c"
integrity sha512-V9mGLlaXN1WETzqQvSu6qf6XVAr3nFuJvWsHcuzCCCo6xUKawwSxOPTpan5CGOSKTn5w/bQuCZcLPJkyysgC3w==

lilconfig@^2.1.0:
Expand Down Expand Up @@ -3872,6 +3882,11 @@ nestjs-prisma@^0.23.0:
"@angular-devkit/schematics" "^13.3.0"
"@schematics/angular" "~13.3.0"

nestjs@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/nestjs/-/nestjs-0.0.1.tgz#e4b3b82fbbd0bbf993a7c7a137b3ad59ccecf6b6"
integrity sha512-LJTf2zDRtWE69B6R2DUW6/LCftgVOq3kp3aqxPsB0eei8OX+lW2RFjzREZWRL1GUD/jGdBSMhM3Q5YWLC2uHNg==

[email protected]:
version "14.2.10"
resolved "https://registry.yarnpkg.com/next/-/next-14.2.10.tgz#331981a4fecb1ae8af1817d4db98fc9687ee1cb6"
Expand Down Expand Up @@ -4890,7 +4905,7 @@ streamsearch@^1.1.0:
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@4.1.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3, string-width@^5.1.2:
string-width@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.1.0.tgz#ba846d1daa97c3c596155308063e075ed1c99aff"
integrity sha512-NrX+1dVVh+6Y9dnQ19pR0pP4FiEIlUvdTGn8pw6CKTNq5sgib2nIhmUNT5TAmhWmvKr3WcxBcP3E8nWezuipuQ==
Expand All @@ -4899,6 +4914,24 @@ [email protected], string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.
is-fullwidth-code-point "^3.0.0"
strip-ansi "^5.2.0"

string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^5.0.1, string-width@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794"
integrity sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==
dependencies:
eastasianwidth "^0.2.0"
emoji-regex "^9.2.2"
strip-ansi "^7.0.1"

string.prototype.matchall@^4.0.10:
version "4.0.11"
resolved "https://registry.npmjs.org/string.prototype.matchall/-/string.prototype.matchall-4.0.11.tgz"
Expand Down Expand Up @@ -5449,7 +5482,7 @@ v8-compile-cache-lib@^3.0.1:

validator@^13.9.0:
version "13.12.0"
resolved "https://registry.npmjs.org/validator/-/validator-13.12.0.tgz"
resolved "https://registry.yarnpkg.com/validator/-/validator-13.12.0.tgz#7d78e76ba85504da3fee4fd1922b385914d4b35f"
integrity sha512-c1Q0mCiPlgdTVVVIJIrBuxNicYE+t/7oKeI9MWLj3fh/uq2Pxh/3eeWbVZ4OcGW1TUf53At0njHw5SMdA3tmMg==

vary@^1, vary@~1.1.2:
Expand Down Expand Up @@ -5598,15 +5631,24 @@ which@^2.0.1:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@7.0.0, wrap-ansi@^6.0.1, wrap-ansi@^6.2.0, wrap-ansi@^8.1.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-6.2.0.tgz#e9393ba07102e6c91a3b221478f0257cd2856e53"
integrity sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
integrity sha512-si7QWI6zUMq56bESFvagtmzMdGOtoxfR+Sez11Mobfc7tm+VkUckk9bW2UeffTGVUbOksxmSw0AA2gs8g71NCQ==
dependencies:
ansi-styles "^6.1.0"
string-width "^5.0.1"
strip-ansi "^7.0.1"

wrappy@1:
version "1.0.2"
resolved "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz"
Expand Down