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
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Getlab

## Injeção de dependências

Como registrar suas dependências, direcionando responsabilidades

### Simples

Classe sem dependência nem substituto concreto

```ts
Expand All @@ -15,6 +17,7 @@ register({ for: MeuService });
```

### Com dependência

Classe contendo uma dependência

Veja que primeiro registramos `DependenciaDoMeuService`, em seguida registramos `MeuServiceComDependencia` indicando para adicionar `DependenciaDoMeuService`, pois ela depende dele pra funcionar corretamente.
Expand All @@ -38,6 +41,7 @@ register(
```

### Direcionando responsabilidade

Classe abstrata e classe concreta
Primeiro criamos uma abstração sem implementação, desta forma:

Expand Down Expand Up @@ -69,6 +73,7 @@ register({
```

### Direcionando responsabilidade com dependências

Classe abstrata e classe concreta que depende de outras classes
Em seguida, criamos a classe concreta, que implementa o contrato da abstração:

Expand Down Expand Up @@ -103,6 +108,7 @@ const meuOutroServico = inject(MeuOutroService);
# Exemplos reais

## Organizando

UseCases precisam da infra registrada e Facades precisas dos UseCases registrados, então precisamos começar registradno a infra

```ts
Expand Down Expand Up @@ -159,15 +165,16 @@ import { Component } from '@angular/core';
@Component({
standalone: true,
selector: 'getlab-root',
providers: [ transfer() ],
providers: [transfer()],
template: `
<pre>
{{ facade.data$ | async | json }}
</pre>
</pre
>
`,
})
export class AppComponent {
facade = inject(TeamFacade)
facade = inject(TeamFacade);
}
```

Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ import { UsersModule } from './users/users.module';
UsersModule,
TeamsModule,
SchedulesModule,
]
],
})
export class AppModule {}
4 changes: 2 additions & 2 deletions apps/server/src/app/schedules/infrastructure/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './schedules.service.impl';
export * from './schedules.service';
export * from './schedules.service.impl';
export * from './schedules.service';
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export class SchedulesServiceImpl implements SchedulesService {
}

remove(id: string) {
return this.scheduleModel.findOneAndRemove({ id });
return this.scheduleModel.findByIdAndRemove(id);
}
}
4 changes: 2 additions & 2 deletions apps/server/src/app/teams/infrastructure/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './teams.service.impl';
export * from './teams.service';
export * from './teams.service.impl';
export * from './teams.service';
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export class TeamsServiceImpl implements TeamsService {
}

async remove(id: string) {
return this.teamModel.findOneAndRemove({ id });
return this.teamModel.findByIdAndRemove(id);
}
}
4 changes: 2 additions & 2 deletions apps/server/src/app/users/infrastructure/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './users.service.impl';
export * from './users.service';
export * from './users.service.impl';
export * from './users.service';
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export class UsersServiceImpl implements UsersService {
}

async remove(id: string) {
return this.userModel.findOneAndRemove({ id });
return this.userModel.findByIdAndRemove(id);
}
}
8 changes: 4 additions & 4 deletions libs/data-access/src/lib/application/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './schedule.facade';
export * from './spreadsheet.facade';
export * from './team.facade';
export * from './user.facade';
export * from './schedule.facade';
export * from './spreadsheet.facade';
export * from './team.facade';
export * from './user.facade';
50 changes: 34 additions & 16 deletions libs/data-access/src/lib/application/schedule.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Store } from '../base/store';

interface ScheduleState {
data: Schedule[];
error: string | null;
schedule: Schedule | null;
filtered: Schedule[];
loading: boolean;
Expand All @@ -34,43 +35,60 @@ export class ScheduleFacade extends Store<ScheduleState> {
) {
super({
data: [],
error: null,
filtered: [],
schedule: null,
loading: false,
});
}

load() {
this.findAllUseCase.execute().then((data) => this.setState({ data }));
this.catch(
this.findAllUseCase.execute().then((data) => this.setState({ data }))
);
}

findSchedule(id: string) {
this.findOneUseCase
.execute(id)
.then((schedule) => this.setState({ schedule }));
this.catch(
this.findOneUseCase
.execute(id)
.then((schedule) => this.setState({ schedule }))
);
}

findSchedules(...ids: string[]) {
this.findManyUseCase
.execute(...ids)
.then((filtered) => this.setState({ filtered }));
this.catch(
this.findManyUseCase
.execute(...ids)
.then((filtered) => this.setState({ filtered }))
);
}

createSchedule(schedule: CreateScheduleDto) {
this.createUseCase.execute(schedule).then(() => {
this.setState({ schedule: null });
this.load();
});
this.catch(
this.createUseCase.execute(schedule).then(() => {
this.setState({ schedule: null });
this.load();
})
);
}

updateSchedule(schedule: UpdateScheduleDto) {
this.updateUseCase.execute(schedule).then(() => {
this.setState({ schedule: null });
this.load();
});
this.catch(
this.updateUseCase.execute(schedule).then(() => {
this.setState({ schedule: null });
this.load();
})
);
}

removeSchedule(id: string) {
this.removeByIdUseCase.execute(id).then(() => this.load());
this.catch(this.removeByIdUseCase.execute(id).then(() => this.load()));
}

catch<T extends Promise<unknown>>(promise: T) {
promise.catch((error) => {
this.setState({ error: error.message });
});
}
}
26 changes: 19 additions & 7 deletions libs/data-access/src/lib/application/spreadsheet.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface SpreadsheetState {
data: SpreadsheetRow[];
loading: boolean;
parsed: string;
error: string | null;
}

export class SpreadsheetFacade extends Store<SpreadsheetState> {
Expand All @@ -27,23 +28,34 @@ export class SpreadsheetFacade extends Store<SpreadsheetState> {
super({
data: [],
parsed: '',
error: null,
loading: false,
});
}

build(value: BuildSpreadsheetDto) {
this.buildUseCase.execute(value).then((data) => {
this.setState({ data });
});
this.catch(
this.buildUseCase.execute(value).then((data) => {
this.setState({ data });
})
);
}

parse(value: ParseSpreadsheetDto) {
this.parseUseCase.execute(value).then((parsed) => {
this.setState({ parsed });
});
this.catch(
this.parseUseCase.execute(value).then((parsed) => {
this.setState({ parsed });
})
);
}

download() {
this.downloadUseCase.execute(this.state.data);
this.catch(this.downloadUseCase.execute(this.state.data));
}

catch<T extends Promise<unknown>>(promise: T) {
promise.catch((error) => {
this.setState({ error: error.message });
});
}
}
38 changes: 27 additions & 11 deletions libs/data-access/src/lib/application/team.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Store } from '../base/store';
interface TeamState {
data: Team[];
team: Team | null;
error: string | null;
loading: boolean;
}

Expand All @@ -34,33 +35,48 @@ export class TeamFacade extends Store<TeamState> {
super({
data: [],
team: null,
error: null,
loading: false,
});
}

load() {
this.findAllUseCase.execute().then((data) => this.setState({ data }));
this.catch(
this.findAllUseCase.execute().then((data) => this.setState({ data }))
);
}

findTeam(id: string) {
this.findOneUseCase.execute(id).then((team) => this.setState({ team }));
this.catch(
this.findOneUseCase.execute(id).then((team) => this.setState({ team }))
);
}

createTeam(team: CreateTeamDto) {
this.createUseCase.execute(team).then(() => {
this.setState({ team: null });
this.load();
});
this.catch(
this.createUseCase.execute(team).then(() => {
this.setState({ team: null });
this.load();
})
);
}

updateTeam(team: UpdateTeamDto) {
this.updateUseCase.execute(team).then(() => {
this.setState({ team: null });
this.load();
});
this.catch(
this.updateUseCase.execute(team).then(() => {
this.setState({ team: null });
this.load();
})
);
}

removeTeam(id: string) {
this.removeByIdUseCase.execute(id).then(() => this.load());
this.catch(this.removeByIdUseCase.execute(id).then(() => this.load()));
}

catch<T extends Promise<unknown>>(promise: T) {
promise.catch((error) => {
this.setState({ error: error.message });
});
}
}
Loading