Skip to content

Commit

Permalink
Setup Layer Template
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Jul 15, 2024
1 parent bbb90fa commit 1080783
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
53 changes: 52 additions & 1 deletion api/routes/layer-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Err from '@openaddresses/batch-error';
import Auth from '../lib/auth.js';
import Config from '../lib/config.js';
import { sql } from 'drizzle-orm';
import { LayerTemplateResponse } from '../lib/types.js';
import { StandardResponse, LayerTemplateResponse } from '../lib/types.js';
import { LayerTemplate } from '../lib/schema.js';
import * as Default from '../lib/limits.js';

Expand Down Expand Up @@ -65,6 +65,57 @@ export default async function router(schema: Schema, config: Config) {
}
});

await schema.patch('/template/:templateid', {
name: 'Update Template',
group: 'LayerTemplate',
description: 'Update a layer template',
params: Type.Object({
templateid: Type.Integer()
}),
body: Type.Object({
name: Default.NameField,
description: Default.DescriptionField,
datasync: Type.Optional(Type.Boolean({ default: true })),
}),
res: LayerTemplateResponse
}, async (req, res) => {
try {
const user = await Auth.as_user(config, req, { admin: true });

const template = await config.models.LayerTemplate.commit(req.params.templateid, {
...req.body,
updated: sql`Now()`,
});

return res.json(template)
} catch (err) {
return Err.respond(err, res);
}
});

await schema.delete('/template/:templateid', {
name: 'Create Template',
group: 'LayerTemplate',
description: 'Create a layer template',
params: Type.Object({
templateid: Type.Integer()
}),
res: StandardResponse
}, async (req, res) => {
try {
const user = await Auth.as_user(config, req, { admin: true });

await config.models.LayerTemplate.delete(req.params.templateid);

return res.json({
status: 200,
message: 'Layer Template Deleted'
})
} catch (err) {
return Err.respond(err, res);
}
});

await schema.post('/template', {
name: 'Create Template',
group: 'LayerTemplate',
Expand Down
41 changes: 36 additions & 5 deletions api/web/src/components/Admin/AdminTemplate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
</div>
</div>
</template>
<template v-else>
<div class='col-md-12 d-flex align-items-center'>
<div class='ms-auto'>
<button @click='updateTemplate' class='btn btn-primary'>Update</button>
</div>
</div>
</template>
</div>
</div>
</div>
Expand Down Expand Up @@ -135,29 +142,53 @@ export default {
this.$router.push("/admin/template");
},
createTemplate: async function() {
isValid: function() {
let fields = ['name', 'description']
for (const field of fields) {
this.errors[field] = !this.template[field] ? 'Cannot be empty' : '';
}
if (this.template.name.length < 4) this.errors.name = 'Name too short'
if (this.template.description.length < 8) this.errors.description = 'Description too short'
for (const e in this.errors) if (this.errors[e]) return;
for (const e in this.errors) if (this.errors[e]) return false;
return true;
},
createTemplate: async function() {
if (!this.isValid()) return;
this.loading = true;
const url = stdurl(`/api/template`);
this.video = await std(url, {
await std(url, {
method: 'POST',
body: this.template
});
this.$route.push('/admin/template');
this.$router.push('/admin/template');
},
deleteTemplate: async function() {
this.loading = true;
const url = stdurl(`/api/template/${this.template.id}`);
await std(url, { method: 'DELETE', });
this.$router.push('/admin/template');
},
updateTemplate: async function() {
if (!this.isValid()) return;
this.loading = true;
const url = stdurl(`/api/template/${this.template.id}`);
await std(url, {
method: 'PATCH',
body: this.template
});
this.$router.push('/admin/template');
},
fetch: async function() {
this.loading = true;
const url = stdurl(`/api/template/${this.$route.params.template}`);
this.video = await std(url);
this.template = await std(url);
this.loading = false;
}
}
Expand Down

0 comments on commit 1080783

Please sign in to comment.