various fixes
raw sql to drizzle
This commit is contained in:
parent
ffd3aabc55
commit
d6a77fcd23
Binary file not shown.
@ -1,7 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth } from "@clerk/nextjs/server";
|
import { auth } from "@clerk/nextjs/server";
|
||||||
import { eq, sql } from "@fitai/database";
|
import { eq, sql } from "@fitai/database";
|
||||||
import { db, users as usersTable } from "@fitai/database";
|
import { db, users as usersTable, gyms as gymsTable } from "@fitai/database";
|
||||||
import { ensureUserSynced } from "@/lib/sync-user";
|
import { ensureUserSynced } from "@/lib/sync-user";
|
||||||
import log from "@/lib/logger";
|
import log from "@/lib/logger";
|
||||||
|
|
||||||
@ -68,16 +68,22 @@ export async function DELETE(
|
|||||||
|
|
||||||
await ensureGymsTable();
|
await ensureGymsTable();
|
||||||
|
|
||||||
// Check if gym exists
|
// Check if gym exists using Drizzle ORM
|
||||||
const gymRows = await db.all(sql`SELECT * FROM gyms WHERE id = ${gymId}`);
|
const existingGym = await db
|
||||||
if (gymRows.length === 0) {
|
.select()
|
||||||
|
.from(gymsTable)
|
||||||
|
.where(eq(gymsTable.id, gymId))
|
||||||
|
.get();
|
||||||
|
|
||||||
|
if (!existingGym) {
|
||||||
return NextResponse.json({ error: "Gym not found" }, { status: 404 });
|
return NextResponse.json({ error: "Gym not found" }, { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Soft delete - mark as inactive
|
// Soft delete - mark as inactive using Drizzle ORM
|
||||||
await db.run(
|
await db
|
||||||
sql`UPDATE gyms SET status = 'inactive', updated_at = ${Date.now()} WHERE id = ${gymId}`,
|
.update(gymsTable)
|
||||||
);
|
.set({ status: "inactive", updatedAt: new Date() })
|
||||||
|
.where(eq(gymsTable.id, gymId));
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { eq, sql } from "@fitai/database";
|
import { eq, sql } from "@fitai/database";
|
||||||
import { db } from "@fitai/database";
|
import { db, gyms as gymsTable } from "@fitai/database";
|
||||||
import log from "@/lib/logger";
|
import log from "@/lib/logger";
|
||||||
|
|
||||||
async function ensureGymsTable() {
|
async function ensureGymsTable() {
|
||||||
@ -27,12 +27,16 @@ export async function GET(
|
|||||||
const { id: gymId } = await params;
|
const { id: gymId } = await params;
|
||||||
await ensureGymsTable();
|
await ensureGymsTable();
|
||||||
|
|
||||||
// Get gym info
|
// Get gym info using Drizzle ORM
|
||||||
const gymRows = await db.all(sql`SELECT * FROM gyms WHERE id = ${gymId}`);
|
const gym = await db
|
||||||
if (gymRows.length === 0) {
|
.select()
|
||||||
return new NextResponse("Gym not found", { status: 404 });
|
.from(gymsTable)
|
||||||
|
.where(eq(gymsTable.id, gymId))
|
||||||
|
.get();
|
||||||
|
|
||||||
|
if (!gym) {
|
||||||
|
return NextResponse.json({ error: "Gym not found" }, { status: 404 });
|
||||||
}
|
}
|
||||||
const gym = gymRows[0];
|
|
||||||
|
|
||||||
// Get user counts
|
// Get user counts
|
||||||
const usersResult = await db.all(
|
const usersResult = await db.all(
|
||||||
@ -100,6 +104,9 @@ export async function GET(
|
|||||||
return NextResponse.json({ gym, stats });
|
return NextResponse.json({ gym, stats });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error("Failed to get gym stats", error);
|
log.error("Failed to get gym stats", error);
|
||||||
return new NextResponse("Internal Server Error", { status: 500 });
|
return NextResponse.json(
|
||||||
|
{ error: "Internal Server Error" },
|
||||||
|
{ status: 500 },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth } from "@clerk/nextjs/server";
|
import { auth } from "@clerk/nextjs/server";
|
||||||
import { eq, sql } from "@fitai/database";
|
import { eq, sql } from "@fitai/database";
|
||||||
import { db, users as usersTable } from "@fitai/database";
|
import { db, users as usersTable, gyms as gymsTable } from "@fitai/database";
|
||||||
import { ensureUserSynced } from "@/lib/sync-user";
|
import { ensureUserSynced } from "@/lib/sync-user";
|
||||||
import log from "@/lib/logger";
|
import log from "@/lib/logger";
|
||||||
|
|
||||||
@ -24,9 +24,12 @@ async function ensureGymsTable() {
|
|||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
await ensureGymsTable();
|
await ensureGymsTable();
|
||||||
const rows = await db.all(
|
const rows = await db
|
||||||
sql`SELECT * FROM gyms WHERE status = 'active' ORDER BY created_at DESC`,
|
.select()
|
||||||
);
|
.from(gymsTable)
|
||||||
|
.where(eq(gymsTable.status, "active"))
|
||||||
|
.orderBy(sql`created_at DESC`)
|
||||||
|
.all();
|
||||||
|
|
||||||
return NextResponse.json(rows);
|
return NextResponse.json(rows);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -146,19 +149,30 @@ export async function POST(req: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const id = generateId();
|
const id = generateId();
|
||||||
const nowTs = Date.now();
|
const nowTs = new Date();
|
||||||
|
|
||||||
await db.run(
|
// Use Drizzle's insert method instead of raw SQL
|
||||||
sql`INSERT INTO gyms (id, name, location, status, admin_user_id, created_at, updated_at)
|
await db.insert(gymsTable).values({
|
||||||
VALUES (${id}, ${name}, ${location ?? null}, 'active', ${adminUserId!}, ${nowTs}, ${nowTs})`,
|
id,
|
||||||
);
|
name,
|
||||||
|
location: location ?? null,
|
||||||
|
status: "active",
|
||||||
|
adminUserId: adminUserId!,
|
||||||
|
createdAt: nowTs,
|
||||||
|
updatedAt: nowTs,
|
||||||
|
});
|
||||||
|
|
||||||
// Assign the admin to this gym immediately after creation
|
// Assign the admin to this gym immediately after creation
|
||||||
await db.run(
|
await db
|
||||||
sql`UPDATE users SET gym_id = ${id}, updated_at = ${nowTs} WHERE id = ${adminUserId!}`,
|
.update(usersTable)
|
||||||
);
|
.set({ gymId: id, updatedAt: nowTs })
|
||||||
|
.where(eq(usersTable.id, adminUserId!));
|
||||||
|
|
||||||
const created = await db.get(sql`SELECT * FROM gyms WHERE id = ${id}`);
|
const created = await db
|
||||||
|
.select()
|
||||||
|
.from(gymsTable)
|
||||||
|
.where(eq(gymsTable.id, id))
|
||||||
|
.get();
|
||||||
return NextResponse.json(created, { status: 201 });
|
return NextResponse.json(created, { status: 201 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error("Failed to create gym", error);
|
log.error("Failed to create gym", error);
|
||||||
|
|||||||
@ -53,10 +53,10 @@ export const gyms = sqliteTable(
|
|||||||
adminUserId: text("admin_user_id")
|
adminUserId: text("admin_user_id")
|
||||||
.notNull()
|
.notNull()
|
||||||
.references(() => users.id, { onDelete: "cascade" }),
|
.references(() => users.id, { onDelete: "cascade" }),
|
||||||
createdAt: integer("created_at", { mode: "timestamp" })
|
createdAt: integer("created_at", { mode: "timestamp_ms" })
|
||||||
.notNull()
|
.notNull()
|
||||||
.$defaultFn(() => new Date()),
|
.$defaultFn(() => new Date()),
|
||||||
updatedAt: integer("updated_at", { mode: "timestamp" })
|
updatedAt: integer("updated_at", { mode: "timestamp_ms" })
|
||||||
.notNull()
|
.notNull()
|
||||||
.$defaultFn(() => new Date()),
|
.$defaultFn(() => new Date()),
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user