mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.5 KiB
59 lines
1.5 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
|
|
*/
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { Observable } from 'rxjs';
|
|
import { map } from 'rxjs/operators';
|
|
|
|
import {
|
|
ApiUrlConfig,
|
|
pretifyError,
|
|
ResourceLinks
|
|
} from '@app/framework';
|
|
|
|
export class SearchResultDto {
|
|
public readonly _links: ResourceLinks;
|
|
|
|
public readonly url: string;
|
|
|
|
constructor(links: ResourceLinks,
|
|
public readonly name: string,
|
|
public readonly type: string,
|
|
public readonly label?: string
|
|
) {
|
|
this._links = links;
|
|
|
|
this.url = this._links['url'].href;
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class SearchService {
|
|
constructor(
|
|
private readonly http: HttpClient,
|
|
private readonly apiUrl: ApiUrlConfig
|
|
) {
|
|
}
|
|
|
|
public getResults(appName: string, query: string): Observable<ReadonlyArray<SearchResultDto>> {
|
|
const url = this.apiUrl.buildUrl(`api/apps/${appName}/search/?query=${encodeURIComponent(query)}`);
|
|
|
|
return this.http.get<ReadonlyArray<any>>(url).pipe(
|
|
map(body => {
|
|
const results = body.map(item =>
|
|
new SearchResultDto(
|
|
item._links,
|
|
item.name,
|
|
item.type,
|
|
item.label));
|
|
|
|
return results;
|
|
}),
|
|
pretifyError('Failed to make search. Please reload.'));
|
|
}
|
|
}
|