64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
import { User } from '../../accounts/models/user.model';
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class UserService {
|
|
|
|
private readonly userURL = '/users';
|
|
|
|
constructor(
|
|
private store: Store<{}>,
|
|
private http: HttpClient
|
|
) {
|
|
}
|
|
|
|
loadUsers(options: LoadUserOptions): Observable<User[]> {
|
|
return this.http.post<User[]>(this.userURL + '/search', options);
|
|
}
|
|
|
|
getUser(id: string): Observable<User> {
|
|
return this.http.get<User>(`${this.userURL}/${id}`);
|
|
}
|
|
|
|
userNameExists(userName: string): Observable<boolean> {
|
|
const reqData = { username: userName };
|
|
return this.http.post<boolean>(`${this.userURL}/exists`, reqData);
|
|
}
|
|
|
|
saveUser(user: User): Observable<User> {
|
|
return user._id !== '0' ? this.updateUser(user) : this.createUser(user);
|
|
}
|
|
|
|
private createUser(user: User): Observable<User> {
|
|
return this.http.post<User>(this.userURL, user);
|
|
}
|
|
|
|
private updateUser(user: User): Observable<User> {
|
|
return this.http.put<User>(`${this.userURL}/${user._id}`, user);
|
|
}
|
|
|
|
updateLang(lang): Observable<User> {
|
|
return this.http.post<User>(`${this.userURL}/updateLang`, { lang: lang });
|
|
}
|
|
|
|
deleteUser(user: User) {
|
|
return this.http.delete<User>(`${this.userURL}/${user._id}`);
|
|
}
|
|
|
|
getUserDetail(username: string) {
|
|
return this.http.post<User>(`${this.userURL}/getUserDetail`, { username: username });
|
|
}
|
|
|
|
}
|
|
|
|
export interface LoadUserOptions {
|
|
byPuid: string;
|
|
accountType?: number;
|
|
}
|