Skip to content

Commit edfb89e

Browse files
committed
feat: add AppComponent inputs/outputs
1 parent 29ff459 commit edfb89e

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

src/app/app.component.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import {Component} from "@angular/core";
1+
import {Component, Input, Output} from "@angular/core";
22
import {AuthService} from "./services/auth/auth.service";
3+
import {TrainrunService} from "./services/data/trainrun.service";
4+
import {TrainrunSectionService} from "./services/data/trainrunsection.service";
5+
import {DataService} from "./services/data/data.service";
36
import {environment} from "../environments/environment";
47
import packageJson from "../../package.json";
58
import {Observable} from "rxjs";
69
import {ProjectDto} from "./api/generated";
10+
import {NetzgrafikDto} from "./data-structures/business.data.structures";
711

812
@Component({
913
selector: "sbb-root",
@@ -33,7 +37,7 @@ export class AppComponent {
3337
return this.authService.claims?.email;
3438
}
3539

36-
constructor(private authService: AuthService) {
40+
constructor(private authService: AuthService, private dataService: DataService, private trainrunService: TrainrunService, private trainrunSectionService: TrainrunSectionService) {
3741
if (!this.disableBackend) {
3842
this.authenticated = authService.initialized;
3943
}
@@ -44,4 +48,18 @@ export class AppComponent {
4448
this.authService.logOut();
4549
}
4650
}
51+
52+
@Input()
53+
get netzgrafikDto() {
54+
return this.dataService.getNetzgrafikDto();
55+
}
56+
set netzgrafikDto(netzgrafikDto: NetzgrafikDto) {
57+
this.dataService.loadNetzgrafikDto(netzgrafikDto);
58+
}
59+
60+
@Output()
61+
trainrunOperation = this.trainrunService.operation;
62+
63+
@Output()
64+
trainrunSectionOperation = this.trainrunSectionService.operation;
4765
}

src/app/models/operation.model.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {Trainrun} from "./trainrun.model";
2+
import {TrainrunSection} from "./trainrunsection.model";
3+
4+
enum OperationType {
5+
create = "create",
6+
update = "update",
7+
delete = "delete"
8+
}
9+
10+
export abstract class Operation{
11+
type: string;
12+
}
13+
14+
export class CreateTrainrunOperation extends Operation {
15+
readonly trainrunSection: TrainrunSection;
16+
17+
constructor(trainrunSection: TrainrunSection){
18+
super();
19+
this.type = OperationType.create;
20+
this.trainrunSection = trainrunSection;
21+
}
22+
}
23+
24+
export class UpdateTrainrunSectionsOperation extends Operation {
25+
readonly trainrunSections: TrainrunSection[];
26+
27+
constructor(trainrunSections: TrainrunSection[]){
28+
super();
29+
this.type = OperationType.update;
30+
this.trainrunSections = trainrunSections;
31+
}
32+
}
33+
34+
export class DeleteTrainrunOperation extends Operation {
35+
readonly trainrun: Trainrun;
36+
37+
constructor(trainrun: Trainrun){
38+
super();
39+
this.type = OperationType.delete;
40+
this.trainrun = trainrun;
41+
}
42+
}

src/app/services/data/trainrun.service.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
TrainrunFrequency,
99
TrainrunTimeCategory,
1010
} from "../../data-structures/business.data.structures";
11-
import {Injectable} from "@angular/core";
11+
import {EventEmitter, Injectable} from "@angular/core";
1212
import {BehaviorSubject} from "rxjs";
1313
import {NodeService} from "./node.service";
1414
import {TrainrunSectionService} from "./trainrunsection.service";
@@ -23,6 +23,7 @@ import {FilterService} from "../ui/filter.service";
2323
import {Transition} from "../../models/transition.model";
2424
import {Port} from "../../models/port.model";
2525
import {Connection} from "../../models/connection.model";
26+
import {DeleteTrainrunOperation, Operation} from "../../models/operation.model";
2627

2728
@Injectable({
2829
providedIn: "root",
@@ -34,6 +35,8 @@ export class TrainrunService {
3435

3536
trainrunsStore: { trainruns: Trainrun[] } = {trainruns: []}; // store the data in memory
3637

38+
readonly operation = new EventEmitter<Operation>();
39+
3740
private dataService: DataService = null;
3841
private nodeService: NodeService = null;
3942
private trainrunSectionService: TrainrunSectionService = null;
@@ -180,6 +183,7 @@ export class TrainrunService {
180183
if (enforceUpdate) {
181184
this.trainrunsUpdated();
182185
}
186+
this.operation.emit(new DeleteTrainrunOperation(trainrun));
183187
}
184188

185189
getSelectedTrainrun(): Trainrun {

src/app/services/data/trainrunsection.service.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
TrainrunSectionDto,
66
} from "../../data-structures/business.data.structures";
77
import {Node} from "../../models/node.model";
8-
import {Injectable, OnDestroy} from "@angular/core";
8+
import {Injectable, OnDestroy, EventEmitter} from "@angular/core";
99
import {BehaviorSubject, Subject} from "rxjs";
1010
import {TrainrunService} from "./trainrun.service";
1111
import {NodeService} from "./node.service";
@@ -22,6 +22,7 @@ import {Transition} from "../../models/transition.model";
2222
import {takeUntil} from "rxjs/operators";
2323
import {FilterService} from "../ui/filter.service";
2424
import {TrainrunSectionNodePair} from "../util/trainrun.iterator";
25+
import {CreateTrainrunOperation, Operation, UpdateTrainrunSectionsOperation} from "../../models/operation.model";
2526

2627
interface DepartureAndArrivalTimes {
2728
nodeFromDepartureTime: number;
@@ -46,6 +47,8 @@ export class TrainrunSectionService implements OnDestroy {
4647
trainrunSections: [],
4748
}; // store the data in memory
4849

50+
readonly operation = new EventEmitter<Operation>();
51+
4952
informSelectedTrainrunClickSubject =
5053
new BehaviorSubject<InformSelectedTrainrunClick>({
5154
trainrunSectionId: undefined,
@@ -668,6 +671,8 @@ export class TrainrunSectionService implements OnDestroy {
668671

669672
createTrainrunSection(sourceNodeId: number, targetNodeId: number, retrieveTravelTimeFromEdge: boolean = false) {
670673
const trainrunSection: TrainrunSection = new TrainrunSection();
674+
const initialTrainrunsLength = this.trainrunService.trainrunsStore.trainruns.length;
675+
671676
trainrunSection.setTrainrun(
672677
this.trainrunService.getSelectedOrNewTrainrun(),
673678
);
@@ -682,11 +687,6 @@ export class TrainrunSectionService implements OnDestroy {
682687
const targetNode = this.nodeService.getNodeFromId(targetNodeId);
683688
trainrunSection.setSourceAndTargetNodeReference(sourceNode, targetNode);
684689
this.trainrunSectionsStore.trainrunSections.push(trainrunSection);
685-
this.logger.log(
686-
"create new trainrunSection between nodes",
687-
sourceNode.getBetriebspunktName(),
688-
targetNode.getBetriebspunktName(),
689-
);
690690

691691
this.handleNodeAndTrainrunSectionDetails(
692692
sourceNode,
@@ -698,6 +698,12 @@ export class TrainrunSectionService implements OnDestroy {
698698
this.propagateTimesForNewTrainrunSection(trainrunSection);
699699
//this.trainrunSectionsUpdated();
700700
this.trainrunService.trainrunsUpdated();
701+
702+
if (initialTrainrunsLength !== this.trainrunService.trainrunsStore.trainruns.length) {
703+
this.operation.emit(new CreateTrainrunOperation(trainrunSection));
704+
} else {
705+
this.operation.emit(new UpdateTrainrunSectionsOperation(this.getAllTrainrunSectionsForTrainrun(trainrunSection.getTrainrunId())));
706+
}
701707
}
702708

703709
reconnectTrainrunSection(

0 commit comments

Comments
 (0)