Skip to content

Commit

Permalink
Merge pull request #92 from boostcampwm2023/BE/task/record-spot-with-…
Browse files Browse the repository at this point in the history
…photo

[BE] ✨ : spot 기록 시 storage에 사진 추가 및 URL 반환하는 기능 추가
  • Loading branch information
vvans authored Nov 23, 2023
2 parents ac44a95 + a5a6b15 commit 6720d9c
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 32 deletions.
133 changes: 119 additions & 14 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,6 +25,8 @@
"@nestjs/mongoose": "^10.0.2",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.15",
"@types/multer": "^1.4.10",
"aws-sdk": "^2.348.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"gitmoji-cli": "^9.0.0",
Expand Down
2 changes: 2 additions & 0 deletions BE/musicspot/src/spot/dto/recordSpot.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export class RecordSpotDTO {

@IsString()
readonly timestamp: string;

readonly photoData: Buffer;
}
23 changes: 20 additions & 3 deletions BE/musicspot/src/spot/spot.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { Controller, Post, Body } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
Controller,
Post,
Body,
UseInterceptors,
UploadedFile,
} from '@nestjs/common';
import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RecordSpotDTO } from './dto/recordSpot.dto';
import { SpotService } from './spot.service';

import { FileInterceptor } from '@nestjs/platform-express';
import { Spot } from './spot.schema';
@Controller('spot')
@ApiTags('spot 관련 API')
export class SpotController {
constructor(private spotService: SpotService) {}

@ApiOperation({
summary: 'spot 기록 시 실행되는 API',
description:
'request로 여정 ID(string), 위치좌표([number, number]), timestamp(string), 유저 이메일(string)을 필요합니다.',
})
@ApiCreatedResponse({
description: '생성된 spot 데이터를 반환',
type: Spot,
})
@Post()
async create(@Body() recordSpotDTO: RecordSpotDTO) {
return await this.spotService.create(recordSpotDTO);
Expand Down
5 changes: 4 additions & 1 deletion BE/musicspot/src/spot/spot.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ export type SpotDocument = HydratedDocument<Spot>;
@Schema({ collection: 'spot' })
export class Spot {
@Prop()
journalId: string;
journeyId: string;

@Prop({ type: [Number] })
coordinate: number[];

@Prop({ type: String })
timestamp: string;

@Prop({ type: String })
photoUrl: string;
}

export const SpotSchema = SchemaFactory.createForClass(Spot);
29 changes: 20 additions & 9 deletions BE/musicspot/src/spot/spot.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
import * as fs from 'fs';
import * as path from 'path';
import { Test, TestingModule } from '@nestjs/testing';
import { SpotService } from './spot.service';
import mongoose from 'mongoose';
import { Spot, SpotSchema } from './spot.schema';
import { getModelToken } from '@nestjs/mongoose';

import { Journey, JourneySchema } from '../journey/journey.schema';
import { ConfigBase } from 'aws-sdk/lib/config';
describe('SpotService', () => {
let service: SpotService;
let spotModel;
beforeEach(async () => {
let journeyModel;
beforeAll(async () => {
mongoose.connect('mongodb://192.168.174.128:27017/musicspotDB');
spotModel = mongoose.model(Spot.name, SpotSchema);
journeyModel = mongoose.model(Journey.name, JourneySchema);
const module: TestingModule = await Test.createTestingModule({
providers: [
SpotService,
{
provide: getModelToken(Spot.name),
useValue: spotModel,
},
{ provide: getModelToken(Journey.name), useValue: journeyModel },
],
}).compile();

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

it('spot 삽입 테스트', async () => {
console.log(await spotModel.find().exec());
const prevData = (await spotModel.find().exec()).length;
const imagePath = path.join(__dirname, 'test/test.png');

// 이미지 파일 동기적으로 읽기
const imageBuffer = fs.readFileSync(imagePath);

const journeyId = '655b6d6bfd9f60fc689789a6';
const data = {
journalId: '65560b729f8c1cbb025ab722',
journeyId,
coordinate: [10, 10],
timestamp: '1시5분',
timestamp: '1시50분',
photo: imageBuffer,
};
await service.create(data);
console.log(prevData);
const nextData = (await spotModel.find().exec()).length;
expect(prevData + 1).toEqual(nextData);
const photoUrl = await service.uploadPhotoToStorage(data.photo);
const createdSpotData = await service.insertToSpot({ ...data, photoUrl });
expect(createdSpotData.journeyId).toEqual(journeyId);
});

afterAll(() => {
Expand Down
Loading

0 comments on commit 6720d9c

Please sign in to comment.