forked from b00tc4mp/isdi-bootcamp-202402
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement asynchronous testing (WIP) b00tc4mp#391
- Loading branch information
Showing
94 changed files
with
8,367 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 @@ | ||
node_modules |
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 @@ | ||
curl -X POST -H "Content-Type: application/json" -d '{"username":"PereHDZ","email":"[email protected]","password":"cuquis1992","confirmedPassword":"cuquis1992"}' http://localhost:8080/users -v |
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 @@ | ||
[{"brand":"Renault","model":"Megane","id":"1"},{"brand":"Peugeot","model":"208","id":"2"}] |
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,188 @@ | ||
import fs from 'fs' | ||
|
||
class Collection { | ||
constructor(name){ | ||
this.name = name | ||
} | ||
|
||
|
||
//helpers | ||
|
||
_generateId() { | ||
return (+((parseInt(Math.random() * 10 ** 17)).toString())).toString(36) | ||
} | ||
|
||
_loadDocuments(callback) { | ||
//validation | ||
if (typeof callback !== 'function') throw new TypeError ('callback is not a Function') | ||
|
||
//logic | ||
fs.readFile(`./data/${this.name}.json`, 'utf8', (error, documentsJSON) => { | ||
if (error){ | ||
callback(error) | ||
|
||
return | ||
} | ||
|
||
const documents = JSON.parse(documentsJSON || '[]') | ||
|
||
callback(null, documents) | ||
}) | ||
} | ||
|
||
_saveDocuments(documents, callback) { | ||
//validation | ||
if (!(documents instanceof Array)) throw new TypeError ('documents is not an Array') | ||
if (typeof callback !== 'function') throw new TypeError('callback is not a Function') | ||
|
||
documents.forEach(document => { | ||
if(!(document instanceof Object)) throw new TypeError('some elements in documents are not a document') | ||
}) | ||
|
||
//logic | ||
const documentsJSON = JSON.stringify(documents) | ||
|
||
fs.writeFile(`./data/${this.name}.json`, documentsJSON, error => { | ||
if (error) { | ||
callback(error) | ||
|
||
return | ||
} | ||
callback(null) | ||
}) | ||
} | ||
|
||
|
||
// CRUD | ||
|
||
findOne (condition, callback){ | ||
//validation | ||
if(typeof condition !== 'function') throw new TypeError ('condition is not a Function') | ||
if (!(callback instanceof Function)) throw new TypeError ('callback is not a Function') | ||
|
||
//logic | ||
this._loadDocuments((error, documents) => { | ||
if (error) { | ||
callback(error) | ||
|
||
return | ||
} | ||
const document = documents.find(condition) | ||
|
||
callback(null, document || null) | ||
}) | ||
} | ||
|
||
insertOne (document, callback){ | ||
//validation | ||
if (!(document instanceof Object)) throw new TypeError ('document is not an Object') | ||
if (typeof callback !== 'function') throw new TypeError ('callback is not a Function') | ||
|
||
//logic | ||
this._loadDocuments((error, documents) => { | ||
if (error) { | ||
callback(error) | ||
|
||
return | ||
} | ||
|
||
document.id = this._generateId() | ||
|
||
documents.push(document) | ||
|
||
this._saveDocuments(documents, error => { | ||
if(error) { | ||
callback(error) | ||
|
||
return | ||
} | ||
|
||
callback(null, document.id) | ||
}) | ||
}) | ||
} | ||
|
||
updateOne(condition, document, callback) { | ||
//validation | ||
if (typeof condition !== 'function') throw new TypeError('condition is not a Function') | ||
if (!(document instanceof Object)) throw new TypeError('document is not an Object') | ||
if (typeof callback !== 'function') throw new TypeError('callback is not a Function') | ||
|
||
//logic | ||
this._loadDocuments((error, documents) => { | ||
if (error) { | ||
callback(error) | ||
|
||
return | ||
} | ||
|
||
const index = documents.findIndex(condition) | ||
|
||
if (index > -1){ | ||
documents.splice(index, 1, document) | ||
|
||
this._saveDocuments(documents, error => { | ||
if(error){ | ||
callback(error) | ||
|
||
return | ||
} | ||
callback(null, true) | ||
}) | ||
return | ||
} | ||
callback(null, false) | ||
}) | ||
} | ||
|
||
deleteOne (condition, callback){ | ||
//validation | ||
if (typeof condition !== 'function') | ||
throw new TypeError ('condition is not a Function') | ||
if (typeof callback !== 'function') | ||
throw new TypeError ('callback is not a Function') | ||
|
||
//logic | ||
this._loadDocuments((error, documents) => { | ||
if (error) { | ||
callback(error) | ||
|
||
return | ||
} | ||
const index = documents.findIndex(condition) | ||
|
||
if (index > -1){ | ||
documents.splice(index, 1) | ||
|
||
this._saveDocuments(documents, error => { | ||
if (error) { | ||
callback(error) | ||
|
||
return | ||
} | ||
callback(null, true) | ||
}) | ||
return | ||
} | ||
callback(null, false) | ||
}) | ||
} | ||
|
||
getAll (callback) { | ||
//validation | ||
if(typeof callback !== 'function') | ||
throw new TypeError('callback is not a Function') | ||
|
||
//logic | ||
this._loadDocuments((error, documents) => { | ||
if (error) { | ||
callback(error) | ||
|
||
return | ||
} | ||
callback(null, documents) | ||
}) | ||
} | ||
} | ||
|
||
export default Collection |
Oops, something went wrong.