Skip to content

Commit

Permalink
fix: dto returns
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Oct 10, 2022
1 parent 4bab33a commit 02beb66
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 17 deletions.
5 changes: 3 additions & 2 deletions backend/src/auth/auth.controller.ts
Expand Up @@ -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")
Expand All @@ -27,7 +28,7 @@ export class AuthController {
}

@Post("signIn")
signIn(@Body() dto: AuthDTO) {
signIn(@Body() dto: AuthSignInDTO) {
return this.authService.signIn(dto);
}

Expand Down
4 changes: 2 additions & 2 deletions backend/src/auth/auth.service.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions 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 {}
4 changes: 2 additions & 2 deletions 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) {}
2 changes: 1 addition & 1 deletion backend/src/file/dto/file.dto.ts
Expand Up @@ -16,7 +16,7 @@ export class FileDTO {

share: ShareDTO;

constructor(partial: Partial<FileDTO>) {
from(partial: Partial<FileDTO>) {
return plainToClass(FileDTO, partial, { excludeExtraneousValues: true });
}
}
3 changes: 2 additions & 1 deletion backend/src/file/file.controller.ts
Expand Up @@ -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";

Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions 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 {
Expand All @@ -14,8 +14,8 @@ export class ShareDTO {
files: FileDTO[];

@Expose()
@Type(() => AuthDTO)
creator: AuthDTO;
@Type(() => AuthSignInDTO)
creator: AuthSignInDTO;

from(partial: Partial<ShareDTO>) {
return plainToClass(ShareDTO, partial, { excludeExtraneousValues: true });
Expand Down
@@ -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;

Expand All @@ -20,7 +20,7 @@ export class AuthDTO {
@IsString()
password: string;

constructor(partial: Partial<AuthDTO>) {
return plainToClass(AuthDTO, partial, { excludeExtraneousValues: true });
from(partial: Partial<UserDTO>) {
return plainToClass(UserDTO, partial, { excludeExtraneousValues: true });
}
}
3 changes: 2 additions & 1 deletion backend/src/user/user.controller.ts
Expand Up @@ -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);
}
}

0 comments on commit 02beb66

Please sign in to comment.