Skip to content

Commit a915f0a

Browse files
committed
Added Search feature and updated ranking table
1 parent d8b7fbc commit a915f0a

File tree

8 files changed

+216
-57
lines changed

8 files changed

+216
-57
lines changed

routes/index.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ const contest = require('../models/contest')
33
const { fetchContest, fetchContestRankings } = require('../services/contests')
44
const router = express.Router()
55

6-
router.get('/',(req,res)=> {
7-
res.render("index")
8-
})
96
//fetchContest()
107
//fetchContestRankings('weekly-contest-242')
8+
//fetchContestRankings('weekly-contest-240')
119

12-
router.get('/contests',async (req,res) => {
10+
router.get('/',async (req,res) => {
1311
try {
14-
let contests = await contest.find({})
12+
let contests = await contest.find({}).sort({'startTime':'desc'})
1513
res.render('contests/index',{contests:contests})
1614
}
1715
catch(error) {
18-
console.log("SHITTT")
16+
console.log("error has occurred")
1917
res.send(error.message)
2018
}
2119
})
@@ -24,14 +22,28 @@ router.get('/contests/:contestSlug/ranking/:page', async (req,res) => {
2422
try {
2523
let pageCount = 50
2624
let {contestSlug, page} = req.params
25+
let intPage = parseInt(page)
26+
if(page!=intPage){
27+
throw Error('Page number should be an integer')
28+
}
29+
if(page%1){
30+
intPage++;
31+
}
32+
page = intPage
2733
let toSkip = (page - 1)*pageCount
2834
let contests = await contest.find({_id:contestSlug}, { 'rankings': { $slice: [toSkip,pageCount] }})
2935
let totalPages = 100
36+
if(contests[0]==null){
37+
throw Error("Invalid Contest")
38+
}
3039
if(contests[0].num_user){
31-
totalPages = contests[0].num_user/50
40+
totalPages = parseInt(contests[0].num_user/50)
41+
if(contests[0].num_user/50%1){
42+
totalPages++;
43+
}
3244
}
3345

34-
console.log(contests)
46+
//console.log(contests)
3547
//res.send(contests[0]['rankings'])
3648
res.render('contests/ranking', {contests,totalPages,page})
3749
}
@@ -40,6 +52,26 @@ router.get('/contests/:contestSlug/ranking/:page', async (req,res) => {
4052
res.send(error.message)
4153
}
4254
})
43-
55+
router.post('/contests/:contestSlug/ranking/search', async (req,res) => {
56+
try {
57+
console.log(req.params)
58+
let {user} = req.body
59+
let contests = await contest.find({ _id: req.params.contestSlug} )
60+
console.log(contests)
61+
let searchUsers = []
62+
for(let i=0;i<contests[0].rankings.length;i++){
63+
if(contests[0].rankings[i]._id.includes(user)){
64+
searchUsers.push(contests[0].rankings[i])
65+
}
66+
}
67+
//console.log(searchUsers)
68+
//res.send(searchUsers)
69+
res.render('contests/search', {searchUsers,contestSlug: req.params.contestSlug})
70+
}
71+
catch(error){
72+
console.log(error.message)
73+
res.send(error.message)
74+
}
75+
})
4476

4577
module.exports = router

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ if(process.env.NODE_ENV !== 'production') {
44

55
const express = require('express')
66
const app = express()
7+
app.use(express.urlencoded({extended: true}))
78
const expressLayouts = require('express-ejs-layouts')
8-
99
const indexRouter = require('./routes/index')
1010
app.set('view engine','ejs')
1111
app.set('views',__dirname +'/views')

services/contests.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,23 @@ const fetchContest = async () => {
9090
});
9191
res = await res.json()
9292
//console.log(res.data.allContests[0])
93-
let contestSlug = res.data.allContests[0].titleSlug
94-
let startTime = res.data.allContests[0].startTime
95-
let endTime = startTime + res.data.allContests[0].duration
93+
//let contestSlug = res.data.allContests[0].titleSlug
94+
//let startTime = res.data.allContests[0].startTime*1000
95+
//let endTime = startTime + res.data.allContests[0].duration*1000
9696
for(let i=0;i<res.data.allContests.length;i++)
9797
{
98+
//console.log(i)
9899
let contest = res.data.allContests[i];
99100
let isfound = await Contest.findById(contest.titleSlug)
100-
if(isfound){
101-
break;
102-
}
101+
if(isfound){
102+
break;
103+
}
103104
let newContest = new Contest({
104105
_id: contest.titleSlug,
105106
startTime: contest.startTime*1000,
106-
endTime: startTime + contest.duration*1000,
107+
endTime: contest.startTime*1000 + contest.duration*1000,
107108
lastUpdated: Date.now(),
109+
num_user : contest.num_user
108110
})
109111
let oldContest = await Contest.findById(contest.titleSlug)
110112
if(oldContest==null){

views/contests/index.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
<tbody>
2525
<% for( let contest of contests ) { %>
2626
<tr>
27-
<td><%= contest._id %> </td>
27+
<td><a href="/contests/<%= contest._id %>/ranking/1"><%= contest._id %></a> </td>
2828
<% if (contest.startTime>Date.now()) { %>
2929
<td>Upcoming</td>
3030
<% } else { %>
3131
<td>Virtual</td>
3232
<% } %>
3333
<td><%= contest.startTime %></td>
34-
<td>1.5 Hours </td>
34+
<td><%= (contest.endTime - contest.startTime)/60000 %> minutes</td>
3535
3636
</tr>
3737
<% } %>

views/contests/ranking.ejs

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,77 @@
99
<nav aria-label="...">
1010
<ul class="pagination">
1111
<% if (page>1) { %>
12-
12+
<li class="page-item">
13+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 1 %>" tabindex="-1"><<</a>
14+
</li>
1315
<li class="page-item"><a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/1">1</a></li>
16+
17+
<% } %>
18+
<% if (page===3) { %>
19+
1420
<li class="page-item">
15-
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 1 %>" tabindex="-1">Previous</a>
21+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 1 %>" tabindex="-1"><%= page - 1 %></a>
1622
</li>
23+
24+
25+
<% } %>
26+
<% if (page>3) { %>
27+
<li class="page-item">
28+
<a class="page-link"style="pointer-events: none;" tabindex="-1">..</a>
29+
</li>
30+
<li class="page-item">
31+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 2 %>" tabindex="-1"><%= page - 2 %></a>
32+
</li>
33+
<li class="page-item">
34+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 1 %>" tabindex="-1"><%= page - 1 %></a>
35+
</li>
36+
37+
1738
<% } %>
18-
1939
<li class="page-item active">
2040
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page%>"><%= page %> <span class="sr-only">(current)</span></a>
2141
</li>
22-
<% if (page<totalPages) { %>
42+
<% if (page===totalPages-2) { %>
2343
<li class="page-item">
24-
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= parseInt(page) + 1 %>" tabindex="-1">Next</a>
44+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= parseInt(page) + 1 %>" tabindex="-1"><%= parseInt(page) + 1 %></a>
45+
</li>
46+
<% } %>
47+
<% if (page<totalPages-2) { %>
48+
<li class="page-item">
49+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= parseInt(page) + 1 %>" tabindex="-1"><%= parseInt(page) + 1 %></a>
2550
</li>
51+
<li class="page-item">
52+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= parseInt(page) + 2 %>" tabindex="-1"><%= parseInt(page) + 2 %></a>
53+
</li>
54+
<li class="page-item">
55+
<a class="page-link"style="pointer-events: none;" tabindex="-1">..</a>
56+
</li>
57+
<% } %>
58+
<% if (page<totalPages) { %>
59+
2660
<li class="page-item"><a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= totalPages %> "><%= totalPages %> </a></li>
61+
<li class="page-item">
62+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page + 1 %>" tabindex="-1">>></a>
63+
</li>
2764
<% } %>
2865

2966
</ul>
3067
</nav>
68+
<form action="/contests/<%= contests[0]._id %>/ranking/search" method='POST'>
69+
<div class="mb-3">
70+
<input type="text" class="form-control" id="user" name="user" placeholder="Search For a Contestant">
71+
<button type="submit" class="btn btn-info mt-3 mb-3">Search</button>
72+
</div>
73+
</form>
3174
<div class="table-responsive">
3275
<table class="table table-hover table-striped table-fixed">
3376
<thead>
3477
<tr>
3578
<th>#</th>
3679
<th>Rank</th>
3780
<th>Name</th>
81+
<th>Current Rating</th>
82+
<th>Predicted Rating</th>
3883
<th>Country Name</th>
3984
</tr>
4085
</thead>
@@ -45,6 +90,17 @@
4590
<td><%= i+1 %> </td>
4691
<td><%= contests[0].rankings[i].rank %> </td>
4792
<td><a href="https://leetcode.com/<%=contests[0].rankings[i]._id%>/"><%= contests[0].rankings[i]._id %> </a></td>
93+
<% if ( contests[0].rankings[i].current_rating != -1) { %>
94+
<td><%= contests[0].rankings[i].current_rating %> </td>
95+
<% } else { %>
96+
<td>?</td>
97+
<% } %>
98+
<% if ( contests[0].rankings[i].predicted_rating != -1) { %>
99+
<td><%= contests[0].rankings[i].predicted_rating %> </td>
100+
<% } else { %>
101+
<td>?</td>
102+
<% } %>
103+
48104
<td><%= contests[0].rankings[i].country_name %> </td>
49105
</tr>
50106
<% } %>
@@ -55,24 +111,62 @@
55111
<nav aria-label="...">
56112
<ul class="pagination">
57113
<% if (page>1) { %>
58-
114+
<li class="page-item">
115+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 1 %>" tabindex="-1"><<</a>
116+
</li>
59117
<li class="page-item"><a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/1">1</a></li>
118+
119+
<% } %>
120+
<% if (page===3) { %>
121+
60122
<li class="page-item">
61-
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 1 %>" tabindex="-1">Previous</a>
123+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 1 %>" tabindex="-1"><%= page - 1 %></a>
62124
</li>
125+
126+
127+
<% } %>
128+
<% if (page>3) { %>
129+
<li class="page-item">
130+
<a class="page-link"style="pointer-events: none;" tabindex="-1">..</a>
131+
</li>
132+
<li class="page-item">
133+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 2 %>" tabindex="-1"><%= page - 2 %></a>
134+
</li>
135+
<li class="page-item">
136+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page - 1 %>" tabindex="-1"><%= page - 1 %></a>
137+
</li>
138+
139+
63140
<% } %>
64-
65141
<li class="page-item active">
66142
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page%>"><%= page %> <span class="sr-only">(current)</span></a>
67143
</li>
68-
<% if (page<totalPages) { %>
144+
<% if (page===totalPages-2) { %>
145+
<li class="page-item">
146+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= parseInt(page) + 1 %>" tabindex="-1"><%= parseInt(page) + 1 %></a>
147+
</li>
148+
<% } %>
149+
<% if (page<totalPages-2) { %>
69150
<li class="page-item">
70-
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= parseInt(page) + 1 %>" tabindex="-1">Next</a>
151+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= parseInt(page) + 1 %>" tabindex="-1"><%= parseInt(page) + 1 %></a>
71152
</li>
153+
<li class="page-item">
154+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= parseInt(page) + 2 %>" tabindex="-1"><%= parseInt(page) + 2 %></a>
155+
</li>
156+
<li class="page-item">
157+
<a class="page-link"style="pointer-events: none;" tabindex="-1">..</a>
158+
</li>
159+
<% } %>
160+
<% if (page<totalPages) { %>
161+
72162
<li class="page-item"><a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= totalPages %> "><%= totalPages %> </a></li>
163+
<li class="page-item">
164+
<a class="page-link" href="/contests/<%= contests[0]._id %>/ranking/<%= page + 1 %>" tabindex="-1">>></a>
165+
</li>
73166
<% } %>
74167
75168
</ul>
76169
</nav>
170+
77171
</div>
78172
</div>

views/contests/search.ejs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
2+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
3+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
4+
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-143874982-1"></script>
5+
<script async defer src="https://buttons.github.io/buttons.js"></script>
6+
<script data-ad-client="ca-pub-5213877146531950" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
7+
<div class="container">
8+
<h1 class="mb-5 mt-3" style="text-align: center;">Ranking For <%= contestSlug %> </h1>
9+
10+
<form action="/contests/<%= contestSlug %>/ranking/search" method='POST'>
11+
<div class="mb-3">
12+
<input type="text" class="form-control" id="user" name="user" placeholder="Search For a Contestant">
13+
<button type="submit" class="btn btn-info mt-3 mb-3">Search</button>
14+
</div>
15+
</form>
16+
<div class="table-responsive">
17+
<table class="table table-hover table-striped table-fixed">
18+
<thead>
19+
<tr>
20+
<th>#</th>
21+
<th>Rank</th>
22+
<th>Name</th>
23+
<th>Current Rating</th>
24+
<th>Predicted Rating</th>
25+
<th>Country Name</th>
26+
</tr>
27+
</thead>
28+
<tbody>
29+
<% if (searchUsers) { %>
30+
<% for( let i = 0; i < searchUsers.length; i++ ) { %>
31+
<tr>
32+
<td><%= i+1 %> </td>
33+
<td><%= searchUsers[i].rank %> </td>
34+
<td><a href="https://leetcode.com/<%=searchUsers[i]._id%>/"><%= searchUsers[i]._id %> </a></td>
35+
<% if ( searchUsers[i].current_rating != -1) { %>
36+
<td><%= searchUsers[i].current_rating %> </td>
37+
<% } else { %>
38+
<td>?</td>
39+
<% } %>
40+
<% if ( searchUsers[i].predicted_rating != -1) { %>
41+
<td><%= searchUsers[i].predicted_rating %> </td>
42+
<% } else { %>
43+
<td>?</td>
44+
<% } %>
45+
<td><%= searchUsers[i].country_name %> </td>
46+
</tr>
47+
<% } %>
48+
<% } %>
49+
50+
</tbody>
51+
</table>
52+
<a href="/contests/weekly-contest-242/ranking/1" class="btn btn-info btn-md" role="button">All Rankings</a>
53+
54+
</div>
55+
</div>

views/layouts/layout.ejs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
<title>Leetcode rating predictor</title>
1111
</head>
1212
<body>
13-
<%- include('./navbar.ejs') %>
14-
<%- body %>
15-
16-
13+
<div class="container">
14+
<a href="/" class="btn btn-info btn-md mt-3" role="button">All Contests</a>
15+
<%- body %>
16+
</div>
1717
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
1818
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
1919
</body>

0 commit comments

Comments
 (0)