Skip to content

Commit

Permalink
feat : 초대장 업로드
Browse files Browse the repository at this point in the history
  • Loading branch information
kangjuhyup committed Oct 27, 2024
1 parent fe534c6 commit 9591dbd
Show file tree
Hide file tree
Showing 32 changed files with 1,111 additions and 270 deletions.
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.3.0",
"@nestjs/core": "^10.0.0",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.4.2",
"@nestjs/typeorm": "^10.0.2",
"aws-sdk": "^2.1691.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"ioredis": "^5.4.1",
"mysql2": "^3.11.3",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"swagger-ui-express": "^5.0.1",
Expand All @@ -45,6 +50,8 @@
"@types/jest": "^29.5.2",
"@types/multer": "^1.4.12",
"@types/node": "^20.3.1",
"@types/passport": "^0",
"@types/passport-jwt": "^4",
"@types/supertest": "^6.0.0",
"@types/uuid": "^10",
"@typescript-eslint/eslint-plugin": "^6.0.0",
Expand Down
Binary file added src/.DS_Store
Binary file not shown.
12 changes: 5 additions & 7 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ const modules = [
StorageModule,
DatabaseModule,
RedisClientModule.forRootAsync({
project : 'invite-service',
isGlobal : true,
imports : [
ConfigModule,
],
project: 'invite-service',
isGlobal: true,
imports: [ConfigModule],
useFactory: (config: ConfigService) => {
return {
host: config.get<string>('REDIS_HOST'),
port: config.get<number>('REDIS_PORT'),
password: config.get<string>('REDIS_PWD'),
};
},
inject : [ConfigService]
})
inject: [ConfigService],
}),
].filter(Boolean);

@Module({
Expand Down
5 changes: 5 additions & 0 deletions src/database/database.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { LetterDataSource } from './datasource/letter.datasource';
import { Enviroments } from '@app/domain/dto/env';
import { plainToInstance } from 'class-transformer';
import { LetterRepository } from './repository/letter';

const repositories = [LetterRepository];

@Global()
@Module({
Expand All @@ -29,5 +32,7 @@ import { plainToInstance } from 'class-transformer';
inject: [ConfigService],
}),
],
providers: [...repositories],
exports: [...repositories],
})
export class DatabaseModule {}
48 changes: 48 additions & 0 deletions src/database/repository/attachment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { AttachmentEntity } from '@database/entity/attachment';
import { InjectRepository } from '@nestjs/typeorm';
import { EntityManager, In, Repository } from 'typeorm';
import { InsertAttachment, SelectAttachment } from './param/attachment';

export class AttachmentRepository {
constructor(
@InjectRepository(AttachmentEntity)
private readonly attachment: Repository<AttachmentEntity>,
) {}

async selectAttachmentIds({
attachmentPaths,
entityManager,
}: Pick<SelectAttachment, 'attachmentPaths' | 'entityManager'>): Promise<
number[]
> {
const repo = this._getRepository('attachment', entityManager);
return (
await repo
.createQueryBuilder()
.select(['attachmentId'])
.where({
attachmentPath: In(attachmentPaths),
})
.getMany()
).map((att) => att.attachmentId);
}

async bulkInsertAttachments({
attachments,
entityManager,
}: Omit<InsertAttachment, 'attachment'>) {
const repo = this._getRepository('attachment', entityManager);
return await repo
.createQueryBuilder()
.insert()
.values(attachments)
.execute();
}

private _getRepository(type: 'attachment', entityManager?: EntityManager) {
if (type === 'attachment')
return entityManager
? entityManager.getRepository(AttachmentEntity)
: this.attachment;
}
}
47 changes: 47 additions & 0 deletions src/database/repository/letter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { LetterEntity } from '@database/entity/letter';
import { LetterAttachmentEntity } from '@database/entity/letter.attachment';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { EntityManager, Repository } from 'typeorm';
import { InsertLetter, InsertLetterAttachment } from './param/letter';

@Injectable()
export class LetterRepository {
constructor(
@InjectRepository(LetterEntity)
private readonly letter: Repository<LetterEntity>,
@InjectRepository(LetterAttachmentEntity)
private readonly letterAttachment: Repository<LetterAttachmentEntity>,
) {}

async insertLetter({ letter, entityManager }: InsertLetter) {
const repo = this._getRepository('letter', entityManager);
return await repo.insert(letter);
}

async insertLetterAttachment({
letterAttachments,
entityManager,
}: InsertLetterAttachment) {
const repo = this._getRepository('letterAttachment', entityManager);
await repo
.createQueryBuilder()
.insert()
.values(letterAttachments)
.execute();
}

private _getRepository(
type: 'letter' | 'letterAttachment',
entityManager?: EntityManager,
) {
if (type === 'letter')
return entityManager
? entityManager.getRepository(LetterEntity)
: this.letter;
if (type === 'letterAttachment')
return entityManager
? entityManager.getRepository(LetterAttachmentEntity)
: this.letterAttachment;
}
}
16 changes: 16 additions & 0 deletions src/database/repository/param/attachment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AttachmentEntity } from '@database/entity/attachment';
import { DefaultParameter } from './default';

export type Attachment = Pick<AttachmentEntity, 'attachmentPath'>;

export class SelectAttachment extends DefaultParameter {
attachmentId: number;
attachmentIds: number[];
attachmentPath: string;
attachmentPaths: string[];
}

export class InsertAttachment extends DefaultParameter {
attachments: Array<Attachment>;
attachment: Attachment;
}
5 changes: 5 additions & 0 deletions src/database/repository/param/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EntityManager } from 'typeorm';

export class DefaultParameter {
entityManager: EntityManager;
}
20 changes: 20 additions & 0 deletions src/database/repository/param/letter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { LetterEntity } from '@database/entity/letter';
import { DefaultParameter } from './default';
import { LetterAttachmentEntity } from '@database/entity/letter.attachment';

export type Letter = Pick<
LetterEntity,
'userId' | 'letterCategoryCode' | 'title' | 'body' | 'commentYn' | 'attendYn'
>;

export type LetterAttachment = Pick<
LetterAttachmentEntity,
'letterId' | 'attachmentId'
>;

export class InsertLetter extends DefaultParameter {
letter: Letter;
}
export class InsertLetterAttachment extends DefaultParameter {
letterAttachments: Array<LetterAttachment>;
}
46 changes: 46 additions & 0 deletions src/database/transaction.base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Injectable } from '@nestjs/common';
import { DataSource, EntityManager, QueryRunner } from 'typeorm';
/**
* 트랜잭션에는 데이터베이스 CRUD 로직과 비즈니스 로직이 모두 포함된다.
* 서비스 레이어와 데이터 레이어 사이에서 트랜잭션을 실행시키기 위한 추상 클래스
* 이 클래스를 상속받아 필요한 트랜잭션을 작성한다.
*/

@Injectable()
export abstract class BaseTransaction<TransactionInput, TransactionOutput> {
protected constructor(private readonly datasource: DataSource) {}

protected abstract execute(
data: TransactionInput,
manager: EntityManager,
): Promise<TransactionOutput>;

private async createRunner(): Promise<QueryRunner> {
return this.datasource.createQueryRunner();
}

async run(data: TransactionInput): Promise<TransactionOutput> {
const queryRunner = await this.createRunner();

await queryRunner.connect();
await queryRunner.startTransaction('REPEATABLE READ'); // 격리수준 RPEATABLE READ

try {
const result = await this.execute(data, queryRunner.manager);
await queryRunner.commitTransaction();
return result;
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
}

async runWithinTransaction(
data: TransactionInput,
manager: EntityManager,
): Promise<TransactionOutput> {
return this.execute(data, manager);
}
}
97 changes: 50 additions & 47 deletions src/domain/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
import { Body, Controller, Delete, Post, Req, Res, UseGuards } from "@nestjs/common";
import { SignRequest } from "./dto/sign";
import { AuthService } from "./auth.service";
import { UserGuard } from "@app/jwt/guard/user.guard";
import {
Body,
Controller,
Delete,
Post,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { SignRequest } from './dto/sign';
import { AuthService } from './auth.service';
import { UserGuard } from '@app/jwt/guard/user.guard';
import { Response } from 'express';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

constructor(
private readonly authService : AuthService
) {}
@Post('signUp')
async signUp(
@Body() dto: SignRequest,
@Res({ passthrough: true }) res: Response,
) {
const data = await this.authService.signUp(dto.phone, dto.password);
res.setHeader('Authorization', `Bearer ${data.access}`);
return {
result: true,
};
}

@Post('signUp')
async signUp(
@Body() dto : SignRequest,
@Res({passthrough : true}) res:Response
) {
const data = await this.authService.signUp(dto.phone,dto.password);
res.setHeader('Authorization',`Bearer ${data.access}`)
return {
result : true
}
}
@Post('signIn')
async signIn(
@Body() dto: SignRequest,
@Res({ passthrough: true }) res: Response,
) {
const data = await this.authService.signIn(dto.phone, dto.password);
res.setHeader('Authorization', `Bearer ${data.access}`);
return {
result: true,
};
}

@Post('signIn')
async signIn(
@Body() dto : SignRequest,
@Res({passthrough : true}) res:Response
) {
const data = await this.authService.signIn(dto.phone,dto.password);
res.setHeader('Authorization',`Bearer ${data.access}`)
return {
result : true
}
}
@Post('signOut')
@UseGuards(UserGuard)
async signOut(@Req() req) {
return {
result: true,
};
}

@Post('signOut')
@UseGuards(UserGuard)
async signOut(
@Req() req
) {
return {
result : true
}
}

@Delete('account')
@UseGuards(UserGuard)
async deleteAccount(@Body() dto: SignRequest) {
return {
result : true
}
}
}
@Delete('account')
@UseGuards(UserGuard)
async deleteAccount(@Body() dto: SignRequest) {
return {
result: true,
};
}
}
Loading

0 comments on commit 9591dbd

Please sign in to comment.