Skip to content

Commit

Permalink
카테고리 관련 배포
Browse files Browse the repository at this point in the history
카테고리 관련 배포
  • Loading branch information
ImNM authored Jul 24, 2022
2 parents d9a03b9 + b4f0276 commit 511b491
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 7 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions src/apis/utils/utils.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
Body,
Controller,
Delete,
Get,
Post,
UseGuards,
} from '@nestjs/common';
import {
ApiBearerAuth,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/auth/guards/jwt.guard';
import { CategoryDto } from 'src/common/dtos/Category.dto';
import { CategoryIdDto } from 'src/common/dtos/CategoryId.dto';
import { UtilsService } from './utils.service';

@ApiTags('')
@Controller('')
@ApiBearerAuth('accessToken')
@UseGuards(JwtAuthGuard)
export class UtilsController {
constructor(private readonly utilsService: UtilsService) { }

@ApiOperation({
summary: '카테고리 정보',
})
@Get('category')
async getCategory() {
return await this.utilsService.findAllCategory()
}

@ApiOperation({
summary: '카테고리 추가',
})
@Post('category')
async createCategory(@Body() categoryDto: CategoryDto): Promise<void> {
return await this.utilsService.createCategory(categoryDto)
}

@ApiOperation({
summary: '카테고리 삭제',
})
@Delete('category')
async deleteCategory(@Body() categoryIdDto: CategoryIdDto) {
return await this.utilsService.deleteCategory(categoryIdDto)
}
}
17 changes: 17 additions & 0 deletions src/apis/utils/utils.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { Category, CategorySchema } from 'src/models/category.model';
import { CategoryRepository } from 'src/repositories/category.repository';

import { UtilsController } from './utils.controller';
import { UtilsService } from './utils.service';

@Module({
imports: [
MongooseModule.forFeature([{ name: Category.name, schema: CategorySchema }])
],
controllers: [UtilsController],
providers: [UtilsService, CategoryRepository],
})
export class UtilsModule { }
23 changes: 23 additions & 0 deletions src/apis/utils/utils.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Injectable } from '@nestjs/common';
import { CategoryDto } from 'src/common/dtos/Category.dto';
import { CategoryIdDto } from 'src/common/dtos/CategoryId.dto';
import { CategoryRepository } from 'src/repositories/category.repository';

@Injectable()
export class UtilsService {
constructor(
private categoryRepository: CategoryRepository,
) { }

async createCategory(categoryDto: CategoryDto): Promise<void> {
await this.categoryRepository.create(categoryDto);
}

async deleteCategory(categoryIdDto: CategoryIdDto): Promise<void> {
await this.categoryRepository.delete(categoryIdDto)
}

async findAllCategory(): Promise<Array<CategoryDto>> {
return await this.categoryRepository.findAll()
}
}
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BullModule } from '@nestjs/bull';
import { FcmModule } from './fcm/fcm.module';
import { ChatModule } from './chat/chat.module';
import * as Joi from 'joi';
import { UtilsModule } from './apis/utils/utils.module';

@Module({
imports: [
Expand Down Expand Up @@ -69,7 +70,8 @@ import * as Joi from 'joi';
QuestionsModule,
AlarmModule,
FcmModule,
ChatModule
ChatModule,
UtilsModule
],
providers: [
{
Expand Down
37 changes: 37 additions & 0 deletions src/common/dtos/Category.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ApiProperty } from '@nestjs/swagger';

import { IsNumber, IsString } from 'class-validator';
import { Expose } from 'class-transformer';
export class CategoryDto {
@ApiProperty({
description: '카테고리 아이디',
type: String,
})
@IsString()
@Expose()
id: string;

@ApiProperty({
description: '카테고리 이름',
type: String,
})
@IsString()
@Expose()
name: string;

@ApiProperty({
description: '카테고리 이미지 경로',
type: String,
})
@IsString()
@Expose()
imageUrl: string;

@ApiProperty({
description: '카테고리 우선순위(낮을수록 우선순위 높음)',
type: Number,
})
@IsNumber()
@Expose()
priority: Number;
}
10 changes: 10 additions & 0 deletions src/common/dtos/CategoryId.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
export class CategoryIdDto {
@ApiProperty({
type: String,
title: '카테고리 아이디',
description: '카테고리 아이디',
example: 'ALL',
})
id: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function MongooseClassSerializerInterceptor(dto: any) {
}

export class SerializeInterceptor implements NestInterceptor {
constructor(private dto: any) {}
constructor(private dto: any) { }

intercept(context: ExecutionContext, handler: CallHandler): Observable<any> {
return handler.handle().pipe(
Expand Down
1 change: 0 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ClassSerializerInterceptor,
ConsoleLogger,
ValidationPipe,
VersioningType,
} from '@nestjs/common';
Expand Down
50 changes: 50 additions & 0 deletions src/models/category.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ApiProperty } from '@nestjs/swagger';
import { Prop, Schema, SchemaFactory, SchemaOptions } from '@nestjs/mongoose';

import { IsNumber, IsString } from 'class-validator';
import { Expose } from 'class-transformer';

const options: SchemaOptions = {
collection: 'category',
timestamps: true,
};

@Schema(options)
export class Category {
@ApiProperty({
description: '카테고리 아이디',
type: String,
})
@Prop({ type: String, required: true })
@IsString()
@Expose()
id: string;

@ApiProperty({
description: '카테고리 이름',
type: String,
})
@Prop({ type: String, required: true })
@IsString()
@Expose()
name: string;

@ApiProperty({
description: '카테고리 이미지 경로',
type: String,
})
@Prop({ type: String, required: true })
@IsString()
@Expose()
imageUrl: string;

@ApiProperty({
description: '카테고리 우선순위',
type: Number,
})
@Prop({ type: Number, required: true })
@IsNumber()
@Expose()
priority: Number;
}
export const CategorySchema = SchemaFactory.createForClass(Category);
7 changes: 7 additions & 0 deletions src/models/room.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ export class Room {
@ApiProperty({ description: '유저 숫자' })
@Expose()
userCount: number;

@ApiProperty({ description: '채팅방 부제목' })
@Prop({ required: false, type: String, default: "" })
@IsNotEmpty()
@IsString()
@Expose()
subtitle: string
}

const _RoomSchema = SchemaFactory.createForClass(Room);
Expand Down
32 changes: 32 additions & 0 deletions src/repositories/category.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { InjectModel } from '@nestjs/mongoose';
import { Injectable } from '@nestjs/common';

import { Model } from 'mongoose';

import { Category } from 'src/models/category.model';
import { CategoryDto } from 'src/common/dtos/Category.dto';
import { CategoryIdDto } from 'src/common/dtos/CategoryId.dto';

@Injectable()
export class CategoryRepository {
constructor(@InjectModel(Category.name) private readonly categoryModel: Model<Category>) { }

async create(categoryDto: CategoryDto): Promise<void> {
await this.categoryModel.create(categoryDto)
}

async delete(categoryIdDto: CategoryIdDto): Promise<void> {
await this.categoryModel.deleteOne({ categoryIdDto })
}

async findAll(): Promise<Array<CategoryDto>> {
return await this.categoryModel.find().sort({ priority: 1 })
.select({
_id: 0,
id: 1,
name: 1,
imageUrl: 1,
})
.lean() || {}
}
}

0 comments on commit 511b491

Please sign in to comment.