datasurvey/src/main/webapp/app/entities/user/user.service.ts

42 lines
1.7 KiB
TypeScript

import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ApplicationConfigService } from 'app/core/config/application-config.service';
import { createRequestOption } from 'app/core/request/request-util';
import { isPresent } from 'app/core/util/operators';
import { Pagination } from 'app/core/request/request.model';
import { IUser, getUserIdentifier } from './user.model';
import { map } from 'rxjs/operators';
export type EntityResponseType = HttpResponse<IUser>;
@Injectable({ providedIn: 'root' })
export class UserService {
private resourceUrl = this.applicationConfigService.getEndpointFor('api/users');
constructor(private http: HttpClient, private applicationConfigService: ApplicationConfigService) {}
query(req?: Pagination): Observable<HttpResponse<IUser[]>> {
const options = createRequestOption(req);
return this.http.get<IUser[]>(this.resourceUrl, { params: options, observe: 'response' });
}
addUserToCollectionIfMissing(userCollection: IUser[], ...usersToCheck: (IUser | null | undefined)[]): IUser[] {
const users: IUser[] = usersToCheck.filter(isPresent);
if (users.length > 0) {
const userCollectionIdentifiers = userCollection.map(userItem => getUserIdentifier(userItem)!);
const usersToAdd = users.filter(userItem => {
const userIdentifier = getUserIdentifier(userItem);
if (userIdentifier == null || userCollectionIdentifiers.includes(userIdentifier)) {
return false;
}
userCollectionIdentifiers.push(userIdentifier);
return true;
});
return [...usersToAdd, ...userCollection];
}
return userCollection;
}
}