diff --git a/src/admin.ts b/src/admin.ts new file mode 100644 index 0000000..f28cd63 --- /dev/null +++ b/src/admin.ts @@ -0,0 +1,3 @@ +import User from './user'; + +export default class Admin extends User {} diff --git a/src/application.ts b/src/application.ts index a8ade52..08161e4 100644 --- a/src/application.ts +++ b/src/application.ts @@ -1,14 +1,31 @@ +import Admin from './admin'; import Article from './article'; import ArticleJsonFactory from './article.json.factory'; import ArticleRepository from './article.repository'; +import Contributor from './contributor'; import Database from './database'; +import { UserType } from './types'; +import User from './user'; export default class Application { run(): number { try { const database = new Database(); const articleRepository = new ArticleRepository(database); - const article = new Article('Mon premier article'); + + const userPseudo = 'pierre'; + const userType: UserType = 'admin'; + + const user: User = (() => { + switch (userType as UserType) { + case 'admin': + return new Admin(userPseudo); + case 'contributor': + return new Contributor(userPseudo); + } + })(); + + const article = new Article(user, 'Mon premier article'); articleRepository.save(article); diff --git a/src/article.json.factory.ts b/src/article.json.factory.ts index feab802..e111eeb 100644 --- a/src/article.json.factory.ts +++ b/src/article.json.factory.ts @@ -5,6 +5,7 @@ export default class ArticleJsonFactory { static createFromArticle(article: Article): ArticleJson { return { title: article.getTitle(), + creatorPseudo: article.getCreator().getPseudo(), }; } } diff --git a/src/article.repository.ts b/src/article.repository.ts index b39e13e..85fa9b3 100644 --- a/src/article.repository.ts +++ b/src/article.repository.ts @@ -5,6 +5,6 @@ export default class ArticleRepository { constructor(private database: Database) {} save(article: Article): void { - this.database.save({ title: article.getTitle() }, 'articles'); + this.database.save(article, 'articles'); } } diff --git a/src/article.ts b/src/article.ts index 03c8353..f713b9a 100644 --- a/src/article.ts +++ b/src/article.ts @@ -1,7 +1,13 @@ +import User from './user'; + export default class Article { - constructor(private title: string) {} + constructor(private creator: User, private title: string) {} getTitle(): string { return this.title; } + + getCreator(): User { + return this.creator; + } } diff --git a/src/contributor.ts b/src/contributor.ts new file mode 100644 index 0000000..25f041c --- /dev/null +++ b/src/contributor.ts @@ -0,0 +1,3 @@ +import User from './user'; + +export default class Contributor extends User {} diff --git a/src/database.ts b/src/database.ts index 21d37ad..7ecf8dc 100644 --- a/src/database.ts +++ b/src/database.ts @@ -3,7 +3,7 @@ export default class Database { console.log('On ouvre une connection à notre base de données MySQL'); } - save(rowData: any, table: string): void { + save(rowData: T, table: string): void { console.log( `On enregistre cette nouvelle donnée dans la table "${table}" de notre base de données MySQL`, rowData, diff --git a/src/types.ts b/src/types.ts index 71c3874..ba8a69e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,6 @@ export type ArticleJson = { title: string; + creatorPseudo: string; }; + +export type UserType = 'admin' | 'contributor'; diff --git a/src/user.ts b/src/user.ts new file mode 100644 index 0000000..ae1077a --- /dev/null +++ b/src/user.ts @@ -0,0 +1,7 @@ +export default class User { + constructor(private pseudo: string) {} + + getPseudo(): string { + return this.pseudo; + } +}