Skip to content

Commit

Permalink
Add name conflict checking API
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Jan 24, 2024
1 parent f6cee41 commit ce61e09
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
26 changes: 24 additions & 2 deletions backend/src/check/check.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import { Controller } from "@nestjs/common";
import { Controller, Post } from "@nestjs/common";
import { CheckService } from "./check.service";
import { CheckNameConflictDto } from "./dto/check-name-conflict.dto";
import { CheckNameConflicReponse } from "./types/check-name-conflict-response.type";
import { Public } from "src/utils/decorators/auth.decorator";
import { ApiBody, ApiOkResponse, ApiOperation, ApiTags } from "@nestjs/swagger";

@ApiTags("Check")
@Controller("check")
export class CheckController {}
export class CheckController {
constructor(private checkService: CheckService) {}

@Public()
@Post("name-conflicts")
@ApiOperation({
summary: "Check Whether The Name Conflicts with Username or Title of Workspace.",
description: "If the name is conflict, it returns true.",
})
@ApiBody({ type: CheckNameConflictDto })
@ApiOkResponse({ type: CheckNameConflicReponse })
async checkNameConflict(
checkNameConflictDto: CheckNameConflictDto
): Promise<CheckNameConflicReponse> {
return this.checkService.checkNameConflict(checkNameConflictDto.name);
}
}
3 changes: 2 additions & 1 deletion backend/src/check/check.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from "@nestjs/common";
import { CheckService } from "./check.service";
import { CheckController } from "./check.controller";
import { PrismaService } from "src/db/prisma.service";

@Module({
providers: [CheckService],
providers: [CheckService, PrismaService],
controllers: [CheckController],
})
export class CheckModule {}
23 changes: 22 additions & 1 deletion backend/src/check/check.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
import { Injectable } from "@nestjs/common";
import { PrismaService } from "src/db/prisma.service";
import { CheckNameConflicReponse } from "./types/check-name-conflict-response.type";

@Injectable()
export class CheckService {}
export class CheckService {
constructor(private prismaService: PrismaService) {}

async checkNameConflict(name: string): Promise<CheckNameConflicReponse> {
const conflictUserList = await this.prismaService.user.findMany({
where: {
nickname: name,
},
});
const conflictWorkspaceList = await this.prismaService.workspace.findMany({
where: {
title: name,
},
});

return {
conflict: Boolean(conflictUserList.length + conflictWorkspaceList.length),
};
}
}
6 changes: 6 additions & 0 deletions backend/src/check/dto/check-name-conflict.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from "@nestjs/swagger";

export class CheckNameConflictDto {
@ApiProperty({ type: String, description: "Name to check conflict" })
name: string;
}
6 changes: 6 additions & 0 deletions backend/src/check/types/check-name-conflict-response.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from "@nestjs/swagger";

export class CheckNameConflicReponse {
@ApiProperty({ type: Boolean, description: "Whether the name is conflict" })
conflict: boolean;
}

0 comments on commit ce61e09

Please sign in to comment.