From 02beb669107f022cf6bd6cf072cf499bbc92914c Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Mon, 10 Oct 2022 17:58:42 +0200 Subject: [PATCH] fix: dto returns --- backend/src/auth/auth.controller.ts | 5 +++-- backend/src/auth/auth.service.ts | 4 ++-- backend/src/auth/dto/authRegister.dto.ts | 4 ++-- backend/src/auth/dto/authSignIn.dto.ts | 4 ++-- backend/src/file/dto/file.dto.ts | 2 +- backend/src/file/file.controller.ts | 3 ++- backend/src/share/dto/share.dto.ts | 6 +++--- backend/src/{auth/dto/auth.dto.ts => user/dto/user.dto.ts} | 6 +++--- backend/src/user/user.controller.ts | 3 ++- 9 files changed, 20 insertions(+), 17 deletions(-) rename backend/src/{auth/dto/auth.dto.ts => user/dto/user.dto.ts} (72%) diff --git a/backend/src/auth/auth.controller.ts b/backend/src/auth/auth.controller.ts index ff4fe072..0cf41437 100644 --- a/backend/src/auth/auth.controller.ts +++ b/backend/src/auth/auth.controller.ts @@ -6,10 +6,11 @@ import { Post, } from "@nestjs/common"; import { ConfigService } from "@nestjs/config"; +import { UserDTO } from "src/user/dto/user.dto"; import { AuthService } from "./auth.service"; -import { AuthDTO } from "./dto/auth.dto"; import { AuthRegisterDTO } from "./dto/authRegister.dto"; +import { AuthSignInDTO } from "./dto/authSignIn.dto"; import { RefreshAccessTokenDTO } from "./dto/refreshAccessToken.dto"; @Controller("auth") @@ -27,7 +28,7 @@ export class AuthController { } @Post("signIn") - signIn(@Body() dto: AuthDTO) { + signIn(@Body() dto: AuthSignInDTO) { return this.authService.signIn(dto); } diff --git a/backend/src/auth/auth.service.ts b/backend/src/auth/auth.service.ts index ea488b1a..a0ff9cbd 100644 --- a/backend/src/auth/auth.service.ts +++ b/backend/src/auth/auth.service.ts @@ -10,8 +10,8 @@ import { User } from "@prisma/client"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; import * as argon from "argon2"; import { PrismaService } from "src/prisma/prisma.service"; -import { AuthDTO } from "./dto/auth.dto"; import { AuthRegisterDTO } from "./dto/authRegister.dto"; +import { AuthSignInDTO } from "./dto/authSignIn.dto"; @Injectable() export class AuthService { @@ -44,7 +44,7 @@ export class AuthService { } } - async signIn(dto: AuthDTO) { + async signIn(dto: AuthSignInDTO) { const user = await this.prisma.user.findUnique({ where: { email: dto.email, diff --git a/backend/src/auth/dto/authRegister.dto.ts b/backend/src/auth/dto/authRegister.dto.ts index 24f392db..12e13484 100644 --- a/backend/src/auth/dto/authRegister.dto.ts +++ b/backend/src/auth/dto/authRegister.dto.ts @@ -1,4 +1,4 @@ import { PickType } from "@nestjs/swagger"; -import { AuthDTO } from "./auth.dto"; +import { UserDTO } from "src/user/dto/user.dto"; -export class AuthRegisterDTO extends AuthDTO {} +export class AuthRegisterDTO extends UserDTO {} diff --git a/backend/src/auth/dto/authSignIn.dto.ts b/backend/src/auth/dto/authSignIn.dto.ts index eedb91d8..61aec2bb 100644 --- a/backend/src/auth/dto/authSignIn.dto.ts +++ b/backend/src/auth/dto/authSignIn.dto.ts @@ -1,7 +1,7 @@ import { PickType } from "@nestjs/swagger"; -import { AuthDTO } from "./auth.dto"; +import { UserDTO } from "src/user/dto/user.dto"; -export class AuthSignInDTO extends PickType(AuthDTO, [ +export class AuthSignInDTO extends PickType(UserDTO, [ "email", "password", ] as const) {} diff --git a/backend/src/file/dto/file.dto.ts b/backend/src/file/dto/file.dto.ts index 856f8f62..473c4dc3 100644 --- a/backend/src/file/dto/file.dto.ts +++ b/backend/src/file/dto/file.dto.ts @@ -16,7 +16,7 @@ export class FileDTO { share: ShareDTO; - constructor(partial: Partial) { + from(partial: Partial) { return plainToClass(FileDTO, partial, { excludeExtraneousValues: true }); } } diff --git a/backend/src/file/file.controller.ts b/backend/src/file/file.controller.ts index 38ba8e81..3e6044c1 100644 --- a/backend/src/file/file.controller.ts +++ b/backend/src/file/file.controller.ts @@ -15,6 +15,7 @@ import { FileInterceptor } from "@nestjs/platform-express"; import { Response } from "express"; import { JwtGuard } from "src/auth/guard/jwt.guard"; import { FileDownloadGuard } from "src/file/guard/fileDownload.guard"; +import { ShareDTO } from "src/share/dto/share.dto"; import { ShareSecurityGuard } from "src/share/guard/shareSecurity.guard"; import { FileService } from "./file.service"; @@ -42,7 +43,7 @@ export class FileController { file: Express.Multer.File, @Param("shareId") shareId: string ) { - return await this.fileService.create(file, shareId); + return new ShareDTO().from( await this.fileService.create(file, shareId)); } @Get(":fileId/download") diff --git a/backend/src/share/dto/share.dto.ts b/backend/src/share/dto/share.dto.ts index e2f75a8d..8eb266b7 100644 --- a/backend/src/share/dto/share.dto.ts +++ b/backend/src/share/dto/share.dto.ts @@ -1,5 +1,5 @@ import { Expose, plainToClass, Type } from "class-transformer"; -import { AuthDTO } from "src/auth/dto/auth.dto"; +import { AuthSignInDTO } from "src/auth/dto/authSignIn.dto"; import { FileDTO } from "src/file/dto/file.dto"; export class ShareDTO { @@ -14,8 +14,8 @@ export class ShareDTO { files: FileDTO[]; @Expose() - @Type(() => AuthDTO) - creator: AuthDTO; + @Type(() => AuthSignInDTO) + creator: AuthSignInDTO; from(partial: Partial) { return plainToClass(ShareDTO, partial, { excludeExtraneousValues: true }); diff --git a/backend/src/auth/dto/auth.dto.ts b/backend/src/user/dto/user.dto.ts similarity index 72% rename from backend/src/auth/dto/auth.dto.ts rename to backend/src/user/dto/user.dto.ts index 6893b98d..ae6411ef 100644 --- a/backend/src/auth/dto/auth.dto.ts +++ b/backend/src/user/dto/user.dto.ts @@ -1,7 +1,7 @@ import { Expose, plainToClass } from "class-transformer"; import { IsEmail, IsNotEmpty, IsString } from "class-validator"; -export class AuthDTO { +export class UserDTO { @Expose() id: string; @@ -20,7 +20,7 @@ export class AuthDTO { @IsString() password: string; - constructor(partial: Partial) { - return plainToClass(AuthDTO, partial, { excludeExtraneousValues: true }); + from(partial: Partial) { + return plainToClass(UserDTO, partial, { excludeExtraneousValues: true }); } } diff --git a/backend/src/user/user.controller.ts b/backend/src/user/user.controller.ts index 01f50c35..a0292d49 100644 --- a/backend/src/user/user.controller.ts +++ b/backend/src/user/user.controller.ts @@ -2,12 +2,13 @@ import { Controller, Get, UseGuards } from "@nestjs/common"; import { User } from "@prisma/client"; import { GetUser } from "src/auth/decorator/getUser.decorator"; import { JwtGuard } from "src/auth/guard/jwt.guard"; +import { UserDTO } from "./dto/user.dto"; @Controller("users") export class UserController { @Get("me") @UseGuards(JwtGuard) async getCurrentUser(@GetUser() user: User) { - return user; + return new UserDTO().from(user); } }