forked from Pabl0cks/se2-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
98 lines (88 loc) · 2.32 KB
/
Copy pathroute.ts
File metadata and controls
98 lines (88 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { NextResponse } from "next/server";
import { Pool } from "pg";
import { Repository, mockRepositories } from "~~/lib/mockData";
const pool = process.env.POSTGRES_URL
? new Pool({
connectionString: process.env.POSTGRES_URL,
})
: null;
function generateCsv(repositories: Repository[]) {
const headers = [
"ID",
"Full Name",
"Name",
"Owner",
"URL",
"Homepage",
"Stars",
"Forks",
"Created At",
"Updated At",
"Last Seen",
"Saved At",
"Source",
];
const csvRows = [
headers.join(","),
...repositories.map(repo => {
return [
repo.id,
`"${repo.full_name}"`,
`"${repo.name}"`,
`"${repo.owner}"`,
`"${repo.url}"`,
repo.homepage ? `"${repo.homepage}"` : "",
repo.stars || 0,
repo.forks || 0,
repo.created_at || "",
repo.updated_at || "",
repo.last_seen || "",
repo.saved_at || "",
`"${(repo.source || []).join("; ")}"`,
].join(",");
}),
];
return csvRows.join("\n");
}
export async function GET() {
if (!pool) {
const csvContent = generateCsv(mockRepositories);
return new NextResponse(csvContent, {
status: 200,
headers: {
"Content-Type": "text/csv",
"Content-Disposition": `attachment; filename="repositories-${new Date().toISOString().split("T")[0]}.csv"`,
},
});
}
let client;
try {
client = await pool.connect();
// Fetch all repositories
const result = await client.query(`
SELECT
id, full_name, name, owner, url, homepage, stars, forks,
created_at, updated_at, last_seen, saved_at, source
FROM repositories
WHERE deleted_at IS NULL
ORDER BY id
`);
const repositories = result.rows;
const csvContent = generateCsv(repositories);
// Return CSV file
return new NextResponse(csvContent, {
status: 200,
headers: {
"Content-Type": "text/csv",
"Content-Disposition": `attachment; filename="repositories-${new Date().toISOString().split("T")[0]}.csv"`,
},
});
} catch (error) {
console.error("Export error:", error);
return NextResponse.json({ error: "Failed to export repositories", details: String(error) }, { status: 500 });
} finally {
if (client) {
client.release();
}
}
}