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

39 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-07-03 21:48:27 +00:00
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';
@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;
}
}