Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update week-3/week-3-models-and-services as ES Modules #111

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions week-3/week-3-models-and-services/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Person = require('./models/person')
const Meetup = require('./models/meetup')
const PersonService = require('./services/person-service')
const MeetupService = require('./services/meetup-service')
import Person from './models/person.js'
import Meetup from './models/meetup.js'
import PersonService from './services/person-service.js'
import MeetupService from './services/meetup-service.js'

console.log('Hello World!')
console.log('Hello World!')
Expand Down
6 changes: 4 additions & 2 deletions week-3/week-3-models-and-services/models/meetup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const chalk = require('chalk')
import chalk from 'chalk'

module.exports = class Meetup {
const Meetup = class {
constructor(name, location, attendees = [], id) {
this.name = name
this.location = location
Expand All @@ -16,3 +16,5 @@ module.exports = class Meetup {
return new Meetup(name, location, attendees, id)
}
}

export default Meetup
4 changes: 3 additions & 1 deletion week-3/week-3-models-and-services/models/person.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = class Person {
const Person = class {
constructor(name, age, meetups = [], id) {
this.name = name
this.age = age
Expand All @@ -15,3 +15,5 @@ module.exports = class Person {
return new Person(name, age, meetups, id);
}
}

export default Person
62 changes: 43 additions & 19 deletions week-3/week-3-models-and-services/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions week-3/week-3-models-and-services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"chalk": "^2.4.2",
"flatted": "^2.0.1"
}
"flatted": "^3.2.9"
},
"type": "module"
}
130 changes: 63 additions & 67 deletions week-3/week-3-models-and-services/services/base-service.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,64 @@
const fs = require('fs')
const Flatted = require('flatted/cjs');

module.exports = class Service {
constructor(model, dbPath) {
this.model = model
this.dbPath = dbPath
}


async findAll() {
return new Promise((resolve, reject) => {
fs.readFile(this.dbPath, 'utf8', async (err, file) => {
if (err) {
if (err.code == 'ENOENT') {
await this.saveAll([])
return resolve([])
}

return reject(err)
}

const items = Flatted.parse(file).map(this.model.create)

resolve(items)
})
})
}

async add(item) {
const allItems = await this.findAll()
const lastItem = allItems[allItems.length - 1]
const lastItemsId = lastItem && lastItem.id || 0
item.id = lastItemsId + 1

allItems.push(item)

await this.saveAll(allItems)

return item
}

async del(itemId) {
const allItems = await this.findAll()
const itemIndex = allItems.findIndex(p => p.id == itemId)
if (itemIndex < 0) return

allItems.splice(itemIndex, 1)

await this.saveAll(allItems)
}

async find(itemId = 1) {
const allItems = await this.findAll()

return allItems.find(p => p.id == itemId)
}

async saveAll(items) {
return new Promise((resolve, reject) => {
fs.writeFile(this.dbPath, Flatted.stringify(items), (err, file) => {
if (err) return reject(err)

resolve()
})
})
}
import {promises as fsp} from 'fs'
import {parse as flattedParse, stringify as flattedStringify} from 'flatted'

const Service = class {
constructor(model, dbPath) {
this.model = model
this.dbPath = dbPath
}


async findAll() {
try{
const file = await fsp.readFile(this.dbPath, 'utf8');
const items = flattedParse(file).map(this.model.create)
return items
}catch(err){
if(err.code == 'ENOENT') {
await this.saveAll([])
return([])
}
console.log(err)
return err
}
}

async add(item) {
const allItems = await this.findAll()
const lastItem = allItems[allItems.length - 1]
const lastItemsId = lastItem && lastItem.id || 0
item.id = lastItemsId + 1

allItems.push(item)

await this.saveAll(allItems)

return item
}

async del(itemId) {
const allItems = await this.findAll()
const itemIndex = allItems.findIndex(p => p.id == itemId)
if (itemIndex < 0) return

allItems.splice(itemIndex, 1)

await this.saveAll(allItems)
}

async find(itemId = 1) {
const allItems = await this.findAll()

return allItems.find(p => p.id == itemId)
}

async saveAll(items) {
try{
return await fsp.writeFile(this.dbPath, flattedStringify(items))
}catch(err){
return err
}
}
}

export default Service
11 changes: 8 additions & 3 deletions week-3/week-3-models-and-services/services/meetup-service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const BaseService = require('./base-service')
const MeetupModel = require('../models/meetup')
import BaseService from './base-service.js'
import MeetupModel from '../models/meetup.js'
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

class MeetupService extends BaseService {
constructor() {
super(MeetupModel, `${__dirname}/../meetup-database.json`)
}
}

module.exports = new MeetupService()
export default new MeetupService()
11 changes: 8 additions & 3 deletions week-3/week-3-models-and-services/services/person-service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const BaseService = require('./base-service')
const PersonModel = require('../models/person')
import BaseService from './base-service.js'
import PersonModel from '../models/person.js'
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

class PersonService extends BaseService {
constructor() {
super(PersonModel, `${__dirname}/../person-database.json`)
}
}

module.exports = new PersonService()
export default new PersonService()