forked from IITP-X-IITG/Codepeak-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.js
More file actions
140 lines (122 loc) · 3.33 KB
/
check.js
File metadata and controls
140 lines (122 loc) · 3.33 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
dotenv.config();
// Define Project Schema
const projectSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
tags: {
type: [String],
default: []
},
mentor: {
type: String,
required: true
},
mentorGithub: {
type: String,
required: true
},
languages: {
type: [String],
default: []
},
githubLink: {
type: String,
required: true,
unique: true
},
image: {
type: String
},
sponsered: { // Note: this matches your existing data which uses "sponsered" not "sponsored"
type: Boolean,
default: true
},
year: {
type: Number,
required: true
}
});
// Create model
const Project = mongoose.model('Project', projectSchema);
// MongoDB connection
const mongoUri = process.env.URL || "mongodb://localhost:27017/codepeak";
// Read projects data
const loadProjects = () => {
try {
const projectsData = fs.readFileSync('./updatedProjects.json', 'utf-8');
return JSON.parse(projectsData);
} catch (error) {
console.error('Error reading projects file:', error);
return [];
}
};
// Upload projects to database
const uploadProjects = async (projects) => {
console.log(`Attempting to upload ${projects.length} projects to database...`);
let addedCount = 0;
let skippedCount = 0;
let errorCount = 0;
for (const project of projects) {
try {
// Check if project already exists
const existingProject = await Project.findOne({ githubLink: project.githubLink });
if (existingProject) {
console.log(`Project already exists: ${project.title}`);
skippedCount++;
continue;
}
// Fill in any missing fields with defaults
const completeProject = {
...project,
tags: project.tags || [],
languages: project.languages || [],
image: project.image || "",
sponsered: project.sponsered !== undefined ? project.sponsered : true
};
// Create and save new project
const newProject = new Project(completeProject);
await newProject.save();
console.log(`Added project: ${project.title}`);
addedCount++;
} catch (error) {
console.error(`Error adding project ${project.title}:`, error.message);
errorCount++;
}
}
return { addedCount, skippedCount, errorCount };
};
// Main function
const main = async () => {
try {
// Connect to MongoDB
await mongoose.connect(mongoUri);
console.log('Connected to MongoDB');
// Load projects
const projects = loadProjects();
console.log(`Loaded ${projects.length} projects from file`);
// Upload projects
const results = await uploadProjects(projects);
console.log('\nSummary:');
console.log(`Projects processed: ${projects.length}`);
console.log(`Projects added: ${results.addedCount}`);
console.log(`Projects skipped (already exist): ${results.skippedCount}`);
console.log(`Errors: ${results.errorCount}`);
} catch (error) {
console.error('Error:', error);
} finally {
// Close MongoDB connection
await mongoose.connection.close();
console.log('MongoDB connection closed');
}
};
main();