Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import User from './user';

export default class Admin extends User {}
19 changes: 18 additions & 1 deletion src/application.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/article.json.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class ArticleJsonFactory {
static createFromArticle(article: Article): ArticleJson {
return {
title: article.getTitle(),
creatorPseudo: article.getCreator().getPseudo(),
};
}
}
2 changes: 1 addition & 1 deletion src/article.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
8 changes: 7 additions & 1 deletion src/article.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions src/contributor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import User from './user';

export default class Contributor extends User {}
2 changes: 1 addition & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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,
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export type ArticleJson = {
title: string;
creatorPseudo: string;
};

export type UserType = 'admin' | 'contributor';
7 changes: 7 additions & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class User {
constructor(private pseudo: string) {}

getPseudo(): string {
return this.pseudo;
}
}