-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from boostcampwm-2022/feat/participate-be
feat BE ์ ์ ์ฐธ์ฌ API ๊ตฌํ
- Loading branch information
Showing
26 changed files
with
380 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
registerDecorator, | ||
ValidationOptions, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
} from "class-validator"; | ||
|
||
@ValidatorConstraint({ name: "isValidDateTime", async: false }) | ||
class isValidDateTimeConstraint implements ValidatorConstraintInterface { | ||
public validate(value: string) { | ||
const dateRegex = /^(2\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/; | ||
const timeRegex = /^(0[0-9]|1\d|2[0-3]):([0-5]\d):([0-5]\d)$/; | ||
|
||
return ( | ||
typeof value === "string" && | ||
value.length === 19 && | ||
dateRegex.test(value.split(" ")[0]) && | ||
timeRegex.test(value.split(" ")[1]) | ||
); | ||
} | ||
|
||
public defaultMessage(): string { | ||
return `user id must be between 6 and 20 character long, only letters and numbers allowed`; | ||
} | ||
} | ||
|
||
export function IsValidDateTime(validationOptions?: ValidationOptions) { | ||
return function (object: any, propertyName: string) { | ||
registerDecorator({ | ||
name: "isValidDateTime", | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
constraints: [], | ||
options: validationOptions, | ||
validator: isValidDateTimeConstraint, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { IsValidId } from "./id.validator"; | ||
import { IsValidPassword } from "./pw.validator"; | ||
import { IsValidDateTime } from "./date.validator"; | ||
|
||
export { IsValidId, IsValidPassword }; | ||
export { IsValidId, IsValidPassword, IsValidDateTime }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Controller } from "@nestjs/common"; | ||
import { CourseService } from "./course.service"; | ||
|
||
@Controller("course") | ||
export class CourseController { | ||
constructor(private readonly courseService: CourseService) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { CourseService } from "./course.service"; | ||
import { CourseController } from "./course.controller"; | ||
|
||
@Module({ | ||
controllers: [CourseController], | ||
providers: [CourseService], | ||
}) | ||
export class CourseModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
|
||
@Injectable() | ||
export class CourseService {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Type } from "class-transformer"; | ||
import { IsNumber, IsNumberString, IsString } from "class-validator"; | ||
import { IsValidDateTime } from "src/common/decorator"; | ||
import { Recruit } from "../../entities/recruit.entity"; | ||
|
||
export class CreateRecruitDto { | ||
@IsString() | ||
private title: string; | ||
|
||
@IsValidDateTime() | ||
private startTime: Date; | ||
|
||
@Type(() => Number) | ||
@IsNumber() | ||
private maxPpl: number; | ||
|
||
@Type(() => Number) | ||
@IsNumber() | ||
private pace: number; | ||
|
||
@IsNumberString() | ||
private zipCode: string; | ||
|
||
@Type(() => Number) | ||
@IsNumber() | ||
private userId: number; | ||
|
||
@Type(() => Number) | ||
@IsNumber() | ||
private courseId: number; | ||
|
||
toEntity() { | ||
return Recruit.of(this.title, this.startTime, this.maxPpl, this.pace, this.zipCode, this.userId, this.courseId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Type } from "class-transformer"; | ||
import { IsNumber } from "class-validator"; | ||
import { UserRecruit } from "../../entities/user_recruit.entity"; | ||
|
||
export class JoinRecruitDto { | ||
@Type(() => Number) | ||
@IsNumber() | ||
private recruitId: number; | ||
|
||
@Type(() => Number) | ||
@IsNumber() | ||
private userId: number; | ||
|
||
getRecruitId() { | ||
return this.recruitId; | ||
} | ||
|
||
getUserId() { | ||
return this.userId; | ||
} | ||
toEntity() { | ||
return UserRecruit.of(this.recruitId, this.userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Body, Controller, Get, Post, UseGuards } from "@nestjs/common"; | ||
import { AccessGuard } from "src/common/guard/access.guard"; | ||
import { CreateRecruitDto } from "./dto/create-recruit.dto"; | ||
import { JoinRecruitDto } from "./dto/join-recruit.dto"; | ||
import { RecruitService } from "./recruit.service"; | ||
|
||
@Controller("recruit") | ||
export class RecruitController { | ||
constructor(private readonly recruitService: RecruitService) {} | ||
|
||
@Get() | ||
async getRecruits() { | ||
const recruitEntityArrays = await this.recruitService.findAll(); | ||
// TODO: ํ์ด์ง๋ค์ด์ ์ ์ฉ | ||
return { | ||
statusCode: 200, | ||
data: recruitEntityArrays, | ||
}; | ||
} | ||
|
||
@Post() | ||
async create(@Body() createRecruitDto: CreateRecruitDto) { | ||
const recruitEntity = await this.recruitService.create(createRecruitDto); | ||
// TODO: ์๋ต ๋ฆฌํฉํ ๋งํ๊ธฐ entity -> ์๋ต dto ๋ณํ ํ ์ธํฐ์ ํฐ๊ฐ ์ํ์ฝ๋ ๋ฃ์ด์ ์ฒ๋ฆฌํ๊ฒ๋ ๋ฐ๊พธ๊ธฐ | ||
// ๋งค๋ฒ ์ํ์ฝ๋์ ๋ฐ์ดํฐ ๋ฃ์ด์ฃผ๋ ๋ฐฉ์์ด ๊น๋ํ์ง ๋ชปํ ๋๋. | ||
return { | ||
statusCode: 201, | ||
data: { | ||
recruitId: recruitEntity.id, | ||
}, | ||
}; | ||
} | ||
// @UseGuards(AccessGuard) | ||
@Post("join") | ||
async register(@Body() joinRecruitDto: JoinRecruitDto) { | ||
const recruitId = joinRecruitDto.getRecruitId(); | ||
const userId = joinRecruitDto.getUserId(); | ||
if (!(await this.recruitService.isExistRecruit(recruitId))) { | ||
return { | ||
statusCode: 409, | ||
error: { | ||
message: "Does not exist or has been deleted", | ||
}, | ||
}; | ||
} | ||
if (await this.recruitService.isAuthorOfRecruit(recruitId, userId)) { | ||
return { | ||
statusCode: 423, | ||
error: { | ||
message: "Cannot participate in your own recruitment", | ||
}, | ||
}; | ||
} | ||
if (await this.recruitService.isParticipating(recruitId, userId)) { | ||
return { | ||
statusCode: 423, | ||
error: { | ||
message: "You have already participated.", | ||
}, | ||
}; | ||
} | ||
if (!(await this.recruitService.isVacancy(recruitId))) { | ||
return { | ||
statusCode: 423, | ||
error: { | ||
message: "Maximum cap reached", | ||
}, | ||
}; | ||
} | ||
return { | ||
statusCode: 201, | ||
success: true, | ||
}; | ||
} | ||
} |
Oops, something went wrong.