add content handler

This commit is contained in:
monoid 2021-01-04 13:38:49 +09:00
parent eaef191c1e
commit 45387936be
3 changed files with 17 additions and 6 deletions

View File

@ -0,0 +1 @@
export {ContentReferrer,createContentReferrer} from './referrer';

View File

@ -37,7 +37,7 @@ export const isContent = (c: any):c is Content =>{
return false;
}
export interface QueryListOption{
export type QueryListOption = {
/**
* search word
*/

View File

@ -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<Content> & {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));