Skip to content

Commit

Permalink
Moment and MomentBody
Browse files Browse the repository at this point in the history
  • Loading branch information
ledyba committed Jul 23, 2021
1 parent 124d477 commit ea971d3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
27 changes: 18 additions & 9 deletions server/src/Server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import Asset from 'lib/asset';
import Config from './Config';
import fastify, { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import fastifyStatic from 'fastify-static';
import path from 'path';

import dayjs from 'dayjs';
import fastify, { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { RouteGenericInterface } from 'fastify/types/route';
import fastifyStatic from 'fastify-static';

import Asset from 'lib/asset';

import Config from './Config';
import Shelf from './shelf/Shelf';

// Omote Controllers
import OmoteIndexController from './controller/omote/IndexController';
import MomentController from './controller/omote/MomentController';
// Ura Controllers
import UraIndexController from './controller/ura/IndexController';
import MomentListController, {MomentListControllerInterface} from './controller/ura/MomentListController';
Expand All @@ -19,7 +24,7 @@ import DeleteController from './controller/ura/DeleteController';
import PreviewController from './controller/ura/PreviewController';
// Both Controllers
import EntityController, {EntityControllerInterface} from './controller/both/EntityController';
import dayjs from "dayjs";
import MomentBodyController from './controller/both/MomentBodyController';

type Handler<Interface extends RouteGenericInterface> =
(req: FastifyRequest<Interface>, reply: FastifyReply) => PromiseLike<void>;
Expand Down Expand Up @@ -98,19 +103,23 @@ class Server {
});
}
{ // (ura/omote)/year/month/day/HH:mm:ss/
const omote = await MomentController.create(this.asset, this.shelf);
const ura = await EditController.create(this.asset, this.shelf);
this.each.get('/:year(^[0-9]{4}$)/:month(^[0-9]{2}$)/:day(^[0-9]{2}$)/:time(^[0-9]{2}:[0-9]{2}:[0-9]{2}$)/', {
omote: async (req, reply) => {
reply
.code(404)
.type('text/plain')
.send('Not found');
await omote.handle(req, reply);
},
ura: async (req, reply) => {
await ura.handle(req, reply);
},
});
}
{ // (both)/moment/year/month/day/HH:mm:ss/
const c = await MomentBodyController.create(this.shelf);
this.both.get('/moment/:year(^[0-9]{4}$)/:month(^[0-9]{2}$)/:day(^[0-9]{2}$)/:time(^[0-9]{2}:[0-9]{2}:[0-9]{2}$)/', async (req, reply) => {
await c.handle(req, reply);
});
}
{ // (ura)/entity
const c = await EntityController.create(this.shelf);
this.both.get<EntityControllerInterface>('/entity/:id', async (req, reply) => {
Expand Down
32 changes: 32 additions & 0 deletions server/src/controller/both/MomentBodyController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {FastifyReply, FastifyRequest} from 'fastify';
import MomentRenderer from '../../renderer/MomentRenderer';
import Shelf from '../../shelf/Shelf';
import {parseMomentPath} from '../../shelf/Moment';
import dayjs from "dayjs";

export default class MomentBodyController {
private readonly shelf: Shelf;
private readonly renderer: MomentRenderer;
private constructor(shelf: Shelf) {
this.shelf = shelf;
this.renderer = new MomentRenderer(shelf);
}
static async create(shelf: Shelf): Promise<MomentBodyController> {
return new MomentBodyController(shelf);
}
async handle(req: FastifyRequest, reply: FastifyReply) {
const date = parseMomentPath(req.url.slice(7));
const moment = await this.shelf.findMoment(date);
if (moment === null) {
reply
.type('text/plain')
.code(404)
.send('Moment not found');
return;
}
reply
.type('text/html')
.code(200)
.send(await this.renderer.render(dayjs(), moment));
}
}
34 changes: 34 additions & 0 deletions server/src/controller/omote/MomentController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Handlebars from 'handlebars';
import {FastifyReply, FastifyRequest} from 'fastify';
import Asset from 'lib/asset';
import Shelf from '../../shelf/Shelf';
import { parseMomentPath } from '../../shelf/Moment';

export default class MomentController {
private readonly shelf: Shelf;
private readonly template: Handlebars.TemplateDelegate;
private constructor(shelf: Shelf, template: Handlebars.TemplateDelegate) {
this.shelf = shelf;
this.template = template;
}
static async create(asset: Asset, shelf: Shelf): Promise<MomentController> {
const src = await asset.loadString('templates/omote/index.hbs');
const template = Handlebars.compile(src);
return new MomentController(shelf, template);
}
async handle(req: FastifyRequest, reply: FastifyReply) {
const date = parseMomentPath(req.url);
const moment = await this.shelf.findMoment(date);
if (moment === null) {
reply
.type('text/plain')
.code(404)
.send('Moment not found');
return;
}
reply
.type('text/html')
.code(200)
.send(this.template({}));
}
}
6 changes: 3 additions & 3 deletions server/src/repo/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ delete from moments where timestamp=$1;
function decodeMoment(row: ResultRow): Moment {
return {
timestamp: dayjs(row.get('timestamp') as Date),
title: row.get('title') as string,
author: row.get('author') as string,
text: row.get('text') as string,
title: row.get('title') as string || '',
author: row.get('author') as string || '',
text: row.get('text') as string || '',
iconID: row.get('icon_id') as (string | undefined),
};
}
Expand Down

0 comments on commit ea971d3

Please sign in to comment.