Skip to content

Latest commit

 

History

History
55 lines (44 loc) · 915 Bytes

Adapter.md

File metadata and controls

55 lines (44 loc) · 915 Bytes

← Назад

Adapter

class Database {
	constructor () {
		this.users = []
	}

	static create (...args) {
		return new Database(...args)
	}

	saveNewUserData (user) {
		this.users.push(
			JSON.parse(JSON.stringify(user))
		)
	}

	findOneUserByOwnId (userId) {
		for (const user of this.users) {
			if (user.id === userId) {
				return user
			}
		}
	}
}

class Adapter {
	constructor (db) {
		this.db = db
	}

	add (user) {
		this.db.saveNewUserData(user)
	}

	find (userId) {
		return this.db.findOneUserByOwnId(userId)
	}
}

const db = new Adapter(Database.create())

db.add({
	id: 1,
	name: "Алексей",
	family: "Данчин"
})

console.log(
	db.find(1) // { id: 1, name: 'Алексей', family: 'Данчин' }
)