From 0bd4205d026038a99399cc1ebc601176923bbe12 Mon Sep 17 00:00:00 2001 From: jfhr Date: Wed, 3 Jun 2020 16:21:47 +0200 Subject: [PATCH] Add task delete + form for add and update --- src/app/app.module.ts | 4 +- src/app/services/backend.service.ts | 43 ++++++++++--------- src/app/task-form/task-form.component.css | 0 src/app/task-form/task-form.component.html | 14 ++++++ src/app/task-form/task-form.component.spec.ts | 25 +++++++++++ src/app/task-form/task-form.component.ts | 39 +++++++++++++++++ src/app/task-list/task-list.component.html | 5 ++- src/app/task-list/task-list.component.ts | 21 ++++++++- 8 files changed, 126 insertions(+), 25 deletions(-) create mode 100644 src/app/task-form/task-form.component.css create mode 100644 src/app/task-form/task-form.component.html create mode 100644 src/app/task-form/task-form.component.spec.ts create mode 100644 src/app/task-form/task-form.component.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 69fef9d..3807b2f 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -7,11 +7,13 @@ import { AppComponent } from './app.component'; import { TaskListComponent } from './task-list/task-list.component'; import { BackendService } from './services/backend.service'; +import { TaskFormComponent } from './task-form/task-form.component'; @NgModule({ declarations: [ AppComponent, - TaskListComponent + TaskListComponent, + TaskFormComponent ], imports: [ BrowserModule, diff --git a/src/app/services/backend.service.ts b/src/app/services/backend.service.ts index 3943e85..d023a1b 100644 --- a/src/app/services/backend.service.ts +++ b/src/app/services/backend.service.ts @@ -1,5 +1,5 @@ import {Injectable} from '@angular/core'; -import {HttpClient} from '@angular/common/http'; +import {HttpClient, HttpResponse} from '@angular/common/http'; import {Observable} from 'rxjs'; import {environment} from '../../environments/environment'; @@ -9,42 +9,43 @@ export class BackendService { constructor(private httpClient: HttpClient) {} - public getTasks(): Observable { + public getTasks(): Observable> { const url = `${environment.apiUrl}/tasks`; - return this.httpClient.get(url); + return this.httpClient.get(url, { observe: 'response' }); } - public getTask(id: number): Observable { + public getTask(id: number): Observable> { const url = `${environment.apiUrl}/tasks/${id}`; - return this.httpClient.get(url); + return this.httpClient.get(url, { observe: 'response' }); } - public postTask(task: Task): Observable { + public postTask(task: Task): Observable> { const url = `${environment.apiUrl}/tasks`; - return this.httpClient.post(url, task); + return this.httpClient.post(url, task, { observe: 'response' }); } - public putTask(task: Task): Observable { + public putTask(task: Task): Observable> { const url = `${environment.apiUrl}/tasks/${task.id}`; - return this.httpClient.put(url, task); + return this.httpClient.put(url, task, { observe: 'response' }); } - public deleteTask(task: Task): Observable { + public deleteTask(task: Task): Observable> { const url = `${environment.apiUrl}/tasks/${task.id}`; - return this.httpClient.delete(url); + return this.httpClient.delete(url, {observe: 'response'}); } } //enum prio{high, middle, low}; --> noch nicht im backend -export class Task{ - id: number; - titel: string; +export interface Task { + id?: number; + title: string; //prio; --> noch nicht im backend - inhalt: string; - status: number; - kategorie: number; - bearbeiter: number; - zugeordneterSprint: number; - projekt: number; - userstory: number;} + content?: string; + status?: number; + category?: number; + assignedto?: number; + sprint?: number; + project?: number; + userstory?: number; +} diff --git a/src/app/task-form/task-form.component.css b/src/app/task-form/task-form.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/task-form/task-form.component.html b/src/app/task-form/task-form.component.html new file mode 100644 index 0000000..241c86b --- /dev/null +++ b/src/app/task-form/task-form.component.html @@ -0,0 +1,14 @@ +
+
+ + +
+ +
+ + +
+ + + +
\ No newline at end of file diff --git a/src/app/task-form/task-form.component.spec.ts b/src/app/task-form/task-form.component.spec.ts new file mode 100644 index 0000000..1a3a1d0 --- /dev/null +++ b/src/app/task-form/task-form.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { TaskFormComponent } from './task-form.component'; + +describe('TaskFormComponent', () => { + let component: TaskFormComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ TaskFormComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(TaskFormComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/task-form/task-form.component.ts b/src/app/task-form/task-form.component.ts new file mode 100644 index 0000000..10bcd4b --- /dev/null +++ b/src/app/task-form/task-form.component.ts @@ -0,0 +1,39 @@ +import { Component, OnInit, Input } from '@angular/core'; +import { BackendService, Task } from '../services/backend.service'; + +@Component({ + selector: 'app-task-form', + templateUrl: './task-form.component.html', + styleUrls: ['./task-form.component.css'] +}) +export class TaskFormComponent implements OnInit { + + public title: string; + public content: string; + + @Input() + public task: Task; + private submitted: boolean; + + constructor(private backendService: BackendService) { } + + ngOnInit(): void { + if (this.task !== null && this.task !== undefined) { + this.title = this.task.title; + this.content = this.task.content; + } + } + + onSubmit() { + if (this.task !== null && this.task !== undefined) { + this.task.title = this.title; + this.task.content = this.content; + this.backendService.putTask(this.task); + } + else { + this.task = { title: this.title, content: this.content }; + this.backendService.postTask(this.task); + } + this.submitted = true; + } +} diff --git a/src/app/task-list/task-list.component.html b/src/app/task-list/task-list.component.html index b10b466..0e31de6 100644 --- a/src/app/task-list/task-list.component.html +++ b/src/app/task-list/task-list.component.html @@ -1,7 +1,8 @@
  • - Titel: {{task.titel}} + Titel: {{task.title}}
    - Inhalt: {{task.inhalt}} + Inhalt: {{task.content}} +
diff --git a/src/app/task-list/task-list.component.ts b/src/app/task-list/task-list.component.ts index 8d7b0d7..734d269 100644 --- a/src/app/task-list/task-list.component.ts +++ b/src/app/task-list/task-list.component.ts @@ -11,10 +11,29 @@ export class TaskListComponent implements OnInit { public tasks: Task[] = []; constructor(private backendService: BackendService) { - backendService.getTasks().subscribe(t => this.tasks.push(...t)); + backendService.getTasks().subscribe(response => { + if (response.status > 399) { + alert('Fehler'); + } + else { + this.tasks.push(...response.body); + } + }); } ngOnInit(): void { } + public deleteTask(task: Task) { + this.backendService.deleteTask(task).subscribe(response => { + if (response.status > 399) { + alert('Fehler'); + } + }); + const index = this.tasks.indexOf(task); + if (index !== -1) { + this.tasks.splice(index, 1); + } + } + }