Skip to content

Commit 0469af6

Browse files
committed
Update: user schema and data fetching methods (store user contests history)
1 parent 800dc12 commit 0469af6

File tree

6 files changed

+284
-117
lines changed

6 files changed

+284
-117
lines changed

models/contest.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
const mongoose = require('mongoose');
22
const Schema = mongoose.Schema
33

4+
const rankingSchema = new Schema({
5+
_id: String,
6+
user_slug: String,
7+
country_code: String,
8+
country_name: String,
9+
data_region: {
10+
type: String,
11+
default: "US"
12+
},
13+
rank: Number,
14+
current_rating:{
15+
type: Number,
16+
default:null
17+
},
18+
delta:{
19+
type: Number,
20+
default:null
21+
}
22+
})
423
const ContestRankingsSchema = new Schema({
524
_id: String,
625
startTime: Date,
@@ -10,25 +29,7 @@ const ContestRankingsSchema = new Schema({
1029
lastUpdated:{
1130
type: Date,
1231
},
13-
rankings : [{
14-
_id: String,
15-
user_slug: String,
16-
country_code: String,
17-
country_name: String,
18-
data_region: {
19-
type: String,
20-
default: "US"
21-
},
22-
rank: Number,
23-
current_rating:{
24-
type: Number,
25-
default:-1
26-
},
27-
predicted_rating:{
28-
type: Number,
29-
default:-1
30-
}
31-
}]
32+
rankings : [rankingSchema]
3233
})
3334

3435

models/users.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
const mongoose = require("mongoose")
22

3+
const contestHistorySchema = new mongoose.Schema({
4+
_id: String,
5+
title: String,
6+
startTime: Number,
7+
rating: {
8+
type: Number,
9+
default:1500,
10+
},
11+
ranking:{
12+
type:Number,
13+
default: 0
14+
},
15+
})
316
const userSchema = new mongoose.Schema({
417
_id:{
518
type: String
@@ -16,9 +29,12 @@ const userSchema = new mongoose.Schema({
1629
type: Number,
1730
default:0
1831
},
32+
contestsHistory: [contestHistorySchema],
1933
lastUpdated:{
2034
type: Date,
2135
default: Date.now,
2236
},
2337
})
24-
module.exports = mongoose.model("User",userSchema)
38+
39+
exports.User = mongoose.model("User",userSchema)
40+
exports.ContestHistory = mongoose.model("ContestHistory",contestHistorySchema)

routes/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,13 @@ router.post('/contests/:contestSlug/ranking/search', async (req,res) => {
6060
console.log(req.params)
6161
let {user} = req.body
6262
let contests = await contest.find({ _id: req.params.contestSlug} )
63-
console.log(contests)
64-
let searchUsers = []
63+
let searchResult = []
6564
for(let i=0;i<contests[0].rankings.length;i++){
6665
if(contests[0].rankings[i]._id.includes(user)){
67-
searchUsers.push(contests[0].rankings[i])
66+
searchResult.push(contests[0].rankings[i])
6867
}
6968
}
70-
//console.log(searchUsers)
71-
//res.send(searchUsers)
72-
res.render('contests/search', {searchUsers,contestSlug: req.params.contestSlug})
69+
res.render('contests/search', {searchResult,contestSlug: req.params.contestSlug})
7370
}
7471
catch(error){
7572
console.log(error.message)

server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ mongoose.connect(process.env.DATABASE_URL,{
2121
const db = mongoose.connection
2222
db.on('error',error => console.error(error))
2323
db.once('open',() => console.log('Connected to Mongoose'))
24+
mongoose.set('useFindAndModify', false);
25+
2426
app.use('/',indexRouter)
2527

2628
app.listen(process.env.PORT || 3000)
2729

2830
// const predict = require('./services/predict')
2931
// const schedualing = require('./services/scheduling')
30-
// schedualing.fetchAllContests()
32+
// schedualing.fetchAllContests()
33+
// const users = require('./services/users')

0 commit comments

Comments
 (0)