Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const pins = pgTable("pins", {
mapId: uuid("map_id").references(() => maps.id, { onDelete: "cascade" }),
latitude: doublePrecision("latitude").notNull(),
longitude: doublePrecision("longitude").notNull(),
memo: text("memo"),
createdAt: timestamp("created_at").defaultNow().notNull(),
});

Expand Down
80 changes: 74 additions & 6 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
HealthSchema,
PinSchema,
PinsArraySchema,
UpdatePinSchema,
UserSchema,
} from "./schemas/pin";

Expand Down Expand Up @@ -259,9 +260,7 @@ app.post(
const userId = c.get("userId");
const body = c.req.valid("json");

if (
!(await validateMapOwnership(c.env.DATABASE_URL, body.mapId, userId))
) {
if (!(await validateMapOwnership(c.env.DATABASE_URL, body.mapId, userId))) {
return c.json({ error: "Map not found" }, 404);
}

Expand All @@ -274,6 +273,7 @@ app.post(
mapId: body.mapId ?? null,
latitude: body.latitude,
longitude: body.longitude,
memo: body.memo ?? null,
})
.returning(),
);
Expand All @@ -286,6 +286,75 @@ app.post(
},
);

app.put(
"/api/pins/:id",
describeRoute({
tags: ["pins"],
summary: "Update a pin",
responses: {
200: {
description: "Pin updated",
content: { "application/json": { schema: resolver(PinSchema) } },
},
400: {
description: "Invalid request",
content: { "application/json": { schema: resolver(ErrorSchema) } },
},
401: {
description: "Unauthorized",
content: { "application/json": { schema: resolver(ErrorSchema) } },
},
404: {
description: "Pin not found",
content: { "application/json": { schema: resolver(ErrorSchema) } },
},
500: {
description: "Internal server error",
content: { "application/json": { schema: resolver(ErrorSchema) } },
},
},
}),
authMiddleware,
validator("json", UpdatePinSchema),
async (c) => {
const userId = c.get("userId");
const pinId = c.req.param("id");
const body = c.req.valid("json");

if (!pinId || !/^[0-9a-f-]{36}$/i.test(pinId)) {
return c.json({ error: "Invalid pin ID" }, 400);
}

try {
const updateData: {
memo?: string | null;
} = {};
if (body.memo !== undefined) updateData.memo = body.memo;

if (Object.keys(updateData).length === 0) {
return c.json({ error: "No fields to update" }, 400);
}

const [data] = await withDb(c.env.DATABASE_URL, (db) =>
db
.update(pins)
.set(updateData)
.where(and(eq(pins.id, pinId), eq(pins.userId, userId)))
.returning(),
);

if (!data) {
return c.json({ error: "Pin not found" }, 404);
}

return c.json(data);
} catch (error) {
console.error("Failed to update pin:", error);
return c.json({ error: "Failed to update pin" }, 500);
}
},
);

app.delete(
"/api/pins/:id",
describeRoute({
Expand Down Expand Up @@ -373,6 +442,7 @@ app.post(
mapId: pin.mapId ?? null,
latitude: pin.latitude,
longitude: pin.longitude,
memo: pin.memo ?? null,
}));

try {
Expand Down Expand Up @@ -463,9 +533,7 @@ app.post(
const userId = c.get("userId");
const body = c.req.valid("json");

if (
!(await validateMapOwnership(c.env.DATABASE_URL, body.mapId, userId))
) {
if (!(await validateMapOwnership(c.env.DATABASE_URL, body.mapId, userId))) {
return c.json({ error: "Map not found" }, 404);
}

Expand Down
7 changes: 7 additions & 0 deletions backend/src/schemas/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const CreatePinSchema = v.object({
latitude: LatitudeSchema,
longitude: LongitudeSchema,
mapId: v.optional(v.nullable(v.string())),
memo: v.optional(v.nullable(v.string())),
});

export const BatchCreatePinsSchema = v.object({
Expand All @@ -25,19 +26,25 @@ export const BatchCreatePinsSchema = v.object({
latitude: LatitudeSchema,
longitude: LongitudeSchema,
mapId: v.optional(v.nullable(v.string())),
memo: v.optional(v.nullable(v.string())),
}),
),
v.maxLength(100),
),
});

export const UpdatePinSchema = v.object({
memo: v.optional(v.nullable(v.string())),
});

export const PinSchema = v.object({
id: v.string(),
userId: v.string(),
mapId: v.nullable(v.string()),
latitude: v.number(),
longitude: v.number(),
createdAt: v.string(),
memo: v.nullable(v.string()),
});

export const PinsArraySchema = v.array(PinSchema);
Expand Down
Loading