Skip to content

Commit

Permalink
dnn-sxc-angular improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
maaaximum-at-2sic committed Mar 9, 2022
1 parent ca4f36e commit d67d206
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SxcData } from './sxc/sxc-data';
import { Sxc } from './sxc/sxc-data';
import { NgModule } from '@angular/core';
import { SxcToolbarDirective } from './beta/edit';
// important: for funny reasons this must really got to the real path - if you get it from '.' index it will fail building
Expand All @@ -18,7 +18,7 @@ import { SxcTagToolbarDirective } from './toolbar/tag-toolbar';
SxcTagToolbarDirective,
],
providers: [
SxcData,
Sxc,
],
exports: [
SxcToolbarDirective,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ export class DnnAppComponent {
// auto-config to pick up tab-id, module id, etc.
context.autoConfigure(element);
// prevent asp.net submit action caused by enter-keys inside our app
if(!enableDefaultSubmit){
element.nativeElement.addEventListener('keydown', function(e: any) {
if(e.target.tagName.toLowerCase() == "input" && e.keyCode==13)
e.preventDefault();
});

}
if (!enableDefaultSubmit)
element.nativeElement.addEventListener('keydown', (e: any) => {
if (e.target.tagName.toLowerCase() === 'input' && e.keyCode === 13)
e.preventDefault();
}
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from './context/context.service';
import { SxcData } from './sxc/sxc-data';
import { Sxc } from './sxc/sxc-data';
import { DnnInterceptor } from './http/dnn.interceptor';
import { NgModule, Optional, SkipSelf } from '@angular/core';

Expand All @@ -17,7 +17,7 @@ import { NgModule, Optional, SkipSelf } from '@angular/core';
// HttpClientModule,
],
providers: [
SxcData,
Sxc,
Context,
DnnInterceptor,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,70 @@ export class Api {
/**
* Do a GET request to the specified 2sxc api controller
*/
fetch<T>(url: string): Observable<T>;
// 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>;
// 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>;
// fetch<T>(url: string, body: T, verb: string): Observable<T>;

/**
* Internal implementation of fetch
*/
fetch<T>(url: string, body: T = null, method: string = null, params?: HttpParams): Observable<T[]> | Observable<T> {
const fullUrl = `${routeApi}/${this.controller}/${url}`;
// fetch<T>(method: string, body: T = null, verb: string = null, params?: HttpParams): Observable<T[]> | Observable<T> {
// const fullUrl = `${routeApi}/${this.controller}/${method}`;

if (body && !method || body && method.toLocaleLowerCase() === 'post')
return this.http.post<T>(fullUrl, body, { params });
// if (body && !verb || body && verb.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 && verb.toLocaleLowerCase() === 'put')
// return this.http.put<T>(fullUrl, body, { params });

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

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

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

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

/**
* Get the request URL of the api
*/
url(url: string): string {
return `${routeApi}/${this.controller}/${url}`;
url(method: string): string {
return `${routeApi}/${this.controller}/${method}`;
}

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

/**
* Do a POST request to the specified 2sxc api controller
*/
post<T>(method: string, body: T, params?: HttpParams): Observable<T> {
return this.http.post<T>(this.url(method), body, { params });
}

/**
* Do a PUT request to the specified 2sxc api controller
*/
put<T>(method: string, body: T, params?: HttpParams): Observable<T> {
return this.http.put<T>(this.url(method), body, { params });
}

/**
* Do a DELETE request to the specified 2sxc api controller
*/
delete<T>(method: string, params?: HttpParams): Observable<T> {
return this.http.put<T>(this.url(method), { params });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ export class Data<T> {
/**
* Internal implementation of create
*/
create(item: T | any, metadataFor: MetaDataFor = null): Observable<T> {
create(item: T | any, metadataFor?: MetaDataFor): Observable<T> {
const url = `${routeContent}/${this.contentType}`;
if (metadataFor != null)
return this.http.post<T>(url, { ...item, For: metadataFor });

item = (metadataFor != null ? { ...item, For: metadataFor } : item);
return this.http.post<T>(url, item);
}

Expand All @@ -61,17 +59,7 @@ export class Data<T> {
}

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

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

/**
* internal implementation with ID/with GUID
* Delete the specific item with ID/with GUID
*/
delete(id: number | string): Observable<T> {
const url = `${routeContent}/${this.contentType}/${id}`;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ export class Query<T> {
getAll(): Observable<T>;

/**
* Get an object with all the streams, each containing an array of all the data in that stream with url-parameters as HttpParams
* Get an object with all the streams, each containing an array of all the data in that stream with url-parameters as HttpParams or string
*/
getAll(params: HttpParams): Observable<T>;

/**
* Get an object with all the streams, each containing an array of all the data in that stream with url-parameters as string
*/
getAll(params: string): Observable<T>;
getAll(params: HttpParams | string): Observable<T>;

/**
* Get an object with all the streams, each containing an array of all the data in that stream with url-parameters as string
Expand All @@ -40,17 +35,11 @@ export class Query<T> {
/**
* Internal implementation of getAll
*/
getAll(params: HttpParams | string = null, data: T = null): Observable<T> {
getAll(params?: HttpParams | string, data?: T): Observable<T> {
const url = `${routeQuery}/${this.name}`;

if (data)
return (typeof(params) === 'string'
? this.http.post<T>(`${url}${params !== null ? `?${params}` : ''}`, data)
: this.http.post<T>(url, data, { params }));
if (params)
return (typeof(params) === 'string'
? this.http.get<T>(`${url}${params !== null ? `?${params}` : ''}`)
: this.http.get<T>(url, { params }));
const streamParams = new HttpParams({fromString: params.toString() });
if (data) return this.http.post<T>(url, data, { params: streamParams });
if (params) return this.http.get<T>(url, { params: streamParams });

return this.http.get<T>(url);
}
Expand All @@ -61,14 +50,9 @@ export class Query<T> {
getStream(stream: string): Observable<T>;

/**
* Get an object with the specified stream, containing an array of all the data in that stream with url-parameters as HttpParams
*/
getStream(stream: string, params: HttpParams): Observable<T>;

/**
* Get an object with the specified stream, containing an array of all the data in that stream with url-parameters as string
* Get an object with the specified stream, containing an array of all the data in that stream with url-parameters as HttpParams or string
*/
getStream(stream: string, params: string): Observable<T>;
getStream(stream: string, params: HttpParams | string): Observable<T>;

/**
* Get an object with the specified stream, containing an array of all the data in that stream with url-parameters as string
Expand All @@ -78,17 +62,11 @@ export class Query<T> {
/**
* Internal implementation of getStream
*/
getStream(stream: string, params: HttpParams | string = null, data: T = null): Observable<T> {
getStream(stream: string, params?: HttpParams | string, data?: T): Observable<T> {
const url = `${routeQuery}/${this.name}?${this.streamParamKey}=${stream}`;

if (data)
return (typeof(params) === 'string'
? this.http.post<T>(`${url}${params !== null ? `?${params}` : ''}`, data)
: this.http.post<T>(url, data, { params }));
if (params)
return (typeof(params) === 'string'
? this.http.get<T>(`${url}${params !== null ? `?${params}` : ''}`)
: this.http.get<T>(url, { params }));
const streamParams = new HttpParams({fromString: params.toString() });
if (data) return this.http.post<T>(url, data, { params: streamParams });
if (params) return this.http.get<T>(url, { params: streamParams });

return this.http.get<T>(url);
}
Expand All @@ -99,14 +77,10 @@ export class Query<T> {
getStreams(streams: string[]): Observable<T>;

/**
* Get an object with the specified streams, each containing an array of all the data in that stream with url-parameters as HttpParams
*/
getStreams(streams: string[], params: HttpParams): Observable<T>;

/**
* Get an object with the specified streams, each containing an array of all the data in that stream with url-parameters as string
* Get an object with the specified streams, each containing an array of all the data in that stream
* with url-parameters as HttpParams or string
*/
getStreams(streams: string[], params: string): Observable<T>;
getStreams(streams: string[], params: HttpParams | string): Observable<T>;

/**
* Get an object with the specified streams, each containing an array of all the data in that stream with url-parameters as string
Expand All @@ -116,17 +90,11 @@ export class Query<T> {
/**
* Internal implementation of getStreams
*/
getStreams(streams: string[], params: HttpParams | string = null, data: T = null): Observable<T> {
getStreams(streams: string[], params?: HttpParams | string, data?: T): Observable<T> {
const url = `${routeQuery}/${this.name}?${this.streamParamKey}=${streams.join(',')}`;

if (data)
return (typeof(params) === 'string'
? this.http.post<T>(`${url}${params !== null ? `?${params}` : ''}`, data)
: this.http.post<T>(url, data, { params }));
if (params)
return (typeof(params) === 'string'
? this.http.get<T>(`${url}${params !== null ? `?${params}` : ''}`)
: this.http.get<T>(url, { params }));
const streamParams = new HttpParams({fromString: params.toString() });
if (data) return this.http.post<T>(url, data, { params: streamParams });
if (params) return this.http.get<T>(url, { params: streamParams });

return this.http.get<T>(url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Query } from './query';
import { QueryConstruction } from './query-construction';

/**
* 2sxc data provider
Expand All @@ -18,7 +17,7 @@ import { QueryConstruction } from './query-construction';
@Injectable({
providedIn: 'root',
})
export class SxcData {
export class Sxc {
constructor(
private http: HttpClient,
) { }
Expand All @@ -33,19 +32,6 @@ export class SxcData {
return new Data<T>(this.http, contentType);
}

/**
* get a stream of content items or (if ID is provided) a stream containing one item
* @param contentType name of the content-type
* @param id optional id of a single item
* @returns an observable containing a single item (if ID is provided) or an array of these items
*/
data$<T>(contentType: string, id?: number): Observable<T> {
// When id is undefined, we would get back an Observable<T[]> instead of Observable<T>.
// Typescript does not take care of this; however we ignore it because we want the data
// service to always return an Observable of T.
return new Data<T>(this.http, contentType).getOne(id);
}

/**
* get a query object to then start queries
* usually you'll be better off using the observable stream query$, this is included primarily for consistency in the api
Expand All @@ -56,23 +42,6 @@ export class SxcData {
return new Query<T>(this.http, name);
}

/**
* retrieve a query stream from the server
* @param name the query name
* @param params optional parameters-object
* @returns a typed observable which will give you the query
*/
public query$<T>(name: string, params?: HttpParams): Observable<T>;
public query$<T>({ name, params, streams }: QueryConstruction): Observable<T>;
public query$<T>(param1: any, param2?: HttpParams) {
if (typeof param1 === 'object') {
const { name, params, streams } = <QueryConstruction>param1;
const query = new Query<T>(this.http, name);
return (Array.isArray(streams) ? query.getStreams(streams, params) : query.getStream(streams, params));
} else return new Query<T>(this.http, param1).getAll();
}


/**
* get an api object to then start api-calls
* usually you'll be better off using the quick observable stream api$, this is included primarily for consistency in the api
Expand All @@ -89,15 +58,15 @@ export class SxcData {
* @param params optional parameters-object
* @returns a typed observable which will give you the query
*/
public api$<T>(apiName: string, params?: HttpParams): Observable<T> {
const separator = apiName.indexOf('/');
if (separator === -1) {
throw new Error(`Trying to get api$ but only got '${apiName}' - expected something in the format of 'controller/method'`);
}
// public api$<T>(apiName: string, params?: HttpParams): Observable<T> {
// const separator = apiName.indexOf('/');
// if (separator === -1) {
// throw new Error(`Trying to get api$ but only got '${apiName}' - expected something in the format of 'controller/method'`);
// }

const method = apiName.substr(separator + 1);
apiName = apiName.substr(0, separator);
// const method = apiName.substr(separator + 1);
// apiName = apiName.substr(0, separator);

return new Api(this.http, apiName).fetch<T>(method);
}
// return new Api(this.http, apiName).fetch<T>(method);
// }
}

0 comments on commit d67d206

Please sign in to comment.