Skip to content

Commit

Permalink
Merge branch 'BE/release' into BE/story/record-spot
Browse files Browse the repository at this point in the history
  • Loading branch information
twoo1999 authored Nov 23, 2023
2 parents 6720d9c + b9d276c commit 86d7bed
Show file tree
Hide file tree
Showing 15 changed files with 725 additions and 38 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# name: musicspot github actions

# on:
# pull_request:
# branches: ["BE/release"]
# types: ["closed"]
# jobs:
# deploy:
# runs-on: ubuntu-latest

# steps:
# - name: Checkout repository
# uses: actions/checkout@v2

# - name: Set up Node.js
# uses: actions/setup-node@v2
# with:
# node-version: "20"

# - name: Install dependencies
# run: |
# cd BE
# cd musicspot
# npm install

# - name: Build Docker image
# run: docker build -t musicspot:latest ./BE/musicspot

# - name: Login to Docker Hub
# run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}

# - name: Push Docker image to Docker Hub
# run: docker push musicspot

# - name: SSH into Ubuntu server and pull the latest image
# uses: appleboy/ssh-action@master
# with:
# host: ${{ secrets.SERVER_IP }}
# username: ${{ secrets.SERVER_USERNAME }}
# password: ${{ secrets.PASSWORD }}
# script: |
# docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
# docker pull musicspot
# docker stop musicspot || true
# docker rm musicspot || true
# docker run -e SSH_HOST=${{ secrets.SSH_HOST }} -e SSH_PORT=${{ secrets.SSH_PORT }} -e SSH_USER=${{ secrets.SSH_USER }} -e SSH_PASSWORD=${{ secrets.SSH_PASSWORD }} -e DB_USERNAME=${{ secrets.DB_USERNAME }} -e DB_PASSWORD=${{ secrets.DB_PASSWORD }} -e DB_NAME=${{ secrets.DB_NAME }} -e DB_HOST=${{ secrets.DB_HOST }} -e SECRET_KEY=${{ secrets.SECRET_KEY }} -v /images:/images -v /images/profile:/images/profile -v /images/story:/images/story -d -p 3000:3000 --name heatpick-container geomgichoi/heatpick
4 changes: 4 additions & 0 deletions BE/musicspot/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.git
Dockerfile
node_modules
dist
1 change: 1 addition & 0 deletions BE/musicspot/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGODB_URI=mongodb://localhost:27017/musicspot
8 changes: 8 additions & 0 deletions BE/musicspot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:18
RUN mkdir -p /var/app
WORKDIR /var/app
COPY . .
RUN npm install
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/main.js"]
6 changes: 4 additions & 2 deletions BE/musicspot/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '@nestjs/mongoose';
import { JourneyModule } from './journey/journey.module';
import { UserModule } from './user/user.module';
import { SpotModule } from './spot/spot.module';
import { UserModule } from './user/user.module';
@Module({
imports: [
MongooseModule.forRoot('mongodb://192.168.174.128:27017/musicspotDB'),
MongooseModule.forRoot(
`mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`,
),
JourneyModule,
UserModule,
SpotModule,
Expand Down
15 changes: 15 additions & 0 deletions BE/musicspot/src/journey/dto/journeyEnd.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
ArrayMaxSize,
ArrayMinSize,
IsArray,
IsNumber,
IsString,
} from 'class-validator';

export class EndJourneyDTO {
@IsString()
readonly _id: string;

// @IsString()
// readonly timestamp: string;
}
3 changes: 0 additions & 3 deletions BE/musicspot/src/journey/dto/journeyStart.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import {
} from 'class-validator';

export class StartJourneyDTO {
@IsString()
readonly title: string;

@IsArray()
@ArrayMaxSize(2, { message: 'coordinate has only 2' })
@ArrayMinSize(2, { message: 'coordinate has only 2' })
Expand Down
47 changes: 44 additions & 3 deletions BE/musicspot/src/journey/journey.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JourneyController } from './journey.controller';

import { StartJourneyDTO } from './dto/journeyStart.dto';
import { JourneyService } from './journey.service';
import mongoose from 'mongoose';
import { User, UserSchema } from '../user/user.schema';
import { Journey, JourneySchema } from './journey.schema';
import { getModelToken } from '@nestjs/mongoose';

describe('JourneyController', () => {
let controller: JourneyController;
let userModel;
let journeyModel;

beforeEach(async () => {
beforeAll(async () => {
mongoose.connect('mongodb://192.168.174.128:27017/musicspotDB');
userModel = mongoose.model(User.name, UserSchema);
journeyModel = mongoose.model(Journey.name, JourneySchema);
const module: TestingModule = await Test.createTestingModule({
controllers: [JourneyController],
providers: [
JourneyService,
{
provide: getModelToken(Journey.name),
useValue: journeyModel,
},
{
provide: getModelToken(User.name),
useValue: userModel,
},
],

}).compile();

controller = module.get<JourneyController>(JourneyController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
it('/POST journey 테스트', async () => {
const coordinate = [37.675987, 126.776033];
const timestamp = '2023-11-22T15:30:00.000+09:00';
const email = 'test-email';

const JourneyData: StartJourneyDTO = {
coordinate,
timestamp,
email,
};
const createdJourneyData = await controller.create(JourneyData);
expect(coordinate).toEqual(createdJourneyData.coordinates[0]);
expect(timestamp).toEqual(createdJourneyData.timestamp);
expect(createdJourneyData.spots).toEqual([]);
});

afterAll(async () => {
mongoose.connection.close();

});
});
18 changes: 17 additions & 1 deletion BE/musicspot/src/journey/journey.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { JourneyService } from './journey.service';
import { StartJourneyDTO } from './dto/journeyStart.dto';
import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Journey } from './journey.schema';
import { EndJourneyDTO } from './dto/journeyEnd.dto';

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

@Post('start')
async create(@Body() startJourneyDTO: StartJourneyDTO) {
return await this.journeyService.create(startJourneyDTO);
}
@ApiOperation({
summary: '여정 종료를 눌렀을 시 실행되는 API',
description: 'request로 id값이 필요합니다',
})
@ApiCreatedResponse({
description: '현재는 좌표 데이터의 길이를 반환, 추후 참 거짓으로 변경 예정',
type: Journey,
})
@Post('end')
async end(@Body() endJourneyDTO: EndJourneyDTO) {
return await this.journeyService.end(endJourneyDTO);
}

}
4 changes: 4 additions & 0 deletions BE/musicspot/src/journey/journey.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class Journey {

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

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

}

export const JourneySchema = SchemaFactory.createForClass(Journey);
45 changes: 26 additions & 19 deletions BE/musicspot/src/journey/journey.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ describe('JourneysService', () => {
let service: JourneyService;
let userModel;
let journeyModel;
beforeEach(async () => {
mongoose.connect('mongodb://192.168.174.128:27017/musicspotDB');

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);

Expand All @@ -35,24 +38,28 @@ describe('JourneysService', () => {
});

it('journey 시작 테스트', async () => {
const insertData: StartJourneyDTO = {
title: 'test2',
coordinate: [100, 100],
timestamp: 'time test code',
email: 'test',
const coordinate = [37.675986, 126.776032];
const timestamp = '2023-11-22T15:30:00.000+09:00';
const email = 'test-email';

const createJourneyData: StartJourneyDTO = {
coordinate,
timestamp,
email,
};
const dataLength = (await journeyModel.find().exec()).length;
const journeyLength = (await userModel.find({ email: 'test' }).exec())[0]
.journeys.length;

await service.create(insertData);
const nextDataLength = (await journeyModel.find().exec()).length;
const nextJourneyLength = (
await userModel.find({ email: 'test' }).exec()
)[0].journeys.length;

expect(dataLength + 1).toEqual(nextDataLength);
expect(journeyLength + 1).toEqual(nextJourneyLength);

const createdJourneyData =
await service.insertJourneyData(createJourneyData);
expect(coordinate).toEqual(createdJourneyData.coordinates[0]);
expect(timestamp).toEqual(createdJourneyData.timestamp);
expect(createdJourneyData.spots).toEqual([]);

const updateUserInfo = await service.pushJourneyIdToUser(
createdJourneyData._id,
email,
);
// console.log(createdUserData);
expect(updateUserInfo.modifiedCount).toEqual(1);
});

afterAll(async () => {
Expand Down
37 changes: 28 additions & 9 deletions BE/musicspot/src/journey/journey.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,46 @@ import { InjectModel } from '@nestjs/mongoose';
import { Injectable } from '@nestjs/common';
import { StartJourneyDTO } from './dto/journeyStart.dto';
import { Journey } from './journey.schema';

import { User } from '../user/user.schema';
import { UserService } from '../user/user.service';
import { User } from 'src/user/user.schema';
import { EndJourneyDTO } from './dto/journeyEnd.dto';

@Injectable()
export class JourneyService {
constructor(
@InjectModel(Journey.name) private journeyModel: Model<Journey>,
@InjectModel(User.name) private personModel: Model<User>,
private userService: UserService,
@InjectModel(User.name) private userModel: Model<User>,
) {}
async insertJourneyData(startJourneyDTO: StartJourneyDTO) {

async create(startJourneyDTO: StartJourneyDTO): Promise<Journey> {
const journeyData = {
...startJourneyDTO,
spots: [],
coordinates: [startJourneyDTO.coordinate],
};
const createdJourney = new this.journeyModel(journeyData);
const returnData = await createdJourney.save();
const journeyId = returnData._id;
this.userService.appendJourneyIdToUser(startJourneyDTO.email, journeyId);
return returnData;
const createdJourneyData = new this.journeyModel(journeyData);
return await createdJourneyData.save();
}
async pushJourneyIdToUser(journeyId, userEmail) {
return await this.userModel.updateOne(
{ email: userEmail },
{ $push: { journeys: journeyId } },
);
}
async create(startJourneyDTO: StartJourneyDTO): Promise<Journey> {
const createdJourneyData = await this.insertJourneyData(startJourneyDTO);
const updateUserInfo = await this.pushJourneyIdToUser(
createdJourneyData._id,
startJourneyDTO.email,
);
return createdJourneyData;
}

async end(endJourneyDTO: EndJourneyDTO) {
const journeyId = endJourneyDTO._id;
const journey = await this.journeyModel.findById(journeyId).exec();
//check 참 조건인지 확인
return journey.coordinates.length;
}
}
4 changes: 3 additions & 1 deletion BE/musicspot/src/user/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ describe('UserService', () => {
let service: UserService;
let userModel;
beforeEach(async () => {
mongoose.connect('mongodb://192.168.174.128:27017/musicspotDB');
mongoose.connect(
`mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`,
);
userModel = mongoose.model(User.name, UserSchema);
const module: TestingModule = await Test.createTestingModule({
providers: [
Expand Down
Loading

0 comments on commit 86d7bed

Please sign in to comment.