Last active
August 7, 2021 13:04
-
-
Save MariusBongarts/84e5847be11a250617d91859d99d93d8 to your computer and use it in GitHub Desktop.
DRY - FeedbackService
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 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