Add task delete + form for add and update

This commit is contained in:
jfhr 2020-06-03 16:21:47 +02:00
parent f52485761e
commit 0bd4205d02
No known key found for this signature in database
GPG Key ID: 8873416D8E4CEF6B
8 changed files with 126 additions and 25 deletions

View File

@ -7,11 +7,13 @@ import { AppComponent } from './app.component';
import { TaskListComponent } from './task-list/task-list.component'; import { TaskListComponent } from './task-list/task-list.component';
import { BackendService } from './services/backend.service'; import { BackendService } from './services/backend.service';
import { TaskFormComponent } from './task-form/task-form.component';
@NgModule({ @NgModule({
declarations: [ declarations: [
AppComponent, AppComponent,
TaskListComponent TaskListComponent,
TaskFormComponent
], ],
imports: [ imports: [
BrowserModule, BrowserModule,

View File

@ -1,5 +1,5 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http'; import {HttpClient, HttpResponse} from '@angular/common/http';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {environment} from '../../environments/environment'; import {environment} from '../../environments/environment';
@ -9,42 +9,43 @@ export class BackendService {
constructor(private httpClient: HttpClient) {} constructor(private httpClient: HttpClient) {}
public getTasks(): Observable<Task[]> { public getTasks(): Observable<HttpResponse<Task[]>> {
const url = `${environment.apiUrl}/tasks`; const url = `${environment.apiUrl}/tasks`;
return this.httpClient.get<Task[]>(url); return this.httpClient.get<Task[]>(url, { observe: 'response' });
} }
public getTask(id: number): Observable<Task> { public getTask(id: number): Observable<HttpResponse<Task>> {
const url = `${environment.apiUrl}/tasks/${id}`; const url = `${environment.apiUrl}/tasks/${id}`;
return this.httpClient.get<Task>(url); return this.httpClient.get<Task>(url, { observe: 'response' });
} }
public postTask(task: Task): Observable<Task> { public postTask(task: Task): Observable<HttpResponse<Task>> {
const url = `${environment.apiUrl}/tasks`; const url = `${environment.apiUrl}/tasks`;
return this.httpClient.post<Task>(url, task); return this.httpClient.post<Task>(url, task, { observe: 'response' });
} }
public putTask(task: Task): Observable<any> { public putTask(task: Task): Observable<HttpResponse<any>> {
const url = `${environment.apiUrl}/tasks/${task.id}`; 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<any> { public deleteTask(task: Task): Observable<HttpResponse<any>> {
const url = `${environment.apiUrl}/tasks/${task.id}`; 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 //enum prio{high, middle, low}; --> noch nicht im backend
export class Task{ export interface Task {
id: number; id?: number;
titel: string; title: string;
//prio; --> noch nicht im backend //prio; --> noch nicht im backend
inhalt: string; content?: string;
status: number; status?: number;
kategorie: number; category?: number;
bearbeiter: number; assignedto?: number;
zugeordneterSprint: number; sprint?: number;
projekt: number; project?: number;
userstory: number;} userstory?: number;
}

View File

@ -0,0 +1,14 @@
<form>
<div class="form-group">
<label for="Title">Titel</label>
<input type="text" class="form-control" id="Title" required [(ngModel)]="title">
</div>
<div class="form-group">
<label for="Inhalt">Inhalt</label>
<input type="text" class="form-control" id="Content" required [(ngModel)]="content">
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>

View File

@ -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<TaskFormComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ TaskFormComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TaskFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -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;
}
}

View File

@ -1,7 +1,8 @@
<ul> <ul>
<li *ngFor="let task of tasks"> <li *ngFor="let task of tasks">
<span>Titel: {{task.titel}}</span> <span>Titel: {{task.title}}</span>
<br/> <br/>
<span>Inhalt: {{task.inhalt}}</span> <span>Inhalt: {{task.content}}</span>
<button (click)="deleteTask(task)">Löschen</button>
</li> </li>
</ul> </ul>

View File

@ -11,10 +11,29 @@ export class TaskListComponent implements OnInit {
public tasks: Task[] = []; public tasks: Task[] = [];
constructor(private backendService: BackendService) { 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 { 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);
}
}
} }