Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test direct drizzle #1

Merged
merged 4 commits into from
Nov 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion apps/nextjs/src/app/(info)/docs/rails/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function OpenApiDocs() {
return redirect("/");
}

const me = (await getAccountMe())?.data;
const me = await getAccountMe();

if (!me?.admin) {
return redirect("/"); // Redirect to home page if account is not an admin)
Expand Down
9 changes: 6 additions & 3 deletions apps/nextjs/src/app/api/uploadthing/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ export const UploadThingRouter = {
})
.onUploadComplete(({ metadata, file }) => {
// This code RUNS ON YOUR SERVER after upload
console.log("Upload complete for userId:", metadata.userId);

console.log("file url", file.url);
console.log(
"Upload complete for userId:",
metadata.userId,
"file url",
file.url,
);

// !!! Whatever is returned here is sent to the client `onClientUploadComplete` callback
return { uploadedBy: metadata.userId };
Expand Down
4 changes: 2 additions & 2 deletions apps/nextjs/src/app/dashboard/@profiles/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { getProfilesByAccountId } from "~/app/server-actions/profiles/actions";
import NewProfile from "~/components/profiles/new-profile";

export default async function Profiles() {
const me = (await getAccountMe())?.data;
const me = await getAccountMe();

if (!me) {
return null;
}

const profiles = await getProfilesByAccountId(me.id);
const profiles = await getProfilesByAccountId(Number(me.id));

return (
<div>
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/src/app/dashboard/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getAccountMe } from "~/app/server-actions/accounts/actions";
export default async function DashboardLayout({
children,
}: Readonly<DashboardLayoutProps>) {
const me = (await getAccountMe())?.data;
const me = await getAccountMe();

return (
<div>
Expand Down
8 changes: 4 additions & 4 deletions apps/nextjs/src/app/organizations/[org_slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export async function generateMetadata(
props: Readonly<OrganizationDetailPageProps>,
) {
const params = await props.params;
const { data: org } = await getOrganization(params.org_slug);
const org = await getOrganization(params.org_slug);

return { title: org?.name ?? "Organization" };
}

export async function generateStaticParams() {
const { partners, nonpartners } = await getOrganizations();
const orgs = await getOrganizations();

return [...partners, ...nonpartners].map(({ slug }) => ({ org_slug: slug }));
return orgs.map(({ slug }) => ({ org_slug: slug }));
}

// const columns = [
Expand Down Expand Up @@ -60,7 +60,7 @@ export default async function OrganizationDetailPage(
props: Readonly<OrganizationDetailPageProps>,
) {
const params = await props.params;
const { data: organization } = await getOrganization(params.org_slug);
const organization = await getOrganization(params.org_slug);

if (!organization) {
return <div>404 - Not Found</div>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from "next/link";
import { Chip } from "@battle-stadium/ui";

import type { OrganizationTournamentProps } from "~/types";
import { getOrganization } from "~/app/server-actions/organizations/actions";
import {
getTournament,
getTournaments,
Expand Down Expand Up @@ -41,7 +42,11 @@ export default async function OrganizationTournament(
return <div>404 - Not Found</div>;
}

const { organization } = tournament;
const organization = await getOrganization(tournament.organization.slug);

if (!organization) {
return <div>404 - Not Found</div>;
}

return (
<>
Expand Down Expand Up @@ -95,6 +100,7 @@ function TournamentDetailChips(props: Readonly<TournamentDetailChipsProps>) {
<Chip variant="light">Light</Chip>
<Chip variant="flat">Flat</Chip>
<Link
prefetch={true}
href={`/organizations/${org_slug}/tournaments/${tournament_id}/register`}
>
<Chip>Register</Chip>
Expand Down
10 changes: 9 additions & 1 deletion apps/nextjs/src/app/organizations/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Metadata } from "next";

import type { Tournament } from "~/lib/api";
import PartneredOrganizations from "~/components/organizations/partnered-organizations";
import { getOrganizations } from "../server-actions/organizations/actions";

export const metadata: Metadata = {
title: "Organizations",
Expand All @@ -11,7 +12,8 @@ export interface OrganizationsPageProps {
orgs: Tournament[];
}

export default function OrganizationsPage() {
export default async function OrganizationsPage() {
const orgs = await getOrganizations();
return (
<>
<div className="mt-8 md:mt-20">
Expand All @@ -20,6 +22,12 @@ export default function OrganizationsPage() {

<div>
<h2>TODO: Organizations Table</h2>
{orgs.map((org) => (
<div key={org.id}>
<h3>{org.name}</h3>
<p>{org.description}</p>
</div>
))}
</div>
</>
);
Expand Down
10 changes: 4 additions & 6 deletions apps/nextjs/src/app/players/[username]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAccount } from "~/app/server-actions/accounts/actions";
import { getProfile } from "~/app/server-actions/profiles/actions";

interface PlayerProfilePageProps {
params: Promise<{
Expand All @@ -10,7 +10,7 @@ export async function generateMetadata(
props: Readonly<PlayerProfilePageProps>,
) {
const params = await props.params;
const { data: player } = await getAccount(params.username);
const player = await getProfile(params.username);

return { title: player?.username ?? "Player" };
}
Expand All @@ -19,14 +19,12 @@ export default async function PlayerProfilePage(
props: Readonly<PlayerProfilePageProps>,
) {
const params = await props.params;
const { data: player } = await getAccount(params.username);
const player = await getProfile(params.username);

return (
<div>
<h1>{player?.username}</h1>
<p>
{player?.first_name} {player?.last_name}
</p>
<p> {player?.slug} </p>
</div>
);
}
4 changes: 2 additions & 2 deletions apps/nextjs/src/app/players/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const columns = [
];

export default async function PlayersPage() {
const { data: players } = await getProfiles();
const players = await getProfiles();

return <PlayersTable columns={columns} players={players ?? []} />;
return <PlayersTable columns={columns} players={players} />;
}
8 changes: 5 additions & 3 deletions apps/nextjs/src/app/players/players-table.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use client";

import type { profiles } from "@battle-stadium/db/schema";

// import { type Key } from "react";

// import Link from "next/link";
import type { Profile } from "~/lib/api";

export interface PlayersTableProps {
players: Profile[];
players: (typeof profiles.$inferSelect)[];
columns: { key: string; label: string }[];
}

Expand All @@ -29,7 +30,8 @@ export default function PlayersTable({
// switch (columnKey) {
// case "username":
// return (
// <Link className="text-primary" href={`/players/${username}`}>
// <Link
// prefetch={ true } className="text-primary" href={`/players/${username}`}>
// {username}
// </Link>
// );
Expand Down
61 changes: 29 additions & 32 deletions apps/nextjs/src/app/server-actions/accounts/actions.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
"use server";

import type { FetchOptions } from "openapi-fetch";
import { auth } from "@clerk/nextjs/server";

import type { paths } from "~/lib/api/openapi-v1";
import { BattleStadiumApiClient, defaultConfig } from "~/lib/api";
import { db, eq } from "@battle-stadium/db";
import { accounts, clerkUsers, profiles } from "@battle-stadium/db/schema";

export async function getAccounts(
options?: FetchOptions<paths["/accounts"]["get"]>,
) {
const usersOptions = {
...defaultConfig("listUsers"),
...options,
};
const skipClerkAuth = true;

return BattleStadiumApiClient(skipClerkAuth).GET("/accounts", usersOptions);
export async function getAccounts() {
return await db.query.accounts.findMany();
}

export async function getAccount(
username: string,
options?: FetchOptions<paths["/accounts/{username}"]["get"]>,
) {
const userOptions = {
...defaultConfig(`getUser-${username}`),
...options,
params: { path: { username } },
};

return BattleStadiumApiClient().GET("/accounts/{username}", userOptions);
export async function getAccount(username: string) {
const profile = await db.query.profiles.findFirst({
where: eq(profiles.username, username),
});

if (!profile?.accountId) {
return null;
}

return await db.query.accounts.findFirst({
where: eq(accounts.id, BigInt(profile.accountId)),
});
}

export async function getAccountMe(
options?: FetchOptions<paths["/accounts/me"]["get"]>,
) {
export async function getAccountMe() {
const { userId } = await auth();

if (!userId) {
return null;
}

const userMeOptions = {
...defaultConfig(`getAccountMe(${userId})`),
...options,
};
const clerkUser = await db.query.clerkUsers.findFirst({
where: eq(clerkUsers.clerkUserId, userId),
});

if (!clerkUser?.accountId) {
return null;
}

const account = await db.query.accounts.findFirst({
where: eq(accounts.id, BigInt(clerkUser.accountId)),
});

return BattleStadiumApiClient().GET("/accounts/me", userMeOptions);
return account;
}
61 changes: 17 additions & 44 deletions apps/nextjs/src/app/server-actions/organizations/actions.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,24 @@
"use server";

import type { FetchOptions } from "openapi-fetch";
import { eq } from "@battle-stadium/db";
import { db } from "@battle-stadium/db/client";
import { organizations } from "@battle-stadium/db/schema";

import type { paths } from "~/lib/api/openapi-v1";
import { BattleStadiumApiClient, defaultConfig } from "~/lib/api";

export async function getOrganizations(
options?: FetchOptions<paths["/organizations"]["get"]>,
) {
const organizationsOptions = {
...defaultConfig("getOrganizations"),
...options,
params: {
query: {
page: options?.params.query.page ?? 0,
per_page: options?.params.query.per_page ?? 20,
},
},
};
const skipClerkAuth = true;

const resp = await BattleStadiumApiClient(skipClerkAuth).GET(
"/organizations",
organizationsOptions,
);
const allOrgs = resp.data?.data;
const partnerOrgs = (allOrgs ?? []).filter((org) => org.partner);
const nonPartnerOrgs = (allOrgs ?? []).filter((org) => !org.partner);

return {
partners: partnerOrgs,
nonpartners: nonPartnerOrgs,
};
export async function getOrganizations() {
const orgs = await db.query.organizations.findMany();
return orgs;
}

export async function getOrganization(
slug: string,
options?: FetchOptions<paths["/organizations/{slug}"]["get"]>,
) {
const organizationOptions = {
...defaultConfig(`getOrganization(${slug})`),
...options,
params: { path: { slug } },
};
export async function getPartneredOrganizations() {
const orgs = await db.query.organizations.findMany({
where: eq(organizations.partner, true),
});
return orgs;
}

return BattleStadiumApiClient().GET(
"/organizations/{slug}",
organizationOptions,
);
export async function getOrganization(slug: string) {
const org = await db.query.organizations.findFirst({
where: eq(organizations.slug, slug),
});
return org;
}
18 changes: 10 additions & 8 deletions apps/nextjs/src/app/server-actions/profiles/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import type { FetchOptions } from "openapi-fetch";
import { revalidateTag } from "next/cache";

import { db, eq } from "@battle-stadium/db";
import { profiles } from "@battle-stadium/db/schema";

import type { paths } from "~/lib/api/openapi-v1";
import { BattleStadiumApiClient, defaultConfig } from "~/lib/api";

export async function getProfiles(
options?: FetchOptions<paths["/profiles"]["get"]>,
) {
const profilesOptions = {
...defaultConfig("getPlayerProfiles"),
...options,
};
export async function getProfiles() {
return await db.query.profiles.findMany();
}

return BattleStadiumApiClient().GET("/profiles", profilesOptions);
export async function getProfile(username: string) {
return await db.query.profiles.findFirst({
where: eq(profiles.username, username),
});
}

export async function getProfilesByAccountId(
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/src/components/battle-stadium.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function BattleStadium() {
className="flex flex-row gap-2 md:gap-4"
>
<div className="flex rounded-full bg-foreground">
<Link className="text-primary" href="/">
<Link prefetch={true} className="text-primary" href="/">
<BattleStadiumIcon
aria-label="Battle Stadium Logo"
className="text-background"
Expand Down
Loading