Skip to content

Commit

Permalink
Revert "[#9] Modify createPost API to receive a markdown file"
Browse files Browse the repository at this point in the history
This reverts commit 3998f5c.
  • Loading branch information
3jins committed Feb 16, 2021
1 parent 3998f5c commit b283cff
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 110 deletions.
5 changes: 3 additions & 2 deletions backend/src/post/PostDto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Language from '@src/common/constant/Language';
import { File } from 'formidable';

export interface AddPostParamDto {
post: File;
categoryId?: string;
tagIdList?: Array<string>;
seriesId?: string;
title: string;
rawContent: string;
language: Language;
thumbnailContent: string;
thumbnailImageId?: string;
createdDate?: Date;
isPrivate?: boolean;
}

Expand Down
23 changes: 7 additions & 16 deletions backend/src/post/PostRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,20 @@ import Router from '@koa/router';
import koaBody from 'koa-body';
import { Context } from 'koa';
import Container from 'typedi';
import _ from 'lodash';
import { BlogErrorCode } from '@src/common/error/BlogErrorCode';
import * as URL from '../common/constant/URL';
import PostService from './PostService';
import { File } from 'formidable';
import Language from '@src/common/constant/Language';

const postRouter = new Router();
const koaBodyOptions = {
multipart: true,
};
const postService: PostService = Container.get(PostService);

postRouter.post(`${URL.PREFIX.API}${URL.ENDPOINT.POST}`, koaBody(koaBodyOptions), (ctx: Context) => {
if (_.isEmpty(ctx.request.files) || _.has(ctx.request.files!.post)) {
throw new Error(BlogErrorCode.FILE_NOT_UPLOADED.code);
postRouter.post(`${URL.PREFIX.API}${URL.ENDPOINT.POST}`, koaBody(), (ctx: Context) => {
try {
const requestBody = ctx.request.body;
postService.createPost(requestBody);
ctx.status = 200;
} catch (err) {
console.log(err);
}

postService.createPost({
post: ctx.request.files!.post,
...ctx.request.body,
});
ctx.status = 200;
});

export default postRouter;
14 changes: 2 additions & 12 deletions backend/src/post/PostService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service } from 'typedi';
import fs from 'fs';
import { AddPostParamDto } from '@src/post/PostDto';
import PostRepository from '@src/post/PostRepository';

Expand All @@ -9,21 +8,12 @@ export default class PostService {
}

public createPost = (paramDto: AddPostParamDto): void => {
const { post } = paramDto;
const rawContent: string = this.readPostContent(post.path);
const { rawContent } = paramDto;
const renderedContent = this.renderContent(rawContent);
this.postRepository.createPost({
...paramDto,
title: post.name,
rawContent,
renderedContent,
});
this.postRepository.createPost({ ...paramDto, renderedContent });
};

private readPostContent = (path: string): string => fs.readFileSync(path).toString();

private renderContent = (rawContent: string): string => {
// TODO: 렌더링 로직 구현
return rawContent;
};
}
18 changes: 0 additions & 18 deletions backend/test/data/test.md

This file was deleted.

13 changes: 0 additions & 13 deletions backend/test/post/PostRepositoryTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import { ClientSession, Connection } from 'mongoose';
import { should } from 'chai';
import sinon from 'sinon';
Expand Down Expand Up @@ -55,17 +54,5 @@ describe('PostRepository test', () => {
const posts: PostDoc[] = await Post.find().session(session);
posts.should.have.lengthOf(1);
const post: PostDoc = posts[0];
post.should.not.be.empty;
post.should.contain(commonTestData.post1);
(post.lastVersionPost !== undefined).should.be.true;
(post.lastVersionPost === null).should.be.true;
(post.isLatestVersion !== undefined).should.be.true;
post.isLatestVersion!.should.be.true;
(post.isDeleted !== undefined).should.be.true;
post.isDeleted!.should.be.false;
(post.createdDate !== undefined).should.be.true;
post.createdDate!.getTime().should.closeTo(new Date().getTime(), 3000); // Expect difference to be less than 3 seconds
(post.commentCount !== undefined).should.be.true;
post.commentCount!.should.equal(0);
});
});
31 changes: 12 additions & 19 deletions backend/test/post/PostRouterTest.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import supertest from 'supertest';
import { should } from 'chai';
import { Server } from 'http';
import { anything, instance, mock, objectContaining, verify } from 'ts-mockito';
import { mock } from 'ts-mockito';
import PostService from '@src/post/PostService';
import { Container } from 'typedi';
import { endApp, startApp } from '../../src/app';
import { appPath } from '../data/testData';
import { common as commonTestData } from '../data/testData';
import * as URL from '../../src/common/constant/URL';
import Language from '@src/common/constant/Language';

const postService: PostService = mock(PostService);
Container.set(PostService, instance(postService));
Container.set(PostService, mock(PostService));
const PostRouter = require('@src/post/PostRouter');

describe('Post router test', () => {
const FILE_NAME = 'test.md';

describe('Image integration test', () => {
let server: Server;
let request: supertest.SuperTest<supertest.Test>;

Expand All @@ -26,21 +22,18 @@ describe('Post router test', () => {
});

it(`${URL.PREFIX.API}${URL.ENDPOINT.POST}`, async () => {
const payload = {
seriesId: '1234',
language: Language.KO,
thumbnailContent: '뚜샤!',
const serviceParamDto = {
title: commonTestData.post1.title,
rawContent: commonTestData.post1.rawContent,
language: commonTestData.post1.language,
thumbnailContent: commonTestData.post1.thumbnailContent,
createdDate: new Date(),
};
await request
.post(`${URL.PREFIX.API}${URL.ENDPOINT.POST}`)
.field(payload)
.attach('post', `${appPath.testData}/${FILE_NAME}`, { contentType: 'application/octet-stream' })
.send(serviceParamDto)
.set('Accept', 'application/json')
.expect(200);

verify(postService.createPost(objectContaining({
post: anything(),
...payload,
}))).once();
});

after(() => endApp(server));
Expand Down
37 changes: 8 additions & 29 deletions backend/test/post/postServiceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,22 @@ import { deepEqual, instance, mock, verify } from 'ts-mockito';
import PostService from '@src/post/PostService';
import PostRepository from '@src/post/PostRepository';
import { AddPostParamDto, CreatePostRepoParamDto } from '@src/post/PostDto';
import { appPath, common as commonTestData } from '@test/data/testData';
import fs from 'fs';
import { File } from 'formidable';
import Language from '@src/common/constant/Language';
import { common as commonTestData } from '@test/data/testData';

describe('PostService test', () => {
let postService: PostService;
let postRepository: PostRepository;
let serviceParamDto: AddPostParamDto;
let post: File;
let fileContent: string;

before(() => {
const fileName = 'test.md';
const filePath = `${appPath.testData}/${fileName}`;
const fileStream: Buffer = fs.readFileSync(filePath);
fileContent = fileStream.toString();
const fileStat: fs.Stats = fs.statSync(filePath);
post = {
size: fileStream.byteLength,
path: filePath,
name: fileName,
type: 'application/octet-stream',
lastModifiedDate: fileStat.mtime,
toJSON(): Object {
return {};
},
};

serviceParamDto = {
post,
seriesId: '1234',
language: Language.KO,
thumbnailContent: '뚜샤!',
title: commonTestData.post1.title,
rawContent: commonTestData.post1.rawContent,
language: commonTestData.post1.language,
thumbnailContent: commonTestData.post1.thumbnailContent,
createdDate: new Date(),
};

postRepository = mock(PostRepository);
postService = new PostService(instance(postRepository));
should();
Expand All @@ -47,9 +28,7 @@ describe('PostService test', () => {
postService.createPost(serviceParamDto);
const repoParamDto: CreatePostRepoParamDto = {
...serviceParamDto,
title: post.name,
rawContent: fileContent,
renderedContent: fileContent,
renderedContent: serviceParamDto.rawContent,
};
verify(postRepository.createPost(deepEqual(repoParamDto))).once();
});
Expand Down
2 changes: 1 addition & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"baseUrl": "./",
"paths": {
"@src/*": ["src/*"],
"@test/*": ["test/*"]
"@test/*": ["test/*"],
},
"esModuleInterop": true,
"emitDecoratorMetadata": true,
Expand Down

0 comments on commit b283cff

Please sign in to comment.