Oddbean new post about | logout
  // @ts-nocheck
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { UserService } from '../user.service';
import { AuthenticationService } from '../../authentication.service';
import { environment } from '../../../environments/environment';
import { LoginResponse } from '../models/loginResponse';
import { AuthTokenService } from './authToken.service';

@Injectable({
  providedIn: 'root'
})
export class UserServiceImpl implements UserService {
  constructor(private httpClient: HttpClient, private authTokenService: AuthTokenService) {}

  getAll(): Observable<any> {
    const url = `${environment.apiUrl}/users`;
    return this.httpClient.get(`${url}?page=${environment.page}&size=${environment.size}`);
  }

  get(id: number): Observable<any> {
    const token = this.authTokenService.getToken();
    const url = `${environment.apiUrl}/users/${id}`;
    return this.httpClient.get(`${url}`, { headers: { Authorization: `Bearer ${token}` } });
  }

  create(user: any): Observable<any> {
    const token = this.authTokenService.getToken();
    const url = `${environment.apiUrl}/users`;
    return this.httpClient.post(`${url}`, user, { headers: { Authorization: `Bearer ${token}` } });
  }

  update(user: any): Observable<any> {
    const token = this.authTokenService.getToken();
    const url = `${environment.apiUrl}/users/${user.id}`;
    return this.httpClient.put(`${url}`, user, { headers: { Authorization: `Bearer ${token}` } });
  }

  delete(id: number): Observable<any> {
    const token = this.authTokenService.getToken();
    const url = `${environment.apiUrl}/users/${id}`;
    return this.httpClient.delete(`${url}`, { headers: { Authorization: `Bearer ${token}` } });
  }

  login(email: string, password: string): Observable<LoginResponse> {
    const url = `${environment.apiUrl}/auth/login`;
    return this.httpClient.post(`${url}`, { email, password }).pipe(map((response) => response as LoginResponse));
  }

  register(user: any): Observable<any> {
    const token = this.authTokenService.getToken();
    const url = `${environment.apiUrl}/users/register`;
    return this.httpClient.post(`${url}`, user, { headers: { Authorization: `Bearer ${token}` } });
  }
}