Skip to content

Commit

Permalink
merge :: extra api (#26)
Browse files Browse the repository at this point in the history
* add :: buy emoji api

* modify :: create emoji api

* add :: upload Emoji Image api

* add :: query my emojis api

* chore :: resolve bug

* modify :: refactoring
  • Loading branch information
jyk1029 authored Mar 20, 2024
1 parent 6552034 commit ae015c8
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/application/domain/emoji/controller/emoji.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Post, Query, UseFilters, UseGuards } from "@nestjs/common";
import { Body, Controller, Get, Param, Post, Query, UploadedFile, UseFilters, UseGuards } from "@nestjs/common";
import { GlobalExceptionFilter } from "../../../../infrastructure/global/filter/global.exception.filter";
import { EmojiService } from "../service/emoji.service";
import { CurrentUser } from "../../../../infrastructure/global/decorator/current-user";
Expand Down Expand Up @@ -29,4 +29,20 @@ export class EmojiController {
async queryEmojiFilter(@Query('min') min: number, @Query('max') max: number) {
return await this.emojiService.queryEmojiFilter(min, max)
}

@UseGuards(AuthGuard())
@Post('buy/:id')
async buyEmoji(@CurrentUser() user: UserEntity, @Param() id) {
await this.emojiService.buyEmoji(user, id)
}

@Post('upload/:id')
async uploadEmojiImage(@Param() id, @UploadedFile('file') file: Express.Multer.File) {
return await this.emojiService.uploadEmojiImage(id, file)
}

@Get('mine')
async queryMyEmoji(@CurrentUser() user: UserEntity) {
return await this.emojiService.queryMyEmoji(user)
}
}
4 changes: 4 additions & 0 deletions src/application/domain/emoji/dto/emoji.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ export class EmojiElement {
image: string
price: number
}

export class UploadImageUrlResponse {
imageUrl: string
}
85 changes: 82 additions & 3 deletions src/application/domain/emoji/service/emoji.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import { Injectable } from "@nestjs/common";
import { HttpException, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { EmojiEntity } from "../../../../infrastructure/domain/emoji/persistence/emoji.entity";
import { Between, Repository } from "typeorm";
import { CreateEmojiRequest, EmojiElement, QueryEmojiResponse } from "../dto/emoji.dto";
import { CreateEmojiRequest, EmojiElement, QueryEmojiResponse, UploadImageUrlResponse } from "../dto/emoji.dto";
import { UserEntity } from "../../../../infrastructure/domain/user/persistence/user.entity";
import { BuyEmojiEntity } from "../../../../infrastructure/domain/emoji/persistence/buy-emoji.entity";
import { AwsService } from "../../../../infrastructure/global/utils/s3/aws.service";
import { randomUUID } from "crypto";

@Injectable()
export class EmojiService {
constructor(
@InjectRepository(EmojiEntity)
private emojiRepository: Repository<EmojiEntity>,
@InjectRepository(UserEntity)
private userRepository: Repository<UserEntity>,
@InjectRepository(BuyEmojiEntity)
private buyEmojiRepository: Repository<BuyEmojiEntity>,
private awsService: AwsService,
) {
}

async createEmoji(req, request: CreateEmojiRequest) {
let { title, content, imageUrl, price } = request
await this.emojiRepository.save({ title: title, content: content, image: imageUrl, price: price, userId: req })
let buyEmoji = new BuyEmojiEntity()

let emoji = await this.emojiRepository.save({
title: title,
content: content,
image: imageUrl,
price: price,
userId: req
})

buyEmoji.userId = req
buyEmoji.emojiId = emoji

await this.buyEmojiRepository.save(buyEmoji)
}

async queryEmojiFilter(min, max) {
Expand All @@ -27,6 +49,63 @@ export class EmojiService {
return this.queryEmoji(emojis)
}

async buyEmoji(req, id) {
let emoji = await this.emojiRepository.findOne({ where: id })
let buyEmoji = new BuyEmojiEntity();

buyEmoji.userId = req
buyEmoji.emojiId = emoji

await this.validateBuyEmoji(req, emoji)

req.point -= emoji.price
await this.userRepository.save(req)
await this.buyEmojiRepository.save(buyEmoji)
}

async uploadEmojiImage(id, file) {
let emoji = await this.emojiRepository.findOne({ where: id })
let response = new UploadImageUrlResponse()
if (!emoji) {
throw new HttpException('Emoji Not Found', 404)
}

response.imageUrl = await this.awsService.upload(randomUUID(), file)

return response
}

async queryMyEmoji(req) {
let buyEmojis = await this.buyEmojiRepository.findBy({ userId: req })
let response = new QueryEmojiResponse()

response.emojis = await Promise.all(buyEmojis.map(async (buyEmoji) => {
let element = new EmojiElement()
let emoji = await buyEmoji.emojiId
console.log(emoji)

element.id = emoji.id
element.title = emoji.title
element.content = emoji.content
element.image = emoji.image
element.price = emoji.price

return element
}))

return response
}

private async validateBuyEmoji(user, emoji) {
if (!emoji) {
throw new HttpException('Emoji Not Found', 404)
}

if (user.point < emoji.price) {
throw new HttpException('Point Not Enough', 400)
}
}

private async queryEmoji(emojis) {
let response = new QueryEmojiResponse()

Expand Down
17 changes: 17 additions & 0 deletions src/infrastructure/domain/emoji/persistence/buy-emoji.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { UserEntity } from "../../user/persistence/user.entity";
import { EmojiEntity } from "./emoji.entity";

@Entity('tbl_buy_emoji')
export class BuyEmojiEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@ManyToOne(() => EmojiEntity, { lazy: true })
@JoinColumn({ name: 'emoji_id' })
emojiId: EmojiEntity;

@ManyToOne(() => UserEntity, { lazy: true })
@JoinColumn({ name: 'user_id' })
userId: UserEntity;
}
6 changes: 4 additions & 2 deletions src/infrastructure/global/module/emoji.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { EmojiEntity } from "../../domain/emoji/persistence/emoji.entity";
import { EmojiService } from "../../../application/domain/emoji/service/emoji.service";
import { EmojiController } from "../../../application/domain/emoji/controller/emoji.controller";
import { BuyEmojiEntity } from "../../domain/emoji/persistence/buy-emoji.entity";
import { AwsService } from "../utils/s3/aws.service";

const EMOJI_REPOSITORY = TypeOrmModule.forFeature([ EmojiEntity ]);
const EMOJI_REPOSITORY = TypeOrmModule.forFeature([ EmojiEntity, BuyEmojiEntity ]);

@Global()
@Module({
imports: [ EMOJI_REPOSITORY ],
controllers: [ EmojiController ],
providers: [ EmojiService ],
providers: [ EmojiService, AwsService ],
exports: [ EMOJI_REPOSITORY ]
})
export class EmojiModule {
Expand Down

0 comments on commit ae015c8

Please sign in to comment.