Skip to content

Commit ec4a96e

Browse files
Merge pull request #4 from arvind-balaji/refactor_modular
Modular Refactor
2 parents aae21c4 + 7a75f65 commit ec4a96e

33 files changed

Lines changed: 1719 additions & 1298 deletions

.env.sample

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
API_PREFIX=/v1
2+
3+
SOLR_PORT=<MY_SOLR_PORT>
4+
SOLR_CORE=<MY_SOLR_CORE>
5+
SOLR_HOST=<MY_SOLR_HOST>
6+
7+
MONGO_DEV_CONN_URL=mongodb://<MY_MONGO_INSTANCE>
8+
MONGO_LOCAL_CONN_URL=mongodb://<MY_MONGO_INSTANCE>
9+
MONGO_DB_NAME=<MY_DB_NAME>

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ node_modules
33
build
44
npm-debug.log
55
yarn-error.log
6-
scrape
76
.env
87
.DS_Store

app.js

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,10 @@
11
require('dotenv').config();
2+
require('module-alias/register');
23

3-
const express = require('express');
4-
const app = express();
5-
const router = express.Router();
6-
const bodyParser = require('body-parser');
7-
const cors = require('cors')
4+
// init database
5+
const mongoose = require("mongoose");
6+
mongoose.connect(process.env.MONGO_CONN_URL, { useNewUrlParser: true })
87

9-
const fs = require('fs');
10-
const port = process.env.PORT || 8080;
11-
12-
app.use(bodyParser.urlencoded({extended: true}));
13-
app.use(bodyParser.json());
14-
app.set('json spaces', 2);
15-
app.use(cors())
16-
17-
18-
// test route to make sure everything is working
19-
router.get('/', function(req, res) {
20-
res.json({message: 'It Works! Welcome to the CardDB API!'});
21-
});
22-
23-
// dynamically load our routes
24-
fs.readdirSync('./routes/').forEach(function(file) {
25-
if(file.indexOf('.js') > 0){
26-
var route="./routes/"+file;
27-
require(route)(router);
28-
}
29-
});
30-
31-
// all of our routes will be prefixed with /api
32-
app.use('/v1', router);
33-
34-
// start the server
35-
app.listen(port);
36-
console.log('Magic happens on port ' + port);
8+
// load modules
9+
require('./workers/api');
10+
require('./workers/parser')

components/card/controller.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const mongoose = require('mongoose');
2+
const path = require('path');
3+
4+
5+
const Card = require('./model');
6+
7+
8+
module.exports = {
9+
10+
// ctxt = {card}
11+
add: async (ctxt) => {
12+
try {
13+
let card = new Card(ctxt);
14+
card = await card.save();
15+
return card
16+
} catch (error) {
17+
throw error;
18+
}
19+
},
20+
21+
// ctxt = {id}
22+
get: async (ctxt) =>{
23+
const {id} = ctxt
24+
try {
25+
const card = await Card.findOne({_id:id});
26+
return card
27+
} catch (error) {
28+
throw error;
29+
}
30+
},
31+
32+
// ctxt = {skip, limit, query}
33+
getAll: async (ctxt) => {
34+
const {skip=0, limit=100} = ctxt;
35+
try {
36+
const cards = await Card
37+
.find({})
38+
.lean()
39+
.skip(skip)
40+
.limit(limit);
41+
return cards
42+
} catch (error) {
43+
throw error;
44+
}
45+
},
46+
47+
// ctxt = {id}
48+
delete: async (ctxt) => {
49+
const {id} = ctxt
50+
try {
51+
const card = await Card.findOne({id});
52+
await Card.deleteOne({id});
53+
return card;
54+
} catch (error) {
55+
return error;
56+
}
57+
}
58+
};

components/card/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
controller: require('./controller')
3+
, model: require('./model')
4+
};

components/card/model.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const mongoose = require('mongoose');
2+
3+
// schema maps to a collection
4+
const Schema = mongoose.Schema;
5+
6+
const cardSchema = new Schema({
7+
file: {
8+
type:mongoose.Schema.Types.ObjectId,
9+
ref:'File'
10+
},
11+
tag: {
12+
type: String,
13+
required: true,
14+
trim: true,
15+
},
16+
cite: {
17+
type: String,
18+
trim: true,
19+
},
20+
text: {
21+
type: String,
22+
required: true,
23+
minlength: 50
24+
},
25+
card: {
26+
type: String,
27+
required: true,
28+
},
29+
set: {
30+
type: String,
31+
required: true,
32+
trim: true,
33+
},
34+
h1: {
35+
type: String,
36+
trim: true,
37+
},
38+
h2: {
39+
type: String,
40+
trim: true,
41+
},
42+
h3: {
43+
type: String,
44+
trim: true,
45+
},
46+
index: {
47+
type: Number,
48+
},
49+
date: {
50+
type: Date,
51+
},
52+
});
53+
54+
module.exports = mongoose.model('Card', cardSchema); // instance of schema

components/file/controller.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const events = require('events');
2+
const eventEmitter = new events.EventEmitter();
3+
4+
5+
6+
7+
const File = require('./model');
8+
9+
10+
module.exports = {
11+
12+
// ctxt = {file}
13+
add: async (ctxt) => {
14+
try {
15+
let file = new File(ctxt);
16+
file = await file.save()
17+
return file;
18+
} catch (error) {
19+
return error;
20+
}
21+
},
22+
23+
// ctxt = {id}
24+
get: async (ctxt) => {
25+
const {id} = ctxt
26+
try {
27+
const file = await File.findOne({id});
28+
return file;
29+
} catch (error) {
30+
throw error;
31+
}
32+
},
33+
34+
// ctxt = {skip, limit, query}
35+
getAll: async (ctxt) => {
36+
const {skip, limit} = ctxt;
37+
try {
38+
const files = await File
39+
.find({})
40+
.skip(skip)
41+
.limit(limit);
42+
return files
43+
} catch (error) {
44+
throw error;
45+
}
46+
},
47+
48+
// ctxt = {id}
49+
delete: async (ctxt) => {
50+
const {id} = ctxt
51+
try {
52+
const file = await File.findOne({id});
53+
await File.deleteOne({id});
54+
return file;
55+
} catch (error) {
56+
return error;
57+
}
58+
}
59+
};

components/file/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
controller: require('./controller'),
3+
model: require('./model')
4+
};

components/file/model.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const mongoose = require('mongoose');
2+
// schema maps to a collection
3+
const Schema = mongoose.Schema;
4+
5+
const fileSchema = new Schema({
6+
name: {
7+
type: String,
8+
required: true,
9+
trim: true,
10+
},
11+
file: {
12+
type: String,
13+
},
14+
date: {
15+
type: Date,
16+
},
17+
set: {
18+
type: String,
19+
required: true,
20+
trim: true,
21+
},
22+
status: {
23+
type: String,
24+
enum: ['complete', 'incomplete', 'parsing'],
25+
default: 'incomplete',
26+
required: true,
27+
},
28+
author: {
29+
type: String,
30+
},
31+
32+
side: {
33+
type: String,
34+
enum: ['AFF', 'NEG'],
35+
},
36+
wikidata: {
37+
school: {
38+
type: String
39+
},
40+
code: {
41+
type: String
42+
}
43+
}
44+
});
45+
46+
module.exports = mongoose.model('File', fileSchema); // instance of schema

env-example.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)