Skip to content

Instantly share code, notes, and snippets.

@henryruhs
Last active April 20, 2022 11:42
Show Gist options
  • Save henryruhs/67ae0012247c2ab4408f6088051c5de0 to your computer and use it in GitHub Desktop.
Save henryruhs/67ae0012247c2ab4408f6088051c5de0 to your computer and use it in GitHub Desktop.
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Options } from './common.interface';
import { Id } from './common.type';
import { RequestBody, ResponseBody } from './example.interface';
import { environment } from '@env';
@Injectable()
export class ExampleService
{
protected apiUrl : string = environment.apiUrl;
protected endpoint : string = environment.routes.example;
constructor(protected http : HttpClient)
{
}
create(body : RequestBody, options? : Options) : Observable<ResponseBody>
{
return this.http.post<ResponseBody>(this.apiUrl + this.endpoint, body, options);
}
read(id : Id, options? : Options) : Observable<ResponseBody>
{
return this.http.get<ResponseBody>(this.apiUrl + this.endpoint + '/' + id, options);
}
find(options? : Options) : Observable<ResponseBody[]>
{
return this.http.get<ResponseBody[]>(this.apiUrl + this.endpoint, options);
}
update(id : Id, body : RequestBody, options? : Options) : Observable<ResponseBody>
{
return this.http.put<ResponseBody>(this.apiUrl + this.endpoint + '/' + id, body, options);
}
patch(id : Id, body : RequestBody, options? : Options) : Observable<ResponseBody>
{
return this.http.patch<ResponseBody>(this.apiUrl + this.endpoint + '/' + id, body, options);
}
delete(id : Id, options? : Options) : Observable<ResponseBody>
{
return this.http.delete<ResponseBody>(this.apiUrl + this.endpoint + '/' + id, options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment