Skip to content

Commit

Permalink
dnn-sxc-angular changes data api, minor paths
Browse files Browse the repository at this point in the history
  • Loading branch information
maaaximum-at-2sic committed Mar 7, 2022
1 parent 9b6c253 commit fea82d9
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 116 deletions.

This file was deleted.

104 changes: 35 additions & 69 deletions projects/dnn-sxc-angular/projects/dnn-sxc-angular/src/lib/sxc/data.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,70 @@
import { Api } from './api';
import { Content } from './content';
import { HttpClient } from '@angular/common/http';
import { HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Query } from './query';
import { QueryConstruction } from './query-construction';
import { routeContent } from '../contants';

/**
* 2sxc data provider
* gives you access to content and query streams using the content$ and query$ commands
* you can also use the content and query managers, but these are currently not so useful.
* A helper to access data from 2sxc
*
* @export
* @class Data
* @template T Type which the system will return
*/
@Injectable({
providedIn: 'root',
})
export class Data {
export class Data<T> {
constructor(
private http: HttpClient,
private contentType: string,
) { }

/**
* Cet a content manager object for a specific ContentType
* usually you will prefer the the observable stream content$
* this manager is currently included for consistency, and will later also offer write commands
* @param contentType name of the content-type
* Get all items of this type
*/
public content<T>(contentType: string): Content<T> {
return new Content<T>(this.http, contentType);
getAll(): Observable<T[]> {
let url = `${routeContent}/${this.contentType}`;
return this.http.get<T[]>(url);
}

/**
* 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
* get the specific item with the ID
*/
content$<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 Content<T>(this.http, contentType).get(id);
getOne(id: number): Observable<T> {
let url = `${routeContent}/${this.contentType}/${id}`;
return this.http.get<T[]>(url);
}

/**
* 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
* @param name the query name
* @returns a query object with a .get()
* Create new item
*/
public query<T>(name: string) {
return new Query<T>(this.http, name);
create(item: T): Observable<T> {
let url = `${routeContent}/${this.contentType}`;
return this.http.post<T>(url, item);
}

/**
* 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
* Update the specific item with the ID to the item
*/
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;
return new Query<T>(this.http, name).get(params, streams);
} else {
return new Query<T>(this.http, param1).get(param2);
}
update(id: number, item: T): Observable<T> {
let url = `${routeContent}/${this.contentType}/${id}`;
return this.http.put<T>(url, item);
}


/**
* 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
* @param controller the api controller
* @returns an API object with a .get<T>() method
* Delete the specific item with the ID
*/
public api(controller: string): Api {
return new Api(this.http, controller);
}
delete(id: number): Observable<T>;

/**
* retrieve a api stream from the server
* @param apiName controller/method
* @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'`);
}
* Delete the specific item with the GUID
*/
delete(id: string): Observable<T>;

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

return new Api(this.http, apiName).get<T>(method, params);
return this.http.delete<T>(url);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './api';
export * from './content';
export * from './data';
export * from './sxc-data';
export * from './query';
export * from './query-construction';
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Api } from './api';
import { Data } from './data';
import { HttpClient } from '@angular/common/http';
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
* gives you access to content and query streams using the content$ and query$ commands
* you can also use the content and query managers, but these are currently not so useful.
*
* @export
* @class SxcData
*/
@Injectable({
providedIn: 'root',
})
export class SxcData {
constructor(
private http: HttpClient,
) { }

/**
* Cet a content manager object for a specific ContentType
* usually you will prefer the the observable stream content$
* this manager is currently included for consistency, and will later also offer write commands
* @param contentType name of the content-type
*/
public data<T>(contentType: string): Data<T> {
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
* @param name the query name
* @returns a query object with a .get()
*/
public query<T>(name: string) {
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;
return new Query<T>(this.http, name).get(params, streams);
} else {
return new Query<T>(this.http, param1).get(param2);
}
}


/**
* 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
* @param controller the api controller
* @returns an API object with a .get<T>() method
*/
public api(controller: string): Api {
return new Api(this.http, controller);
}

/**
* retrieve a api stream from the server
* @param apiName controller/method
* @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'`);
}

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

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

0 comments on commit fea82d9

Please sign in to comment.