-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adicionar nova funcionalidade para usuário poder fazer o crud d…
…o proprios usuários
- Loading branch information
Fábio Henrique
authored and
Fábio Henrique
committed
Nov 24, 2024
1 parent
ddc7686
commit 37afe06
Showing
13 changed files
with
682 additions
and
57 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient, HttpHeaders } from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { environment } from 'src/environments/environment'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class UnidadeService { | ||
private apiUrl = `${environment.apiUrl}/api`; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
private getHeaders(): HttpHeaders { | ||
const token = localStorage.getItem('token'); | ||
return new HttpHeaders({ | ||
Authorization: `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
}); | ||
} | ||
|
||
/** | ||
* Obter todas as unidades | ||
* Endpoint: GET /units | ||
*/ | ||
getUnidades(): Observable<any> { | ||
return this.http.get<any>(`${this.apiUrl}/units`, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
|
||
/** | ||
* Obter unidade por ID | ||
* Endpoint: GET /units/:id | ||
*/ | ||
getUnidadeById(id: number): Observable<any> { | ||
return this.http.get<any>(`${this.apiUrl}/units/${id}`, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
|
||
/** | ||
* Criar uma nova unidade | ||
* Endpoint: POST /create-unit | ||
*/ | ||
criarUnidade(data: { name: string }): Observable<any> { | ||
return this.http.post<any>(`${this.apiUrl}/create-unit`, data, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
|
||
/** | ||
* Atualizar unidade existente | ||
* Endpoint: PUT /units/:id | ||
*/ | ||
atualizarUnidade(id: number, data: { name: string; pixKey?: string }): Observable<any> { | ||
return this.http.put<any>(`${this.apiUrl}/units/${id}`, data, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
|
||
/** | ||
* Deletar unidade | ||
* Endpoint: DELETE /units/:id | ||
*/ | ||
deletarUnidade(id: number): Observable<any> { | ||
return this.http.delete<any>(`${this.apiUrl}/units/${id}`, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient, HttpHeaders } from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { environment } from 'src/environments/environment'; | ||
|
||
export interface Usuario { | ||
id?: number; | ||
name?: string; | ||
email?: string; | ||
password?: string; | ||
role?: 'USER' | 'ADMIN'; // Tipo consistente | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class UsuarioService { | ||
private apiUrl = `${environment.apiUrl}/api`; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
private getHeaders(): HttpHeaders { | ||
const token = localStorage.getItem('token'); | ||
if (!token) { | ||
console.warn('Token não encontrado no localStorage.'); | ||
} | ||
|
||
return new HttpHeaders({ | ||
Authorization: token ? `Bearer ${token}` : '', | ||
'Content-Type': 'application/json', | ||
}); | ||
} | ||
|
||
/** | ||
* Obter todos os usuários | ||
* Endpoint: GET /users | ||
*/ | ||
getUsuarios(): Observable<{ users: Usuario[] }> { | ||
return this.http.get<{ users: Usuario[] }>(`${this.apiUrl}/users`, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
|
||
/** | ||
* Obter usuário por ID | ||
* Endpoint: GET /users/:id | ||
*/ | ||
getUsuarioById(id: number): Observable<Usuario> { | ||
return this.http.get<Usuario>(`${this.apiUrl}/users/${id}`, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
|
||
/** | ||
* Criar um novo usuário | ||
* Endpoint: POST /users | ||
*/ | ||
criarUsuario(data: Usuario): Observable<Usuario> { | ||
return this.http.post<Usuario>(`${this.apiUrl}/users`, data, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
|
||
/** | ||
* Atualizar usuário existente | ||
* Endpoint: PUT /users/:id | ||
*/ | ||
atualizarUsuario(data: Usuario): Observable<Usuario> { | ||
return this.http.put<Usuario>(`${this.apiUrl}/users/${data.id}`, data, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
|
||
/** | ||
* Deletar usuário | ||
* Endpoint: DELETE /users/:id | ||
*/ | ||
deletarUsuario(id: number): Observable<void> { | ||
return this.http.delete<void>(`${this.apiUrl}/users/${id}`, { | ||
headers: this.getHeaders(), | ||
}); | ||
} | ||
} |
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
1 change: 0 additions & 1 deletion
1
src/app/modules/collaboration/company-unit/company-unit.component.html
This file was deleted.
Oops, something went wrong.
Empty file.
23 changes: 0 additions & 23 deletions
23
src/app/modules/collaboration/company-unit/company-unit.component.spec.ts
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/app/modules/collaboration/company-unit/company-unit.component.ts
This file was deleted.
Oops, something went wrong.
13 changes: 12 additions & 1 deletion
13
src/app/modules/collaboration/pages/company-unit/company-unit.component.html
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
<p>company-unit works!</p> | ||
<div> | ||
<h1>Unidades</h1> | ||
<button (click)="criarUnidade()">Criar Unidade</button> | ||
|
||
<ul> | ||
<li *ngFor="let unidade of unidades"> | ||
{{ unidade.name }} (ID: {{ unidade.id }}) | ||
<button (click)="atualizarUnidade(unidade.id)">Atualizar</button> | ||
<button (click)="deletarUnidade(unidade.id)">Deletar</button> | ||
</li> | ||
</ul> | ||
</div> |
60 changes: 59 additions & 1 deletion
60
src/app/modules/collaboration/pages/company-unit/company-unit.component.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,70 @@ | ||
import { NgFor } from '@angular/common'; | ||
import { Component } from '@angular/core'; | ||
import { UnidadeService } from 'src/app/core/services/unidade.service'; | ||
|
||
|
||
@Component({ | ||
selector: 'app-company-unit', | ||
standalone: true, | ||
imports: [], | ||
imports: [NgFor], | ||
templateUrl: './company-unit.component.html', | ||
styleUrl: './company-unit.component.scss' | ||
}) | ||
export class CompanyUnitComponent { | ||
unidades: any[] = []; | ||
|
||
constructor(private unidadeService: UnidadeService) {} | ||
|
||
ngOnInit(): void { | ||
this.carregarUnidades(); | ||
} | ||
|
||
carregarUnidades(): void { | ||
this.unidadeService.getUnidades().subscribe( | ||
(response) => { | ||
this.unidades = response.units; | ||
}, | ||
(error) => { | ||
console.error('Erro ao carregar unidades:', error); | ||
} | ||
); | ||
} | ||
|
||
criarUnidade(): void { | ||
const novaUnidade = { name: 'Unidade Nova' }; | ||
this.unidadeService.criarUnidade(novaUnidade).subscribe( | ||
(response) => { | ||
console.log('Unidade criada com sucesso:', response); | ||
this.carregarUnidades(); | ||
}, | ||
(error) => { | ||
console.error('Erro ao criar unidade:', error); | ||
} | ||
); | ||
} | ||
|
||
atualizarUnidade(id: number): void { | ||
const unidadeAtualizada = { name: 'Unidade Atualizada', pixKey: 'novaPixKey' }; | ||
this.unidadeService.atualizarUnidade(id, unidadeAtualizada).subscribe( | ||
(response) => { | ||
console.log('Unidade atualizada com sucesso:', response); | ||
this.carregarUnidades(); | ||
}, | ||
(error) => { | ||
console.error('Erro ao atualizar unidade:', error); | ||
} | ||
); | ||
} | ||
|
||
deletarUnidade(id: number): void { | ||
this.unidadeService.deletarUnidade(id).subscribe( | ||
(response) => { | ||
console.log('Unidade deletada com sucesso:', response); | ||
this.carregarUnidades(); | ||
}, | ||
(error) => { | ||
console.error('Erro ao deletar unidade:', error); | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.