Skip to content

Commit 0395082

Browse files
committed
feat: patch every changed asset in database
1 parent b24251c commit 0395082

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

apps/client/src/components/timeline-asset.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
<!-- svelte-ignore a11y_no_static_element_interactions -->
5757
<div
58-
id={`${asset.row}:${asset.col}`}
58+
id={asset.id}
5959
bind:this={assetRef}
6060
onmousedown={handleMouseDown}
6161
class="absolute flex h-full items-center justify-center bg-violet-500"

apps/client/src/routes/editor/[id]/+page.server.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,18 @@ export const actions: Actions = {
6363
}
6464
},
6565
patchAsset: async ({ request }) => {
66-
console.log(await request.formData());
66+
try {
67+
const formData = await request.formData();
68+
await fetch('http://localhost:3000/v1/asset', {
69+
headers: {
70+
'Content-Type': 'application/json'
71+
},
72+
method: 'PATCH',
73+
body: formData.get('asset-data')
74+
});
75+
} catch (err) {
76+
console.error(err);
77+
}
6778
},
6879
deleteAsset: async ({ request }) => {
6980
console.log(await request.formData());

apps/client/src/states/timeline-assets.svelte.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface ITimelineAssetInput {
22
id?: string;
33
row: number;
44
col: number;
5-
content: { name: string; url: string }; // Nested content object
5+
content: { name: string; url: string };
66
start: number;
77
length: number;
88
}
@@ -61,8 +61,9 @@ export function assign(array: ITimelineAssetInput[]) {
6161

6262
export function handleOverlap(asset: ITimelineAsset) {
6363
const rowAssets = [...timelineAssetState.assets[asset.row]];
64+
const files: ITimelineAsset[] = [];
6465

65-
rowAssets[asset.col] = asset;
66+
rowAssets[asset.col] = { ...rowAssets[asset.col], ...asset };
6667
rowAssets.sort((a, b) => a.start - b.start);
6768
rowAssets.forEach((a, i) => (a.col = i));
6869

@@ -76,6 +77,16 @@ export function handleOverlap(asset: ITimelineAsset) {
7677
}
7778

7879
timelineAssetState.assets[asset.row] = rowAssets;
80+
files.push(timelineAssetState.assets[asset.row][asset.col]);
81+
82+
const form = timelineAssetState.formRef;
83+
84+
if (form) {
85+
const item = form.elements.item(0) as HTMLInputElement;
86+
form.action = '?/patchAsset';
87+
item.value = JSON.stringify(files);
88+
form.requestSubmit();
89+
}
7990
}
8091

8192
export function createAsset(event: DragEvent & { currentTarget: EventTarget & HTMLDivElement }) {

apps/server/src/routes/asset.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { db } from '../database';
22
import { assetTable } from '../database/schema';
3-
import { eq } from 'drizzle-orm';
3+
import { eq, sql } from 'drizzle-orm';
44
import { Router } from 'express';
55

66
const router = Router();
@@ -15,14 +15,30 @@ router.post('/', async (req, res) => {
1515
}
1616
});
1717

18-
router.patch('/:id', async (req, res) => {
18+
router.patch('/', async (req, res) => {
1919
try {
20-
const { id } = req.params;
21-
const asset = await db
20+
const assets = req.body;
21+
22+
if (assets.length === 0) {
23+
res.status(400).json({ message: 'assets are empty' });
24+
return;
25+
}
26+
const batch = await db
2227
.update(assetTable)
23-
.set({ ...req.body })
24-
.where(eq(assetTable.id, id));
25-
res.status(200).json(asset);
28+
.set({
29+
start: sql.raw(`
30+
CASE
31+
${assets.map((asset: { id: string; start: number }) => `WHEN id = '${asset.id}' THEN ${asset.start}`).join('\n')}
32+
END
33+
`),
34+
})
35+
.where(
36+
sql.raw(
37+
`id IN (${assets.map((asset: { id: string; start: number }) => `'${asset.id}'`).join(', ')})`
38+
)
39+
);
40+
console.log(batch);
41+
res.status(200).json(batch);
2642
} catch (err) {
2743
console.error(err);
2844
res.status(500).json({ message: err });

0 commit comments

Comments
 (0)