-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe534c6
commit 9591dbd
Showing
32 changed files
with
1,111 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { EntityManager } from 'typeorm'; | ||
|
||
export class DefaultParameter { | ||
entityManager: EntityManager; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
Oops, something went wrong.