Skip to content

Commit

Permalink
merge :: emoji api (#23)
Browse files Browse the repository at this point in the history
* add :: query emoji api

* chore :: code format
  • Loading branch information
jyk1029 authored Mar 16, 2024
1 parent c913b40 commit 22bd000
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
12 changes: 11 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, Injectable, Post, UseFilters, UseGuards } from "@nestjs/common";
import { Body, Controller, Get, Post, Query, 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 All @@ -19,4 +19,14 @@ export class EmojiController {
async createEmoji(@CurrentUser() user: UserEntity, @Body() request: CreateEmojiRequest) {
await this.emojiService.createEmoji(user, request)
}

@Get('all')
async queryEmojiAll() {
return this.emojiService.queryEmojiAll()
}

@Get()
async queryEmojiFilter(@Query('min') min: number, @Query('max') max: number) {
return await this.emojiService.queryEmojiFilter(min, max)
}
}
14 changes: 13 additions & 1 deletion src/application/domain/emoji/dto/emoji.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsNumber, IsString, Matches } from "class-validator";
import { IsNotEmpty, IsNumber, IsString } from "class-validator";

export class CreateEmojiRequest {
@IsNotEmpty()
Expand All @@ -14,3 +14,15 @@ export class CreateEmojiRequest {
@IsNumber()
price: number;
}

export class QueryEmojiResponse {
emojis: EmojiElement[]
}

export class EmojiElement {
id: string
title: string
content: string
image: string
price: number
}
32 changes: 30 additions & 2 deletions src/application/domain/emoji/service/emoji.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { EmojiEntity } from "../../../../infrastructure/domain/emoji/persistence/emoji.entity";
import { Repository } from "typeorm";
import { CreateEmojiRequest } from "../dto/emoji.dto";
import { Between, Repository } from "typeorm";
import { CreateEmojiRequest, EmojiElement, QueryEmojiResponse } from "../dto/emoji.dto";

@Injectable()
export class EmojiService {
Expand All @@ -16,4 +16,32 @@ export class EmojiService {
let { title, content, imageUrl, price } = request
await this.emojiRepository.save({ title: title, content: content, image: imageUrl, price: price, userId: req })
}

async queryEmojiFilter(min, max) {
let emojis = await this.emojiRepository.findBy({ price: Between(min, max) })
return this.queryEmoji(emojis)
}

async queryEmojiAll() {
let emojis = await this.emojiRepository.find()
return this.queryEmoji(emojis)
}

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

response.emojis = await Promise.all(emojis.map(async (emoji) => {
let element = new EmojiElement()

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

return element
}))

return response
}
}

0 comments on commit 22bd000

Please sign in to comment.