Skip to content

Commit

Permalink
Merge pull request #352 from boostcampwm2023/BE/task/modify-db
Browse files Browse the repository at this point in the history
[BE] โ™ป๏ธ : db์ˆ˜์ •(mongodb -> mysql)
  • Loading branch information
twoo1999 authored Jan 27, 2024
2 parents b912c42 + 13935e1 commit 5ccdf6d
Show file tree
Hide file tree
Showing 31 changed files with 1,240 additions and 696 deletions.
585 changes: 517 additions & 68 deletions BE/musicspot/package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion BE/musicspot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@nestjs/mongoose": "^10.0.2",
"@nestjs/platform-express": "^10.2.10",
"@nestjs/swagger": "^7.1.15",
"@nestjs/typeorm": "^10.0.1",
"@turf/turf": "^6.5.0",
"@types/dotenv": "^8.2.0",
"@types/multer": "^1.4.10",
Expand All @@ -35,12 +36,15 @@
"gitmoji-cli": "^9.0.0",
"handlebars": "^4.7.8",
"mongoose": "^8.0.0",
"mysql2": "^3.7.0",
"nest-winston": "^1.9.4",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"typeorm": "^0.3.19",
"uuid": "^9.0.1",
"winston": "^3.11.0",
"winston-daily-rotate-file": "^4.7.1"
"winston-daily-rotate-file": "^4.7.1",
"wkx": "^0.5.0"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
26 changes: 23 additions & 3 deletions BE/musicspot/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@ import { UserModule } from './user/module/user.module';

import { ReleaseController } from './releasePage/release.controller';
import { LoggerMiddleware } from './common/middleware/logger.middleware';
import { TypeOrmModule } from '@nestjs/typeorm';

import * as dotenv from 'dotenv';
import { User } from './user/entities/user.entity';
import { Journey } from './journey/entities/journey.entity';
import {Spot} from './spot/entities/spot.entity'
dotenv.config();

@Module({
imports: [
MongooseModule.forRoot(
`mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`,
),
TypeOrmModule.forRoot({
type: 'mysql',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: [
User, Journey, Spot,
],
synchronize: false,
legacySpatialSupport: false,
}),
// MongooseModule.forRoot(
// `mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`,
// ),

JourneyModule,
UserModule,
SpotModule,
Expand Down
10 changes: 10 additions & 0 deletions BE/musicspot/src/common/decorator/customRepository.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SetMetadata } from "@nestjs/common";

export const TYPEORM_EX_CUSTOM_REPOSITORY = "TYPEORM_EX_CUSTOM_REPOSITORY";

export function CustomRepository(entity : Function): ClassDecorator{
return SetMetadata(TYPEORM_EX_CUSTOM_REPOSITORY, entity);
}

// SetMetadata๋Š” key, value ํ˜•ํƒœ์˜ ๊ฐ’์„ ์ทจํ•จ
// ์ฆ‰ TYPEORM_EX_CUSTOM_REPOSITORY : entity
30 changes: 29 additions & 1 deletion BE/musicspot/src/common/util/coordinate.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@ export const isInCoordinateRange = (pos) => {
return false;
}
return true;
};
};



export const parseCoordinateFromDtoToGeo = (coordinate: [number, number])=>{
// coordinate = [1, 2]
return `${coordinate.join(' ')}`

}

export const parseCoordinateFromGeoToDto = (coordinate: string) =>{
// coordinate = 'POINT(1 2)'

const pointLen = 'POINT('.length;
const numberOfCoordinate = coordinate.slice(pointLen, -1);
return numberOfCoordinate.split(' ').map(pos=>Number(pos));

}

export const parseCoordinatesFromDtoToGeo = (coordinates: number[][]) =>{
// coordinates = [[1,2], [3,4]]
return `${coordinates.map(coordinate=>`${coordinate[0]} ${coordinate[1]}`).join(',')}`
}

export const parseCoordinatesFromGeoToDto = (coordinates: string) :number[][]=>{
const lineStringLen = 'LINESTRING('.length;
const numberOfCoordinates = coordinates.slice(lineStringLen, -1);
return numberOfCoordinates.split(',').map(coordinate=>coordinate.split(' ').map(num=>Number(num.trim())))
}
32 changes: 32 additions & 0 deletions BE/musicspot/src/dynamic.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DynamicModule, Provider } from "@nestjs/common";
import { TYPEORM_EX_CUSTOM_REPOSITORY } from "./common/decorator/customRepository.decorator";
import { getDataSourceToken } from "@nestjs/typeorm";
import { DataSource } from "typeorm";

export class TypeOrmExModule{
public static forFeature<T extends new (...args: any[]) => any>(repositories: T[]): DynamicModule {
const providers: Provider[] = [];
for(const repository of repositories){

const entity = Reflect.getMetadata(TYPEORM_EX_CUSTOM_REPOSITORY, repository);

if(!entity){
continue;
}

providers.push({
inject: [getDataSourceToken()],
provide : repository,
useFactory : (dataSource:DataSource) : typeof repository =>{
const baseRepository = dataSource.getRepository<any>(entity);
return new repository(baseRepository.target, baseRepository.manager, baseRepository)
}
})
}
return {
exports : providers,
module : TypeOrmExModule,
providers
}
}
}
29 changes: 8 additions & 21 deletions BE/musicspot/src/journey/controller/journey.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ import {
Delete,
} from '@nestjs/common';
import { JourneyService } from '../service/journey.service';

import { StartJourneyReqDTO } from '../dto/journeyStart/journeyStart.dto';

import {
ApiCreatedResponse,
ApiOperation,
ApiQuery,
ApiTags,
} from '@nestjs/swagger';
import { Journey } from '../schema/journey.schema';
import {
CheckJourneyResDTO,
CheckJourneyReqDTO,
Expand All @@ -35,6 +32,7 @@ import {
} from '../dto/journeyRecord/journeyRecord.dto';
import { StartJourneyResDTO } from '../dto/journeyStart/journeyStart.dto';
import { DeleteJourneyReqDTO } from '../dto/journeyDelete.dto';
import { Journey } from '../entities/journey.entity';

@Controller('journey')
@ApiTags('journey ๊ด€๋ จ API')
Expand All @@ -51,7 +49,7 @@ export class JourneyController {
})
@Post('start')
async create(@Body() startJourneyDTO: StartJourneyReqDTO) {
return await this.journeyService.create(startJourneyDTO);
return await this.journeyService.insertJourneyData(startJourneyDTO);
}

@ApiOperation({
Expand Down Expand Up @@ -81,6 +79,8 @@ export class JourneyController {
await this.journeyService.pushCoordianteToJourney(recordJourneyDTO);
return returnData;
}


@ApiOperation({
summary: '์—ฌ์ • ์กฐํšŒ API',
description: 'ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์˜ ์—ฌ์ •๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.',
Expand Down Expand Up @@ -124,7 +124,7 @@ export class JourneyController {
minCoordinate,
maxCoordinate,
};
return await this.journeyService.checkJourney(checkJourneyDTO);
return await this.journeyService.getJourneyByCoordinationRange(checkJourneyDTO);
}

@ApiOperation({
Expand All @@ -135,9 +135,9 @@ export class JourneyController {
description: '์‚ฌ์šฉ์ž๊ฐ€ ์ง„ํ–‰์ค‘์ด์—ˆ๋˜ ์—ฌ์ • ์ •๋ณด',
type: Journey,
})
@Get(':userId/last')
async loadLastData(@Param('userId') userId: string) {
return await this.journeyService.loadLastJourney(userId);
@Get('last')
async loadLastData(@Body('userId') userId) {
return await this.journeyService.getLastJourneyByUserId(userId);
}

@ApiOperation({
Expand Down Expand Up @@ -167,16 +167,3 @@ export class JourneyController {
}
}

// @ApiOperation({
// summary: '์—ฌ์ • ์กฐํšŒ API',
// description: 'ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์˜ ์—ฌ์ •๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.',
// })
// @ApiCreatedResponse({
// description: '๋ฒ”์œ„์— ์žˆ๋Š” ์—ฌ์ •์˜ ๊ธฐ๋ก๋“ค์„ ๋ฐ˜ํ™˜',
// type: CheckJourneyResDTO,
// })
// @Post('check')
// @UsePipes(ValidationPipe) //์œ ํšจ์„ฑ ์ฒดํฌ
// async checkPost(@Body() checkJourneyDTO: CheckJourneyReqDTO) {
// return await this.journeyService.checkJourney(checkJourneyDTO);
// }
14 changes: 3 additions & 11 deletions BE/musicspot/src/journey/dto/journeyDelete.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsDateString } from 'class-validator';
import { IsString, IsDateString, IsNumber } from 'class-validator';
import { UUID } from 'crypto';
export class DeleteJourneyReqDTO {
@ApiProperty({
Expand All @@ -15,8 +15,8 @@ export class DeleteJourneyReqDTO {
description: '์—ฌ์ • id',
required: true,
})
@IsString()
readonly journeyId: string;
@IsNumber()
readonly journeyId: number;
}

export class DeleteJourneyResDTO {
Expand All @@ -42,12 +42,4 @@ export class DeleteJourneyResDTO {
})
@IsString()
readonly journeyId: string;

// @ApiProperty({
// example: '[email protected]',
// description: '์ด๋ฉ”์ผ',
// required: true,
// })
// @IsString()
// readonly email: string;
}
29 changes: 13 additions & 16 deletions BE/musicspot/src/journey/dto/journeyEnd/journeyEnd.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ import {
IsDefined,
IsNumber,
IsArray,
IsObject,
} from 'class-validator';
import {
IsCoordinate,
IsCoordinates,
} from '../../../common/decorator/coordinate.decorator';
import { Type } from 'class-transformer';
import { SongDTO } from '../song/song.dto';
import { IsObjectId } from 'src/common/decorator/objectId.decorator';



export class EndJourneyReqDTO {
@ApiProperty({
example: '655efda2fdc81cae36d20650',
example: 20,
description: '์—ฌ์ • id',
required: true,
})
@IsObjectId({ message: 'ObjectId ํ˜•์‹๋งŒ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.' })
readonly journeyId: string;
// @IsObjectId({ message: 'ObjectId ํ˜•์‹๋งŒ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.' })
@IsNumber()
readonly journeyId: number;

@ApiProperty({
example: [
Expand All @@ -31,6 +34,7 @@ export class EndJourneyReqDTO {
],
description: '์œ„์น˜ ์ขŒํ‘œ',
required: true,

})
@IsCoordinates({
message:
Expand All @@ -54,25 +58,22 @@ export class EndJourneyReqDTO {
@IsString()
readonly title: string;

@IsObject()
@ApiProperty({
type: SongDTO,
description: '๋…ธ๋ž˜ ์ •๋ณด',
required: true,
})
@ValidateNested()
@Type(() => SongDTO)
@IsDefined()
readonly song: SongDTO;
readonly song: object;
}

export class EndJourneyResDTO {
@ApiProperty({
example: '655efda2fdc81cae36d20650',
example: 20,
description: '์—ฌ์ • id',
required: true,
})
@IsString()
readonly journeyId: string;
readonly journeyId: number;

@ApiProperty({
example: [
Expand Down Expand Up @@ -102,12 +103,8 @@ export class EndJourneyResDTO {
readonly numberOfCoordinates: number;

@ApiProperty({
type: SongDTO,
description: '๋…ธ๋ž˜ ์ •๋ณด',
required: true,
})
@ValidateNested({ each: true })
@Type(() => SongDTO)
@IsDefined()
readonly song: SongDTO;
readonly song: Object;
}
38 changes: 20 additions & 18 deletions BE/musicspot/src/journey/dto/journeyRecord/journeyRecord.dto.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsString } from 'class-validator';
import { IsNumber} from 'class-validator';
import {
IsCoordinate,
IsCoordinates,
} from '../../../common/decorator/coordinate.decorator';
import { IsObjectId } from 'src/common/decorator/objectId.decorator';
export class RecordJourneyResDTO {
@ApiProperty({
example: [
[37.555946, 126.972384],
[37.555946, 126.972384],
],
description: '์ €์žฅ๋œ ์œ„์น˜ ์ขŒํ‘œ',
required: true,
})
@IsCoordinates()
readonly coordinates: number[][];
}

export class RecordJourneyReqDTO {
@ApiProperty({
example: '655efda2fdc81cae36d20650',
example: 20,
description: '์—ฌ์ • id',
required: true,
})
@IsObjectId({ message: 'ObjectId ํ˜•์‹๋งŒ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.' })
readonly journeyId: string;
@IsNumber()
readonly journeyId: number;

@ApiProperty({
example: [
Expand All @@ -41,3 +27,19 @@ export class RecordJourneyReqDTO {
})
readonly coordinates: number[][];
}


export class RecordJourneyResDTO {
@ApiProperty({
example: [
[37.555946, 126.972384],
[37.555946, 126.972384],
],
description: '์ €์žฅ๋œ ์œ„์น˜ ์ขŒํ‘œ',
required: true,
})
@IsCoordinates()
readonly coordinates: number[][];
}


Loading

0 comments on commit 5ccdf6d

Please sign in to comment.