|
|
|
@ -1,48 +1,23 @@
|
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
import {Headers, Http} from '@angular/http';
|
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
|
|
|
|
import {DatasharingService} from '../datasharing.service';
|
|
|
|
|
import {Router} from '@angular/router';
|
|
|
|
|
import {environment} from 'src/environments/environment';
|
|
|
|
|
import { User } from 'src/app/models/user';
|
|
|
|
|
import { GroupInfo } from 'src/app/models/groupinfo';
|
|
|
|
|
import {User} from 'src/app/models/user';
|
|
|
|
|
import {GroupInfo} from 'src/app/models/groupinfo';
|
|
|
|
|
import {Observable} from 'rxjs';
|
|
|
|
|
import {ISearchResult} from '../../models/interfaces/ISearchResult';
|
|
|
|
|
import {environment} from '../../../environments/environment';
|
|
|
|
|
import {BaseService} from '../base.service';
|
|
|
|
|
import {tap} from 'rxjs/operators';
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class SearchService {
|
|
|
|
|
|
|
|
|
|
users: Array<User>;
|
|
|
|
|
constructor(private http: Http, private data: DatasharingService, private router: Router) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public renderUsers(pResponse: any): Array<User> {
|
|
|
|
|
const users = new Array<User>();
|
|
|
|
|
for (const user of pResponse.data.search.users) {
|
|
|
|
|
const pUser = new User();
|
|
|
|
|
pUser.profilePicture = user.profilePicture;
|
|
|
|
|
pUser.username = user.name;
|
|
|
|
|
pUser.userID = user.id;
|
|
|
|
|
pUser.handle = user.handle;
|
|
|
|
|
pUser.points = user.points;
|
|
|
|
|
pUser.level = user.level;
|
|
|
|
|
pUser.friends = user.friends;
|
|
|
|
|
users.push(pUser);
|
|
|
|
|
}
|
|
|
|
|
return users;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public renderGroups(pResponse: any): Array<GroupInfo> {
|
|
|
|
|
const groups = new Array<GroupInfo>();
|
|
|
|
|
for (const group of pResponse.data.search.groups) {
|
|
|
|
|
groups.push(new GroupInfo(group.id, group.name));
|
|
|
|
|
}
|
|
|
|
|
return groups;
|
|
|
|
|
}
|
|
|
|
|
interface ISearchRequestResult {
|
|
|
|
|
data: {
|
|
|
|
|
search: ISearchResult;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public buildJsonUser(name_: String): any {
|
|
|
|
|
const body = {
|
|
|
|
|
query: `query($name: String!) {
|
|
|
|
|
search(query:$name, first: 100, offset: 0) {
|
|
|
|
|
const graphqlQuery = `query($query: String!, $first: Int, $offset: Int) {
|
|
|
|
|
search(query:$query, first: $first, offset: $offset) {
|
|
|
|
|
users{
|
|
|
|
|
profilePicture,
|
|
|
|
|
name,
|
|
|
|
@ -61,11 +36,74 @@ export class SearchService {
|
|
|
|
|
members{id name handle}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}`
|
|
|
|
|
, variables: {
|
|
|
|
|
name: name_
|
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class SearchService extends BaseService {
|
|
|
|
|
constructor(private http: HttpClient, private data: DatasharingService, private router: Router) {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
users: User[];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds the body for the request
|
|
|
|
|
* @param query - the search query
|
|
|
|
|
* @param first - the limit of elements to fetch
|
|
|
|
|
* @param offset - offset
|
|
|
|
|
*/
|
|
|
|
|
private static buildRequestBody(query: String, first: number = 20, offset: number = 0): any {
|
|
|
|
|
return {
|
|
|
|
|
query: graphqlQuery,
|
|
|
|
|
variables: {
|
|
|
|
|
query,
|
|
|
|
|
first,
|
|
|
|
|
offset
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps the users in the response to the user class
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
|
|
|
|
public getUsersForResponse(response: ISearchRequestResult): User[] {
|
|
|
|
|
const users = new Array<User>();
|
|
|
|
|
for (const foundUser of response.data.search.users) {
|
|
|
|
|
const user = new User();
|
|
|
|
|
user.profilePicture = foundUser.profilePicture;
|
|
|
|
|
user.username = foundUser.name;
|
|
|
|
|
user.userID = foundUser.id;
|
|
|
|
|
user.handle = foundUser.handle;
|
|
|
|
|
user.points = foundUser.points;
|
|
|
|
|
user.level = foundUser.level;
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
user.friends = foundUser.friends;
|
|
|
|
|
users.push(user);
|
|
|
|
|
}
|
|
|
|
|
return users;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps the groups in the response to the group class
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
|
|
|
|
public getGroupsForResponse(response: ISearchRequestResult): Array<GroupInfo> {
|
|
|
|
|
const groups = new Array<GroupInfo>();
|
|
|
|
|
for (const group of response.data.search.groups) {
|
|
|
|
|
groups.push(new GroupInfo(group.id, group.name));
|
|
|
|
|
}
|
|
|
|
|
return groups;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Searches for users, groups, events and posts with a specified query.
|
|
|
|
|
* @param query
|
|
|
|
|
*/
|
|
|
|
|
public search(query: string): Observable<ISearchRequestResult> {
|
|
|
|
|
const body = SearchService.buildRequestBody(query);
|
|
|
|
|
return this.http.post<ISearchRequestResult>(environment.graphQLUrl, body, {headers: this.headers});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|