From c83e14d3b0fc09cdfce38f94c800105283a5a3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeppe=20Elkj=C3=A6r=20J=C3=B8rgensen?= Date: Mon, 3 Aug 2020 15:18:30 +0200 Subject: [PATCH] IOT-2: Add application service + Refactor. --- .../_services/application.service.spec.ts | 31 ++++++++++++++++ .../shared/_services/application.service.ts | 35 +++++++++++++++++++ .../form-body-application.component.ts | 20 +++++------ .../applications-table.component.ts | 19 +++++----- 4 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 src/app/shared/_services/application.service.spec.ts create mode 100644 src/app/shared/_services/application.service.ts diff --git a/src/app/shared/_services/application.service.spec.ts b/src/app/shared/_services/application.service.spec.ts new file mode 100644 index 00000000..596d891e --- /dev/null +++ b/src/app/shared/_services/application.service.spec.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@angular/core'; +import { Observable, Subscription } from 'rxjs'; +import { RestService } from './rest.service'; +import { Application } from 'src/app/models/application'; + +@Injectable({ + providedIn: 'root' +}) +export class ApplicationService { + + public applicationsSubscription: Subscription; + private applicaitonUrl: string = 'application' + + constructor( + private restService: RestService, + + ) { } + + public getApplication(id: number): Observable { + return this.restService.get(this.applicaitonUrl, {}, id) + } + + public updateApplication(id: number): Observable { + return this.restService.update(this.applicaitonUrl, {}, id) + } + + public postApplication(application: Application): Observable { + return this.restService.post(this.applicaitonUrl, {}, application) + } + +} diff --git a/src/app/shared/_services/application.service.ts b/src/app/shared/_services/application.service.ts new file mode 100644 index 00000000..6e6c70aa --- /dev/null +++ b/src/app/shared/_services/application.service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { Observable, Subscription } from 'rxjs'; +import { RestService } from './rest.service'; +import { Application } from 'src/app/models/application'; + +@Injectable({ + providedIn: 'root' +}) +export class ApplicationService { + + public applicationsSubscription: Subscription; + private applicaitonUrl: string = 'application' + + constructor( + private restService: RestService, + + ) { } + + public get(id: number = null, params = {}): Observable { + return this.restService.get(this.applicaitonUrl, params, id) + } + + public update(json, id: number): Observable { + return this.restService.replace(this.applicaitonUrl, json, id) + } + + public post(json): Observable { + return this.restService.post(this.applicaitonUrl, json) + } + + public delete(id: number): Observable { + return this.restService.delete(this.applicaitonUrl, id) + } + +} diff --git a/src/app/shared/form/form-body-application/form-body-application.component.ts b/src/app/shared/form/form-body-application/form-body-application.component.ts index 51b42d3f..d8da55af 100644 --- a/src/app/shared/form/form-body-application/form-body-application.component.ts +++ b/src/app/shared/form/form-body-application/form-body-application.component.ts @@ -9,6 +9,7 @@ import { RestService } from '../../_services/rest.service'; import { QuestionBase } from '../question-base'; import { Application } from 'src/app/models/application'; import { TranslateService } from '@ngx-translate/core'; +import { ApplicationService } from '../../_services/application.service'; @Component({ selector: 'app-form-body-application', @@ -26,10 +27,10 @@ export class FormBodyApplicationComponent implements OnInit, OnDestroy { constructor( private qcs: QuestionControlService, - private restService: RestService, private route: ActivatedRoute, public translate: TranslateService, - private router: Router + private router: Router, + private applicationService: ApplicationService, ) {} ngOnInit(): void { @@ -40,10 +41,9 @@ export class FormBodyApplicationComponent implements OnInit, OnDestroy { this.getApplication(this.id); } } - + getApplication(id: number): void { - this.applicationsSubscription = this.restService - .get('application', {}, id) + this.applicationService.get(id) .subscribe((application: Application) => { this.form.controls['name'].setValue(application.name); this.form.controls['description'].setValue( @@ -58,13 +58,12 @@ export class FormBodyApplicationComponent implements OnInit, OnDestroy { if (this.id) { this.updateApplication(this.id); } else { - this.postApplication(); + this.postApplication(this.payLoad); } } updateApplication(id: number): void { - this.restService - .replace('application', JSON.stringify(this.form.getRawValue()), id) + this.applicationService.update(this.payLoad, id) .subscribe((response) => { if (response.ok) { this.router.navigateByUrl('/mine-applikationer'); @@ -72,9 +71,8 @@ export class FormBodyApplicationComponent implements OnInit, OnDestroy { }); } - postApplication(): void { - this.restService - .create('application', JSON.stringify(this.form.getRawValue())) + postApplication(payload): void { + this.applicationService.post(payload) .subscribe((response) => { console.log(response); if (response.ok) { diff --git a/src/app/views/mine-applikationer/applications-table/applications-table.component.ts b/src/app/views/mine-applikationer/applications-table/applications-table.component.ts index 939269bf..32c5c9a0 100644 --- a/src/app/views/mine-applikationer/applications-table/applications-table.component.ts +++ b/src/app/views/mine-applikationer/applications-table/applications-table.component.ts @@ -5,6 +5,7 @@ import { RestService } from 'src/app/shared/_services/rest.service'; import { TranslateService } from '@ngx-translate/core'; import { Application } from 'src/app/models/application'; import { Sort } from 'src/app/models/sort'; +import { ApplicationService } from 'src/app/shared/_services/application.service'; @Component({ selector: 'app-applications-table', @@ -22,7 +23,8 @@ export class ApplicationsTableComponent implements OnInit, OnChanges, OnDestroy constructor( private restService: RestService, - public translate: TranslateService + public translate: TranslateService, + private applicationService: ApplicationService ) { translate.use('da'); } @@ -37,8 +39,8 @@ export class ApplicationsTableComponent implements OnInit, OnChanges, OnDestroy } getApplications(): void { - this.applicationsSubscription = this.restService - .get('application', { + this.applicationsSubscription = this.applicationService + .get(null, { limit: this.pageLimit, offset: this.pageOffset * this.pageLimit, sort: this.selectedSortObject.dir, @@ -54,11 +56,12 @@ export class ApplicationsTableComponent implements OnInit, OnChanges, OnDestroy } deleteApplication(id: number) { - this.restService.delete('application', id).subscribe((response) => { - if (response.ok && response.body.affected > 0) { - this.getApplications(); - } - }); + this.applicationService.delete(id) + .subscribe((response) => { + if (response.ok && response.body.affected > 0) { + this.getApplications(); + } + }); } prevPage() {