Skip to content

Instantly share code, notes, and snippets.

@ScriptBytes
Created December 11, 2019 04:47
Show Gist options
  • Save ScriptBytes/a0c3c2cc245921f9fc533c8b379a1698 to your computer and use it in GitHub Desktop.
Save ScriptBytes/a0c3c2cc245921f9fc533c8b379a1698 to your computer and use it in GitHub Desktop.
VS Code snippet for creating an Angular CRUD service
{
"Angular CRUD Service": {
"prefix": "crud-service",
"body": [
"import { Injectable } from '@angular/core';",
"import { HttpClient } from '@angular/common/http';",
"import { Observable, throwError } from 'rxjs';",
"import { ${1:className} } from '@cook/models/${2:classFileName}.interface';",
"import { environment } from '@cook/environment/environment';",
"import { catchError } from 'rxjs/operators';",
"",
"@Injectable({",
" providedIn: 'root'",
"})",
"export class ${1:className}Service {",
" url = environment.apiServerName + environment.apiUrl + '${3:apiEndpoint}/';",
"",
" constructor(private http: HttpClient) { }",
"",
" get(): Observable<${1:className}[]> {",
" return this.http.get<${1:className}[]>(this.url)",
" .pipe(",
" catchError(() => {",
" return throwError('Error getting ${4:errorText}s');",
" })",
" );",
" }",
"",
" getById(id: string): Observable<${1:className}> {",
" return this.http.get<${1:className}>(this.url + id)",
" .pipe(",
" catchError(() => {",
" return throwError('Error getting ${4:errorText}');",
" })",
" );",
" }",
"",
" update(${5:parameterName}: ${1:className}): Observable<${1:className}> {",
" return this.http.put<${1:className}>(this.url + ${5:parameterName}.id, ${5:parameterName})",
" .pipe(",
" catchError(() => {",
" return throwError('Error saving ${4:errorText}');",
" })",
" );",
" }",
"",
" create(${5:parameterName}: ${1:className}): Observable<${1:className}> {",
" return this.http.post<${1:className}>(this.url, ${5:parameterName})",
" .pipe(",
" catchError(() => {",
" return throwError('Error creating ${4:errorText}');",
" })",
" );",
" }",
"",
" delete(id: string): Observable<void> {",
" return this.http.delete<void>(this.url + id)",
" .pipe(",
" catchError(() => {",
" return throwError('Error deleting ${4:errorText}');",
" })",
" );",
" }",
"}",
""
],
"description": "Creates an angular service for CRUD http calls"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment