-
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.
- Loading branch information
Showing
11 changed files
with
200 additions
and
42 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 @@ | ||
[{"id":10,"name":"Batman","age":"50","power":"rich"},{"id":2,"name":"Chapolin"}] |
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
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
import { randomUUID } from "node:crypto"; | ||
|
||
export default class Hero { | ||
constructor({ name, age, power }) { | ||
this.id = randomUUID() | ||
this.name = name | ||
this.age = age | ||
this.power = power | ||
} | ||
} |
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,21 @@ | ||
import HeroRepository from "../repositories/heroRepository.js" | ||
import HeroService from "../services/heroService.js" | ||
|
||
const generateInstance = ({ | ||
filePath | ||
}) => { | ||
//hero goeas all db connections | ||
const heroRepository = new HeroRepository({ | ||
file: filePath | ||
}) | ||
|
||
const heroService = new HeroService({ | ||
heroRepository | ||
}) | ||
|
||
return heroService | ||
} | ||
|
||
export { | ||
generateInstance | ||
} |
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
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,45 @@ | ||
import { readFile, writeFile } from "node:fs/promises" | ||
|
||
export default class HeroRepository { | ||
constructor({ | ||
file | ||
}) { | ||
this.file = file | ||
} | ||
|
||
async #currentiFileContent() { | ||
return JSON.parse(await readFile(this.file)) | ||
} | ||
|
||
find() { | ||
return this.#currentiFileContent() | ||
} | ||
|
||
async create(data) { | ||
const currentFile = await this.#currentiFileContent() | ||
currentFile.push(data) | ||
|
||
await writeFile( | ||
this.file, | ||
JSON.stringify(currentFile) | ||
) | ||
|
||
return data.id | ||
} | ||
} | ||
/* | ||
const heroRepository = new HeroRepository({ | ||
file: 'database/data.json' | ||
}) | ||
console.log( | ||
await heroRepository.create({ | ||
id: 2, | ||
name: 'Chapolin' | ||
}) | ||
) | ||
console.log( | ||
await heroRepository.find() | ||
) | ||
*/ |
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,32 @@ | ||
import { once } from "node:events"; | ||
import Hero from "../entities/hero.js"; | ||
import { DEFAULT_HEADER } from "../utils/utils.js"; | ||
|
||
const routes = ({ | ||
heroService | ||
}) => ({ | ||
'/heroes:get': async (req, res) => { | ||
res.write('GET') | ||
res.end() | ||
}, | ||
|
||
'/heroes:post': async (req, res) => { | ||
const data = await once(req, 'data'); | ||
const item = JSON.parse(data) | ||
|
||
const hero = new Hero(item) | ||
|
||
const id = await heroService.create(hero) | ||
|
||
res.writeHead(201, DEFAULT_HEADER) | ||
res.write(JSON.stringify({ | ||
id, | ||
success: 'User created with success', | ||
})) | ||
return res.end() | ||
}, | ||
}) | ||
|
||
export { | ||
routes | ||
} |
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,15 @@ | ||
export default class HeroService { | ||
constructor({ | ||
heroRepository | ||
}) { | ||
this.heroRepository = heroRepository | ||
} | ||
|
||
find() { | ||
return this.heroRepository.find() | ||
} | ||
|
||
create(data) { | ||
return this.heroRepository.create(data) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,51 @@ | ||
import test from 'node:test' | ||
import assert from 'node:assert' | ||
import { promisify } from 'node:util' | ||
|
||
|
||
test('Hero Integration Test Suite', async (t) => { | ||
const testPort = 9009 | ||
|
||
process.env.PORT = testPort | ||
const { server } = await import('../../src/index.js') | ||
|
||
const testServerAddress = `http://localhost:${testPort}/heroes` | ||
|
||
await t.test('it should create a hero', async (t) => { | ||
const data = { | ||
"name": "Batman", | ||
"age": "50", | ||
"power": "rich" | ||
} | ||
|
||
const request = await fetch(testServerAddress, { | ||
method: 'POST', | ||
body: JSON.stringify(data) | ||
}) | ||
|
||
|
||
assert.deepStrictEqual( | ||
request.headers.get('content-type'), | ||
'application/json' | ||
) | ||
|
||
assert.strictEqual(request.status, 201) | ||
|
||
const result = await request.json() | ||
|
||
assert.deepStrictEqual( | ||
result.success, | ||
'User created with success', | ||
'it should return a valid text message' | ||
) | ||
|
||
assert.ok( | ||
result.id.length > 30, | ||
'id should be a valid uuid' | ||
) | ||
}) | ||
|
||
//Promisify convert callback to promise | ||
await promisify(server.close.bind(server)) | ||
|
||
}) |