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 drizzle/0003_favorites_add_name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `favorites` ADD `name` text NOT NULL DEFAULT '';
90 changes: 45 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,16 @@
"typescript": "~6.0.2",
"typescript-eslint": "^8.58.2",
"vite": "^8.0.10",
"vitest": "^4.1.9",
"vitest": "^4.1.10",
"wrangler": "^4.93.0"
},
"allowScripts": {
"[email protected]": true,
"[email protected]": true,
"[email protected]": true,
"[email protected]": true,
"[email protected]": true,
"[email protected]": true,
"@parcel/[email protected]": true
}
}
4 changes: 3 additions & 1 deletion src/__test_utils__/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ export async function seedFavorite(
db: Db,
insertableId: string,
userId: string = TEST_USER_ID,
sortOrder = 0
sortOrder = 0,
name = "test-favorite"
): Promise<string> {
const id = crypto.randomUUID();
await seedUser(db, userId);
Expand All @@ -161,6 +162,7 @@ export async function seedFavorite(
userId,
libraryId: TEST_LIBRARY_ID,
insertableId,
name,
sortOrder
})
.onConflictDoNothing();
Expand Down
2 changes: 1 addition & 1 deletion src/backend/library-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Insertables,
Groups
} from "../shared/api-models";
import { buildSearchDb } from "../shared/search";
import { buildSearchDb } from "../shared/build-insertable-search";

/**
* Assembles the full `LibraryOut` (groups + insertables, in sort order) for a
Expand Down
8 changes: 4 additions & 4 deletions src/backend/routes/favorites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("favorites routes", () => {

const app = createTestApp();
const res = await app.request(
`${favoritesUrl}?insertableId=${TEST_ASSEMBLY_ID}&id=fav-new`,
`${favoritesUrl}?insertableId=${TEST_ASSEMBLY_ID}&id=fav-new&name=fav-new`,
jsonRequest("POST"),
env
);
Expand Down Expand Up @@ -110,7 +110,7 @@ describe("favorites routes", () => {
it("is idempotent on a conflicting id", async () => {
await seedPartStudio(db);
const app = createTestApp();
const url = `${favoritesUrl}?insertableId=${TEST_PART_STUDIO_ID}&id=dup`;
const url = `${favoritesUrl}?insertableId=${TEST_PART_STUDIO_ID}&id=dup&name=dup`;

await app.request(url, jsonRequest("POST"), env);
await app.request(url, jsonRequest("POST"), env);
Expand Down Expand Up @@ -196,15 +196,15 @@ describe("favorites routes", () => {
});
});

describe("POST /default-configuration/:favoriteId", () => {
describe("POST /favorite/:favoriteId", () => {
it("persists the default configuration", async () => {
await seedPartStudio(db);
const favoriteId = await seedFavorite(db, TEST_PART_STUDIO_ID);
const app = createTestApp();

const defaultConfiguration = { "param-id": "value" };
const res = await app.request(
`/api/default-configuration/${favoriteId}`,
`/api/favorites/${favoriteId}`,
jsonRequest("POST", { defaultConfiguration }),
env
);
Expand Down
52 changes: 32 additions & 20 deletions src/backend/routes/favorites.ts
Comment thread
Lyinlyon marked this conversation as resolved.
Comment thread
Lyinlyon marked this conversation as resolved.
Comment thread
Lyinlyon marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ async function getFavorites(
id: row.id,
insertableId: row.insertableId,
libraryId,
defaultConfiguration: row.defaultConfiguration ?? undefined
defaultConfiguration: row.defaultConfiguration ?? undefined,
name: row.name ?? undefined
};
favoritesOut[row.id] = fav;
favoriteOrder.push(row.id);
Expand All @@ -58,23 +59,18 @@ favoriteRoutes.post("/favorites" + libraryRoute(), async (c) => {
const userId = await c.var.getUserId();
const insertableId = c.req.query("insertableId");
const favoriteId = c.req.query("id");
const name = c.req.query("name");
if (!insertableId) return c.json({ error: "insertableId required" }, 400);
if (!favoriteId) return c.json({ error: "id required" }, 400);

if (!name) return c.json({ error: "name required" }, 400);
const db = getDb(c.env.DB);

await db.insert(users).values({ id: userId }).onConflictDoNothing();

const existingCount = await db
.select({ sortOrder: favorites.sortOrder })
.from(favorites)
.where(
and(
eq(favorites.userId, userId),
eq(favorites.libraryId, libraryId)
)
)
.all();
const existingFavorites = await db.$count(
favorites,
and(eq(favorites.userId, userId), eq(favorites.libraryId, libraryId))
);

await db
.insert(favorites)
Expand All @@ -83,7 +79,8 @@ favoriteRoutes.post("/favorites" + libraryRoute(), async (c) => {
userId,
libraryId,
insertableId,
sortOrder: existingCount.length
name,
sortOrder: existingFavorites
})
.onConflictDoNothing();

Expand Down Expand Up @@ -126,18 +123,33 @@ favoriteRoutes.post("/favorite-order" + libraryRoute(), async (c) => {
return c.json({ success: true });
});

/** POST /api/default-configuration/:favoriteId */
favoriteRoutes.post("/default-configuration/:favoriteId", async (c) => {
/** POST /api/favorites/:favoriteId */
favoriteRoutes.post("/favorites/:favoriteId", async (c) => {
const favoriteId = c.req.param("favoriteId");
const body = await c.req.json<{
defaultConfiguration: Configuration;
defaultConfiguration?: Configuration | null;
name?: string;
}>();

if (!favoriteId) {
return c.json({ error: "favoriteId is required" }, 400);
}

if (Object.keys(body).length === 0) {
return c.json(
{ error: "defaultConfiguration or name is required" },
400
);
}

const db = getDb(c.env.DB);
await db
.update(favorites)
.set({ defaultConfiguration: body.defaultConfiguration })
.where(eq(favorites.id, favoriteId));

if (Object.keys(body).length > 0) {
await db
.update(favorites)
.set(body)
.where(eq(favorites.id, favoriteId));
}

return c.json({ success: true });
});
Loading