Fully generic CRUD pages
This commit is contained in:
@@ -1,41 +1,47 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Injectable, Inject } from '@angular/core';
|
||||
import { Schema } from "./jsonschemas.service";
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class CrudService {
|
||||
export class ApiService {
|
||||
constructor(protected http: HttpClient) {}
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
protected api_root: string = '/api/v1'
|
||||
|
||||
public getSchema() {
|
||||
return this.http.get<Schema>(`${this.api_root}/openapi.json`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class CrudService extends ApiService {
|
||||
|
||||
public loading: boolean = false;
|
||||
|
||||
public getSchema() {
|
||||
return this.http.get<Schema>(`/api/v1/openapi.json`);
|
||||
}
|
||||
|
||||
public getList(page: number, size: number, sortColumn: string, sortDirection: string) {
|
||||
return this.http.get<{ menu: [{}] }>(
|
||||
`/api/v1/entity/?size=${size}&page=${page + 1}&sort_by=${sortDirection}(${sortColumn})`
|
||||
public getList(resource: string, page: number, size: number, sortColumn: string, sortDirection: string) {
|
||||
return this.http.get<{ items: [{}] }>(
|
||||
`${this.api_root}/${resource.toLowerCase()}/?size=${size}&page=${page + 1}&sort_by=${sortDirection}(${sortColumn})`
|
||||
);
|
||||
}
|
||||
|
||||
public get(id: string) {
|
||||
public get(resource: string, id: string) {
|
||||
return this.http.get<{}>(
|
||||
`/api/v1/entity/${id}`
|
||||
`${this.api_root}/${resource.toLowerCase()}/${id}`
|
||||
);
|
||||
}
|
||||
|
||||
public update(model: any) {
|
||||
public update(resource: string, model: any) {
|
||||
return this.http.put<{ menu: [{}] }>(
|
||||
`/api/v1/entity/${model._id}`,
|
||||
`${this.api_root}/${resource.toLowerCase()}/${model._id}`,
|
||||
model
|
||||
);
|
||||
}
|
||||
|
||||
public create(model: any) {
|
||||
public create(resource: string, model: any) {
|
||||
return this.http.post<{ menu: [{}] }>(
|
||||
`/api/v1/entity/`,
|
||||
`${this.api_root}/${resource.toLowerCase()}/`,
|
||||
model
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user