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.
85 lines
2.3 KiB
85 lines
2.3 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Sebastian Stehle. All rights reserved
|
|
*/
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { Observable } from 'rxjs';
|
|
|
|
import 'framework/angular/http-extensions';
|
|
|
|
import {
|
|
AnalyticsService,
|
|
ApiUrlConfig,
|
|
DateTime,
|
|
HTTP
|
|
} from 'framework';
|
|
|
|
export class AppDto {
|
|
constructor(
|
|
public readonly id: string,
|
|
public readonly name: string,
|
|
public readonly permission: string,
|
|
public readonly created: DateTime,
|
|
public readonly lastModified: DateTime
|
|
) {
|
|
}
|
|
}
|
|
|
|
export class CreateAppDto {
|
|
constructor(
|
|
public readonly name: string
|
|
) {
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class AppsService {
|
|
constructor(
|
|
private readonly http: HttpClient,
|
|
private readonly apiUrl: ApiUrlConfig,
|
|
private readonly analytics: AnalyticsService
|
|
) {
|
|
}
|
|
|
|
public getApps(): Observable<AppDto[]> {
|
|
const url = this.apiUrl.buildUrl('/api/apps');
|
|
|
|
return HTTP.getVersioned<any>(this.http, url)
|
|
.map(response => {
|
|
const body = response.payload.body;
|
|
|
|
const items: any[] = body;
|
|
|
|
return items.map(item => {
|
|
return new AppDto(
|
|
item.id,
|
|
item.name,
|
|
item.permission,
|
|
DateTime.parseISO(item.created),
|
|
DateTime.parseISO(item.lastModified));
|
|
});
|
|
})
|
|
.pretifyError('Failed to load apps. Please reload.');
|
|
}
|
|
|
|
public postApp(dto: CreateAppDto, now?: DateTime): Observable<AppDto> {
|
|
const url = this.apiUrl.buildUrl('api/apps');
|
|
|
|
return HTTP.postVersioned<any>(this.http, url, dto)
|
|
.map(response => {
|
|
const body = response.payload.body;
|
|
|
|
now = now || DateTime.now();
|
|
|
|
return new AppDto(body.id, dto.name, 'Owner', now, now);
|
|
})
|
|
.do(() => {
|
|
this.analytics.trackEvent('App', 'Created', dto.name);
|
|
})
|
|
.pretifyError('Failed to create app. Please reload.');
|
|
}
|
|
}
|