Skip to content

Commit

Permalink
Merge branch 'main' into feat/71-diary-create-api
Browse files Browse the repository at this point in the history
  • Loading branch information
dmson1218 committed Nov 21, 2023
2 parents 94f7f26 + cf1ffd8 commit 1e9ff60
Show file tree
Hide file tree
Showing 23 changed files with 324 additions and 70 deletions.
21 changes: 20 additions & 1 deletion BE/package-lock.json

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

11 changes: 6 additions & 5 deletions BE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start": "cross-env NODE_ENV=production nest start",
"start:dev": "cross-env NODE_ENV=dev nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test": "cross-env NODE_ENV=test jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "cross-env NODE_ENV=test jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand All @@ -29,9 +29,9 @@
"@nestjs/typeorm": "^10.0.0",
"@types/dotenv": "^8.2.0",
"@types/passport-jwt": "^3.0.13",
"aws-sdk": "^2.1499.0",
"bcryptjs": "^2.4.3",
"class-transformer": "^0.5.1",
"aws-sdk": "^2.1499.0",
"class-validator": "^0.14.0",
"dotenv": "^16.3.1",
"mysql2": "^3.6.3",
Expand All @@ -51,6 +51,7 @@
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"cross-env": "^7.0.3",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
Expand Down
31 changes: 29 additions & 2 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
import "dotenv/config";
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { typeORMConfig } from "./configs/typeorm.config";
import { UsersModule } from "./users/users.module";
import { DiariesModule } from "./diaries/diaries.module";
import { AuthModule } from "./auth/auth.module";
import { IntroduceModule } from "./introduce/introduce.module";
import { ShapesModule } from "./shapes/shapes.module";
import { ShapesRepository } from "./shapes/shapes.repository";
import { UsersRepository } from "./users/users.repository";
import { typeORMTestConfig } from "./configs/typeorm.test.config";

@Module({
imports: [
TypeOrmModule.forRoot(typeORMConfig),
TypeOrmModule.forRoot(
process.env.NODE_ENV === "test" ? typeORMTestConfig : typeORMConfig,
),
UsersModule,
DiariesModule,
AuthModule,
IntroduceModule,
ShapesModule,
],
providers: [ShapesRepository, UsersRepository],
})
export class AppModule {}
export class AppModule {
constructor(
private shapesRepository: ShapesRepository,
private usersRepository: UsersRepository,
) {}

async onModuleInit() {
const commonUser =
(await this.usersRepository.getUserByUserId("commonUser")) ||
(await this.usersRepository.createUser({
userId: "commonUser",
password: process.env.COMMON_USER_PASS,
nickname: "commonUser",
email: "[email protected]",
}));

await this.shapesRepository.createDefaultShapes(commonUser);
}
}
9 changes: 9 additions & 0 deletions BE/src/auth/get-user.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createParamDecorator, ExecutionContext } from "@nestjs/common";
import { User } from "src/users/users.entity";

export const GetUser = createParamDecorator(
(_: unknown, ctx: ExecutionContext): User => {
const req = ctx.switchToHttp().getRequest();
return req.user;
},
);
14 changes: 1 addition & 13 deletions BE/src/configs/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ export const typeORMConfig: TypeOrmModuleOptions = {
password: process.env.DB_PASS,
database: process.env.DB_NAME,
entities: ["dist/**/*.entity{.ts,.js}"],
synchronize: true,
timezone: "+09:00",
};

export const typeORMTestConfig: TypeOrmModuleOptions = {
type: "mysql",
host: process.env.DB_HOST,
port: 3306,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
entities: ["src/**/*.entity{.ts,.js}"],
synchronize: true,
synchronize: false,
timezone: "+09:00",
};
14 changes: 14 additions & 0 deletions BE/src/configs/typeorm.test.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "dotenv/config";
import { TypeOrmModuleOptions } from "@nestjs/typeorm";

export const typeORMTestConfig: TypeOrmModuleOptions = {
type: "mysql",
host: process.env.DB_HOST,
port: 3306,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.TEST_DB_NAME,
entities: ["src/**/*.entity{.ts,.js}"],
synchronize: true,
timezone: "+09:00",
};
16 changes: 14 additions & 2 deletions BE/src/diaries/diaries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CreateDiaryDto {
content: string;

@IsString()
@Matches(RegExp("^-?d+(.d+)?,-?d+(.d+)?,-?d+(.d+)?$"), {
@Matches(RegExp(/^-?d+(.d+)?,-?d+(.d+)?,-?d+(.d+)?$/), {
message: "적절하지 않은 포인트 양식입니다",
})
point: string;
Expand All @@ -18,6 +18,9 @@ export class CreateDiaryDto {

@IsArray()
tags: string[];

@IsUUID()
shapeUuid: string;
}

export class ReadDiaryDto {
Expand All @@ -35,10 +38,19 @@ export class UpdateDiaryDto {
@IsString()
content: string;

@IsString()
@Matches(RegExp("^-?d+(.d+)?,-?d+(.d+)?,-?d+(.d+)?$"), {
message: "적절하지 않은 포인트 양식입니다",
})
point: string;

@IsDate()
date: Date;

@IsString()
@IsArray()
tags: string[];

@IsUUID()
shapeUuid: string;
}

Expand Down
3 changes: 0 additions & 3 deletions BE/src/diaries/diaries.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ export class Diary extends BaseEntity {
@Column({ type: "text" })
content: string;

@Column({ length: 7 })
color: string;

@Column({ type: "float" })
positiveRatio: number;

Expand Down
2 changes: 0 additions & 2 deletions BE/src/diaries/diaries.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class DiariesRepository {
const content = encodedContent;

// 미구현 기능을 대체하기 위한 임시 값
const color = "#FFFFFF";
const positiveRatio = 0.0;
const negativeRatio = 100.0;
const neutralRatio = 0.0;
Expand All @@ -34,7 +33,6 @@ export class DiariesRepository {
content,
point,
date,
color,
positiveRatio,
negativeRatio,
neutralRatio,
Expand Down
1 change: 1 addition & 0 deletions BE/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppModule } from "./app.module";

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors();
await app.listen(process.env.BE_PORT);
}
bootstrap();
25 changes: 25 additions & 0 deletions BE/src/shapes/shapes.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller, Get, Param, UseGuards } from "@nestjs/common";
import { ShapesService } from "./shapes.service";
import { AuthGuard } from "@nestjs/passport";
import { Shape } from "./shapes.entity";
import { GetUser } from "src/auth/get-user.decorator";
import { User } from "src/users/users.entity";

@Controller("shapes")
export class ShapesController {
constructor(private shapesService: ShapesService) {}

@Get("/default")
async getDefaultShapeFiles(): Promise<Shape[]> {
return this.shapesService.getDefaultShapeFiles();
}

@Get("/:uuid")
@UseGuards(AuthGuard())
async getShapeFilesByUuid(
@Param("uuid") uuid: string,
@GetUser() user: User,
): Promise<object> {
return this.shapesService.getShapeFileByUuid(uuid, user);
}
}
7 changes: 7 additions & 0 deletions BE/src/shapes/shapes.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Shape } from "./shapes.entity";

export const defaultShapes: Partial<Shape>[] = [
{ shapePath: "test-basic-shape-1.png" },
{ shapePath: "test-basic-shape-2.png" },
{ shapePath: "test-basic-shape-3.png" },
];
15 changes: 15 additions & 0 deletions BE/src/shapes/shapes.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { UsersModule } from "src/users/users.module";
import { Shape } from "./shapes.entity";
import { ShapesController } from "./shapes.controller";
import { ShapesService } from "./shapes.service";
import { ShapesRepository } from "./shapes.repository";
import { AuthModule } from "src/auth/auth.module";

@Module({
imports: [TypeOrmModule.forFeature([Shape]), UsersModule, AuthModule],
controllers: [ShapesController],
providers: [ShapesService, ShapesRepository],
})
export class ShapesModule {}
34 changes: 34 additions & 0 deletions BE/src/shapes/shapes.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { defaultShapes } from "./shapes.default";
import { User } from "src/users/users.entity";
import { Shape } from "./shapes.entity";
import { NotFoundException } from "@nestjs/common";

export class ShapesRepository {
async createDefaultShapes(commonUser: User) {
await Promise.all(
defaultShapes.map(async (defaultShape) => {
const existingShape = await this.getShapeByShapePath(
defaultShape.shapePath,
);

if (existingShape) return;

defaultShape.user = commonUser;
const shape = Shape.create(defaultShape);
await shape.save();
}),
);
}

async getShapeByShapePath(shapePath: string): Promise<Shape | null> {
return Shape.findOne({ where: { shapePath } });
}

async getShapeByUuid(uuid: string): Promise<Shape> {
const found = await Shape.findOne({ where: { uuid } });
if (!found) {
throw new NotFoundException(`Can't find Shape with uuid: [${uuid}]`);
}
return found;
}
}
30 changes: 30 additions & 0 deletions BE/src/shapes/shapes.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable, UnauthorizedException } from "@nestjs/common";
import { ShapesRepository } from "./shapes.repository";
import { getFileFromS3 } from "src/utils/e3";
import { defaultShapes } from "./shapes.default";
import { Shape } from "./shapes.entity";
import { User } from "src/users/users.entity";

@Injectable()
export class ShapesService {
constructor(private shapesRepository: ShapesRepository) {}

async getDefaultShapeFiles(): Promise<Shape[]> {
const promises = defaultShapes.map(async (defaultShape) => {
return this.shapesRepository.getShapeByShapePath(defaultShape.shapePath);
});

const shapeFiles = await Promise.all(promises);
return shapeFiles;
}

async getShapeFileByUuid(uuid: string, user: User): Promise<object> {
const shape = await this.shapesRepository.getShapeByUuid(uuid);
const { userId, id } = await shape.user;

if (userId !== "commonUser" && id !== user.id) {
throw new UnauthorizedException();
}
return getFileFromS3(shape.shapePath);
}
}
Loading

0 comments on commit 1e9ff60

Please sign in to comment.