add countdown apis
This commit is contained in:
37
src/app/api/relationships/countdown/get/route.ts
Normal file
37
src/app/api/relationships/countdown/get/route.ts
Normal file
@ -0,0 +1,37 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
import { getCountdown } from '~/server/functions';
|
||||
import { middleware } from '~/middleware';
|
||||
import type { Countdown } 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') ?? '');
|
||||
if (!userId || isNaN(userId))
|
||||
return NextResponse.json(
|
||||
{ message: 'Missing userId' }, { status: 400 }
|
||||
);
|
||||
const countdown: Countdown | null = await getCountdown(userId);
|
||||
if (!countdown) {
|
||||
return NextResponse.json(
|
||||
{ message: 'No countdown found' }, { status: 404 }
|
||||
);
|
||||
}
|
||||
return NextResponse.json(countdown);
|
||||
} catch (error) {
|
||||
console.error('Error getting countdown:', error);
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json(
|
||||
{ message: error.message }, { status: 500 }
|
||||
);
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ message: 'Unknown error occurred' }, { status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
40
src/app/api/relationships/countdown/set/route.ts
Normal file
40
src/app/api/relationships/countdown/set/route.ts
Normal file
@ -0,0 +1,40 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
import { setCountdown } from '~/server/functions';
|
||||
import { middleware } from '~/middleware';
|
||||
import type { Countdown } 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 {
|
||||
userId: number;
|
||||
countdown: Countdown;
|
||||
};
|
||||
const { userId, countdown } = body;
|
||||
if (!userId || !countdown || isNaN(userId))
|
||||
return NextResponse.json(
|
||||
{ message: 'Missing userId or countdown' }, { status: 400 }
|
||||
);
|
||||
const newCountdown: Countdown = await setCountdown(userId, countdown);
|
||||
if (!newCountdown) {
|
||||
return NextResponse.json(
|
||||
{ message: 'Error setting countdown' }, { status: 500 }
|
||||
);
|
||||
}
|
||||
return NextResponse.json(newCountdown);
|
||||
} catch (error) {
|
||||
console.error('Error setting countdown:', error);
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json(
|
||||
{ message: error.message }, { status: 500 }
|
||||
);
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ message: 'Unknown error occurred' }, { status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
80
src/app/api/websocket/messages/route.ts
Normal file
80
src/app/api/websocket/messages/route.ts
Normal file
@ -0,0 +1,80 @@
|
||||
// WebSocket for client to receive messages
|
||||
//import { Server } from 'socket.io';
|
||||
//import { Client, Notification } from 'pg';
|
||||
//import { NextResponse } from 'next/server';
|
||||
//import type { NextRequest } from 'next/server';
|
||||
//import type { Message } from '~/server/types';
|
||||
|
||||
//let isInitialized = false;
|
||||
//const io = new Server();
|
||||
|
||||
//const pgClient = new Client({
|
||||
//connectionString: process.env.DATABASE_URL,
|
||||
//});
|
||||
|
||||
//const initializePostgres = async () => {
|
||||
//if (isInitialized) return;
|
||||
//try {
|
||||
//await pgClient.connect();
|
||||
//console.log('Connected to PostgreSQL');
|
||||
//await pgClient.query('LISTEN new_message');
|
||||
|
||||
//pgClient.on('notification', (msg: Notification) => {
|
||||
//try {
|
||||
//const newMessage: Message = JSON.parse(msg.payload ?? '{}') as Message;
|
||||
//const { receiverId, text, id } = newMessage;
|
||||
//if (receiverId && typeof receiverId === 'number') {
|
||||
//io.to(receiverId.toString()).emit('message', newMessage);
|
||||
//console.log(`Message sent to room ${receiverId} with text: ${text}`);
|
||||
//} else {
|
||||
//console.error('Invalid receiverId:', receiverId);
|
||||
//}
|
||||
//} catch (error) {
|
||||
//console.error('Error parsing notification payload:', error);
|
||||
//}
|
||||
//});
|
||||
//isInitialized = true;
|
||||
//} catch (error) {
|
||||
//console.error('Error connecting to PostgreSQL:', error);
|
||||
//throw error;
|
||||
//}
|
||||
//};
|
||||
|
||||
//io.on('connection', (socket) => {
|
||||
//console.log('WebSocket client connected', socket.id);
|
||||
|
||||
//socket.on('join', async (userId: number) => {
|
||||
//const roomId = userId.toString();
|
||||
//await socket.join(roomId);
|
||||
//console.log(`WebSocket client joined room ${userId}`);
|
||||
//});
|
||||
|
||||
//socket.on('error', (error) => {
|
||||
//console.error('WebSocket error:', error);
|
||||
//});
|
||||
|
||||
//socket.on('disconnect', () => {
|
||||
//console.log('WebSocket client disconnected');
|
||||
//});
|
||||
//});
|
||||
|
||||
//export const runtime = 'edge';
|
||||
|
||||
//export const GET = async (request: NextRequest) => {
|
||||
//try {
|
||||
//await initializePostgres();
|
||||
//// @ts-expect-error: Socket.IO types conflict with Next.js Request
|
||||
//const upgrade = await io.handleUpgrade(request);
|
||||
|
||||
//if (!upgrade || !upgrade.headers)
|
||||
//throw new Error('Failed to upgrade connection');
|
||||
|
||||
//return new NextResponse(null, {
|
||||
//status: 101,
|
||||
//headers: upgrade.headers,
|
||||
//});
|
||||
//} catch (error) {
|
||||
//console.error('Error handling upgrade:', error);
|
||||
//return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
|
||||
//}
|
||||
//};
|
@ -26,6 +26,29 @@ export const getUser = async (userId: number) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getRelationship = async (userId: number) => {
|
||||
try {
|
||||
const user = await getUser(userId);
|
||||
if (!user) throw new Error("User not found");
|
||||
const userRelationship = await db.select()
|
||||
.from(schema.userRelationships)
|
||||
.where(eq(schema.userRelationships.userId, user.id))
|
||||
.limit(1)
|
||||
.then(results => results[0]);
|
||||
if (!userRelationship) throw new Error('No relationships found for user');
|
||||
const relationship = await db.select()
|
||||
.from(schema.relationships)
|
||||
.where(eq(schema.relationships.id, userRelationship.relationshipId))
|
||||
.limit(1)
|
||||
.then(results => results[0] as Relationship);
|
||||
if (!relationship) throw new Error('Relationship not found');
|
||||
return relationship;
|
||||
} catch (error) {
|
||||
console.error('Error getting relationship:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getInitialDataByAppleId = async (appleId: string) => {
|
||||
try {
|
||||
const users = await db.select().from(schema.users)
|
||||
@ -358,3 +381,53 @@ export const sendMessage = async (message: Message) => {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const setCountdown = async (userId: number, countdown: Countdown) => {
|
||||
try {
|
||||
const user = await getUser(userId);
|
||||
if (!user) throw new Error("User not found");
|
||||
const relationship = await getRelationship(userId);
|
||||
if (!relationship) throw new Error("Relationship not found");
|
||||
const existingCountdown: Countdown | null = await getCountdown(userId);
|
||||
let result;
|
||||
if (existingCountdown !== null) {
|
||||
result = await db.update(schema.countdowns)
|
||||
.set({ title: countdown.title, date: countdown.date })
|
||||
.where(eq(schema.countdowns.id, existingCountdown.id))
|
||||
.returning();
|
||||
} else {
|
||||
result = await db.insert(schema.countdowns)
|
||||
.values({
|
||||
relationshipId: relationship.id,
|
||||
title: countdown.title,
|
||||
date: countdown.date,
|
||||
}).returning();
|
||||
}
|
||||
return result[0] as Countdown;
|
||||
} catch (error) {
|
||||
console.error('Error setting countdown:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getCountdown = async (userId: number) => {
|
||||
try {
|
||||
const user = await getUser(userId);
|
||||
if (!user) throw new Error("User not found");
|
||||
const relationship = await getRelationship(userId);
|
||||
if (!relationship) throw new Error("Relationship not found");
|
||||
const countdown = await db.select()
|
||||
.from(schema.countdowns)
|
||||
.where(eq(schema.countdowns.relationshipId, relationship.id))
|
||||
.limit(1)
|
||||
.then(results => results[0] as Countdown);
|
||||
if (!countdown) {
|
||||
console.log('No countdown found');
|
||||
return null;
|
||||
}
|
||||
return countdown;
|
||||
} catch (error) {
|
||||
console.error('Error getting countdown:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user