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
type UserRole = 'ADMIN' | 'USER'; | |
interface User { | |
id: string; | |
isActivated: boolean; | |
role: UserRole; | |
} |
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
export const userIsPermitted = (user: User) => user.isActivated && user.role === 'ADMIN'; |
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
async create(user: User, product: Product): Promise<Product | undefined> { | |
if (userIsPermitted(user)) { | |
console.log(`Create product.`); | |
return this.db.create({user, product}); | |
} | |
return undefined; | |
} |
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
class ProductService { | |
private db: DB<Product>; | |
private entity = 'product'; | |
constructor() { | |
this.db = new DB<Product>(this.entity); | |
} | |
async create(user: User, product: Product): Promise<Product | undefined> { | |
if (userIsPermitted(user)) { |
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
createVehicle("red", "black", 4, 4); |
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
createVehicle({ exteriorColor: "red", interiorColor: "black", wheels: 4, cylinders: 4 }); |
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 ProductOperationOptions { | |
user: User; | |
entity: Product; | |
} | |
export class ProductService { | |
private db: DB<Product>; | |
private entity = 'product'; | |
constructor() { |
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 Feedback { | |
id: string; | |
title: string; | |
} | |
interface FeedbackOperationOptions { | |
user: User; | |
entity: Feedback; | |
} |
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 Entity { | |
id: string; | |
} | |
interface CrudOperationOptions<T extends Entity> { | |
user: User; | |
entity: T; | |
} | |
type CrudReturnType<T> = Promise<T | undefined>; |
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
class FeedbackService extends CRUDService<Feedback> { | |
constructor() { | |
super('feedback'); | |
} | |
async create(options: CrudOperationOptions<Feedback>) { | |
if (userIsPermitted(options.user)) { | |
return super.create(options); | |
} |