Skip to content

Commit

Permalink
implement asynchronous testing (WIP) b00tc4mp#391
Browse files Browse the repository at this point in the history
  • Loading branch information
PereHDZ committed Apr 2, 2024
1 parent 852efe7 commit a7c688b
Show file tree
Hide file tree
Showing 94 changed files with 8,367 additions and 0 deletions.
1 change: 1 addition & 0 deletions staff/pere-hernandez/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions staff/pere-hernandez/app/api/README.md
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
1 change: 1 addition & 0 deletions staff/pere-hernandez/app/api/cars.json
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"}]
188 changes: 188 additions & 0 deletions staff/pere-hernandez/app/api/data/Collection.mjs
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
Loading

0 comments on commit a7c688b

Please sign in to comment.