Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List all templates (MVP) #2

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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