-
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 #209 from boostcampwm2023/feat/205-purchase-design…
…-api [Feat] 유료 디자인 구매 API 구현
- Loading branch information
Showing
9 changed files
with
157 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IsNotEmpty } from "class-validator"; | ||
|
||
export class PurchaseDesignDto { | ||
@IsNotEmpty({ message: "구매 항목 종류는 비어있지 않아야 합니다." }) | ||
domain: string; | ||
|
||
@IsNotEmpty({ message: "디자인은 비어있지 않아야 합니다." }) | ||
design: string; | ||
} |
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,21 @@ | ||
import { Controller, Post, UseGuards, Body, HttpCode } from "@nestjs/common"; | ||
import { JwtAuthGuard } from "src/auth/guard/auth.jwt-guard"; | ||
import { PurchaseService } from "./purchase.service"; | ||
import { PurchaseDesignDto } from "./dto/purchase.design.dto"; | ||
import { GetUser } from "src/auth/get-user.decorator"; | ||
import { User } from "src/auth/users.entity"; | ||
|
||
@Controller("purchase") | ||
@UseGuards(JwtAuthGuard) | ||
export class PurchaseController { | ||
constructor(private purchaseService: PurchaseService) {} | ||
|
||
@Post("/design") | ||
@HttpCode(204) | ||
async purchaseDesign( | ||
@GetUser() user: User, | ||
@Body() purchaseDesignDto: PurchaseDesignDto, | ||
): Promise<void> { | ||
await this.purchaseService.purchaseDesign(user, purchaseDesignDto); | ||
} | ||
} |
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,33 @@ | ||
import { User } from "src/auth/users.entity"; | ||
import { designEnum, domainEnum } from "src/utils/enum"; | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
BaseEntity, | ||
Column, | ||
ManyToOne, | ||
} from "typeorm"; | ||
|
||
@Entity() | ||
export class Purchase extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ | ||
type: "enum", | ||
enum: domainEnum, | ||
}) | ||
domain: domainEnum; | ||
|
||
@Column({ | ||
type: "enum", | ||
enum: designEnum, | ||
}) | ||
design: designEnum; | ||
|
||
@ManyToOne(() => User, (user) => user.userId, { | ||
nullable: false, | ||
eager: true, | ||
}) | ||
user: User; | ||
} |
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,14 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { TypeOrmModule } from "@nestjs/typeorm"; | ||
import { Purchase } from "./purchase.entity"; | ||
import { PurchaseController } from "./purchase.controller"; | ||
import { PurchaseService } from "./purchase.service"; | ||
import { PurchaseRepository } from "./purchase.repository"; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Purchase])], | ||
controllers: [PurchaseController], | ||
providers: [PurchaseService, PurchaseRepository], | ||
exports: [PurchaseRepository], | ||
}) | ||
export class PurchaseModule {} |
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,20 @@ | ||
import { Purchase } from "./purchase.entity"; | ||
import { PurchaseDesignDto } from "./dto/purchase.design.dto"; | ||
import { User } from "src/auth/users.entity"; | ||
import { designEnum, domainEnum } from "src/utils/enum"; | ||
|
||
export class PurchaseRepository { | ||
async purchaseDesign( | ||
user: User, | ||
domain: domainEnum, | ||
design: designEnum, | ||
): Promise<void> { | ||
const purchase = await Purchase.create(); | ||
|
||
purchase.domain = domain; | ||
purchase.design = design; | ||
purchase.user = user; | ||
|
||
purchase.save(); | ||
} | ||
} |
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,42 @@ | ||
import { Injectable, BadRequestException } from "@nestjs/common"; | ||
import { PurchaseDesignDto } from "./dto/purchase.design.dto"; | ||
import { User } from "src/auth/users.entity"; | ||
import { PurchaseRepository } from "./purchase.repository"; | ||
import { Purchase } from "./purchase.entity"; | ||
import { designEnum, domainEnum } from "src/utils/enum"; | ||
|
||
@Injectable() | ||
export class PurchaseService { | ||
constructor(private purchaseRepository: PurchaseRepository) {} | ||
|
||
async purchaseDesign( | ||
user: User, | ||
purchaseDesignDto: PurchaseDesignDto, | ||
): Promise<void> { | ||
const domain = domainEnum[purchaseDesignDto.domain]; | ||
const design = designEnum[purchaseDesignDto.design]; | ||
|
||
if (user.credit < 500) { | ||
throw new BadRequestException( | ||
`보유한 별가루가 부족합니다. 현재 ${user.credit} 별가루`, | ||
); | ||
} | ||
|
||
if ( | ||
Purchase.findOne({ | ||
where: { | ||
user: { userId: user.userId }, | ||
domain: domain, | ||
design: design, | ||
}, | ||
}) | ||
) { | ||
throw new BadRequestException(`이미 구매한 디자인입니다.`); | ||
} | ||
|
||
user.credit -= 500; | ||
user.save(); | ||
|
||
await this.purchaseRepository.purchaseDesign(user, domain, design); | ||
} | ||
} |
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