Skip to content

Commit 807fefb

Browse files
committed
change ID from string to number
1 parent 12cf76c commit 807fefb

9 files changed

Lines changed: 59 additions & 31 deletions

src/app/class/class.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div *ngIf="!id">
1+
<div *ngIf="overview()">
22
<div class="container">
33
<h1>Klassenübersicht</h1>
44
<div class="row row-spacing">
@@ -48,7 +48,7 @@ <h4 class="modal-title">Klasse hinzufügen</h4>
4848
</div>
4949

5050

51-
<div *ngIf="id">
51+
<div *ngIf="!overview()">
5252
<div class="container">
5353
<h1>Klasse bearbeiten</h1>
5454
<form class="form-horizontal" (ngSubmit)="update(id)" #modifyClassForm="ngForm">

src/app/class/class.component.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
1010
})
1111
export class ClassComponent implements OnInit, OnDestroy {
1212

13-
private id: string;
13+
private id: number;
1414
private sub: any;
1515

1616
model = {name: "", grade: 0, classTeacher: ""};
@@ -23,8 +23,8 @@ export class ClassComponent implements OnInit, OnDestroy {
2323

2424
ngOnInit() {
2525
this.sub = this.route.params.subscribe(params => {
26-
this.id = params['id']; //get ID from route parameter
27-
if (this.id) {
26+
this.id = +params['id']; //get ID from route parameter
27+
if (!this.overview()) {
2828
this.model = this.dataService.getClass(this.id);
2929
}
3030
});
@@ -38,6 +38,10 @@ export class ClassComponent implements OnInit, OnDestroy {
3838
return !(this.model.name && this.model.grade);
3939
}
4040

41+
overview() {
42+
return isNaN(this.id);
43+
}
44+
4145
@ViewChild('addClass') addClass;
4246
add() {
4347
this.modalService.open(this.addClass).result.then((res) => {

src/app/data.service.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ export class DataService {
4646
{id: 2, firstName: 'Schüler', lastName: '3', classId: 1},
4747
{id: 3, firstName: 'Schüler', lastName: '4', classId: 2},
4848
{id: 4, firstName: 'Schüler', lastName: '5', classId: 3},
49+
]);
50+
this.saveLocalstorage('notes', [
51+
{id: 0, pupilId: 0, teacherId: 1, text: "Hat Mitschüler geschlagen.", date: new Date(1505400000000)},
52+
{id: 1, pupilId: 0, teacherId: 2, text: "Hat Hausaufgaben 3 Mal nicht gemacht.", date: new Date(1505300000000)},
53+
{id: 2, pupilId: 1, teacherId: 1, text: "Hat Tafel mit Edding bemalt.", date: new Date(1505200000000)},
54+
{id: 3, pupilId: 1, teacherId: 2, text: "Hat Druckschrift anstatt Schreibschrift geschrieben. Strafarbeit: 5 Seiten Schreibschrift mit Füller.", date: new Date(1505100000000)},
55+
{id: 4, pupilId: 1, teacherId: 1, text: "Hat \"Hosenscheißer\" zu Mitschüler gesagt.", date: new Date(1505000000000)},
56+
{id: 5, pupilId: 3, teacherId: 3, text: "Schlägt Mitschülerin ins Gesicht.", date: new Date(1504900000000)},
4957
])
5058
}
5159

@@ -121,7 +129,7 @@ export class DataService {
121129
updateTeacher(id, fName, lName, mail) {
122130
let t = this.getTeachers();
123131
// password unset due to asynchronous encryption
124-
t[id] = {id: id, firstName: fName, lastName: lName, mail: mail, pass: ''};
132+
t[id] = {id: +id, firstName: fName, lastName: lName, mail: mail, pass: ''};
125133
this.saveLocalstorage('teachers', t);
126134
}
127135

@@ -161,7 +169,7 @@ export class DataService {
161169

162170
updateClass(id, name, grade) {
163171
let c = this.getClasses();
164-
c[id] = {id: id, name: name, grade: grade};
172+
c[id] = {id: +id, name: name, grade: grade};
165173
this.saveLocalstorage('classes', c);
166174
}
167175

@@ -198,14 +206,14 @@ export class DataService {
198206
let pupils = this.getPupils();
199207
let id = Math.max.apply(this, pupils.map(e => e.id)) + 1; //generate new id
200208
if (id < 0) id = 0; //set id to zero, if no previous id was found
201-
let p = {id: id, firstName: firstName, lastName: lastName, classId: classId};
209+
let p = {id: id, firstName: firstName, lastName: lastName, classId: +classId};
202210
pupils.push(p);
203211
this.saveLocalstorage('pupils', pupils);
204212
}
205213

206214
updatePupil(id, firstName, lastName, classId) {
207215
let p = this.getPupils();
208-
p[id] = {id: id, firstName: firstName, lastName: lastName, classId: classId};
216+
p[id] = {id: +id, firstName: firstName, lastName: lastName, classId: +classId};
209217
this.saveLocalstorage('pupils', p);
210218
}
211219

@@ -228,7 +236,8 @@ export class DataService {
228236
getNotes() {
229237
let ret = this.getLocalstorage('notes');
230238
if (!ret) { //no data found in localstorage
231-
return [];
239+
this.generateInitialData();
240+
return this.getNotes();
232241
}
233242
else {
234243
ret = ret.map(elem => {
@@ -239,7 +248,6 @@ export class DataService {
239248
this.notes = ret;
240249
return this.notes;
241250
}
242-
243251
}
244252

245253
getNote(id) {
@@ -275,14 +283,14 @@ export class DataService {
275283
let notes = this.getNotes();
276284
let id = Math.max.apply(this, notes.map(e => e.id)) + 1; //generate new id
277285
if (id < 0) id = 0; //set id to zero, if no previous id was found
278-
let n = {id: id, pupilId: pupilId, teacherId: teacherId, text: text, date: date};
286+
let n = {id: id, pupilId: +pupilId, teacherId: +teacherId, text: text, date: date};
279287
notes.push(n);
280288
this.saveLocalstorage('notes', notes);
281289
}
282290

283291
updateNote(id, pupilId, teacherId, text, date) {
284292
let n = this.getNotes();
285-
n[id] = {id: id, pupilId: pupilId, teacherId: teacherId, text: text, date: date};
293+
n[id] = {id: +id, pupilId: +pupilId, teacherId: +teacherId, text: text, date: date};
286294
this.saveLocalstorage('notes', n);
287295
}
288296

src/app/notes/notes.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div *ngIf="!id">
1+
<div *ngIf="overview()">
22
<div class="container">
33
<h1 *ngIf="!pupilId">Einträgeübersicht</h1>
44
<h1 *ngIf="pupilId" >Einträgeübersicht für {{dataService.getPupilName(pupilId)}}</h1>
@@ -57,7 +57,7 @@ <h4 class="modal-title">Eintrag hinzufügen</h4>
5757
</div>
5858

5959

60-
<div *ngIf="id">
60+
<div *ngIf="!overview()">
6161
<div class="container">
6262
<h1>Eintrag bearbeiten</h1>
6363
<form class="form-horizontal" (ngSubmit)="update(id)" #modifyNoteForm="ngForm">

src/app/notes/notes.component.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {Location} from "@angular/common";
1111
})
1212
export class NotesComponent implements OnInit, OnDestroy {
1313

14-
private id: string;
15-
private pupilId: string;
14+
private id: number;
15+
private pupilId: number;
1616
private sub: any;
1717
notes: any;
1818
interval: any;
@@ -28,12 +28,13 @@ export class NotesComponent implements OnInit, OnDestroy {
2828

2929
ngOnInit() {
3030
this.sub = this.route.params.subscribe(params => {
31-
this.id = params['id']; //get ID from route parameter
32-
if (this.id) {
31+
this.id = +params['id']; //get ID from route parameter
32+
if (!this.overview()) {
3333
this.model = this.dataService.getNote(this.id);
34-
this.convertToModelDate(this.model.date);
34+
if(this.model)
35+
this.convertToModelDate(this.model.date);
3536
}
36-
this.pupilId = params['pId'];
37+
this.pupilId = +params['pId'];
3738
});
3839
}
3940

@@ -42,6 +43,10 @@ export class NotesComponent implements OnInit, OnDestroy {
4243
this.sub.unsubscribe();
4344
}
4445

46+
overview() {
47+
return isNaN(this.id);
48+
}
49+
4550
goBack() {
4651
this.location.back();
4752
}
@@ -91,6 +96,8 @@ export class NotesComponent implements OnInit, OnDestroy {
9196
add() {
9297
let d = new Date();
9398
this.convertToModelDate(d);
99+
if(this.pupilId)
100+
this.model.pupilId = this.pupilId;
94101
this.modalService.open(this.addNote).result.then((res) => {
95102
if (res) { //if modal got closed with data
96103
let m = this.model;

src/app/pupil/pupil.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div *ngIf="!id">
1+
<div *ngIf="overview()">
22
<div class="container">
33
<h1>Schülerübersicht</h1>
44
<div class="row row-spacing">
@@ -56,7 +56,7 @@ <h4 class="modal-title">Schüler hinzufügen</h4>
5656
</div>
5757

5858

59-
<div *ngIf="id">
59+
<div *ngIf="!overview()">
6060
<div class="container">
6161
<h1>Schüler bearbeiten</h1>
6262
<form class="form-horizontal" (ngSubmit)="update(id)" #modifyPupilForm="ngForm">

src/app/pupil/pupil.component.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
1010
})
1111
export class PupilComponent implements OnInit, OnDestroy {
1212

13-
private id: string;
13+
private id: number;
1414
private sub: any;
1515

1616
model = {firstName: "", lastName: "", classId: -1};
@@ -23,8 +23,8 @@ export class PupilComponent implements OnInit, OnDestroy {
2323

2424
ngOnInit() {
2525
this.sub = this.route.params.subscribe(params => {
26-
this.id = params['id']; //get ID from route parameter
27-
if (this.id) {
26+
this.id = +params['id']; //get ID from route parameter
27+
if (!this.overview()) {
2828
this.model = this.dataService.getPupil(this.id);
2929
}
3030
});
@@ -39,8 +39,13 @@ export class PupilComponent implements OnInit, OnDestroy {
3939
return !(m.firstName && m.lastName && m.classId >= 0);
4040
}
4141

42+
overview() {
43+
return isNaN(this.id);
44+
}
45+
4246
@ViewChild('addPupil') addPupil;
4347
add() {
48+
console.log(this.model);
4449
this.modalService.open(this.addPupil).result.then((res) => {
4550
if (res) { //if modal got closed with data
4651
let m = this.model;

src/app/teacher/teacher.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div *ngIf="!id">
1+
<div *ngIf="overview()">
22
<div class="container">
33
<h1>Lehrerübersicht</h1>
44
<div class="row row-spacing">
@@ -50,7 +50,7 @@ <h4 class="modal-title">Lehrer hinzufügen</h4>
5050
</div>
5151

5252

53-
<div *ngIf="id">
53+
<div *ngIf="!overview()">
5454
<div class="container">
5555
<h1>Lehrer bearbeiten</h1>
5656
<form class="form-horizontal" (ngSubmit)="update(id)" #addTeacherForm="ngForm">

src/app/teacher/teacher.component.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
1212
})
1313
export class TeacherComponent implements OnInit, OnDestroy {
1414

15-
id: string;
15+
id: number;
1616
private sub: any;
1717

1818
model = { firstName: "", lastName: "", mail: "", pass: "" };
@@ -26,8 +26,8 @@ export class TeacherComponent implements OnInit, OnDestroy {
2626

2727
ngOnInit() {
2828
this.sub = this.route.params.subscribe(params => {
29-
this.id = params['id']; //get ID from route parameter
30-
if (this.id) {
29+
this.id = +params['id']; //get ID from route parameter
30+
if (!this.overview()) {
3131
this.model = this.dataService.getTeacher(this.id);
3232
}
3333
});
@@ -41,6 +41,10 @@ export class TeacherComponent implements OnInit, OnDestroy {
4141
return !(this.model.firstName && this.model.lastName && this.model.mail && this.model.pass);
4242
}
4343

44+
overview() {
45+
return isNaN(this.id);
46+
}
47+
4448
add(template) {
4549
this.modalService.open(template).result.then((res) => {
4650
if (res) { //if modal got closed with data

0 commit comments

Comments
 (0)