nightly - 2025-09-21
This commit is contained in:
33
webapp/src/routes/api/prefixes/+server.js
Normal file
33
webapp/src/routes/api/prefixes/+server.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { db } from "$lib/server/db";
|
||||||
|
import { prefixes } from "$lib/server/db/schema";
|
||||||
|
import { env } from "$env/dynamic/private";
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
if (env.TAM3_REMOTE) {
|
||||||
|
const res = await fetch(`${env.TAM3_REMOTE}/api/prefixes/?api_key=${env.TAM3_REMOTE_KEY}`);
|
||||||
|
if (!res.ok) {
|
||||||
|
return new Response(JSON.stringify({status: "Issue getting prefixes from remote."}), {status: res.status, statusText: res.statusText});
|
||||||
|
}
|
||||||
|
const data = await res.json();
|
||||||
|
return new Response(JSON.stringify(data), {status: 200, statusText: "Prefixes fetched from remote successfully."});
|
||||||
|
} else {
|
||||||
|
const data = await db.select().from(prefixes);
|
||||||
|
return new Response(JSON.stringify(data), {status: 200, statusText: "Prefixes loaded successfully."});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST({ request }) {
|
||||||
|
const { name, color, weight } = await request.json();
|
||||||
|
console.log({name, color, weight})
|
||||||
|
await db.insert(prefixes).values({name: name, color: color, weight: weight}).onConflictDoUpdate({target: prefixes.name, set: {color: color, weight: weight}});
|
||||||
|
if (env.TAM3_REMOTE) {
|
||||||
|
const res = await fetch(`${env.TAM3_REMOTE}/api/prefixes/?api_key=${env.TAM3_REMOTE_KEY}`, {
|
||||||
|
body: JSON.stringify({name, color, weight}), method: 'POST', headers: {'Content-Type': 'application/json'}
|
||||||
|
});
|
||||||
|
if (!res.ok) {
|
||||||
|
return new Response(JSON.stringify({status: "Issue posting prefixes"}), {status: res.status, statusText: res.statusText});
|
||||||
|
}
|
||||||
|
const data = await res.json();
|
||||||
|
}
|
||||||
|
return new Response(JSON.stringify({status: "Prefix posted successfully."}), {status: 200, statusText: "Prefix posted successfully."})
|
||||||
|
}
|
||||||
23
webapp/src/routes/api/prefixes/[name]/+server.js
Normal file
23
webapp/src/routes/api/prefixes/[name]/+server.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { db } from "$lib/server/db";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
import { prefixes } from "$lib/server/db/schema";
|
||||||
|
import { env } from "$env/dynamic/private";
|
||||||
|
|
||||||
|
export async function GET({ params }) {
|
||||||
|
let { name } = params;
|
||||||
|
if (env.TAM3_REMOTE) {
|
||||||
|
const res = await fetch(`${env.TAM3_REMOTE}/prefixes/${name}/?api_key=${env.TAM3_REMOTE_KEY}`);
|
||||||
|
if (!res.ok) {
|
||||||
|
return new Response(JSON.stringify({status: "Issue getting prefix."}), {status: res.status, statusText: res.statusText});
|
||||||
|
}
|
||||||
|
const data = await res.json();
|
||||||
|
return new Response(JSON.stringify(data), {status: 200, statusText: "Prefix fetched from remote successfully."})
|
||||||
|
} else {
|
||||||
|
const data = await db.select().from(prefixes).where(eq(prefixes.name, name));
|
||||||
|
if (data[0]) {
|
||||||
|
return new Response(JSON.stringify(data[0]), {status: 200, statusText: "Prefix loaded successfully."})
|
||||||
|
} else {
|
||||||
|
return new Response(JSON.stringify({status: "Issue loading prefix"}), {status: 404, statusText: "Prefix not found."})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
124
webapp/src/routes/prefixes/+page.svelte
Normal file
124
webapp/src/routes/prefixes/+page.svelte
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
<script>
|
||||||
|
import { browser } from "$app/environment";
|
||||||
|
|
||||||
|
let all_prefixes = $state([]);
|
||||||
|
let prefix_form = $state({name: "", color: "white", weight: 0})
|
||||||
|
let status = $state(``)
|
||||||
|
|
||||||
|
async function getPrefixes() {
|
||||||
|
const res = await fetch("/api/prefixes/");
|
||||||
|
if (!res.ok) {
|
||||||
|
status = `[${res.status}]: ${res.statusText}`;
|
||||||
|
} else {
|
||||||
|
const data = await res.json();
|
||||||
|
if (data[0]) {
|
||||||
|
all_prefixes = [...data];
|
||||||
|
};
|
||||||
|
status = `${res.statusText}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function savePrefix() {
|
||||||
|
const post_body = JSON.stringify({...prefix_form});
|
||||||
|
console.log(post_body);
|
||||||
|
const res = await fetch("/api/prefixes/", {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({...prefix_form}),
|
||||||
|
headers: {'Content-Type': 'application/json'}
|
||||||
|
});
|
||||||
|
if (!res.ok) {
|
||||||
|
status = `[${res.status}]: ${res.statusText}`;
|
||||||
|
} else {
|
||||||
|
status = `${res.statusText}`;
|
||||||
|
getPrefixes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (browser) {
|
||||||
|
getPrefixes()
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<h1>Prefix Editor</h1>
|
||||||
|
<div class="form">
|
||||||
|
<div>
|
||||||
|
<div>Name</div>
|
||||||
|
<div><input type="text" class="one-hundred" bind:value={prefix_form.name}></div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div>Color</div>
|
||||||
|
<div><select style="display: block; width: 100%" bind:value={prefix_form.color}>
|
||||||
|
<option value="white">White</option>
|
||||||
|
<option value="blue">Blue</option>
|
||||||
|
<option value="yellow">Yellow</option>
|
||||||
|
<option value="orange">Orange</option>
|
||||||
|
<option value="red">Red</option>
|
||||||
|
<option value="green">Green</option>
|
||||||
|
</select></div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div>Weight</div>
|
||||||
|
<div><input class="one-hundred" type="number" bind:value={prefix_form.weight}></div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div>Commands</div>
|
||||||
|
<div>
|
||||||
|
<button onclick={savePrefix}>Save/Update</button>
|
||||||
|
<button onclick={() => {
|
||||||
|
prefix_form = {name: "", color: "white", weight: 0};
|
||||||
|
}}>Reset</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="prefix-list">
|
||||||
|
<div>
|
||||||
|
<div>Name</div>
|
||||||
|
<div>Color</div>
|
||||||
|
<div>Weight</div>
|
||||||
|
<div>Commands</div>
|
||||||
|
</div>
|
||||||
|
{#each all_prefixes as prefix}
|
||||||
|
<div>
|
||||||
|
<div>{prefix.name}</div>
|
||||||
|
<div>{prefix.color}</div>
|
||||||
|
<div>{prefix.weight}</div>
|
||||||
|
<div>
|
||||||
|
<button>Edit</button>
|
||||||
|
<button>Delete</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
<div class="status">
|
||||||
|
{status}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.one-hundred {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.prefix-list {
|
||||||
|
margin-top: 3rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.prefix-list div {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(4, 1fr);
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status {
|
||||||
|
position: fixed;
|
||||||
|
bottom: 0;
|
||||||
|
background: #fff;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Reference in New Issue
Block a user