add relations
This commit is contained in:
34
app/api/data/route.ts
Normal file
34
app/api/data/route.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { db } from '@/app/connection';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const database = await db;
|
||||
|
||||
// Fetch all users
|
||||
const users = await database.userRepository.find();
|
||||
|
||||
// Fetch all posts with their related users and tags
|
||||
const posts = await database.postRepository.find({
|
||||
relations: ['user', 'tags'],
|
||||
order: { createdAt: 'DESC' }
|
||||
});
|
||||
|
||||
// Fetch all tags
|
||||
const tags = await database.tagRepository.find({
|
||||
order: { name: 'ASC' }
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
users,
|
||||
posts,
|
||||
tags
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching data:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch data' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
64
app/api/posts/route.ts
Normal file
64
app/api/posts/route.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/app/connection';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { content, username, email } = await request.json();
|
||||
|
||||
if (!content || !username || !email) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Content, username, and email are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const database = await db;
|
||||
|
||||
// Check if user exists, if not create one
|
||||
let user = await database.userRepository.findOne({
|
||||
where: { email }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
user = database.userRepository.create({
|
||||
name: username,
|
||||
email: email,
|
||||
});
|
||||
await database.userRepository.save(user);
|
||||
}
|
||||
|
||||
// Extract hashtags from content
|
||||
const hashtagRegex = /#([a-zA-Z0-9_]+)/g;
|
||||
const hashtags = [...content.matchAll(hashtagRegex)].map(match => match[1].toLowerCase());
|
||||
const uniqueHashtags = [...new Set(hashtags)];
|
||||
|
||||
// Create or find tags
|
||||
const tags = [];
|
||||
for (const tagName of uniqueHashtags) {
|
||||
let tag = await database.tagRepository.findOne({ where: { name: tagName } });
|
||||
if (!tag) {
|
||||
tag = database.tagRepository.create({ name: tagName });
|
||||
await database.tagRepository.save(tag);
|
||||
}
|
||||
tags.push(tag);
|
||||
}
|
||||
|
||||
// Create the post
|
||||
const post = database.postRepository.create({
|
||||
content,
|
||||
username,
|
||||
userEmail: email,
|
||||
tags
|
||||
});
|
||||
|
||||
const savedPost = await database.postRepository.save(post);
|
||||
|
||||
return NextResponse.json(savedPost, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating post:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create post' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user