Skip to content

Commit

Permalink
dnn-sxc-angular improvements data, changes api
Browse files Browse the repository at this point in the history
  • Loading branch information
maaaximum-at-2sic committed Mar 8, 2022
1 parent bd2548a commit de9e18b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,43 @@ export class Api {
) { }

/**
* Do a GET request a 2sxc api controller method
* Do a GET request to the specified 2sxc api controller
*/
get<T>(method: string, params?: HttpParams): Observable<T> {
const url = `${routeApi}/${this.controller}/${method}`;
return this.http.get<T>(url, { params });
}
fetch<T>(url: string): Observable<T>;

/**
* Do a POST request to the specified 2sxc api controller with the specified body
*/
fetch<T>(url: string, body: T): Observable<T>;

/**
* Do a request to the specified 2sxc api controller method with the specified body
*/
fetch<T>(url: string, body: T, method: string): Observable<T>;

/**
* Do a POST request to a 2sxc api controller method
* Internal implementation of fetch
*/
post<T>(method: string, body: any, params?: HttpParams): Observable<T> {
const url = `${routeApi}/${this.controller}/${method}`;
return this.http.post<T>(url, body, { params });
fetch<T>(url: string, body: T = null, method: string = null, params?: HttpParams): Observable<T[]> | Observable<T> {
const fullUrl = `${routeApi}/${this.controller}/${url}`;

if (body && !method || body && method.toLocaleLowerCase() === 'post') {
return this.http.post<T>(fullUrl, body, { params });
}

if (body && method.toLocaleLowerCase() === 'put') {
return this.http.put<T>(fullUrl, body, { params });
}

if (body && method.toLocaleLowerCase() === 'delete') {
return this.http.delete<T>(fullUrl, { params });
}

return this.http.get<T>(fullUrl, { params });
}

url(url: string): string {
return `${routeApi}/${this.controller}/${url}`;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ export class Data<T> {
* Get all items of this type
*/
getAll(): Observable<T[]> {
let url = `${routeContent}/${this.contentType}`;
const url = `${routeContent}/${this.contentType}`;
return this.http.get<T[]>(url);
}

/**
* get the specific item with the ID
*/
getOne(id: number): Observable<T> {
let url = `${routeContent}/${this.contentType}/${id}`;
const url = `${routeContent}/${this.contentType}/${id}`;
return this.http.get<T>(url);
}

/**
* Create new item
*/
create(item: T): Observable<T> {
let url = `${routeContent}/${this.contentType}`;
const url = `${routeContent}/${this.contentType}`;
return this.http.post<T>(url, item);
}

/**
* Update the specific item with the ID to the item
*/
update(id: number, item: T): Observable<T> {
let url = `${routeContent}/${this.contentType}/${id}`;
const url = `${routeContent}/${this.contentType}/${id}`;
return this.http.put<T>(url, item);
}

Expand All @@ -52,16 +52,16 @@ export class Data<T> {
delete(id: number): Observable<T>;

/**
* Delete the specific item with the GUID
*/
* Delete the specific item with the GUID
*/
delete(id: string): Observable<T>;

/**
* internal implementation with ID/with GUID
*/
* internal implementation with ID/with GUID
*/
delete(id: number | string): Observable<T[]> | Observable<T> {
let url = `${routeContent}/${this.contentType}/${id}`;
if (typeof(id) == 'string') {
const url = `${routeContent}/${this.contentType}/${id}`;
if (typeof(id) === 'string') {
throw new Error('not implemented yet');
}

Expand Down

0 comments on commit de9e18b

Please sign in to comment.