Skip to content

Instantly share code, notes, and snippets.

@MariusBongarts
Last active August 7, 2021 13:04
Show Gist options
  • Save MariusBongarts/84e5847be11a250617d91859d99d93d8 to your computer and use it in GitHub Desktop.
Save MariusBongarts/84e5847be11a250617d91859d99d93d8 to your computer and use it in GitHub Desktop.
DRY - FeedbackService
interface Feedback {
id: string;
title: string;
}
interface FeedbackOperationOptions {
user: User;
entity: Feedback;
}
class FeedbackService {
private db: DB<Feedback>;
private entity = 'feedback';
constructor() {
this.db = new DB<Feedback>(this.entity);
}
async create(options: FeedbackOperationOptions): Promise<Feedback | undefined> {
if (userIsPermitted(options.user)) {
console.log(`Create ${this.entity}.`);
return this.db.create(options);
}
return undefined;
}
async read(options: FeedbackOperationOptions): Promise<Feedback | undefined> {
console.log(`Get ${this.entity}`);
return this.db.read(options);
}
async update(options: FeedbackOperationOptions): Promise<Feedback | undefined> {
if (userIsPermitted(options.user)) {
console.log(`Update ${this.entity}.`);
return this.db.update(options);
}
return undefined;
}
async delete(options: FeedbackOperationOptions): Promise<Feedback | undefined> {
if (userIsPermitted(options.user)) {
console.log(`Deleted ${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