first commit
This commit is contained in:
commit
b7b0f253ce
85 files changed
+7560
No files matched your search
@@ -0,0 +1,116 @@
|
||||
"use server";
|
||||
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { deleteImage, isUpload, saveImage } from "@/lib/files";
|
||||
import { revalidateBook, str } from "@/lib/form";
|
||||
import { MARKER_COLORS } from "@/lib/kinds";
|
||||
import { requireBook } from "@/lib/session";
|
||||
|
||||
async function requireMap(bookId: string, mapId: string) {
|
||||
const book = await requireBook(bookId);
|
||||
const map = await prisma.bookMap.findFirst({ where: { id: mapId, bookId: book.id } });
|
||||
if (!map) throw new Error("Map not found");
|
||||
return { book, map };
|
||||
}
|
||||
|
||||
export async function createMap(formData: FormData) {
|
||||
const book = await requireBook(str(formData, "bookId"));
|
||||
const name = str(formData, "name", 200);
|
||||
const kind = str(formData, "kind") === "IMAGE" ? "IMAGE" : "GEO";
|
||||
if (!name) return;
|
||||
|
||||
let imageFile: string | null = null;
|
||||
if (kind === "IMAGE") {
|
||||
const upload = formData.get("image");
|
||||
if (!isUpload(upload)) return;
|
||||
imageFile = await saveImage(upload);
|
||||
}
|
||||
|
||||
const map = await prisma.bookMap.create({ data: { bookId: book.id, name, kind, imageFile } });
|
||||
revalidateBook(book.id);
|
||||
redirect(`/books/${book.id}/maps/${map.id}`);
|
||||
}
|
||||
|
||||
export async function renameMap(formData: FormData) {
|
||||
const { book, map } = await requireMap(str(formData, "bookId"), str(formData, "mapId"));
|
||||
const name = str(formData, "name", 200);
|
||||
if (!name) return;
|
||||
await prisma.bookMap.update({ where: { id: map.id }, data: { name } });
|
||||
revalidateBook(book.id);
|
||||
}
|
||||
|
||||
export async function deleteMap(formData: FormData) {
|
||||
const { book, map } = await requireMap(str(formData, "bookId"), str(formData, "mapId"));
|
||||
await prisma.bookMap.delete({ where: { id: map.id } });
|
||||
await deleteImage(map.imageFile);
|
||||
revalidateBook(book.id);
|
||||
redirect(`/books/${book.id}/maps`);
|
||||
}
|
||||
|
||||
export async function saveMapView(bookId: string, mapId: string, lat: number, lng: number, zoom: number) {
|
||||
const { book, map } = await requireMap(bookId, mapId);
|
||||
if (![lat, lng, zoom].every(Number.isFinite)) return;
|
||||
await prisma.bookMap.update({
|
||||
where: { id: map.id },
|
||||
data: { centerLat: lat, centerLng: lng, zoom: Math.round(zoom) },
|
||||
});
|
||||
revalidateBook(book.id);
|
||||
}
|
||||
|
||||
export type MarkerInput = {
|
||||
label: string;
|
||||
note: string;
|
||||
color: string;
|
||||
entityId: string | null;
|
||||
lat: number;
|
||||
lng: number;
|
||||
};
|
||||
|
||||
async function cleanMarker(bookId: string, input: MarkerInput) {
|
||||
const label = String(input.label ?? "").trim().slice(0, 200);
|
||||
if (!label) throw new Error("Label is required");
|
||||
if (!Number.isFinite(input.lat) || !Number.isFinite(input.lng)) throw new Error("Invalid position");
|
||||
const color = input.color in MARKER_COLORS ? input.color : "blue";
|
||||
let entityId: string | null = null;
|
||||
if (input.entityId) {
|
||||
const entity = await prisma.entity.findFirst({ where: { id: input.entityId, bookId }, select: { id: true } });
|
||||
entityId = entity?.id ?? null;
|
||||
}
|
||||
return {
|
||||
label,
|
||||
note: String(input.note ?? "").trim().slice(0, 5000) || null,
|
||||
color,
|
||||
entityId,
|
||||
lat: input.lat,
|
||||
lng: input.lng,
|
||||
};
|
||||
}
|
||||
|
||||
export async function createMarker(bookId: string, mapId: string, input: MarkerInput) {
|
||||
const { book, map } = await requireMap(bookId, mapId);
|
||||
await prisma.marker.create({ data: { ...(await cleanMarker(book.id, input)), mapId: map.id } });
|
||||
revalidateBook(book.id);
|
||||
}
|
||||
|
||||
export async function updateMarker(bookId: string, mapId: string, markerId: string, input: MarkerInput) {
|
||||
const { book, map } = await requireMap(bookId, mapId);
|
||||
await prisma.marker.updateMany({
|
||||
where: { id: markerId, mapId: map.id },
|
||||
data: await cleanMarker(book.id, input),
|
||||
});
|
||||
revalidateBook(book.id);
|
||||
}
|
||||
|
||||
export async function moveMarker(bookId: string, mapId: string, markerId: string, lat: number, lng: number) {
|
||||
const { book, map } = await requireMap(bookId, mapId);
|
||||
if (![lat, lng].every(Number.isFinite)) return;
|
||||
await prisma.marker.updateMany({ where: { id: markerId, mapId: map.id }, data: { lat, lng } });
|
||||
revalidateBook(book.id);
|
||||
}
|
||||
|
||||
export async function deleteMarker(bookId: string, mapId: string, markerId: string) {
|
||||
const { book, map } = await requireMap(bookId, mapId);
|
||||
await prisma.marker.deleteMany({ where: { id: markerId, mapId: map.id } });
|
||||
revalidateBook(book.id);
|
||||
}
|
||||
Reference in new issue
Block a user