Skip to content

Commit

Permalink
Merge pull request #209 from boostcampwm2023/feat/205-purchase-design…
Browse files Browse the repository at this point in the history
…-api

[Feat] 유료 디자인 구매 API 구현
  • Loading branch information
JoonSoo-Kim authored Dec 5, 2023
2 parents f557dca + 116b24d commit 22ef42c
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 2 deletions.
6 changes: 4 additions & 2 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { ShapesRepository } from "./shapes/shapes.repository";
import { UsersRepository } from "./auth/users.repository";
import { typeORMTestConfig } from "./configs/typeorm.test.config";
import { RedisModule } from "@liaoliaots/nestjs-redis";
import { StatModule } from './stat/stat.module';
import { LinesModule } from './lines/lines.module';
import { StatModule } from "./stat/stat.module";
import { LinesModule } from "./lines/lines.module";
import { PurchaseModule } from "./purchase/purchase.module";

@Module({
imports: [
Expand All @@ -31,6 +32,7 @@ import { LinesModule } from './lines/lines.module';
ShapesModule,
StatModule,
LinesModule,
PurchaseModule,
],
providers: [ShapesRepository, UsersRepository],
})
Expand Down
4 changes: 4 additions & 0 deletions BE/src/auth/users.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { premiumStatus, providerEnum } from "src/utils/enum";
import { Diary } from "../diaries/diaries.entity";
import { Shape } from "src/shapes/shapes.entity";
import { Purchase } from "src/purchase/purchase.entity";

@Entity()
@Unique(["userId"])
Expand Down Expand Up @@ -54,4 +55,7 @@ export class User extends BaseEntity {

@OneToMany(() => Shape, (shape) => shape.user)
shapes: Diary[];

@OneToMany(() => Purchase, (purchase) => purchase.user)
purchases: Purchase[];
}
9 changes: 9 additions & 0 deletions BE/src/purchase/dto/purchase.design.dto.ts
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;
}
21 changes: 21 additions & 0 deletions BE/src/purchase/purchase.controller.ts
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);
}
}
33 changes: 33 additions & 0 deletions BE/src/purchase/purchase.entity.ts
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;
}
14 changes: 14 additions & 0 deletions BE/src/purchase/purchase.module.ts
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 {}
20 changes: 20 additions & 0 deletions BE/src/purchase/purchase.repository.ts
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();
}
}
42 changes: 42 additions & 0 deletions BE/src/purchase/purchase.service.ts
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);
}
}
10 changes: 10 additions & 0 deletions BE/src/utils/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ export enum providerEnum {
NAVER = "naver",
KAKAO = "kakao",
}

export enum domainEnum {
GROUND = "ground",
SKY = "sky",
}

export enum designEnum {
GROUND_GREEN = "#254117",
GROUND_MOCHA = "#493D26",
}

0 comments on commit 22ef42c

Please sign in to comment.