Created
June 2, 2026 19:38
-
-
Save nicolasdanelon/311d214c9128c5ff08fdaec93e353237 to your computer and use it in GitHub Desktop.
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
| class APIUserController { | |
| constructor(private service: UserService) {} | |
| update(): void { | |
| const id = 4; | |
| const name = "Nicolás"; | |
| this.service.setUserName(id, name); | |
| } | |
| show(): void { | |
| const name = this.service.getUserName(4); | |
| } | |
| } |
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
| class User { | |
| constructor(private name: string) {} | |
| static findById(id: number): User { | |
| return new User("Nico"); | |
| } | |
| getName(): string { | |
| return this.name; | |
| } | |
| setName(name: string): void { | |
| this.name = name; | |
| } | |
| } |
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
| class UserRepository { | |
| updateUserName(id: number, name: string): void { | |
| const user = User.findById(id); | |
| user.setName(name); | |
| // persistir cambios | |
| } | |
| getUserName(id: number): string { | |
| return User.findById(id).getName(); | |
| } | |
| } |
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
| class UserService { | |
| constructor(private repo: UserRepository) {} | |
| private ensureNonEmptyName(name: string): void { | |
| if (!name.trim()) { | |
| throw new Error("Name cannot be empty"); | |
| } | |
| } | |
| setUserName(id: number, name: string): void { | |
| this.ensureNonEmptyName(name); | |
| this.repo.updateUserName(id, name); | |
| } | |
| getUserName(id: number): string { | |
| return this.repo.getUserName(id); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment