update
This commit is contained in:
@@ -4,70 +4,168 @@ import SMTPTransport from "nodemailer/lib/smtp-transport";
|
||||
import Mail from "nodemailer/lib/mailer";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const body = (await request.json()) as {
|
||||
email: string;
|
||||
name: string;
|
||||
message: string;
|
||||
};
|
||||
const { email, name, message } = body;
|
||||
try {
|
||||
const body = (await request.json()) as {
|
||||
email: string;
|
||||
name: string;
|
||||
subject: string;
|
||||
message: string;
|
||||
};
|
||||
const { email, name, subject, message } = body;
|
||||
|
||||
const user = process.env.MY_EMAIL ?? "";
|
||||
const pass = process.env.MY_PASSWORD ?? "";
|
||||
console.log('📧 Email request received:', { email, name, subject, messageLength: message.length });
|
||||
|
||||
if (!user || !pass) {
|
||||
console.error("Missing email/password environment variables");
|
||||
return NextResponse.json(
|
||||
{ error: "Missing EMAIL or PASSWORD" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
// Validate input
|
||||
if (!email || !name || !subject || !message) {
|
||||
console.error('❌ Validation failed: Missing required fields');
|
||||
return NextResponse.json(
|
||||
{ error: "Alle Felder sind erforderlich" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
if (!email || !name || !message) {
|
||||
console.error("Invalid request body");
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid request body" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
// Validate email format
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(email)) {
|
||||
console.error('❌ Validation failed: Invalid email format');
|
||||
return NextResponse.json(
|
||||
{ error: "Ungültige E-Mail-Adresse" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const transportOptions: SMTPTransport.Options = {
|
||||
host: "smtp.ionos.de",
|
||||
port: 587,
|
||||
secure: false,
|
||||
requireTLS: true,
|
||||
auth: {
|
||||
type: "login",
|
||||
user,
|
||||
pass,
|
||||
},
|
||||
};
|
||||
// Validate message length
|
||||
if (message.length < 10) {
|
||||
console.error('❌ Validation failed: Message too short');
|
||||
return NextResponse.json(
|
||||
{ error: "Nachricht muss mindestens 10 Zeichen lang sein" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const transport = nodemailer.createTransport(transportOptions);
|
||||
const user = process.env.MY_EMAIL ?? "";
|
||||
const pass = process.env.MY_PASSWORD ?? "";
|
||||
|
||||
const mailOptions: Mail.Options = {
|
||||
from: user,
|
||||
to: user, // Ensure this is the correct email address
|
||||
subject: `Message from ${name} (${email})`,
|
||||
text: message + "\n\n" + email,
|
||||
};
|
||||
|
||||
const sendMailPromise = () =>
|
||||
new Promise<string>((resolve, reject) => {
|
||||
transport.sendMail(mailOptions, function (err, info) {
|
||||
if (!err) {
|
||||
resolve(info.response);
|
||||
} else {
|
||||
console.error("Error sending email:", err);
|
||||
reject(err.message);
|
||||
}
|
||||
});
|
||||
console.log('🔑 Environment check:', {
|
||||
hasEmail: !!user,
|
||||
hasPassword: !!pass,
|
||||
emailHost: user.split('@')[1] || 'unknown'
|
||||
});
|
||||
|
||||
try {
|
||||
await sendMailPromise();
|
||||
return NextResponse.json({ message: "Email sent" });
|
||||
if (!user || !pass) {
|
||||
console.error("❌ Missing email/password environment variables");
|
||||
return NextResponse.json(
|
||||
{ error: "E-Mail-Server nicht konfiguriert" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
const transportOptions: SMTPTransport.Options = {
|
||||
host: "smtp.ionos.de",
|
||||
port: 587,
|
||||
secure: false,
|
||||
requireTLS: true,
|
||||
auth: {
|
||||
type: "login",
|
||||
user,
|
||||
pass,
|
||||
},
|
||||
// Add timeout and debug options
|
||||
connectionTimeout: 10000,
|
||||
greetingTimeout: 10000,
|
||||
socketTimeout: 10000,
|
||||
};
|
||||
|
||||
console.log('🚀 Creating transport with options:', {
|
||||
host: transportOptions.host,
|
||||
port: transportOptions.port,
|
||||
secure: transportOptions.secure,
|
||||
user: user.split('@')[0] + '@***' // Hide full email in logs
|
||||
});
|
||||
|
||||
const transport = nodemailer.createTransport(transportOptions);
|
||||
|
||||
// Verify transport configuration
|
||||
try {
|
||||
await transport.verify();
|
||||
console.log('✅ SMTP connection verified successfully');
|
||||
} catch (verifyError) {
|
||||
console.error('❌ SMTP verification failed:', verifyError);
|
||||
return NextResponse.json(
|
||||
{ error: "E-Mail-Server-Verbindung fehlgeschlagen" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
const mailOptions: Mail.Options = {
|
||||
from: `"Portfolio Contact" <${user}>`,
|
||||
to: "contact@dki.one", // Send to your contact email
|
||||
replyTo: email,
|
||||
subject: `Portfolio Kontakt: ${subject}`,
|
||||
html: `
|
||||
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
|
||||
<h2 style="color: #3b82f6;">Neue Kontaktanfrage von deinem Portfolio</h2>
|
||||
|
||||
<div style="background: #f8fafc; padding: 20px; border-radius: 8px; margin: 20px 0;">
|
||||
<h3 style="color: #1e293b; margin-top: 0;">Nachricht von ${name}</h3>
|
||||
<p style="color: #475569; margin: 8px 0;"><strong>E-Mail:</strong> ${email}</p>
|
||||
<p style="color: #475569; margin: 8px 0;"><strong>Betreff:</strong> ${subject}</p>
|
||||
</div>
|
||||
|
||||
<div style="background: #ffffff; padding: 20px; border-radius: 8px; border-left: 4px solid #3b82f6;">
|
||||
<h4 style="color: #1e293b; margin-top: 0;">Nachricht:</h4>
|
||||
<p style="color: #374151; line-height: 1.6; white-space: pre-wrap;">${message}</p>
|
||||
</div>
|
||||
|
||||
<div style="text-align: center; margin-top: 30px; padding: 20px; background: #f1f5f9; border-radius: 8px;">
|
||||
<p style="color: #64748b; margin: 0; font-size: 14px;">
|
||||
Diese E-Mail wurde automatisch von deinem Portfolio generiert.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
`,
|
||||
text: `
|
||||
Neue Kontaktanfrage von deinem Portfolio
|
||||
|
||||
Von: ${name} (${email})
|
||||
Betreff: ${subject}
|
||||
|
||||
Nachricht:
|
||||
${message}
|
||||
|
||||
---
|
||||
Diese E-Mail wurde automatisch von deinem Portfolio generiert.
|
||||
`,
|
||||
};
|
||||
|
||||
console.log('📤 Sending email...');
|
||||
|
||||
const sendMailPromise = () =>
|
||||
new Promise<string>((resolve, reject) => {
|
||||
transport.sendMail(mailOptions, function (err, info) {
|
||||
if (!err) {
|
||||
console.log('✅ Email sent successfully:', info.response);
|
||||
resolve(info.response);
|
||||
} else {
|
||||
console.error("❌ Error sending email:", err);
|
||||
reject(err.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const result = await sendMailPromise();
|
||||
console.log('🎉 Email process completed successfully');
|
||||
|
||||
return NextResponse.json({
|
||||
message: "E-Mail erfolgreich gesendet",
|
||||
messageId: result
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error("Error sending email:", err);
|
||||
return NextResponse.json({ error: "Failed to send email" }, { status: 500 });
|
||||
console.error("❌ Unexpected error in email API:", err);
|
||||
return NextResponse.json({
|
||||
error: "Fehler beim Senden der E-Mail",
|
||||
details: err instanceof Error ? err.message : 'Unbekannter Fehler'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
74
app/api/projects/[id]/route.ts
Normal file
74
app/api/projects/[id]/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
|
||||
const project = await prisma.project.findUnique({
|
||||
where: { id }
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Project not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(project);
|
||||
} catch (error) {
|
||||
console.error('Error fetching project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch project' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
const data = await request.json();
|
||||
|
||||
const project = await prisma.project.update({
|
||||
where: { id },
|
||||
data: { ...data, updatedAt: new Date() }
|
||||
});
|
||||
|
||||
return NextResponse.json(project);
|
||||
} catch (error) {
|
||||
console.error('Error updating project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update project' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
|
||||
await prisma.project.delete({
|
||||
where: { id }
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error deleting project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete project' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
78
app/api/projects/route.ts
Normal file
78
app/api/projects/route.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const limit = parseInt(searchParams.get('limit') || '50');
|
||||
const category = searchParams.get('category');
|
||||
const featured = searchParams.get('featured');
|
||||
const published = searchParams.get('published');
|
||||
const difficulty = searchParams.get('difficulty');
|
||||
const search = searchParams.get('search');
|
||||
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
|
||||
if (category) where.category = category;
|
||||
if (featured !== null) where.featured = featured === 'true';
|
||||
if (published !== null) where.published = published === 'true';
|
||||
if (difficulty) where.difficulty = difficulty;
|
||||
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ title: { contains: search, mode: 'insensitive' } },
|
||||
{ description: { contains: search, mode: 'insensitive' } },
|
||||
{ tags: { hasSome: [search] } },
|
||||
{ content: { contains: search, mode: 'insensitive' } }
|
||||
];
|
||||
}
|
||||
|
||||
const [projects, total] = await Promise.all([
|
||||
prisma.project.findMany({
|
||||
where,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
skip,
|
||||
take: limit
|
||||
}),
|
||||
prisma.project.count({ where })
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
projects,
|
||||
total,
|
||||
pages: Math.ceil(total / limit),
|
||||
currentPage: page
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching projects:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch projects' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const data = await request.json();
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: {
|
||||
...data,
|
||||
performance: data.performance || { lighthouse: 90, bundleSize: '50KB', loadTime: '1.5s' },
|
||||
analytics: data.analytics || { views: 0, likes: 0, shares: 0 }
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json(project);
|
||||
} catch (error) {
|
||||
console.error('Error creating project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create project' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
81
app/api/projects/search/route.ts
Normal file
81
app/api/projects/search/route.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const slug = searchParams.get('slug');
|
||||
const search = searchParams.get('search');
|
||||
const category = searchParams.get('category');
|
||||
|
||||
if (slug) {
|
||||
// Search by slug (convert title to slug format)
|
||||
const projects = await prisma.project.findMany({
|
||||
where: {
|
||||
published: true
|
||||
},
|
||||
orderBy: { createdAt: 'desc' }
|
||||
});
|
||||
|
||||
// Find exact match by converting titles to slugs
|
||||
const foundProject = projects.find(project => {
|
||||
const projectSlug = project.title.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
return projectSlug === slug;
|
||||
});
|
||||
|
||||
if (foundProject) {
|
||||
return NextResponse.json({ projects: [foundProject] });
|
||||
}
|
||||
|
||||
// If no exact match, return empty array
|
||||
return NextResponse.json({ projects: [] });
|
||||
}
|
||||
|
||||
if (search) {
|
||||
// General search
|
||||
const projects = await prisma.project.findMany({
|
||||
where: {
|
||||
published: true,
|
||||
OR: [
|
||||
{ title: { contains: search, mode: 'insensitive' } },
|
||||
{ description: { contains: search, mode: 'insensitive' } },
|
||||
{ tags: { hasSome: [search] } },
|
||||
{ content: { contains: search, mode: 'insensitive' } }
|
||||
]
|
||||
},
|
||||
orderBy: { createdAt: 'desc' }
|
||||
});
|
||||
|
||||
return NextResponse.json({ projects });
|
||||
}
|
||||
|
||||
if (category && category !== 'All') {
|
||||
// Filter by category
|
||||
const projects = await prisma.project.findMany({
|
||||
where: {
|
||||
published: true,
|
||||
category: category
|
||||
},
|
||||
orderBy: { createdAt: 'desc' }
|
||||
});
|
||||
|
||||
return NextResponse.json({ projects });
|
||||
}
|
||||
|
||||
// Return all published projects if no specific search
|
||||
const projects = await prisma.project.findMany({
|
||||
where: { published: true },
|
||||
orderBy: { createdAt: 'desc' }
|
||||
});
|
||||
|
||||
return NextResponse.json({ projects });
|
||||
} catch (error) {
|
||||
console.error('Error searching projects:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to search projects' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user