From 45387936be3f4d857b18946eab3639d0f5bba046 Mon Sep 17 00:00:00 2001 From: monoid Date: Mon, 4 Jan 2021 13:38:49 +0900 Subject: [PATCH] add content handler --- src/content/mod.ts | 1 + src/model/contents.ts | 2 +- src/route/contents.ts | 20 +++++++++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/content/mod.ts b/src/content/mod.ts index e69de29..db96484 100644 --- a/src/content/mod.ts +++ b/src/content/mod.ts @@ -0,0 +1 @@ +export {ContentReferrer,createContentReferrer} from './referrer'; \ No newline at end of file diff --git a/src/model/contents.ts b/src/model/contents.ts index 960fe5b..b3d32eb 100644 --- a/src/model/contents.ts +++ b/src/model/contents.ts @@ -37,7 +37,7 @@ export const isContent = (c: any):c is Content =>{ return false; } -export interface QueryListOption{ +export type QueryListOption = { /** * search word */ diff --git a/src/route/contents.ts b/src/route/contents.ts index c278b45..c7c892c 100644 --- a/src/route/contents.ts +++ b/src/route/contents.ts @@ -1,6 +1,6 @@ import { Context, Next } from 'koa'; import Router from 'koa-router'; -import {ContentAccessor, isContentContent} from './../model/contents'; +import {Content, ContentAccessor, isContentContent} from './../model/contents'; import {QueryListOption} from './../model/contents'; import {ParseQueryNumber, ParseQueryArray, ParseQueryBoolean} from './util' import {sendError} from './error_handler'; @@ -57,6 +57,15 @@ const ContentQueryHandler = (controller : ContentAccessor) => async (ctx: Contex ctx.body = content; ctx.type = 'json'; } +const UpdateContentHandler = (controller : ContentAccessor) => async (ctx: Context, next: Next) => { + const num = Number.parseInt(ctx.params['num']); + const content_desc: Partial & {id: number} = { + id:num,...ctx.request.body + }; + const success = await controller.update(content_desc); + ctx.body = JSON.stringify(success); + ctx.type = 'json'; +} const CreateContentHandler = (controller : ContentAccessor) => async (ctx: Context, next: Next) => { const content_desc = ctx.request.body; if(!isContentContent(content_desc)){ @@ -64,7 +73,7 @@ const CreateContentHandler = (controller : ContentAccessor) => async (ctx: Conte return; } const id = await controller.add(content_desc); - ctx.body = {"ret":id}; + ctx.body = JSON.stringify(id); ctx.type = 'json'; }; const AddTagHandler = (controller: ContentAccessor)=>async (ctx: Context, next: Next)=>{ @@ -80,7 +89,7 @@ const AddTagHandler = (controller: ContentAccessor)=>async (ctx: Context, next: return; } const r = await controller.addTag(c,tag_name); - ctx.body = {ret:r} + ctx.body = JSON.stringify(r); ctx.type = 'json'; }; const DelTagHandler = (controller: ContentAccessor)=>async (ctx: Context, next: Next)=>{ @@ -96,13 +105,13 @@ const DelTagHandler = (controller: ContentAccessor)=>async (ctx: Context, next: return; } const r = await controller.delTag(c,tag_name); - ctx.body = {ret:r} + ctx.body = JSON.stringify(r); ctx.type = 'json'; } const DeleteContentHandler = (controller : ContentAccessor) => async (ctx: Context, next: Next) => { const num = Number.parseInt(ctx.params['num']); const r = await controller.del(num); - ctx.body = {"ret":r}; + ctx.body = JSON.stringify(r); ctx.type = 'json'; }; const ContentHandler = (controller : ContentAccessor) => async (ctx:Context, next:Next) => { @@ -121,6 +130,7 @@ export const getContentRouter = (controller: ContentAccessor)=>{ const ret = new Router(); ret.get("/search",ContentQueryHandler(controller)); ret.get("/:num(\\d+)",ContentIDHandler(controller)); + ret.get("/:num(\\d+)",UpdateContentHandler(controller)); ret.use("/:num(\\d+)/:content_type"); ret.post("/",CreateContentHandler(controller)); ret.get("/:num(\\d+)/tags",ContentTagIDHandler(controller));