From 1388f7be67ffb59f2dd70d060a68dbcc5bb94187 Mon Sep 17 00:00:00 2001 From: Oleg Isonen Date: Sat, 7 May 2022 15:20:45 +0200 Subject: [PATCH] add breakpoints rest --- app/routes/rest/breakpoints.$projectId.ts | 23 +++++++++++++++++++++++ app/shared/db/breakpoints.server.ts | 6 +++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 app/routes/rest/breakpoints.$projectId.ts diff --git a/app/routes/rest/breakpoints.$projectId.ts b/app/routes/rest/breakpoints.$projectId.ts new file mode 100644 index 0000000000..9cd0dea7e2 --- /dev/null +++ b/app/routes/rest/breakpoints.$projectId.ts @@ -0,0 +1,23 @@ +import { type LoaderFunction } from "remix"; +import type { Breakpoint } from "@webstudio-is/sdk"; +import * as db from "~/shared/db"; + +export type ErrorData = { + errors: string; +}; + +export const loader: LoaderFunction = async ({ + params, +}): Promise | ErrorData> => { + try { + const data = await db.breakpoints.load(params.projectId); + return data?.values || []; + } catch (error) { + if (error instanceof Error) { + return { + errors: error.message, + }; + } + } + return { errors: "Unexpected error" }; +}; diff --git a/app/shared/db/breakpoints.server.ts b/app/shared/db/breakpoints.server.ts index 86b79d31dd..8d455c18f1 100644 --- a/app/shared/db/breakpoints.server.ts +++ b/app/shared/db/breakpoints.server.ts @@ -8,7 +8,11 @@ import ObjectId from "bson-objectid"; import { applyPatches, type Patch } from "immer"; import { prisma } from "./prisma.server"; -export const load = async (projectId: Project["id"]) => { +export const load = async (projectId?: Project["id"]) => { + if (typeof projectId !== "string") { + throw new Error("Project ID required"); + } + return await prisma.breakpoints.findUnique({ where: { projectId }, });