-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters