Added User Management to Admin Panel, reorganized some stuff

This commit is contained in:
itzMiney committed 2025-01-03 23:59:17 +01:00
1 parent d83992d3ce
commit 83260af2e6
22 files changed
+439 -36

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import { Observable } from 'rxjs';
import {map} from 'rxjs/operators';
interface Username {
id: number;
username: string;
password: string;
createdAt: string;
updatedAt: string;
}
@Injectable({
providedIn: 'root'
})
export class UserService {
private apiUrl = '/api/users';
constructor(private http: HttpClient) {}
getAllUsers(token: string | null): Observable<any[]> {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
return this.http.get<any[]>(`${this.apiUrl}/?timestamp=${new Date().getTime()}`, { headers });
}
createUser(token: string, username: string, password: string): Observable<any> {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
return this.http.post<any>(this.apiUrl, {
username,
password
}, { headers });
}
editUser(id: number, token: string | null, username: string, password: string): Observable<any> {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
const body: any = {};
if (username) {
body.username = username
}
if (password) {
body.password = password;
}
return this.http.put<any>(`${this.apiUrl}/${id}`, body, { headers });
}
deleteUser(id: number, token: string): Observable<boolean> {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
return this.http.delete<any>(`${this.apiUrl}/${id}`, { headers }).pipe(
map((response) => {
// If the backend responds successfully, return true
return !!(response && response.message === 'User deleted');
})
);
}
getUserById(id: number, token: string): Observable<Username | undefined> {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
return this.http.get<Username>(`${this.apiUrl}/${id}`, { headers }).pipe(
map(user => user)
);
}
}