Skip to content

Instantly share code, notes, and snippets.

View krishna-acondy's full-sized avatar

Krishna Acondy krishna-acondy

View GitHub Profile
@krishna-acondy
krishna-acondy / pizza.service.ts
Last active February 14, 2018 08:02
PizzaService example for a service using the HttpClient
export class PizzaService {
private url='http://pizzaService';
private endpoint = 'pizzas';
constructor(
protected httpClient: HttpClient) {}
public create(pizza: Pizza): Observable<Pizza> {
return this.httpClient
@krishna-acondy
krishna-acondy / pizza.serializer.ts
Created February 11, 2018 18:55
Pizza Serializer
export class PizzaSerializer {
fromJson(json: any): Pizza {
const pizza = new Pizza();
pizza.id = json.id;
pizza.name = json.name;
pizza.cookedOn = moment(json.cookedOn, 'mm-dd-yyyy hh:mm');
return pizza;
}
@krishna-acondy
krishna-acondy / pizza.service.ts
Last active May 23, 2019 05:17
Advanced Pizza Service with Serializer
export class PizzaService {
private url='http://pizzaService';
private endpoint = 'pizzas';
private serializer = new PizzaSerializer();
constructor(
protected httpClient: HttpClient) {}
public create(pizza: Pizza): Observable<Pizza> {
@krishna-acondy
krishna-acondy / serializer.ts
Created February 11, 2018 19:50
Serializer Interface
export interface Serializer {
fromJson(json: any): Resource;
toJson(resource: Resource): any;
}
@krishna-acondy
krishna-acondy / resource.service.ts
Last active October 30, 2021 04:03
Generic Angular HTTP Resource Service
export class ResourceService<T extends Resource> {
constructor(
private httpClient: HttpClient,
private url: string,
private endpoint: string,
private serializer: Serializer) {}
public create(item: T): Observable<T> {
return this.httpClient
.post<T>(`${this.url}/${this.endpoint}`, this.serializer.toJson(item))
@krishna-acondy
krishna-acondy / pizza.service.ts
Created February 11, 2018 20:08
Generic Pizza Service
export class PizzaService extends ResourceService<Pizza> {
constructor(httpClient: HttpClient) {
super(
httpClient,
'http://pizzaService',
'pizzas',
new PizzaSerializer());
}
}
@krishna-acondy
krishna-acondy / subresource.service.ts
Last active June 8, 2020 19:20
Generic Angular Sub-Resource Service
export class SubResourceService<T extends Resource> {
constructor(
private httpClient: HttpClient,
private url: string,
private parentEndpoint: string,
private endpoint: string,
private serializer: Serializer) { }
public create(item: T): Observable<T> {
return this.httpClient
@krishna-acondy
krishna-acondy / query-options.ts
Created April 4, 2018 14:21
Query Options Type
export interface QueryBuilder {
toQueryMap: () => Map<string, string>;
toQueryString: () => string;
}
export class QueryOptions implements QueryBuilder {
public pageNumber: number;
public pageSize: number;
constructor() {
@krishna-acondy
krishna-acondy / todos.component.spec.ts
Last active May 23, 2019 21:26
Angular Unit Testing - TodosComponent
describe('TodosComponent', () => {
let component: TodosComponent;
let fixture: ComponentFixture<TodosComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ TodosComponent ],
providers: [
{ provide: TodoService, useClass: TodoServiceMock }
]
@krishna-acondy
krishna-acondy / dashboard.component.spec.ts
Created May 23, 2019 20:47
Angular Unit Testing - DashboardComponent
describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
DashboardComponent,
TodosComponent
],