Skip to content

Instantly share code, notes, and snippets.

@MariusBongarts
Last active August 8, 2021 15:28
Show Gist options
  • Save MariusBongarts/d20a3ba96bd1bc43fdf5542db5922195 to your computer and use it in GitHub Desktop.
Save MariusBongarts/d20a3ba96bd1bc43fdf5542db5922195 to your computer and use it in GitHub Desktop.
DRY - ProductService Options object
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