Skip to content

Commit 358b19f

Browse files
committed
cleaned up the backend code
1 parent d219f98 commit 358b19f

File tree

4 files changed

+208
-165
lines changed

4 files changed

+208
-165
lines changed

routes/api/route.js

Lines changed: 71 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
const express = require('express');
22
const router = express.Router();
33
const mongoose = require('mongoose');
4-
const Book = require('../../models/Book');
54
const {
6-
validateAndGetBookData,
7-
octokit,
5+
getBooksByLanguage,
6+
getBooksByNameAndLanguage,
87
createIssue,
98
} = require('../../util/util');
9+
const {
10+
validateLanguageInQuery,
11+
validateBugReportRequestBody,
12+
validateNewBookBody,
13+
} = require('../../util/middleware');
1014

1115
const connectMongoose = async () => {
1216
await mongoose.connect(process.env.MONGODB_URI).catch((err) => {
@@ -28,7 +32,7 @@ connectMongoose();
2832
handleMongooseConnectionEvents();
2933

3034
/**
31-
* @endpoint: /api/v1/get
35+
* @endpoint: /api/v1/book
3236
*
3337
* @method GET
3438
* @access public
@@ -39,12 +43,13 @@ handleMongooseConnectionEvents();
3943
*
4044
* @return response
4145
**/
42-
router.get('/get', (req, res) => {
43-
return validateAndGetBookData(req, res);
46+
router.get('/book', validateLanguageInQuery, (req, res) => {
47+
const { language } = req.query;
48+
return getBooksByLanguage(res, language);
4449
});
4550

4651
/**
47-
* @endpoint: /api/v1/search
52+
* @endpoint: /api/v1/book/search
4853
*
4954
* @method GET
5055
* @access public
@@ -56,37 +61,21 @@ router.get('/get', (req, res) => {
5661
*
5762
* @return response
5863
**/
59-
router.get('/search', (req, res) => {
64+
router.get('/book/search', validateLanguageInQuery, (req, res) => {
6065
const { language, name } = req.query;
61-
if (!language) {
62-
return res.status(400).json({
63-
message: 'Bad Request because of no language parameter',
64-
status: 400,
65-
});
66-
} else if (!name) {
67-
return validateAndGetBookData(req, res);
66+
if (!name) {
67+
return getBooksByLanguage(res, language);
6868
} else {
69-
let expression = new RegExp(name, 'i');
70-
Book.find({ language: language, name: expression }, (err, result) => {
71-
if (err) {
72-
console.log(err);
73-
return res.status(404).json({
74-
message: 'Requested information not found',
75-
status: 404,
76-
});
77-
} else {
78-
return res.json(result);
79-
}
80-
});
69+
return getBooksByNameAndLanguage(res, language, name);
8170
}
8271
});
8372

8473
/**
85-
* @endpoint /api/v1/bugs/report
74+
* @endpoint /api/v1/bug/report
8675
*
8776
* @method POST
8877
**/
89-
router.post('/bugs/report', (req, res) => {
78+
router.post('/bug/report', validateBugReportRequestBody, async (req, res) => {
9079
const {
9180
title,
9281
description,
@@ -98,53 +87,39 @@ router.post('/bugs/report', (req, res) => {
9887
username,
9988
} = req.body;
10089

101-
if (
102-
!title ||
103-
!description ||
104-
!expectedBehaviour ||
105-
!device ||
106-
!os ||
107-
!browser ||
108-
!version ||
109-
!username
110-
) {
111-
return res.status(400).json({
112-
message: 'Bad Request because of missing parameter',
113-
status: 400,
90+
const issueBody = ''.concat(
91+
'**Describe the bug**\n',
92+
description,
93+
'\n\n**Expected behavior**\n',
94+
expectedBehaviour,
95+
'\n\n**Desktop/Smartphone**\n-Device: ',
96+
device,
97+
'\n-OS: ',
98+
os,
99+
'\n-Browser: ',
100+
browser,
101+
'\n-Version: ',
102+
version,
103+
'\n\n**Github Username**\n',
104+
username
105+
);
106+
107+
try {
108+
await createIssue(`Bug Report: ${title}`, issueBody, ['bug']);
109+
return res.status(200).json({
110+
message: 'Bug reported successfully',
111+
status: 200,
112+
});
113+
} catch (err) {
114+
return res.status(500).json({
115+
message: 'Internal Server Error',
116+
status: 500,
114117
});
115-
} else {
116-
// check valid github username
117-
octokit.users
118-
.getByUsername({
119-
username,
120-
})
121-
.then((response) => {
122-
if (response.data) {
123-
let issueBody = ''.concat(
124-
'**Describe the bug**\n',
125-
description,
126-
'\n\n**Expected behavior**\n',
127-
expectedBehaviour,
128-
'\n\n**Desktop/Smartphone**\n-Device: ',
129-
device,
130-
'\n-OS: ',
131-
os,
132-
'\n-Browser: ',
133-
browser,
134-
'\n-Version: ',
135-
version,
136-
'\n\n**Github Username**\n',
137-
username
138-
);
139-
createIssue(title, issueBody, ['bug'], res);
140-
}
141-
})
142-
.catch((err) => res.status(404).json(err));
143118
}
144119
});
145120

146121
/**
147-
* @endpoint /api/v1/book/add
122+
* @endpoint /api/v1/book
148123
*
149124
* @method POST
150125
* @access public
@@ -156,36 +131,34 @@ router.post('/bugs/report', (req, res) => {
156131
*
157132
* @return response
158133
**/
159-
router.post('/book/add', (req, res) => {
134+
router.post('/book', validateNewBookBody, async (req, res) => {
160135
const { title, bookName, language, downloadLink, username } = req.body;
161136

162-
if (!title || !bookName || !language || !downloadLink || !username) {
163-
return res.status(400).json({
164-
message: 'Bad Request because of missing parameters',
165-
status: 400,
137+
let issueBody = ''.concat(
138+
'**Book Name**\n',
139+
bookName,
140+
'\n\n**Programming Language**\n',
141+
language,
142+
'\n\n**Book Download Link**\n',
143+
downloadLink,
144+
'\n\n**Github Username**\n',
145+
username
146+
);
147+
148+
try {
149+
await createIssue(`New Book Request: ${title}`, issueBody, [
150+
'question',
151+
]);
152+
return res.status(200).json({
153+
message: 'Book add request submitted successfully',
154+
status: 200,
155+
});
156+
} catch (err) {
157+
console.log(err);
158+
return res.status(500).json({
159+
message: 'Internal Server Error',
160+
status: 500,
166161
});
167-
} else {
168-
// check valid github username
169-
octokit.users
170-
.getByUsername({
171-
username,
172-
})
173-
.then((response) => {
174-
if (response.data) {
175-
let issueBody = ''.concat(
176-
'**Book Name**\n',
177-
bookName,
178-
'\n\n**Programming Language**\n',
179-
language,
180-
'\n\n**Book Download Link**\n',
181-
downloadLink,
182-
'\n\n**Github Username**\n',
183-
username
184-
);
185-
createIssue(title, issueBody, ['question'], res);
186-
}
187-
})
188-
.catch((err) => res.status(404).json(err));
189162
}
190163
});
191164

test/server.test.js

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,50 @@ const app = require('../server');
22
const request = require('supertest');
33
const assert = require('assert');
44

5-
describe('Testing Free Algorithm books website server APIs', ()=>{
6-
7-
describe('1. Testing /api/v1/get', ()=>{
8-
it('a. Normal Test', async ()=>{
5+
describe('Testing Free Algorithm books website server APIs', () => {
6+
describe('1. Testing /api/v1/book', () => {
7+
it('a. Normal Test', async () => {
98
const getResponse = await request(app)
10-
.get('/api/v1/get')
11-
.expect(400)
12-
assert.strictEqual(getResponse.body.message, 'Bad Request because of no language parameter')
13-
})
14-
15-
it('b. Check if API is returning the data', async ()=>{
16-
await request(app)
17-
.get('/api/v1/get?language=JAVA')
18-
.expect(200)
19-
})
20-
})
21-
22-
describe('2. Testing API: /api/v1/search',()=>{
23-
it('a. Normal test',async ()=>{
24-
await request(app)
25-
.get('/api/v1/search')
26-
.expect(400)
27-
})
28-
29-
it('b. Test with name query' , async ()=>{
30-
await request(app)
31-
.get('/api/v1/search?name=algo')
32-
.expect(400)
33-
})
34-
35-
it('c. Test with language query', async ()=>{
9+
.get('/api/v1/book')
10+
.expect(400);
11+
assert.strictEqual(
12+
getResponse.body.message,
13+
'Bad Request because of no language parameter'
14+
);
15+
});
16+
17+
it('b. Check if API is returning the data', async () => {
18+
await request(app).get('/api/v1/book?language=JAVA').expect(200);
19+
});
20+
});
21+
22+
describe('2. Testing API: /api/v1/book/search', () => {
23+
it('a. Normal test', async () => {
24+
await request(app).get('/api/v1/book/search').expect(400);
25+
});
26+
27+
it('b. Test with name query', async () => {
28+
await request(app).get('/api/v1/book/search?name=algo').expect(400);
29+
});
30+
31+
it('c. Test with language query', async () => {
3632
await request(app)
37-
.get('/api/v1/search?language=C')
38-
.expect(200)
39-
})
33+
.get('/api/v1/book/search?language=C')
34+
.expect(200);
35+
});
4036

41-
it('d. Test with both language and name', async()=>{
37+
it('d. Test with both language and name', async () => {
4238
await request(app)
43-
.get('/api/v1/search?language=JAVA&name=algo')
44-
.expect(200)
45-
})
46-
})
39+
.get('/api/v1/book/search?language=JAVA&name=algo')
40+
.expect(200);
41+
});
42+
});
4743

4844
// it('Testing POST APIs', async ()=>{
4945
// const postResponse = await request(app)
50-
// .post('/api/v1/post')
46+
// .post('/api/v1/book')
5147
// .send({'KEY':'VALUE'})
5248
// .expect(201)
5349
// assert.match(postResponse.body.message, /POST/);
5450
// })
55-
})
51+
});

0 commit comments

Comments
 (0)