Skip to content

Commit

Permalink
List all templates (MVP) (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartolomej authored Nov 12, 2023
2 parents 8187c64 + adc2067 commit b61c39c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
14 changes: 3 additions & 11 deletions api/src/routes/template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import express, { Request, Response, Router } from "express";
import { body } from "express-validator";
import { validateRequest } from "../middlewares/validate-request";
import { TemplateService } from "../services/template";
import { genHash } from "../utils/gen-hash";
import { mixpanelTrack } from "../utils/mixpanel";
Expand All @@ -13,18 +11,12 @@ function templateRouter(
const router = express.Router();

router.get("/templates", async (req: Request, res: Response) => {
const name = req.query.name as string;
const name = req.query.name as string | undefined;

if (!name) {
mixpanelTrack("get_template_by_name", {
name,
status: 400,
});
const allTemplates = await templateService.getAllTemplates();

res.status(400);
return res.send(
`GET /templates-- Required query parameter "name" not provided.`
);
return res.send(allTemplates)
}

let templateId: string = "";
Expand Down
25 changes: 16 additions & 9 deletions api/src/services/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ class TemplateService {
return newTemplate;
}

async getTemplate(templateId: string) {
let foundTemplate: Template;
async getAllTemplates() {
const foundTemplates = await Template.query();

return foundTemplates.map(template => this.deserializeTemplateJson(template))
}

foundTemplate = (
async getTemplate(templateId: string) {
const foundTemplate = (
await Template.query().where({
id: templateId,
})
)[0];

let foundTemplateJson = foundTemplate?.json_string || null;

if (typeof foundTemplateJson === "string") {
foundTemplateJson = JSON.parse(foundTemplateJson);
}
if (foundTemplate) {
return this.deserializeTemplateJson(foundTemplate)
} else {
return null;
}
}

return foundTemplateJson;
private deserializeTemplateJson(template: Template) {
let foundTemplateJson = template.json_string;
return JSON.parse(foundTemplateJson);
}

async getTemplateByCadenceASTHash(cadenceASTHash: string, network: string) {
Expand Down

0 comments on commit b61c39c

Please sign in to comment.