-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacques Berger
committed
Oct 5, 2016
1 parent
3716066
commit 247f0d1
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2016 Jacques Berger. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
var fs = require("fs"); | ||
var mongo = require("mongodb"); | ||
|
||
function parseData(data) { | ||
var result = []; | ||
var elements = data.split("\n"); | ||
for (var i = 0; i < elements.length - 1; i++) { | ||
var line = elements[i]; | ||
var identifier = line.substr(0, 7); | ||
var title = line.substr(8); | ||
result.push({identifier: identifier, title:title}); | ||
} | ||
return result; | ||
} | ||
|
||
function sendToDatabase(list) { | ||
var server = new mongo.Server("localhost", 27017); | ||
var db = new mongo.Db("atelier", server, {safe:true}); | ||
db.open(function (err, db) { | ||
if (err) { | ||
console.log("Impossible d'ouvrir une connexion sur la base de données.", err); | ||
} else { | ||
db.collection("laboNode", function (err, collection) { | ||
if (err) { | ||
console.log("Erreur avec la base de données.", err); | ||
db.close(); | ||
} else { | ||
collection.insert(list, function (err, result) { | ||
if (err) { | ||
console.log("Erreur lors de l'insertion.", err); | ||
} | ||
db.close(); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
fs.readFile("fichiertexte.txt", 'utf8', function(err, data) { | ||
if (err) { | ||
console.log("Erreur avec le fichier d'entrée", err); | ||
} else { | ||
var classes = parseData(data); | ||
sendToDatabase(classes); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "ateliernode", | ||
"version": "1.0.0", | ||
"description": "Atelier Node.js pour INF4375", | ||
"main": "Solution1.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/jacquesberger/exemplesINF4375.git" | ||
}, | ||
"author": "Jacques Berger", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/jacquesberger/exemplesINF4375/issues" | ||
}, | ||
"homepage": "https://github.com/jacquesberger/exemplesINF4375#readme", | ||
"dependencies": { | ||
"mongodb": "^2.2.10" | ||
} | ||
} |