import { Injectable} from '@angular/core';
import { HttpClient} from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../../../../environments/environment';

import { User } from '../model/user.interface';
@Injectable({
    providedIn: 'root'
})
export class apiUserService {
    ServeurURL = environment.ServeurURL;
    ApiURL = this.ServeurURL+"/api";

    constructor(
        private http: HttpClient){}    

    // User CRUD  

    getUser(_id: String): Observable<User> {
        return this.http.get<User>(this.ApiURL+'/user/user/'+_id);
    }

    saveUser(User: User): Observable<User> {
        return this.http.post<User>(this.ApiURL+'/user/user/', User);
    }

    updateUser(User: User): Observable<User> {
        return this.http.put<User>(this.ApiURL+'/user/user/'+User._id, User);
    }

    deleteUser(_id: String): Observable<any> {
        return this.http.delete<any>(this.ApiURL+'/user/user/'+_id);
    }

    // User function
    getCurrentUser(): Observable<User> {
        return this.http.get<User>(this.ApiURL+'/user/current');
    }

    getAllUsers(): Observable<User[]> {
        return this.http.get<User[]>(this.ApiURL+'/user/getAllUsers');
    }
}
