Skip to content

Commit

Permalink
[BE] ✨ : 실시간 위치 기록 api 구현 (#99)
Browse files Browse the repository at this point in the history
* swagger 설정

* 📝 .gitignore에 .env추가

* 📝 Dotenv 설치

* ✨ 실시간 위치 기록 추가

* 🧪 실시간 기록 테스트파일 추가

* ✨ Swagger 추가

실시간 위치 기록 api에 swagger추가
  • Loading branch information
twoo1999 authored Nov 23, 2023
1 parent 2a42238 commit 266a0b4
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 15 deletions.
4 changes: 3 additions & 1 deletion BE/musicspot/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ lerna-debug.log*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/extensions.json

.env
22 changes: 22 additions & 0 deletions BE/musicspot/package-lock.json

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

2 changes: 2 additions & 0 deletions BE/musicspot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"@nestjs/mongoose": "^10.0.2",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.15",
"@types/dotenv": "^8.2.0",
"@types/multer": "^1.4.10",
"aws-sdk": "^2.348.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"dotenv": "^16.3.1",
"gitmoji-cli": "^9.0.0",
"mongoose": "^8.0.0",
"reflect-metadata": "^0.1.13",
Expand Down
3 changes: 3 additions & 0 deletions BE/musicspot/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { MongooseModule } from '@nestjs/mongoose';
import { JourneyModule } from './journey/journey.module';
import { SpotModule } from './spot/spot.module';
import { UserModule } from './user/user.module';
import * as dotenv from 'dotenv';
dotenv.config();

@Module({
imports: [
MongooseModule.forRoot(
Expand Down
29 changes: 29 additions & 0 deletions BE/musicspot/src/journey/dto/journeyRecord.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ApiProperty } from '@nestjs/swagger';
import {
ArrayMaxSize,
ArrayMinSize,
IsArray,
IsNumber,
IsString,
} from 'class-validator';

export class RecordJourneyDTO {
@ApiProperty({
example: '655efda2fdc81cae36d20650',
description: '여정 id',
required: true,
})
@IsString()
readonly journeyId: string;

@ApiProperty({
example: [37.555946, 126.972384],
description: '위치 좌표',
required: true,
})
@IsArray()
@ArrayMaxSize(2, { message: 'coordinate has only 2' })
@ArrayMinSize(2, { message: 'coordinate has only 2' })
@IsNumber({}, { each: true })
readonly coordinate: number[];
}
14 changes: 13 additions & 1 deletion BE/musicspot/src/journey/journey.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { StartJourneyDTO } from './dto/journeyStart.dto';
import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Journey } from './journey.schema';
import { EndJourneyDTO } from './dto/journeyEnd.dto';
import { RecordJourneyDTO } from './dto/journeyRecord.dto';

@Controller('journey')
@ApiTags('journey 관련 API')
Expand All @@ -19,7 +20,6 @@ export class JourneyController {
description: '생성된 여정 데이터를 반환',
type: Journey,
})

@Post('start')
async create(@Body() startJourneyDTO: StartJourneyDTO) {
return await this.journeyService.create(startJourneyDTO);
Expand All @@ -37,4 +37,16 @@ export class JourneyController {
return await this.journeyService.end(endJourneyDTO);
}

@ApiOperation({
summary: '여정 좌표 기록API',
description: '여정의 좌표를 기록합니다.',
})
@ApiCreatedResponse({
description: '생성된 여정 데이터를 반환',
type: Journey,
})
@Post('record')
async record(@Body() recordJourneyDTO: RecordJourneyDTO) {
return await this.journeyService.pushCoordianteToJourney(recordJourneyDTO);
}
}
55 changes: 45 additions & 10 deletions BE/musicspot/src/journey/journey.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,50 @@ import { UserService } from '../user/user.service';
describe('JourneysService', () => {
let service: JourneyService;
let userModel;
let journeyModel;

let mockJourneyModel = {
save: jest.fn(),
updateOne: jest.fn(),
};
beforeAll(async () => {
mongoose.connect(
`mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`,
);
userModel = mongoose.model(User.name, UserSchema);
journeyModel = mongoose.model(Journey.name, JourneySchema);
// mongoose.connect(
// `mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`,
// );
// userModel = mongoose.model(User.name, UserSchema);
// journeyModel = mongoose.model(Journey.name, JourneySchema);

// const module: TestingModule = await Test.createTestingModule({
// providers: [
// JourneyService,
// UserService,
// {
// provide: getModelToken(Journey.name),
// useValue: journeyModel,
// },
// {
// provide: getModelToken(User.name),
// useValue: userModel,
// },
// ],
// }).compile();

const module: TestingModule = await Test.createTestingModule({
providers: [
JourneyService,
UserService,
{
provide: getModelToken(Journey.name),
useValue: journeyModel,
useValue: mockJourneyModel,
},
{
provide: getModelToken(User.name),
useValue: userModel,
},
],
}).compile();

service = module.get<JourneyService>(JourneyService);
});

it('journey 시작 테스트', async () => {
it.skip('journey 시작 테스트', async () => {
const coordinate = [37.675986, 126.776032];
const timestamp = '2023-11-22T15:30:00.000+09:00';
const email = 'test-email';
Expand All @@ -62,6 +78,25 @@ describe('JourneysService', () => {
expect(updateUserInfo.modifiedCount).toEqual(1);
});

it('실시간 위치 기록 테스트', async () => {
const coordinate = [37.675986, 126.776032];
const journeyId = '655efda2fdc81cae36d20650';
mockJourneyModel.updateOne.mockResolvedValue({
acknowledged: true,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1,
});
const returnData = await service.pushCoordianteToJourney(
journeyId,
coordinate,
);

expect(returnData.modifiedCount).toEqual(1);
// 위치와 여정 아이디를 받음
// journey에 저장
});
afterAll(async () => {
mongoose.connection.close();
});
Expand Down
13 changes: 11 additions & 2 deletions BE/musicspot/src/journey/journey.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Journey } from './journey.schema';
import { User } from '../user/user.schema';
import { UserService } from '../user/user.service';
import { EndJourneyDTO } from './dto/journeyEnd.dto';
import { RecordJourneyDTO } from './dto/journeyRecord.dto';

@Injectable()
export class JourneyService {
Expand All @@ -15,9 +16,9 @@ export class JourneyService {
@InjectModel(User.name) private userModel: Model<User>,
) {}
async insertJourneyData(startJourneyDTO: StartJourneyDTO) {

const journeyData = {
const journeyData: Journey = {
...startJourneyDTO,
title: '',
spots: [],
coordinates: [startJourneyDTO.coordinate],
};
Expand Down Expand Up @@ -45,4 +46,12 @@ export class JourneyService {
//check 참 조건인지 확인
return journey.coordinates.length;
}

async pushCoordianteToJourney(recordJourneyDTO: RecordJourneyDTO) {
const { journeyId, coordinate } = recordJourneyDTO;
return await this.journeyModel.updateOne(
{ _id: journeyId },
{ $push: { coordinates: coordinate } },
);
}
}
2 changes: 1 addition & 1 deletion BE/musicspot/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ValidationPipe } from '@nestjs/common';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = new DocumentBuilder()
.setTitle('Cats example') // 문서의 제목
.setTitle('Music Spot') // 문서의 제목
.setDescription('iOS01 Music Spot App API') // 문서의 간단한 설명
.setVersion('1.0') // API의 버전(업데이트 버전)
.addTag('Music Spot')
Expand Down

0 comments on commit 266a0b4

Please sign in to comment.