Skip to content

Commit

Permalink
feat: return a default value for ctx.response.body
Browse files Browse the repository at this point in the history
  • Loading branch information
orzyyyy committed Sep 24, 2019
1 parent 916d107 commit 830cb82
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
14 changes: 6 additions & 8 deletions server/controller/DocumentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Controller, Request } from '../utils/decorator';
import md5 from 'blueimp-md5';
import { getWriteMappingPaths, updateSider } from '../utils/document';
import DocumentService from '../service/DocumentService';
import { Context } from 'koa';

export interface MappingProps {
[x: string]: any;
Expand All @@ -19,7 +20,7 @@ export interface MappingProps {
@Controller('/document')
export default class MarkdownController {
@Request({ url: '/update', method: 'post' })
async updateTargetDocument(ctx: any) {
async updateTargetDocument(ctx: Context) {
const {
layout,
id,
Expand Down Expand Up @@ -55,11 +56,10 @@ export default class MarkdownController {
);
updateSider();
service.updateContent(category, writeFilesPaths, originContent);
ctx.response.body = true;
}

@Request({ url: '/add', method: 'post' })
async initDocument(ctx: any) {
async initDocument(ctx: Context) {
const { title, type, subType, category } = ctx.request.body;
const service = new DocumentService();
const timeStamp = new Date().getTime();
Expand All @@ -74,28 +74,26 @@ export default class MarkdownController {
});
service.updateContent(category, writeFilesPaths);
updateSider();
ctx.response.body = id;
return id;
}

@Request({ url: '/delete', method: 'delete' })
async deleteTargetDocument(ctx: any) {
async deleteTargetDocument(ctx: Context) {
const { id, category } = ctx.request.body;
const service = new DocumentService();
const writeFilesPaths = getWriteMappingPaths(category, id);
service.deleteTargetDocument(writeFilesPaths);
service.updateMapping({ id }, true);
updateSider();
ctx.response.body = true;
}

@Request({ url: '/hide', method: 'post' })
async hideTargetDocument(ctx: any) {
async hideTargetDocument(ctx: Context) {
const { id } = ctx.request.body;
const service = new DocumentService();
const item = service.getOriginMappingItem(id);
item.visible = false;
service.updateMapping(item);
updateSider();
ctx.response.body = true;
}
}
31 changes: 14 additions & 17 deletions server/controller/ExhentaiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getEmptyRestDetailUrlInfo,
} from '../utils/exhentai';
import path from 'path';
import { Context } from 'koa';

export interface ExHentaiInfoItem {
name?: string;
Expand All @@ -26,7 +27,7 @@ export interface ExHentaiInfoItem {
@Controller('/exhentai')
export default class ExhentaiController {
@Request({ url: '/', method: 'get' })
async getThumbnaiInfo(ctx: { response: { body: string } }) {
async getThumbnaiInfo() {
const service = new ExhentaiService();
await service.initBrowser();
const latestListInfo = await getLatestListInfo();
Expand All @@ -37,16 +38,16 @@ export default class ExhentaiController {
`dist/assets/exhentai/${createTime}`,
].map(item => writeIntoJsonFile(item, results));
success('fetch completed');
ctx.response.body = `./assets/exhentai/${createTime}.json`;
return `./assets/exhentai/${createTime}.json`;
}

@Request({ url: '/getLatestSet', method: 'get' })
async getLatestExHentaiSet(ctx: any) {
ctx.response.body = `./assets/exhentai/${getLatestListFileName()}.json`;
async getLatestExHentaiSet() {
return `./assets/exhentai/${getLatestListFileName()}.json`;
}

@Request({ url: '/download', method: 'post' })
async downloadImages(ctx: any) {
async downloadImages(ctx: Context) {
const { url } = ctx.request.body;
const service = new ExhentaiService();
await service.initBrowser();
Expand All @@ -73,25 +74,22 @@ export default class ExhentaiController {
writeIntoJsonFile(`${prefixPath}/detailImageUrls`, images);

await service.downloadImages(images, prefixPath);

ctx.response.body = 'success';
}

@Request({ url: '/download/target', method: 'get' })
async downloadFromExistUrls(ctx: any) {
async downloadFromExistUrls(ctx: Context) {
const { name, dateStamp } = ctx.query;
const service = new ExhentaiService();
const prefixPath = `exhentai/${dateStamp}/${name}`;
const detailImageUrls = readJsonFile(
joinWithRootPath(`${prefixPath}/detailImageUrls.json`),
);
service.downloadImages(detailImageUrls, prefixPath);
ctx.response.body = 'success';
}

@Request({ url: '/download/stat', method: 'get' })
async statisticsFailedDownloadImgUrls(ctx: any) {
const { name, dateStamp, length } = ctx.query;
async statisticsFailedDownloadImgUrls(ctx: Context) {
const { name, dateStamp } = ctx.query;
const prefixPath = `exhentai/${dateStamp}/${name}`;
const detailImageUrls: string[] = readJsonFile(
joinWithRootPath(`${prefixPath}/detailImageUrls.json`),
Expand All @@ -101,7 +99,7 @@ export default class ExhentaiController {
);
const imgUrls: string[] = getBaseNameOfImage(prefixPath);
const result: { detail: string; index: string; i: number }[] = [];
for (let i = 1; i < parseInt(length, 10) + 1; i++) {
for (let i = 1; i < indexImageUrls.length + 1; i++) {
if (!imgUrls.includes(i.toString())) {
result.push({
detail: detailImageUrls[i - 1],
Expand All @@ -110,23 +108,23 @@ export default class ExhentaiController {
});
}
}
ctx.response.body = result;
return result;
}

@Request({ url: '/dateSet', method: 'get' })
async getDateSet(ctx: any) {
async getDateSet() {
const listFiles = getListFiles().reverse();
const result: { key: string; name: string }[] = [];

for (const item of listFiles) {
result.push({ key: `./assets/exhentai/${item}.json`, name: item });
}

ctx.response.body = listFiles;
return listFiles;
}

@Request({ url: '/sync', method: 'get' })
async sync(ctx: any) {
async sync() {
const service = new ExhentaiService();
await service.initBrowser();
const targetComic = getEmptyRestDetailUrlInfo();
Expand All @@ -139,6 +137,5 @@ export default class ExhentaiController {

success('fetch all image completed');
}
ctx.response.body = 'succcess';
}
}
5 changes: 3 additions & 2 deletions server/controller/MainPageController.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Controller, Request } from '../utils/decorator';
import MainPageService from '../service/MainPageService';
import { Context } from 'koa';

@Controller('/')
export default class MainPageController {
@Request({ url: '/', method: 'get' })
async getMainPage(ctx: any) {
async getMainPage(ctx: Context) {
const service = new MainPageService();
ctx.type = 'html';
ctx.response.body = service.getDist();
return service.getDist();
}

@Request({ url: '/test', method: 'get' })
Expand Down
14 changes: 9 additions & 5 deletions server/controller/ToyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ import { Controller, Request } from '../utils/decorator';
import ToyService from '../service/ToyService';
import { Context } from 'koa';

const common = async (key: string, query: any) => {
const getData = async (key: string, query: any) => {
const service = new ToyService();
const { result } = await service.getDataBySqlKey(key, query);
return result;
};

const common = async (ctx: Context) => {
const { key } = ctx.params;
const result = await getData(key, ctx.query);
return result;
};

@Controller('/toy')
export default class MainPageController {
@Request({ url: '/get/:key', method: 'get' })
async getDataByGet(ctx: Context) {
const { key } = ctx.params;
ctx.response.body = await common(key, ctx.query);
return common(ctx);
}

@Request({ url: '/post/:key', method: 'post' })
async getDataByPost(ctx: Context) {
const { key } = ctx.params;
ctx.response.body = await common(key, ctx.query);
return common(ctx);
}
}
7 changes: 6 additions & 1 deletion server/utils/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export function Request({ url, method }: { url: string; method: HttpMehtod }) {
const fn = descriptor.value;
descriptor.value = (router: KoaRouter) => {
router[method](url, async (ctx: Context, next: any) => {
await fn(ctx, next);
const result = await fn(ctx, next);
if (result !== undefined) {
ctx.response.body = result;
return;
}
ctx.response.body = 'success';
});
};
};
Expand Down

0 comments on commit 830cb82

Please sign in to comment.