initial commit
This commit is contained in:
71
app/api/topics/[slug]/related/route.ts
Normal file
71
app/api/topics/[slug]/related/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import connectDB from '@/lib/mongodb'
|
||||
import TopicModel, { transformToTopics } from '@/models/topic'
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string }> }
|
||||
) {
|
||||
try {
|
||||
await connectDB()
|
||||
|
||||
const { slug } = await params
|
||||
const searchParams = request.nextUrl.searchParams
|
||||
const limit = parseInt(searchParams.get('limit') || '2')
|
||||
|
||||
if (!slug) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: {
|
||||
message: 'Topic slug is required',
|
||||
code: 'SLUG_REQUIRED',
|
||||
},
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// First, get the current post to find its ID and tags
|
||||
const currentPost = await TopicModel.findOne({
|
||||
slug,
|
||||
isDraft: false
|
||||
}).lean()
|
||||
|
||||
if (!currentPost) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: [],
|
||||
})
|
||||
}
|
||||
|
||||
// Find related posts by excluding the current post and sorting by publishedAt
|
||||
const relatedPosts = await TopicModel.find({
|
||||
id: { $ne: currentPost.id },
|
||||
isDraft: false,
|
||||
})
|
||||
.sort({ publishedAt: -1 })
|
||||
.limit(limit)
|
||||
.lean()
|
||||
|
||||
// Transform to proper format
|
||||
const transformedPosts = transformToTopics(relatedPosts)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: transformedPosts,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching related posts:', error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: {
|
||||
message: 'Failed to fetch related posts',
|
||||
code: 'SERVER_ERROR',
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
79
app/api/topics/[slug]/route.ts
Normal file
79
app/api/topics/[slug]/route.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import connectDB from '@/lib/mongodb'
|
||||
import TopicModel, { transformToTopic } from '@/models/topic'
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string }> }
|
||||
) {
|
||||
try {
|
||||
await connectDB()
|
||||
|
||||
const { slug } = await params
|
||||
|
||||
if (!slug) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: {
|
||||
message: 'Topic slug is required',
|
||||
code: 'SLUG_REQUIRED',
|
||||
},
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Find the topic post by slug (only published posts)
|
||||
const post = await TopicModel.findOne({
|
||||
slug,
|
||||
isDraft: false
|
||||
}).lean()
|
||||
|
||||
if (!post) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: {
|
||||
message: 'Topic post not found',
|
||||
code: 'POST_NOT_FOUND',
|
||||
},
|
||||
},
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// Transform to proper format
|
||||
const transformedPost = transformToTopic(post)
|
||||
|
||||
if (!transformedPost) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: {
|
||||
message: 'Failed to process topic post',
|
||||
code: 'PROCESSING_ERROR',
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: transformedPost,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching topic post:', error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: {
|
||||
message: 'Failed to fetch topic post',
|
||||
code: 'SERVER_ERROR',
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
75
app/api/topics/[slug]/view/route.ts
Normal file
75
app/api/topics/[slug]/view/route.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import connectDB from '@/lib/mongodb'
|
||||
import TopicModel from '@/models/topic'
|
||||
|
||||
// Track topic view (increment view count and daily analytics)
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string }> }
|
||||
) {
|
||||
try {
|
||||
const { slug } = await params
|
||||
|
||||
if (!slug) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: { message: 'Topic slug is required', code: 'MISSING_SLUG' },
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
await connectDB()
|
||||
|
||||
// Get current date in YYYY-MM-DD format
|
||||
const today = new Date().toISOString().split('T')[0]
|
||||
|
||||
// Find and update the topic
|
||||
const topic = await TopicModel.findOneAndUpdate(
|
||||
{ slug: slug, isDraft: false }, // Only track views for published posts
|
||||
{
|
||||
$inc: { views: 1 }, // Increment total views
|
||||
$push: {
|
||||
viewHistory: {
|
||||
$each: [{ date: today, count: 1 }],
|
||||
$slice: -30, // Keep only last 30 days
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
new: true,
|
||||
projection: { views: 1, slug: 1, title: 1, authorId: 1 } // Include authorId for cache invalidation
|
||||
}
|
||||
)
|
||||
|
||||
if (!topic) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: { message: 'Topic not found or is a draft', code: 'TOPIC_NOT_FOUND' },
|
||||
},
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
slug: topic.slug,
|
||||
title: topic.title,
|
||||
views: topic.views,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('View tracking error:', error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: { message: 'Failed to track view', code: 'INTERNAL_ERROR' },
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user