Skip to content

Commit

Permalink
Add Repo Service Route
Browse files Browse the repository at this point in the history
  • Loading branch information
HamzaDams committed Aug 18, 2022
1 parent 0fec660 commit 665f506
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 42 deletions.
1 change: 1 addition & 0 deletions database/data.json
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"}]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"start": "node src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node --test tests/"
},
"keywords": [],
"author": "",
Expand Down
8 changes: 0 additions & 8 deletions src/database/data.json

This file was deleted.

10 changes: 10 additions & 0 deletions src/entities/hero.js
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
}
}
21 changes: 21 additions & 0 deletions src/factories/heroFactory.js
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
}
30 changes: 24 additions & 6 deletions src/handler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import path from 'node:path'
import { parse } from 'node:url'
import {
join,
dirname
} from 'node:path'

import { parse, fileURLToPath } from 'node:url'
import { generateInstance } from './factories/heroFactory.js'
import { routes } from './routes/heroRoute.js'
import { DEFAULT_HEADER } from './utils/utils.js'

const currentDir = dirname(
fileURLToPath(
import.meta.url
)
)

const filePath = join(currentDir, './database', 'data.json')

const heroService = generateInstance({
filePath
})

const heroRoutes = routes({
heroService
})

const allRoutes = {
'/heroes:get': async (req, res) => {
res.write('GET')
res.end()
},
...heroRoutes,
//404 Routes
default: (req, res) => {
res.writeHead(404, DEFAULT_HEADER)
Expand Down
45 changes: 45 additions & 0 deletions src/repositories/heroRepository.js
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()
)
*/
32 changes: 32 additions & 0 deletions src/routes/heroRoute.js
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
}
15 changes: 15 additions & 0 deletions src/services/heroService.js
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)
}
}
27 changes: 0 additions & 27 deletions test/integration/hero.test.js

This file was deleted.

51 changes: 51 additions & 0 deletions tests/integration/hero.test.js
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))

})

0 comments on commit 665f506

Please sign in to comment.