dont even know if I changed anything

This commit is contained in:
Gabriel Brown 2024-10-22 16:38:45 -05:00
parent fc8f26dd0b
commit 2c2cb819e1
12 changed files with 202 additions and 39 deletions

View File

@ -0,0 +1,20 @@
CREATE OR REPLACE FUNCTION notify_new_message()
RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
PERFORM pg_notify(
'new_message',
json_build_object(
'id', NEW.id,
'text', NEW.text,
'senderId', NEW.senderId,
'receiverId', NEW.receiverId,
'createdAt', NEW.createdAt
)::text
);
RETURN NEW;
END;
$$;
CREATE TRIGGER message_insert_trigger
AFTER INSERT ON messages
FOR EACH ROW EXECUTE FUNCTION notify_new_message();

View File

@ -0,0 +1,15 @@
{
"id": "ba7dd70a-8dd6-41be-b1d2-158ab83ba830",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1729627211020,
"tag": "0000_warm_edwin_jarvis",
"breakpoints": true
}
]
}

View File

@ -12,8 +12,8 @@
"dev": "next dev",
"lint": "next lint",
"start": "next start",
"go": "pnpm dev",
"go:prod": "pnpm build && pnpm start"
"go": "next dev",
"go:prod": "next build && next start"
},
"dependencies": {
"@t3-oss/env-nextjs": "^0.10.1",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

BIN
public/favicon.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -0,0 +1,41 @@
'use server';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getMessages } from '~/server/functions';
import { middleware } from '~/middleware';
import type { Message } from '~/server/types';
export const GET = async (request: NextRequest) => {
const middlewareResponse = await middleware(request);
if (middlewareResponse) return middlewareResponse;
try {
const url = new URL(request.url);
const userId = Number.parseInt(url.searchParams.get('userId') ?? '');
const limit = Number.parseInt(url.searchParams.get('limit') ?? '');
const offset = Number.parseInt(url.searchParams.get('offset') ?? '0');
if (!userId || isNaN(userId) || !limit || isNaN(limit) || isNaN(offset)) {
console.log('userId: ', userId, 'limit: ', limit, 'offset: ', offset);
return NextResponse.json(
{ message: 'Missing userId, limit, or offset' }, { status: 400 }
);
}
const messages: Message[] | undefined = await getMessages(userId, limit, offset);
if (messages === undefined) {
return NextResponse.json(
{ message: 'No messages found' }, { status: 404 }
);
}
return NextResponse.json(messages);
} catch (error) {
console.error('Error getting messages:', error);
if (error instanceof Error) {
return NextResponse.json(
{ message: error.message }, { status: 500 }
);
} else {
return NextResponse.json(
{ message: 'Unknown error occurred' }, { status: 500 }
);
}
}
};

View File

@ -0,0 +1,41 @@
'use server'
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { sendMessage } from '~/server/functions';
import { middleware } from '~/middleware';
import type { Message } from '~/server/types';
export const POST = async (request: NextRequest) => {
const middlewareResponse = await middleware(request);
if (middlewareResponse) return middlewareResponse;
try {
const body = await request.json() as {
message: Message;
};
const { message } = body;
if (!message) {
console.log('Message:', message);
return NextResponse.json(
{ message: 'Missing message' }, { status: 400 }
);
}
const newMessage: Message | null = await sendMessage(message);
if (!newMessage) {
return NextResponse.json(
{ message: 'Error sending message' }, { status: 500 }
);
}
return NextResponse.json(newMessage);
} catch (error) {
console.error('Error sending message:', error);
if (error instanceof Error) {
return NextResponse.json(
{ message: error.message }, { status: 500 }
);
} else {
return NextResponse.json(
{ message: 'Unknown error occurred' }, { status: 500 }
);
}
}
};

View File

@ -4,9 +4,9 @@ import { GeistSans } from "geist/font/sans";
import { type Metadata } from "next";
export const metadata: Metadata = {
title: "Create T3 App",
description: "Generated by create-t3-app",
icons: [{ rel: "icon", url: "/favicon.ico" }],
title: "Is Madeline the Cutest?",
description: "Answering the easiest question in the world!",
icons: [{ rel: "icon", url: "/favicon.png" }],
};
export default function RootLayout({

View File

@ -1,37 +1,35 @@
import Link from "next/link";
"use client";
import { useState, useEffect } from "react";
const interestingYes = () => {
const yesArray = [
"Absolutely, yes.",
"Without a doubt.",
"Of course.",
"Definitely.",
"Obviously!",
"Certainly!",
"Positively.",
"100%",
];
return yesArray[Math.floor(Math.random() * yesArray.length)];
}
export default function HomePage() {
const [currentText, setCurrentText] = useState("");
useEffect(() => {
setCurrentText(interestingYes() ?? "Absolutely, yes.");
}, []);
const handleClick = () => {
setCurrentText(interestingYes() ?? "Absolutely, yes.");
};
return (
<main className="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#2e026d] to-[#15162c] text-white">
<div className="container flex flex-col items-center justify-center gap-12 px-4 py-16">
<h1 className="text-5xl font-extrabold tracking-tight text-white sm:text-[5rem]">
Create <span className="text-[hsl(280,100%,70%)]">T3</span> App
</h1>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:gap-8">
<Link
className="flex max-w-xs flex-col gap-4 rounded-xl bg-white/10 p-4 text-white hover:bg-white/20"
href="https://create.t3.gg/en/usage/first-steps"
target="_blank"
>
<h3 className="text-2xl font-bold">First Steps </h3>
<div className="text-lg">
Just the basics - Everything you need to know to set up your
database and authentication.
</div>
</Link>
<Link
className="flex max-w-xs flex-col gap-4 rounded-xl bg-white/10 p-4 text-white hover:bg-white/20"
href="https://create.t3.gg/en/introduction"
target="_blank"
>
<h3 className="text-2xl font-bold">Documentation </h3>
<div className="text-lg">
Learn more about Create T3 App, the libraries it uses, and how to
deploy it.
</div>
</Link>
</div>
</div>
<main className="flex min-h-screen flex-col items-center justify-center
bg-gradient-to-b from-pink-500 to-orange-400 text-white cursor-pointer">
<h3 className="text-5xl font-extrabold tracking-tight
text-white sm:text-[5rem] text-center" onClick={handleClick}>
{currentText}
</h3>
</main>
);
}

View File

@ -1,4 +1,5 @@
// https://orm.drizzle.team/docs/sql-schema-declaration
import { db } from '~/server/db';
import { sql } from "drizzle-orm";
import {
boolean,
@ -161,3 +162,4 @@ export const quickReplyOptions = pgTable(
quickReplyIdIndex: index('qr_options_quick_reply_id_idx').on(table.quickReplyId),
})
);

View File

@ -1,7 +1,7 @@
import 'server-only';
import { db } from '~/server/db';
import * as schema from '~/server/db/schema';
import { eq, and, or, like, not } from 'drizzle-orm';
import { eq, and, or, like, not, desc, sql } from 'drizzle-orm';
import { User,
Relationship,
UserRelationship,
@ -227,7 +227,7 @@ export const updateRelationshipStatus = async (
}
const relationship = relationships[0] as Relationship;
// Everything above is just getting info
if (userId === relationship.requestorId) {
if (userId === relationship.requestorId && status === 'accepted') {
throw new Error('The requestor cannot accept the relationship they requested.');
}
if (status === 'accepted') {
@ -255,7 +255,7 @@ export const updateRelationshipStatus = async (
return relationshipData;
} else if (status === 'rejected') {
await db.delete(schema.userRelationships)
.where(eq(schema.userRelationships.id, userRelationship.id));
.where(eq(schema.userRelationships.relationshipId, relationship.id));
await db.delete(schema.relationships)
.where(eq(schema.relationships.id, relationship.id));
return null;
@ -325,3 +325,36 @@ export const createRelationshipRequest = async (userId: number, partnerId: numbe
}
};
export const getMessages = async (userId: number, limit: number, offset: number) => {
try {
const messages = await db.select().from(schema.messages)
.where(or(
eq(schema.messages.senderId, userId),
eq(schema.messages.receiverId, userId)
))
.limit(limit)
.offset(offset)
.orderBy(desc(schema.messages.createdAt), desc(schema.messages.id));
return messages as Message[];
} catch (error) {
console.error('Error getting messages:', error);
throw error;
}
};
export const sendMessage = async (message: Message) => {
try {
const [newMessage] = await db.insert(schema.messages).values({
senderId: message.senderId,
receiverId: message.receiverId,
text: message.text,
hasLocation: message.hasLocation,
hasMedia: message.hasMedia,
hasQuickReply: message.hasQuickReply,
}).returning();
return newMessage as Message;
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
};