Last active
August 8, 2021 15:29
-
-
Save MariusBongarts/d1d9efb6cbe438b6db9bbe18f3a3e274 to your computer and use it in GitHub Desktop.
DRY article - ProductService
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
interface User { | |
id: string; | |
isAdmin: boolean; | |
} | |
interface Product { | |
id: string; | |
title: string; | |
} | |
class ProductService { | |
private db: DB<Product>; | |
constructor() { | |
this.db = new DB<Product>('product'); | |
} | |
async create(user: User, product: Product): Promise<Product | undefined> { | |
if (user.isAdmin) { | |
console.log(`Create product.`); | |
return this.db.create({user, product}); | |
} | |
return undefined; | |
} | |
async read(user: User, product: Product): Promise<Product | undefined> { | |
console.log(`Get product`); | |
return this.db.get({user, product}); | |
} | |
async update(user: User, product: Product): Promise<Product | undefined> { | |
if (user.isAdmin) { | |
console.log(`Update product.`); | |
return this.db.update({user, product}); | |
} | |
return undefined; | |
} | |
async delete(user: User, product: Product): Promise<Product | undefined> { | |
if (user.isAdmin) { | |
console.log(`Delete product.`); | |
return this.db.delete({user, product}); | |
} | |
return undefined; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment