-
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.
Merge pull request #70 from boostcampwm2023/feat/64-save-default-shap…
…e-to-db [Feat] 기본 모양 객체 응답 및 uuid에 해당하는 모양 스트림 반환 API 구현
- Loading branch information
Showing
8 changed files
with
144 additions
and
2 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
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,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; | ||
}, | ||
); |
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,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); | ||
} | ||
} |
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,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" }, | ||
]; |
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,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 {} |
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,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 = Promise.resolve(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; | ||
} | ||
} |
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,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); | ||
} | ||
} |