-
-
Save StalkAltan/b13d6dc740504d73a377f57c52a7897f to your computer and use it in GitHub Desktop.
angular service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Resource { | |
readonly id: number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Resource} from "./resource.model"; | |
export abstract class ResourceService<T extends Resource> { | |
constructor(protected http: HttpClient, protected resourceUrl: string) { } | |
find(query: object): Observable<T[]> { | |
let options = { params: new HttpParams() }; | |
for (let key, value of Object.entries(query)) { | |
options.params.set(key, value); | |
} | |
return this.http.get<T[]>(this.resourceUrl, options); | |
} | |
findById(id: string): Observable<T> { | |
return this.http.get<T>(`${this.resourceUrl}/${id}`); | |
} | |
create(resource: T): Observable<T> { | |
let options = { | |
headers: new HttpHeaders({ 'Content-Type': 'application/json' }) | |
}; | |
return this.http.post<T>(this.resourceUrl, resource, options); | |
} | |
update(resource: T): Observable<T> { | |
let id = resource.id; | |
let options = { | |
headers: new HttpHeaders({ 'Content-Type': 'application/json' }) | |
}; | |
return this.http.put<T>(`${this.resourceUrl}/${id}`, resource, options); | |
} | |
save(resource: T): Observable<T> { | |
if (resource.id) { | |
return this.update(resource); | |
} else { | |
return this.create(resource); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="widgets-list-container"> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { Widget } from './widget.model'; | |
import { WidgetService } from './widget.service'; | |
@Component({ | |
templateUrl: './widget.component.html', | |
styleUrls: ['./widget.component.scss'], | |
}) | |
export class WidgetsComponent implements OnInit { | |
constructor( | |
widgetService: WidgetService | |
) {} | |
public ngOnInit(): void { | |
this.widgetService.find({ }).subscribe((widgets) => { | |
this.widgets = widgets; | |
}); | |
} | |
public createWidget(widget: Widget): { | |
this.widgetService.save(widget).subscribe((widget) => { | |
this.widgets.unshift(widget); | |
}); | |
} | |
public updateWidget(widget: Widget): { | |
this.widgetService.save(widget).subscribe((widget) => { | |
let index; | |
for (let i=0; i<this.widgets.length; i++) { | |
if (this.widgets[i].id = widget.id) { | |
return index = i; | |
break; | |
} | |
} | |
if (index) { | |
this.widgets[index] = widget; | |
} else { | |
this.widgets.unshift(widget); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Resource} from "./resource.model"; | |
export interface Widget extends Resource { | |
name: string; | |
count: number; | |
createdAt: Date; | |
updatedAt: Date; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
import { Widget } from './widget.model.ts'; | |
import { ResourceService } from "./resource.service"; | |
@Injectable() | |
export class WidgetService extends ResourceService<Widget>{ | |
constructor(http: HttpClient) { | |
super(http, '/api/widgets'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment