Last active
August 8, 2021 15:28
-
-
Save MariusBongarts/d20a3ba96bd1bc43fdf5542db5922195 to your computer and use it in GitHub Desktop.
DRY - ProductService Options object
This file contains hidden or 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 ProductOperationOptions { | |
user: User; | |
entity: Product; | |
} | |
export class ProductService { | |
private db: DB<Product>; | |
private entity = 'product'; | |
constructor() { | |
this.db = new DB<Product>(this.entity); | |
} | |
async create(options: ProductOperationOptions): Promise<Product | undefined> { | |
if (userIsPermitted(options.user)) { | |
console.log(`Create ${this.entity}.`); | |
return this.db.create(options); | |
} | |
return undefined; | |
} | |
async read(options: ProductOperationOptions): Promise<Product | undefined> { | |
console.log(`Get ${this.entity}`); | |
return this.db.read(options); | |
} | |
async update(options: ProductOperationOptions): Promise<Product | undefined> { | |
if (userIsPermitted(options.user)) { | |
console.log(`Update ${this.entity}.`); | |
return this.db.update(options); | |
} | |
return undefined; | |
} | |
async delete(options: ProductOperationOptions): Promise<Product | undefined> { | |
if (userIsPermitted(options.user)) { | |
console.log(`Delete ${this.entity}.`); | |
return this.db.delete(options); | |
} | |
return undefined; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment