Skip to content

Commit 4df95e0

Browse files
author
Yiwen Dong
authored
Merge pull request #2 from CastleKey/backend-setup
Backend setup
2 parents 44f1aee + 393bb2b commit 4df95e0

12 files changed

Lines changed: 2346 additions & 0 deletions

.gitignore

Lines changed: 409 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@
22

33
## This project is a collaborative effort required for Web Dev
44

5+
Start server on windows, don't forget to "SET EVANBUSSE_APIKEY=$$" to set the api key replacing $$ with the actual api key before doing npm start.
6+
7+
Start server on Mac OS or linux, do EVANBUSSE_APIKEY=$$ npm start
8+
9+
[Search for a strain contain letter b](https://tranquil-stream-31009.herokuapp.com/api/strain/b)
10+
11+
[Search for a strain contain name afpak](https://tranquil-stream-31009.herokuapp.com/api/strain/afpak)
12+
13+
[Search for a strain with name that doesn't existe](https://tranquil-stream-31009.herokuapp.com/api/strain/afpakaa)
14+

nodesrc/model/effect.model.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var mongoose = require('mongoose');
2+
var Schema = mongoose.Schema;
3+
4+
var effectSchema = new Schema({
5+
_id: { type: String },
6+
category: { type: String,
7+
required: true,
8+
default: "POSITIVE",
9+
validate: {
10+
validator: function(text) {
11+
return text === "POSITIVE" || text === "NEGATIVE" || text === "MEDICAL";
12+
},
13+
message: "effect must be one of positive, negative or medical"
14+
}
15+
}
16+
});
17+
18+
effectSchema.virtual("name")
19+
.get(function() {
20+
return this._id;
21+
})
22+
.set(function(n) {
23+
this._id = n;
24+
});
25+
26+
27+
var Effect = mongoose.model("Effect", effectSchema);
28+
29+
module.exports = Effect;

nodesrc/model/flavor.model.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var mongoose = require('mongoose');
2+
var Schema = mongoose.Schema;
3+
4+
var flavorSchema = new Schema({
5+
_id: { type: String }
6+
});
7+
8+
flavorSchema.virtual("name")
9+
.get(function() {
10+
return this._id;
11+
})
12+
.set(function(n) {
13+
this._id = n;
14+
});
15+
16+
var Flavor = mongoose.model("Flavor", flavorSchema);
17+
18+
module.exports = Flavor;

nodesrc/model/strain.model.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var mongoose = require('mongoose');
2+
var Schema = mongoose.Schema;
3+
4+
var strainSchema = new Schema({
5+
_id: { type: String },
6+
eb_id: { type: Number, required: true, unique: true },
7+
desc: { type: String, required: false },
8+
race: { type: String,
9+
required: true,
10+
default: "HYBRID",
11+
validate: {
12+
validator: function(text) {
13+
return text === "HYBRID" || text === "SATIVA" || text === "INDICA";
14+
},
15+
message: "Race is hybrid or sativa or indica"
16+
}
17+
},
18+
flavors:
19+
{ type: [{ type: Schema.Types.String, ref: 'Flavor' }],
20+
required: true,
21+
default: []
22+
},
23+
effects:
24+
{ type: [{ type: Schema.Types.String, ref: 'Effect' }],
25+
required: true,
26+
default: []
27+
}
28+
});
29+
30+
strainSchema.virtual("name")
31+
.get(function() {
32+
return this._id;
33+
})
34+
.set(function(n) {
35+
this._id = n;
36+
});
37+
38+
var Strain = mongoose.model("Strain", strainSchema);
39+
40+
module.exports = Strain;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const effectModel = require('../model/effect.model');
2+
3+
findAllEffects = () => {
4+
return effectModel.find();
5+
}
6+
7+
findEffect = (name) => {
8+
return effectModel.findById(name);
9+
}
10+
11+
saveEffect = (effect) => {
12+
return effectModel(effect).save();
13+
}
14+
15+
upsertEffect = (effect) => {
16+
return effectModel.findByIdAndUpdate(effect._id, effect, {upsert:true, new:true}).exec();
17+
}
18+
19+
saveEffects = (effects) => {
20+
effects.forEach((effect) => {
21+
saveEffect(effect).then((err) => {if (err) {throw JSON.stringify(err);}});
22+
});
23+
}
24+
25+
upsertEffects = (effects) => {
26+
effects.forEach((effect) => {
27+
upsertEffect(effect).then((err) => {});
28+
});
29+
}
30+
31+
searchEffectsByName = (q) => {
32+
return effects.find({_id: new RegExp('^'+q+'$', "i")})
33+
}
34+
35+
module.exports = {
36+
findAllEffects,
37+
findEffect,
38+
saveEffect,
39+
saveEffects,
40+
upsertEffect,
41+
upsertEffects,
42+
searchEffectsByName
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const flavorModel = require('../model/flavor.model');
2+
3+
findAllFlavors = () => {
4+
return flavorModel.find();
5+
}
6+
7+
findFlavor = (name) => {
8+
return flavorModel.findById(name);
9+
}
10+
11+
saveFlavor = (flavor) => {
12+
return flavorModel(flavor).save();
13+
}
14+
15+
upsertFlavor = (flavor) => {
16+
return flavorModel.findByIdAndUpdate(flavor._id, flavor, {upsert:true, new:true}).exec();
17+
}
18+
19+
saveFlavors = (flavors) => {
20+
flavors.forEach((flavor) => {
21+
saveFlavor(flavor).then((err) => {if (err) {throw JSON.stringify(err);}});
22+
});
23+
}
24+
25+
upsertFlavors = (flavors) => {
26+
flavors.forEach((flavor) => {
27+
upsertFlavor(flavor).then((err) => {});
28+
});
29+
}
30+
31+
searchFlavorsByName = (q) => {
32+
return flavors.find({_id: new RegExp('^'+q+'$', "i")})
33+
}
34+
35+
module.exports = {
36+
findAllFlavors,
37+
findFlavor,
38+
saveFlavor,
39+
saveFlavors,
40+
upsertFlavor,
41+
upsertFlavors,
42+
searchFlavorsByName
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const strainModel = require('../model/strain.model');
2+
3+
findAllStrains = () => {
4+
return strainModel.find();//.then((err, strains) => {
5+
6+
// });
7+
}
8+
9+
findStrain = (name) => {
10+
return strainModel.findById(name);
11+
}
12+
13+
saveStrain = (strain) => {
14+
return strainModel(strain).save();
15+
}
16+
17+
upsertStrain = (strain) => {
18+
return strainModel.findByIdAndUpdate(strain._id, strain, {upsert:true, new:true}).exec();
19+
}
20+
21+
saveStrains = (strains) => {
22+
strains.forEach((strain) => {
23+
saveStrain(strain).then((err) => {if (err) {throw JSON.stringify(err);}});
24+
});
25+
}
26+
27+
upsertStrains = (strains) => {
28+
strains.forEach((strain) => {
29+
upsertStrain(strain).then((err) => {});
30+
});
31+
}
32+
33+
searchStrainsByName = (q) => {
34+
return strains.find({_id: new RegExp('^'+q+'$', "i")})
35+
}
36+
37+
module.exports = {
38+
findAllStrains,
39+
findStrain,
40+
saveStrain,
41+
saveStrains,
42+
upsertStrain,
43+
upsertStrains,
44+
searchStrainsByName
45+
}

nodesrc/service/strain.service.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
module.exports = app => {
2+
const mongoose = require('mongoose');
3+
const strainRepo = require('../repository/strain.repository');
4+
const http = require('http');
5+
const url = "http://strainapi.evanbusse.com/" + process.env.EVANBUSSE_APIKEY;
6+
7+
var makeReq = (link, func) => {
8+
http.get(link, res => {
9+
res.setEncoding("utf8");
10+
let body = "";
11+
res.on("data", data => {
12+
body += data;
13+
});
14+
res.on("end", () => {
15+
let parsedBd = JSON.parse(body);
16+
func(parsedBd)
17+
});
18+
});
19+
};
20+
21+
app.get("/api/strain", function (req, res) {
22+
strainRepo.findAllStrains().then((err, strains) => {
23+
if (err || strains == null) {
24+
res.status(400).send(JSON.stringify(err));
25+
return;
26+
}
27+
res.json(strains);
28+
});
29+
});
30+
31+
app.get("/api/strain/:sname", function (req, res) {
32+
strainRepo.findStrain().then((err, strain) => {
33+
if (err) {
34+
res.status(400).send(JSON.stringify(err));
35+
return;
36+
}
37+
if (strain == null) {
38+
makeReq(url + "/strains/search/name/" + req.params['sname'], (strains) => {
39+
var firstTime = {first: true};
40+
if (strains.length == 0) {
41+
res.sendStatus(400);
42+
return;
43+
}
44+
strains.forEach((str) => {
45+
strainRepo.findStrain(str.name).then((err, dbStr) => {
46+
if (err || dbStr == null) {
47+
makeReq(url + "/strains/data/effects/" + str.id, (eff) => {
48+
return ((eff, firstTime) => {
49+
makeReq(url + "/strains/data/flavors/" + str.id, (fla) => {
50+
return ((fla, firstTime) => {
51+
var first = false;
52+
if (firstTime.first == true) {
53+
firstTime.first = false;
54+
first = true;
55+
}
56+
var obj = {};
57+
obj._id = str.name;
58+
obj.eb_id = str.id;
59+
obj.race = str.race.toUpperCase();
60+
obj.desc = str.desc;
61+
obj.effects = eff.positive.concat(
62+
eff.negative.concat(
63+
eff.medical)).map((eff) => {
64+
return eff.toString();
65+
});
66+
obj.flavors = fla.map((fla) => {
67+
return fla.toString();
68+
});
69+
strainRepo.upsertStrain(obj).then((dbObj) => {
70+
if (first) {
71+
res.json(dbObj);
72+
}
73+
});
74+
})(fla, firstTime)
75+
});
76+
})(eff, firstTime)
77+
});
78+
}
79+
});
80+
});
81+
//res.json(strain);
82+
});
83+
return;
84+
}
85+
res.json(strain);
86+
});
87+
});
88+
}

0 commit comments

Comments
 (0)