Skip to content

Commit aea78d6

Browse files
authored
Merge pull request #4 from leetcode-roulette/feature/get-submission-information
Feature/get submission information
2 parents 40e4663 + 388ff51 commit aea78d6

File tree

5 files changed

+56
-25
lines changed

5 files changed

+56
-25
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"build": "tsc",
8-
"dev": "nodemon ./src/app.ts",
9-
"start": "node ./dist/app.js",
8+
"dev": "nodemon ./src/index.ts",
9+
"start": "node ./dist/index.js",
1010
"test": "jest --passWithNoTests"
1111
},
1212
"repository": {

src/app.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
import { config } from 'dotenv';
22
import express, { Application, NextFunction, Request, Response } from 'express';
3-
import { PopulateProblems } from './db/problems';
4-
import { Database } from './db/db.config';
53
import healthcheck from './healthcheck';
64

75
config();
86

9-
const serve = async () : Promise<void> => {
7+
export const app = (() => {
108
const app: Application = express();
11-
12-
await Database.connect();
13-
14-
const PORT = process.env.PORT || 3000;
15-
169
app.use("/", healthcheck);
1710

18-
app.listen(PORT, () => {
19-
console.log(`Server is listening on port ${PORT}`);
20-
});
21-
22-
await PopulateProblems.populate();
23-
}
24-
25-
serve();
11+
return app;
12+
})();

src/db/problems.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,45 @@ export class PopulateProblems {
2323
const rowAlreadyExists : boolean = await this.rowAlreadyExists(problem);
2424

2525
if (rowAlreadyExists) {
26+
await this.updateProblem(problem);
2627
return;
2728
}
2829

30+
await this.saveProblem(problem);
31+
}
32+
33+
private static async rowAlreadyExists(problem : LeetcodeProblem) : Promise<boolean> {
34+
return await Problems.exists({ problemId: problem.stat.question_id }) !== null;
35+
}
36+
37+
private static async updateProblem(problem : LeetcodeProblem) : Promise<void> {
38+
Problems.findOneAndUpdate({ problemId: problem.stat.question_id}, {
39+
problemId: problem.stat.question_id,
40+
title: problem.stat.question__title,
41+
titleSlug: problem.stat.question__title_slug,
42+
isPremium: problem.paid_only,
43+
difficulty: problem.difficulty.level,
44+
frontEndId: problem.stat.frontend_question_id,
45+
numSubmitted: problem.stat.total_submitted,
46+
numAccepted: problem.stat.total_acs
47+
});
48+
}
49+
50+
private static async saveProblem(problem : LeetcodeProblem) : Promise<void> {
2951
const p : IProblem = await Problems.create({
3052
problemId: problem.stat.question_id,
3153
title: problem.stat.question__title,
3254
titleSlug: problem.stat.question__title_slug,
3355
isPremium: problem.paid_only,
3456
difficulty: problem.difficulty.level,
35-
frontEndId: problem.stat.frontend_question_id
57+
frontEndId: problem.stat.frontend_question_id,
58+
numSubmitted: problem.stat.total_submitted,
59+
numAccepted: problem.stat.total_acs
3660
});
3761

3862
await p.save();
3963
}
4064

41-
private static async rowAlreadyExists(problem : LeetcodeProblem) : Promise<boolean> {
42-
return await Problems.exists({ problemId: problem.stat.question_id }) !== null;
43-
}
44-
4565
public static get problems() : Promise<LeetcodeProblem[] | null> | null {
4666
if (this.data === null) {
4767
this.data = ProblemExtractor.problems;

src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { app } from "./app";
2+
import { Database } from "./db/db.config";
3+
import { PopulateProblems } from "./db/problems";
4+
5+
const serve = async () : Promise<void> => {
6+
const PORT : string | number = process.env.PORT || 3000;
7+
8+
try {
9+
await Database.connect();
10+
await PopulateProblems.populate();
11+
} catch(e) {
12+
console.log("Exception caught populating database: " + e);
13+
}
14+
15+
app.listen(PORT, () => {
16+
console.log(`Server is listening on port ${PORT}`);
17+
});
18+
}
19+
20+
serve();

src/models/problems.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export interface IProblem extends Document {
66
titleSlug: string,
77
isPremium: boolean,
88
difficulty: number,
9-
frontEndId: number
9+
frontEndId: number,
10+
numSubmitted: number,
11+
numAccepted: number
1012
};
1113

1214
const problemsSchema : Schema<IProblem> = new mongoose.Schema({
@@ -15,7 +17,9 @@ const problemsSchema : Schema<IProblem> = new mongoose.Schema({
1517
titleSlug: 'string',
1618
isPremium: 'boolean',
1719
difficulty: 'number',
18-
frontEndId: 'number'
20+
frontEndId: 'number',
21+
numSubmitted: 'number',
22+
numAccepted: 'number'
1923
});
2024

2125
const Problems : Model<IProblem> = mongoose.model('problems', problemsSchema);

0 commit comments

Comments
 (0)