Skip to content

Commit e49774c

Browse files
committed
solution
1 parent 1ee9209 commit e49774c

4 files changed

+97
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.error-message {
2+
margin-bottom: 20px;
3+
color: darkred;
4+
}

src/app/flight-edit/flight-edit.component.html

+22
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,25 @@ <h1>Flight Edit</h1>
22

33
<p>Id: {{ id() }}</p>
44
<p>ShowDetails: {{ showDetails() }}</p>
5+
6+
@if (error()) {
7+
<div class="error-message">{{ error() }}</div>
8+
}
9+
10+
<form>
11+
<div class="form-group">
12+
<label>From:</label>
13+
<input [(ngModel)]="from" name="from" class="form-control" />
14+
</div>
15+
<div class="form-group">
16+
<label>From:</label>
17+
<input [(ngModel)]="to" name="to" class="form-control" />
18+
</div>
19+
<div class="form-group">
20+
<label>From:</label>
21+
<input [(ngModel)]="date" name="date" size="40" class="form-control" />
22+
</div>
23+
<div class="form-group">
24+
<button class="btn btn-default" (click)="save()">Save</button>
25+
</div>
26+
</form>
+51-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,64 @@
1-
import { booleanAttribute, Component, input, numberAttribute, OnInit } from '@angular/core';
1+
import {
2+
booleanAttribute,
3+
Component,
4+
inject,
5+
input,
6+
numberAttribute,
7+
OnInit,
8+
signal,
9+
} from '@angular/core';
10+
import { FlightService } from '../flight.service';
11+
import { Flight } from '../data/flight';
12+
import { FormsModule } from '@angular/forms';
213

314
@Component({
415
selector: 'app-flight-edit',
5-
imports: [],
16+
imports: [FormsModule],
617
templateUrl: './flight-edit.component.html',
7-
styleUrl: './flight-edit.component.css'
18+
styleUrl: './flight-edit.component.css',
819
})
920
export class FlightEditComponent implements OnInit {
21+
private flightService = inject(FlightService);
1022

1123
id = input.required({ transform: numberAttribute });
1224
showDetails = input.required({ transform: booleanAttribute });
1325

26+
from = signal('');
27+
to = signal('');
28+
date = signal('');
29+
30+
error = signal('');
31+
1432
ngOnInit(): void {
15-
console.log('id', this.id())
33+
this.flightService.findById(this.id()).subscribe((flight) => {
34+
this.setFlight(flight);
35+
});
36+
}
37+
38+
private setFlight(flight: Flight) {
39+
this.from.set(flight.from);
40+
this.to.set(flight.to);
41+
this.date.set(flight.date);
42+
}
43+
44+
save(): void {
45+
this.error.set('');
46+
47+
const flight: Flight = {
48+
id: this.id(),
49+
from: this.from(),
50+
to: this.to(),
51+
date: this.date(),
52+
};
53+
54+
this.flightService.save(flight).subscribe({
55+
next: (flight) => {
56+
this.setFlight(flight);
57+
},
58+
error: (err) => {
59+
this.error.set(err.error ?? err.message ?? err);
60+
throw err;
61+
}
62+
});
1663
}
1764
}

src/app/flight.service.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { inject, Injectable } from '@angular/core';
33
import { Observable } from 'rxjs';
44
import { Flight } from './data/flight';
55

6+
const url = 'https://demo.angulararchitects.io/api/flight';
7+
68
@Injectable({
79
providedIn: 'root'
810
})
@@ -16,9 +18,25 @@ export class FlightService {
1618
accept: 'application/json'
1719
};
1820

19-
const url = 'https://demo.angulararchitects.io/api/flight';
20-
2121
return this.http.get<Flight[]>(url, { params, headers });
2222
}
2323

24+
findById(id: number): Observable<Flight> {
25+
const params = { id };
26+
27+
const headers = {
28+
accept: 'application/json'
29+
};
30+
31+
return this.http.get<Flight>(url, { params, headers });
32+
}
33+
34+
save(flight: Flight): Observable<Flight> {
35+
const headers = {
36+
accept: 'application/json'
37+
};
38+
39+
return this.http.post<Flight>(url, flight, { headers });
40+
}
41+
2442
}

0 commit comments

Comments
 (0)