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