Cleaned up the way you make controllers and started working on the API vs. pages organization.

main
Zed A. Shaw 9 months ago
parent adf69936bb
commit 52a2b2caab
  1. 17
      api/todo_alpine.js
  2. 44
      commands/app.js
  3. 29
      controllers/todo.js
  4. 30
      controllers/todo_alpine.js
  5. 0
      pages/footer.html
  6. 0
      pages/header.html
  7. 0
      pages/todo.html
  8. 23
      pages/todo.js
  9. 0
      pages/todo_alpine.html

@ -0,0 +1,17 @@
import { ToDo } from "../lib/models.js";
import nunjucks from "nunjucks";
export const get = async (req, rep) => {
const todo_list = await ToDo.all({});
rep.code(200).send(todo_list);
}
export const post = async (req, rep) => {
if(req.query.todo_id !== undefined) {
await ToDo.delete({id: req.query.todo_id});
} else {
await ToDo.insert({task: req.body.task});
}
await get(req, rep);
}

@ -23,30 +23,38 @@ const configure = async (fastify, opts) => {
const app = fastify(opts);
// forces reload of the modules
for(let mod of fg.sync("controllers/*.js")) {
const control_mod = await import(`../${mod}?update=${new Date()}`);
// TODO: mappings are very simple for now
for(let [url, controller] of Object.entries(control_mod.default)) {
const handler = new controller();
// this goes through all the allowed verbs
for(let verb of ["get", "post", "put", "delete"]) {
if(verb in handler) {
app[verb](`/${url}`, async (req, rep) => {
try {
await handler[verb](req, rep);
} catch(error) {
console.error(error);
console.error(error.stack);
console.error(error.source);
}
});
}
for(let mod of fg.sync("{api,pages}/*.js")) {
const control_mod = await import(`../${mod}?update=${Date.now()}`);
const pf = path.parse(mod);
const url = `/${pf.name}`;
console.log("URL IS", url);
for(let verb of ["get", "post", "put", "delete"]) {
const handler = control_mod[verb];
if(handler !== undefined) {
console.log("ADDING VERB", verb);
app[verb](url, async (req, rep) => {
try {
await handler(req, rep);
} catch(error) {
console.error(error);
console.error(error.stack);
console.error(error.source);
}
});
}
}
}
app.register(Formbody);
app.get("/pages/todo_alpine.html", (req, rep) => {
const result = nunjucks.render("todo_alpine.html");
rep.code(200).type("text/html").send(result);
});
app.register(FastifyStatic, {
root: path.join(path.resolve("."), 'static'),
prefix: '/', // optional: default '/'

@ -1,29 +0,0 @@
import { ToDo } from "../lib/models.js";
import nunjucks from "nunjucks";
export class Todo {
async get(req, rep) {
const todo_list = await ToDo.all({});
const result = nunjucks.render("todo.html",
{todo_list, name: "Zed"});
rep.code(200).type("text/html").send(result);
}
async post(req, rep) {
if(req.query.todo_id !== undefined) {
await ToDo.delete({id: req.query.todo_id});
} else {
console.log("ADDING TASK", req.body.task);
await ToDo.insert({task: req.body.task});
}
// this is how you can prevent the annoying double submit
rep.redirect("/todo");
}
}
// you use the export default to set the route URL?
// I mean, it works not sure if I like it though
export default { todo: Todo };

@ -1,30 +0,0 @@
import { ToDo } from "../lib/models.js";
import nunjucks from "nunjucks";
export class Todo {
async get(req, rep) {
if(req.headers.accept === "application/json") {
const todo_list = await ToDo.all({});
rep.code(200).send(todo_list);
} else {
const result = nunjucks.render("todo_alpine.html", {name: "Zed"});
rep.code(200).type("text/html").send(result);
}
}
async post(req, rep) {
if(req.query.todo_id !== undefined) {
await ToDo.delete({id: req.query.todo_id});
} else {
await ToDo.insert({task: req.body.task});
}
await this.get(req, rep);
}
}
// you use the export default to set the route URL?
// I mean, it works not sure if I like it though
export default {
todo_alpine: Todo,
};

@ -0,0 +1,23 @@
import { ToDo } from "../lib/models.js";
import nunjucks from "nunjucks";
export const get = async (req, rep) => {
const todo_list = await ToDo.all({});
const result = nunjucks.render("todo.html",
{todo_list, name: "Zed"});
rep.code(200).type("text/html").send(result);
}
export const post = async (req, rep) => {
if(req.query.todo_id !== undefined) {
await ToDo.delete({id: req.query.todo_id});
} else {
console.log("ADDING TASK", req.body.task);
await ToDo.insert({task: req.body.task});
}
// this is how you can prevent the annoying double submit
rep.redirect("/todo");
}
Loading…
Cancel
Save