-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFullStack-Todo-Tutorial.html
2315 lines (2297 loc) · 358 KB
/
FullStack-Todo-Tutorial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="generator" content="pandoc">
<meta name="description" content="">
<title>FullStack-Todo-Tutorial</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="https://maxcdn.bootstrapcdn.com/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="https://mushiyo.github.io/pandoc-toc-sidebar/css/dashboard.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">.sidebar ul{padding-left: 10px;}</style>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div id="sidebar" class="col-sm-3 col-md-2 sidebar">
<!--<ul class="nav nav-sidebar">
<li class="active"><a href="#">Overview <span class="sr-only">(current)</span></a></li>
</ul>-->
<ul>
<li><a href="#todo-full-stack-with-angular-and-spring-boot"
id="toc-todo-full-stack-with-angular-and-spring-boot">Todo
Full Stack with Angular and Spring Boot</a>
<ul>
<li><a href="#table-of-contents"
id="toc-table-of-contents">Table of contents</a></li>
<li><a href="#frontend" id="toc-frontend">Frontend</a>
<ul>
<li><a href="#introduction"
id="toc-introduction">Introduction</a></li>
<li><a href="#todo-list-design" id="toc-todo-list-design">Todo
List Design</a></li>
<li><a href="#header-footer-error-component"
id="toc-header-footer-error-component">Header-Footer-Error
Component</a></li>
<li><a href="#frontend-authentication"
id="toc-frontend-authentication">Frontend
Authentication</a></li>
<li><a href="#routeguard-service"
id="toc-routeguard-service">RouteGuard Service</a></li>
</ul></li>
<li><a href="#backend" id="toc-backend">Backend</a>
<ul>
<li><a href="#connnecting-backend-to-frontend"
id="toc-connnecting-backend-to-frontend">Connnecting Backend
to Frontend</a></li>
<li><a href="#crud-operation" id="toc-crud-operation">CRUD
Operation</a></li>
<li><a href="#read" id="toc-read">READ</a></li>
<li><a href="#delete" id="toc-delete">DELETE</a></li>
<li><a href="#update" id="toc-update">UPDATE</a></li>
<li><a href="#create" id="toc-create">CREATE</a></li>
</ul></li>
<li><a href="#authentication-with-jwt"
id="toc-authentication-with-jwt">Authentication with JWT</a>
<ul>
<li><a href="#hardcoded-authentication"
id="toc-hardcoded-authentication">HardCoded
Authentication</a></li>
<li><a href="#authentication-using-backend-creds"
id="toc-authentication-using-backend-creds">Authentication
using Backend Creds</a></li>
<li><a href="#spring-security-with-jwt"
id="toc-spring-security-with-jwt">Spring Security with
JWT</a></li>
<li><a href="#endpoints" id="toc-endpoints">Endpoints</a></li>
</ul></li>
<li><a href="#using-database" id="toc-using-database">Using
Database</a></li>
<li><a href="#deploying-fullstack-in-heroku"
id="toc-deploying-fullstack-in-heroku">Deploying FullStack in
Heroku</a>
<ul>
<li><a href="#deploying-backend"
id="toc-deploying-backend">Deploying Backend</a></li>
<li><a href="#deploying-frontend-with-environment"
id="toc-deploying-frontend-with-environment">Deploying
Frontend with Environment</a></li>
</ul></li>
<li><a href="#logging-by-slf4j"
id="toc-logging-by-slf4j">Logging by SLF4j</a></li>
<li><a href="#caching-by-hazlecast"
id="toc-caching-by-hazlecast">Caching by Hazlecast</a></li>
</ul></li>
</ul>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<p></p>
<h1 id="todo-full-stack-with-angular-and-spring-boot">Todo Full Stack
with Angular and Spring Boot</h1>
<p><a href="https://todo-fullstack-swarna.herokuapp.com/">Live Link</a>
: https://todo-fullstack-swarna.herokuapp.com/</p>
<p>Repository Link :
https://github.com/in28minutes/full-stack-with-angular-and-spring-boot</p>
<h2 id="table-of-contents">Table of contents</h2>
<ul>
<li><a href="#frontend">Frontend</a>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#todo-list-design">Todo List Design</a>
<ul>
<li><a href="#list-todos-component">List-Todos Component</a></li>
</ul></li>
<li><a href="#header-footer-error-component">Header-Footer-Error
Component</a></li>
<li><a href="#frontend-authentication">Frontend Authentication</a></li>
<li><a href="#routeguard-service">RouteGuard Service</a></li>
</ul></li>
<li><a href="#backend">Backend</a>
<ul>
<li><a href="#basic-backend-setup">Basic Backend Setup</a></li>
<li><a href="#connnecting-backend-to-frontend">Connnecting Backend to
Frontend</a></li>
<li><a href="#crud-operation">CRUD Operation</a></li>
<li><a href="#read">READ</a></li>
<li><a href="#delete">DELETE</a></li>
<li><a href="#update">UPDATE</a></li>
<li><a href="#create">CREATE</a></li>
</ul></li>
<li><a href="#authentication-with-jwt">Authentication with JWT</a>
<ul>
<li><a href="#hardcoded-authentication">HardCoded Authentication</a>
<ul>
<li><a href="#backend-hardcoded-auth">Backend Hardcoded Auth</a></li>
<li><a href="#frontend-hardcoded-auth">Frontend Hardcoded Auth</a></li>
</ul></li>
<li><a href="#authentication-using-backend-creds">Authentication using
Backend Creds</a>
<ul>
<li><a href="#steps">Steps</a></li>
</ul></li>
<li><a href="#spring-security-with-jwt">Spring Security with JWT</a>
<ul>
<li><a href="#frontend-jwt-changes">Frontend JWT Changes</a></li>
<li><a href="#backend-jwt-changes">Backend JWT Changes</a></li>
</ul></li>
<li><a href="#endpoints">Endpoints</a></li>
</ul></li>
<li><a href="#using-database">Using Database</a></li>
<li><a href="#deploying-fullstack-in-heroku">Deploying FullStack in
Heroku</a>
<ul>
<li><a href="#deploying-backend">Deploying Backend</a></li>
<li><a href="#deploying-frontend-with-environment">Deploying Frontend
with Environment</a></li>
</ul></li>
<li><a href="#logging-by-slf4j">Logging by SLF4j</a>
<ul>
<li><a href="#rolling-log">Rolling Log</a></li>
</ul></li>
<li><a href="#caching-by-hazlecast">Caching by Hazlecast</a></li>
</ul>
<h2 id="frontend">Frontend</h2>
<h3 id="introduction">Introduction</h3>
<p>Final App we’ll build</p>
<p><img src="images/finalapp.png" alt="finalapp" style="zoom: 67%;" /></p>
<p>Learning Steps</p>
<p><img src="images/LearningSteps.png" alt="LearningSteps" style="zoom: 67%;" /></p>
<p>AngularLearningTopics</p>
<p><img src="images/AngularLearningTopics.png" alt="AngularLearningTopics" style="zoom:50%;" /></p>
<ul>
<li>Generated frontend project - <code>ng new todo</code></li>
</ul>
<h5 id="appcomponent">AppComponent</h5>
<p><strong><em>app.component.html</em></strong></p>
<div class="sourceCode" id="cb1"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><router-outlet></router-outlet></span></span></code></pre></div>
<p> <strong><em>app-routing.module.ts</em></strong></p>
<div class="sourceCode" id="cb2"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> routes<span class="op">:</span> Routes <span class="op">=</span> [</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">''</span><span class="op">,</span> component<span class="op">:</span> LoginComponent }<span class="op">,</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'login'</span><span class="op">,</span> component<span class="op">:</span> LoginComponent }<span class="op">,</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'welcome/:name'</span><span class="op">,</span> component<span class="op">:</span> WelcomeComponent }<span class="op">,</span> <span class="co">//Activated Route Parameters</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'**'</span><span class="op">,</span> component<span class="op">:</span> ErrorComponent }<span class="op">,</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>]<span class="op">;</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="op">...</span></span></code></pre></div>
<p><strong><em>app.module.ts</em></strong></p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>@<span class="fu">NgModule</span>({</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> declarations<span class="op">:</span> [</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> AppComponent<span class="op">,</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> WelcomeComponent<span class="op">,</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> LoginComponent<span class="op">,</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> ErrorComponent</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> ]<span class="op">,</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> imports<span class="op">:</span> [</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> BrowserModule<span class="op">,</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> AppRoutingModule<span class="op">,</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> FormsModule</span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a> ]<span class="op">,</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a><span class="op">...</span></span></code></pre></div>
<h5 id="logincomponent">LoginComponent</h5>
<p><strong><em>login.component.html</em></strong></p>
<div class="sourceCode" id="cb4"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>Username : <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">"text"</span> <span class="er">name</span><span class="ot">=</span><span class="st">"username"</span> <span class="er">[(ngModel)]</span> <span class="ot">=</span> <span class="st">"username"</span><span class="kw">></span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>Password : <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">"password"</span> <span class="er">name</span><span class="ot">=</span><span class="st">"password"</span> <span class="er">[(ngModel)]</span> <span class="ot">=</span> <span class="st">"password"</span><span class="kw">></span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="kw"><button</span> <span class="er">(click)</span><span class="ot">=</span><span class="st">"handleLogin()"</span><span class="kw">></span>Login<span class="kw"></button></span> <span class="kw"><br></span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="kw"><small</span> <span class="er">*ngIf</span><span class="ot">=</span><span class="st">"invalidLogin"</span><span class="kw">></span>Login Unsuccessful<span class="kw"></small></span></span></code></pre></div>
<p><strong><em>login.component.ts</em></strong></p>
<div class="sourceCode" id="cb5"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> LoginComponent <span class="kw">implements</span> OnInit {</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> username <span class="op">=</span> <span class="st">''</span><span class="op">;</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> password <span class="op">=</span> <span class="st">''</span><span class="op">;</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> invalidLogin <span class="op">=</span> <span class="kw">false</span><span class="op">;</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(<span class="kw">private</span> router<span class="op">:</span> Router) {}</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">ngOnInit</span>()<span class="op">:</span> <span class="dt">void</span> {}</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">handleLogin</span>() {</span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="kw">this</span><span class="op">.</span><span class="at">username</span> <span class="op">===</span> <span class="st">'user'</span> <span class="op">&&</span> <span class="kw">this</span><span class="op">.</span><span class="at">password</span> <span class="op">===</span> <span class="st">'pass'</span>) {</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">router</span><span class="op">.</span><span class="fu">navigate</span>([<span class="st">'welcome'</span><span class="op">,</span> <span class="kw">this</span><span class="op">.</span><span class="at">username</span>])</span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">invalidLogin</span> <span class="op">=</span> <span class="kw">false</span><span class="op">;</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> <span class="co">// console.log("Login successful of User : " + this.username)</span></span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">invalidLogin</span> <span class="op">=</span> <span class="kw">true</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<h5 id="welcomecomponent">WelcomeComponent</h5>
<p><strong><em>welcome.component.html</em></strong></p>
<div class="sourceCode" id="cb6"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><p></span>welcome {{name}} !! to our awesome app<span class="kw"></p></span></span></code></pre></div>
<p><strong><em>welcome.component.ts</em></strong></p>
<div class="sourceCode" id="cb7"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> WelcomeComponent <span class="kw">implements</span> OnInit {</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> name <span class="op">=</span> <span class="st">''</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(<span class="kw">private</span> aroute<span class="op">:</span> ActivatedRoute) { }</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">ngOnInit</span>()<span class="op">:</span> <span class="dt">void</span> {</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">name</span> <span class="op">=</span><span class="kw">this</span><span class="op">.</span><span class="at">aroute</span><span class="op">.</span><span class="at">snapshot</span><span class="op">.</span><span class="at">params</span>[<span class="st">'name'</span>]</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<h3 id="todo-list-design">Todo List Design</h3>
<h4 id="list-todos-component">List-Todos Component</h4>
<p><strong><em>app-routing.module.ts</em></strong></p>
<div class="sourceCode" id="cb8"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>{ path<span class="op">:</span> <span class="st">'todos'</span><span class="op">,</span> component<span class="op">:</span> ListTodosComponent }<span class="op">,</span></span></code></pre></div>
<p><strong><em>list-todos.component.html</em></strong></p>
<div class="sourceCode" id="cb9"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><table</span> <span class="er">border</span><span class="ot">=</span><span class="st">"1"</span><span class="kw">></span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><caption></span>My Todos<span class="kw"></caption></span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><thead></span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tr></span></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Description<span class="kw"></th></span></span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Date<span class="kw"></th></span></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Is Completed ?<span class="kw"></th></span></span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tr></span></span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a> <span class="kw"></thead></span></span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tbody></span></span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tr</span> <span class="er">*ngFor</span><span class="ot">=</span><span class="st">"let todo of todos"</span><span class="kw">></span></span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.description }}<span class="kw"></td></span></span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.targetDate | date }}<span class="kw"></td></span></span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.done }}<span class="kw"></td></span></span>
<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tr></span></span>
<span id="cb9-16"><a href="#cb9-16" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tbody></span></span>
<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a><span class="kw"></table></span></span></code></pre></div>
<p><strong><em>list-todos.component.ts</em></strong></p>
<div class="sourceCode" id="cb10"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> Todo {</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> id<span class="op">:</span> <span class="dt">number</span><span class="op">,</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> description<span class="op">:</span> <span class="dt">string</span><span class="op">,</span></span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> done<span class="op">:</span> <span class="dt">boolean</span><span class="op">,</span></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> targetDate<span class="op">:</span> <span class="bu">Date</span></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> ) {}</span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a>@<span class="fu">Component</span>({</span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> <span class="op">...</span> </span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> ListTodosComponent <span class="kw">implements</span> OnInit {</span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a> todos <span class="op">=</span> [</span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">new</span> <span class="fu">Todo</span>(<span class="dv">1</span><span class="op">,</span> <span class="st">'My first Todo'</span><span class="op">,</span> <span class="kw">true</span><span class="op">,</span> <span class="kw">new</span> <span class="bu">Date</span>())<span class="op">,</span></span>
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true" tabindex="-1"></a> <span class="kw">new</span> <span class="fu">Todo</span>(<span class="dv">1</span><span class="op">,</span> <span class="st">'Welcome to India'</span><span class="op">,</span> <span class="kw">false</span><span class="op">,</span> <span class="kw">new</span> <span class="bu">Date</span>())<span class="op">,</span></span>
<span id="cb10-16"><a href="#cb10-16" aria-hidden="true" tabindex="-1"></a> <span class="kw">new</span> <span class="fu">Todo</span>(<span class="dv">1</span><span class="op">,</span> <span class="st">'Angular project'</span><span class="op">,</span> <span class="kw">true</span><span class="op">,</span> <span class="kw">new</span> <span class="bu">Date</span>())<span class="op">,</span></span>
<span id="cb10-17"><a href="#cb10-17" aria-hidden="true" tabindex="-1"></a> ]<span class="op">;</span></span>
<span id="cb10-18"><a href="#cb10-18" aria-hidden="true" tabindex="-1"></a><span class="op">...</span></span>
<span id="cb10-19"><a href="#cb10-19" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>welcome.component.html</em></strong> - adding routing
from welcome page to TodoList page</p>
<div class="sourceCode" id="cb11"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><p></span>welcome {{name}} !! to our awesome app<span class="kw"></p></span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="kw"><h3></span>You can manage your todos <span class="kw"><a</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/todos"</span><span class="kw">></span>here<span class="kw"></a></span>.<span class="kw"></h3></span></span></code></pre></div>
<p>Snapshot after TodoList component. (Before Bootstrap styling)</p>
<p><img src="images\before-bootstrap.png" alt="before-bootstrap" style="zoom:67%;" /></p>
<h5 id="added-bootstrap">Added Bootstrap</h5>
<pre class="shell"><code>$ npm install bootstrap
$ npm install jquery
// "bootstrap": "^4.6.1",
// "jquery": "^3.6.0",</code></pre>
<p><strong><em>angular.json</em></strong></p>
<div class="sourceCode" id="cb13"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="er">"architect":</span> <span class="fu">{</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="dt">"build"</span><span class="fu">:</span> <span class="fu">{</span> <span class="er">...</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="dt">"styles"</span><span class="fu">:</span> <span class="ot">[</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"src/styles.css"</span><span class="ot">,</span></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"./node_modules/bootstrap/dist/css/bootstrap.min.css"</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="ot">]</span><span class="fu">,</span></span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">"scripts"</span><span class="fu">:</span> <span class="ot">[</span></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"./node_modules/jquery/dist/jquery.js"</span><span class="ot">,</span></span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"./node_modules/bootstrap/dist/js/bootstrap.js"</span></span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a> <span class="ot">]</span> <span class="er">...</span></span></code></pre></div>
<h3 id="header-footer-error-component">Header-Footer-Error
Component</h3>
<p>Created 3 components for Header, Footer and Error.</p>
<p><strong><em>nav.component.html</em></strong> - Header</p>
<div class="sourceCode" id="cb14"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><nav</span> <span class="er">class</span><span class="ot">=</span><span class="st">"navbar navbar-expand-lg navbar-dark mb-2"</span> <span class="er">style</span><span class="ot">=</span><span class="st">"background-color: #016f77;"</span><span class="kw">></span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><a</span> <span class="er">class</span><span class="ot">=</span><span class="st">"navbar-brand"</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/"</span><span class="kw">></span>Todo Fullstack<span class="kw"></a></span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><button</span> <span class="er">class</span><span class="ot">=</span><span class="st">"navbar-toggler"</span> <span class="er">type</span><span class="ot">=</span><span class="st">"button"</span> <span class="er">data-toggle</span><span class="ot">=</span><span class="st">"collapse"</span> <span class="er">data-target</span><span class="ot">=</span><span class="st">"#navbarSupportedContent"</span> <span class="er">aria-controls</span><span class="ot">=</span><span class="st">"navbarSupportedContent"</span> <span class="er">aria-expanded</span><span class="ot">=</span><span class="st">"false"</span> <span class="er">aria-label</span><span class="ot">=</span><span class="st">"Toggle navigation"</span><span class="kw">></span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> <span class="kw"><span</span> <span class="er">class</span><span class="ot">=</span><span class="st">"navbar-toggler-icon"</span><span class="kw">></span></span></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"></button></span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a> <span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"collapse navbar-collapse"</span> <span class="er">id</span><span class="ot">=</span><span class="st">"navbarSupportedContent"</span><span class="kw">></span></span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a> <span class="kw"><ul</span> <span class="er">class</span><span class="ot">=</span><span class="st">"navbar-nav mr-auto"</span><span class="kw">></span></span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a> <span class="co"><!--<li *ngIf="!loggedIn()" class="nav-item"></span></span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a><span class="co"> <a class="nav-link" routerLink="/login" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">Login <span class="sr-only">(current)</span></a></span></span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a><span class="co"> </li>--></span></span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a> <span class="kw"><li</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-item"</span><span class="kw">></span></span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a> <span class="kw"><a</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-link"</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/welcome/user"</span> <span class="er">routerLinkActive</span><span class="ot">=</span><span class="st">"active"</span><span class="kw">></span>Home<span class="kw"></a></span></span>
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true" tabindex="-1"></a> <span class="kw"></li></span></span>
<span id="cb14-15"><a href="#cb14-15" aria-hidden="true" tabindex="-1"></a> <span class="kw"><li</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-item"</span><span class="kw">></span></span>
<span id="cb14-16"><a href="#cb14-16" aria-hidden="true" tabindex="-1"></a> <span class="kw"><a</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-link"</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/todos"</span> <span class="er">routerLinkActive</span><span class="ot">=</span><span class="st">"active"</span><span class="kw">></span>Todos<span class="kw"></a></span></span>
<span id="cb14-17"><a href="#cb14-17" aria-hidden="true" tabindex="-1"></a> <span class="kw"></li></span></span>
<span id="cb14-18"><a href="#cb14-18" aria-hidden="true" tabindex="-1"></a> <span class="kw"></ul></span></span>
<span id="cb14-19"><a href="#cb14-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-20"><a href="#cb14-20" aria-hidden="true" tabindex="-1"></a> <span class="kw"><ul</span> <span class="er">class</span><span class="ot">=</span><span class="st">"navbar-nav"</span><span class="kw">></span></span>
<span id="cb14-21"><a href="#cb14-21" aria-hidden="true" tabindex="-1"></a> <span class="kw"><li</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-item"</span><span class="kw">></span></span>
<span id="cb14-22"><a href="#cb14-22" aria-hidden="true" tabindex="-1"></a> <span class="kw"><a</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-link"</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/login"</span> <span class="er">routerLinkActive</span><span class="ot">=</span><span class="st">"active"</span><span class="kw">></span>Login<span class="kw"></a></span></span>
<span id="cb14-23"><a href="#cb14-23" aria-hidden="true" tabindex="-1"></a> <span class="kw"></li></span></span>
<span id="cb14-24"><a href="#cb14-24" aria-hidden="true" tabindex="-1"></a> <span class="kw"><li</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-item"</span><span class="kw">></span></span>
<span id="cb14-25"><a href="#cb14-25" aria-hidden="true" tabindex="-1"></a> <span class="kw"><a</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-link"</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/logout"</span> <span class="er">routerLinkActive</span><span class="ot">=</span><span class="st">"active"</span><span class="kw">></span>Logout<span class="kw"></a></span></span>
<span id="cb14-26"><a href="#cb14-26" aria-hidden="true" tabindex="-1"></a> <span class="kw"></li></span></span>
<span id="cb14-27"><a href="#cb14-27" aria-hidden="true" tabindex="-1"></a> <span class="kw"></ul></span></span>
<span id="cb14-28"><a href="#cb14-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-29"><a href="#cb14-29" aria-hidden="true" tabindex="-1"></a> <span class="kw"></div></span></span>
<span id="cb14-30"><a href="#cb14-30" aria-hidden="true" tabindex="-1"></a><span class="kw"></nav></span></span></code></pre></div>
<p><strong><em>footer.component.html</em></strong> - Footer</p>
<div class="sourceCode" id="cb15"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><footer</span> <span class="er">class</span><span class="ot">=</span><span class="st">"footer text-light"</span><span class="kw">></span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"container align-middle text-center"</span><span class="kw">></span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><i></span>Made with ❤ by <span class="kw"><a</span> <span class="er">class</span><span class="ot">=</span><span class="st">"text-light"</span> <span class="er">href</span><span class="ot">=</span><span class="st">"https://swarnadeepghosh.github.io/"</span><span class="kw">></span>Swarnadeep<span class="kw"></a></i></span></span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a> <span class="kw"></div></span></span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a><span class="kw"></footer></span></span></code></pre></div>
<p><strong><em>footer.component.css</em></strong> - Footer</p>
<div class="sourceCode" id="cb16"><pre
class="sourceCode css"><code class="sourceCode css"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">.footer</span>{</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">position</span>: <span class="dv">absolute</span><span class="op">;</span></span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">bottom</span>: <span class="dv">0</span><span class="op">;</span></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">width</span>: <span class="dv">100</span><span class="dt">%</span><span class="op">;</span></span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">height</span>: <span class="dv">40</span><span class="dt">px</span><span class="op">;</span></span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">background-color</span>: <span class="cn">#016f77</span><span class="op">;</span></span>
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a><span class="fu">.container</span>{</span>
<span id="cb16-9"><a href="#cb16-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">padding</span>: <span class="dv">8</span><span class="dt">px</span><span class="op">;</span></span>
<span id="cb16-10"><a href="#cb16-10" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>error.component.html</em></strong> - For error page</p>
<div class="sourceCode" id="cb17"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"d-flex my-5 justify-content-center align-items-center align-middle"</span> <span class="er">id</span><span class="ot">=</span><span class="st">"error"</span><span class="kw">></span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><h1</span> <span class="er">class</span><span class="ot">=</span><span class="st">"mr-3 pr-3 align-top border-right inline-block align-content-center"</span><span class="kw">></span>404<span class="kw"></h1></span></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"inline-block align-middle"</span><span class="kw">></span></span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> <span class="kw"><h2</span> <span class="er">class</span><span class="ot">=</span><span class="st">"font-weight-normal lead"</span> <span class="er">id</span><span class="ot">=</span><span class="st">"desc"</span><span class="kw">></span>The page you requested was not found. Please contact administrator.<span class="kw"></h2></span></span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"></div></span></span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a><span class="kw"></div></span></span></code></pre></div>
<p><strong><em>error.component.css</em></strong> - For error page</p>
<div class="sourceCode" id="cb18"><pre
class="sourceCode css"><code class="sourceCode css"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#error</span> { <span class="kw">height</span>: <span class="dv">100</span><span class="dt">%</span><span class="op">;</span> }</span></code></pre></div>
<h5 id="setting-bootstrap-designs">Setting Bootstrap designs</h5>
<p><strong><em>app-routing.module.ts</em></strong> - Routing Module</p>
<div class="sourceCode" id="cb19"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> routes<span class="op">:</span> Routes <span class="op">=</span> [</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">''</span><span class="op">,</span> redirectTo<span class="op">:</span> <span class="st">'/login'</span><span class="op">,</span> pathMatch<span class="op">:</span> <span class="st">'full'</span> }<span class="op">,</span></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'login'</span><span class="op">,</span> component<span class="op">:</span> LoginComponent }<span class="op">,</span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'welcome/:name'</span><span class="op">,</span> component<span class="op">:</span> WelcomeComponent }<span class="op">,</span></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'todos'</span><span class="op">,</span> component<span class="op">:</span> ListTodosComponent }<span class="op">,</span></span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'**'</span><span class="op">,</span> component<span class="op">:</span> ErrorComponent }<span class="op">,</span></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a>]<span class="op">;</span></span></code></pre></div>
<p><strong><em>app.component.html</em></strong></p>
<div class="sourceCode" id="cb20"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><app-nav></app-nav></span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"container"</span><span class="kw">></span></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><router-outlet></router-outlet></span></span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="kw"></div></span></span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a><span class="kw"><app-footer></app-footer></span></span></code></pre></div>
<p><strong><em>list-todos.component.html</em></strong></p>
<div class="sourceCode" id="cb21"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><h2></span>My Todo's<span class="kw"></h2></span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a><span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"container"</span><span class="kw">></span></span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a> <span class="kw"><table</span> <span class="er">class</span><span class="ot">=</span><span class="st">"table"</span><span class="kw">></span></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><thead></span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tr></span></span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Description<span class="kw"></th></span></span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Date<span class="kw"></th></span></span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Is Completed ?<span class="kw"></th></span></span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tr></span></span>
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a> <span class="kw"></thead></span></span>
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tbody></span></span>
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tr</span> <span class="er">*ngFor</span><span class="ot">=</span><span class="st">"let todo of todos"</span><span class="kw">></span></span>
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.description }}<span class="kw"></td></span></span>
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.targetDate | date }}<span class="kw"></td></span></span>
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.done }}<span class="kw"></td></span></span>
<span id="cb21-17"><a href="#cb21-17" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tr></span></span>
<span id="cb21-18"><a href="#cb21-18" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tbody></span></span>
<span id="cb21-19"><a href="#cb21-19" aria-hidden="true" tabindex="-1"></a> <span class="kw"></table></span></span>
<span id="cb21-20"><a href="#cb21-20" aria-hidden="true" tabindex="-1"></a><span class="kw"></div></span></span></code></pre></div>
<p><strong><em>login.component.html</em></strong></p>
<div class="sourceCode" id="cb22"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><form></span></span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"form-group"</span><span class="kw">></span></span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><label</span> <span class="er">for</span><span class="ot">=</span><span class="st">"user"</span><span class="kw">></span>Username<span class="kw"></label></span></span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">"text"</span> <span class="er">class</span><span class="ot">=</span><span class="st">"form-control"</span> <span class="er">id</span><span class="ot">=</span><span class="st">"user"</span> <span class="er">name</span><span class="ot">=</span><span class="st">"username"</span> <span class="er">[(ngModel)]</span> <span class="ot">=</span> <span class="st">"username"</span><span class="kw">></span></span>
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"></div></span></span>
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a> <span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"form-group"</span><span class="kw">></span></span>
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a> <span class="kw"><label</span> <span class="er">for</span><span class="ot">=</span><span class="st">"password"</span><span class="kw">></span>Password<span class="kw"></label></span></span>
<span id="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">"password"</span> <span class="er">class</span><span class="ot">=</span><span class="st">"form-control"</span> <span class="er">id</span><span class="ot">=</span><span class="st">"password"</span> <span class="er">name</span><span class="ot">=</span><span class="st">"password"</span> <span class="er">[(ngModel)]</span> <span class="ot">=</span> <span class="st">"password"</span><span class="kw">></span></span>
<span id="cb22-9"><a href="#cb22-9" aria-hidden="true" tabindex="-1"></a> <span class="kw"></div></span></span>
<span id="cb22-10"><a href="#cb22-10" aria-hidden="true" tabindex="-1"></a> <span class="kw"><button</span> <span class="er">type</span><span class="ot">=</span><span class="st">"submit"</span> <span class="er">class</span><span class="ot">=</span><span class="st">"btn btn-primary"</span> <span class="er">(click)</span><span class="ot">=</span><span class="st">"handleLogin()"</span><span class="kw">></span>Login<span class="kw"></button></span></span>
<span id="cb22-11"><a href="#cb22-11" aria-hidden="true" tabindex="-1"></a> <span class="kw"><i</span> <span class="er">*ngIf</span><span class="ot">=</span><span class="st">"invalidLogin"</span> <span class="er">class</span><span class="ot">=</span><span class="st">"text-danger m-4"</span><span class="kw">></span>Login Unsuccessful<span class="kw"></i></span></span>
<span id="cb22-12"><a href="#cb22-12" aria-hidden="true" tabindex="-1"></a><span class="kw"></form></span></span></code></pre></div>
<p><strong><em>welcome.component.html</em></strong></p>
<div class="sourceCode" id="cb23"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><h1></span>Welcome!!<span class="kw"></h1></span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="kw"><p></span>Welcome <span class="kw"><b></span>{{ name }}<span class="kw"></b></span> to Todo Fullstack app designed with -<span class="kw"></p></span></span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a><span class="kw"><ul></span></span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a> <span class="kw"><li></span>Frontend using Angular<span class="kw"></li></span></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><li></span>Backend using Spring Boot<span class="kw"></li></span></span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a> <span class="kw"><li></span>Authentication using Spring Security<span class="kw"></li></span></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a><span class="kw"></ul></span></span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a><span class="kw"><p></span>You can manage all your todos <span class="kw"><a</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/todos"</span><span class="kw">><b></span>here<span class="kw"></b></a></span>.<span class="kw"></p></span></span></code></pre></div>
<h3 id="frontend-authentication">Frontend Authentication</h3>
<p>Created a service hardcodedAuth for Hardcoded Authentication.</p>
<p><code>$ ng g service hardcodedAuth</code></p>
<h5 id="hardcoded-authentication-service">Hardcoded Authentication
Service</h5>
<p><strong><em>hardcoded-auth.service.ts</em></strong> – We are
hardcoding, checking if Logged In and removing user on logout thorugh
this service.</p>
<div class="sourceCode" id="cb24"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> HardcodedAuthService {</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>() {}</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">authenticate</span>(username<span class="op">:</span> <span class="dt">string</span><span class="op">,</span> password<span class="op">:</span> <span class="dt">string</span>) {</span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (username <span class="op">===</span> <span class="st">'user'</span> <span class="op">&&</span> password <span class="op">===</span> <span class="st">'pass'</span>) {</span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a> sessionStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">'authenticatedUser'</span><span class="op">,</span> username)<span class="op">;</span></span>
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">true</span><span class="op">;</span></span>
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">false</span><span class="op">;</span></span>
<span id="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb24-11"><a href="#cb24-11" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb24-12"><a href="#cb24-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-13"><a href="#cb24-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">isUserLoggedIn</span>() {</span>
<span id="cb24-14"><a href="#cb24-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> user <span class="op">=</span> sessionStorage<span class="op">.</span><span class="fu">getItem</span>(<span class="st">'authenticatedUser'</span>)<span class="op">;</span></span>
<span id="cb24-15"><a href="#cb24-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="op">!</span>(user <span class="op">===</span> <span class="kw">null</span>)<span class="op">;</span></span>
<span id="cb24-16"><a href="#cb24-16" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb24-17"><a href="#cb24-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-18"><a href="#cb24-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">logout</span>() { <span class="kw">let</span> user <span class="op">=</span> sessionStorage<span class="op">.</span><span class="fu">removeItem</span>(<span class="st">'authenticatedUser'</span>)<span class="op">;</span> }</span>
<span id="cb24-19"><a href="#cb24-19" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>login.component.ts</em></strong></p>
<div class="sourceCode" id="cb25"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="op">...</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="fu">handleLogin</span>() {</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> <span class="co">// if (this.username === 'user' && this.password === 'pass') {</span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="kw">this</span><span class="op">.</span><span class="at">hardcodedAuthService</span><span class="op">.</span><span class="fu">authenticate</span>(<span class="kw">this</span><span class="op">.</span><span class="at">username</span><span class="op">,</span> <span class="kw">this</span><span class="op">.</span><span class="at">password</span>)) {</span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="op">...</span></span></code></pre></div>
<h5 id="logout-component">Logout Component</h5>
<p><strong><em>logout.component.html</em></strong></p>
<div class="sourceCode" id="cb26"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><h1></span> You have successfully logged out.<span class="kw"></h1></span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="kw"><p></span>Thanks for using our application.<span class="kw"></p></span></span></code></pre></div>
<p><strong><em>logout.component.ts</em></strong></p>
<div class="sourceCode" id="cb27"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> LogoutComponent <span class="kw">implements</span> OnInit {</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(<span class="kw">private</span> hardcodedAuthService<span class="op">:</span> HardcodedAuthService) {}</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ngOnInit</span>()<span class="op">:</span> <span class="dt">void</span> { <span class="kw">this</span><span class="op">.</span><span class="at">hardcodedAuthService</span><span class="op">.</span><span class="fu">logout</span>()<span class="op">;</span> }</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<h5 id="allowing-nav-items-to-loggedin-user">Allowing Nav items to
Loggedin User</h5>
<p><strong><em>nav.component.html</em></strong> - configured the below
lines.</p>
<div class="sourceCode" id="cb28"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>...</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="kw"><a</span> <span class="er">*ngIf</span><span class="ot">=</span><span class="st">"hardcodedAuthService.isUserLoggedIn()"</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-link"</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/todos"</span> <span class="er">routerLinkActive</span><span class="ot">=</span><span class="st">"active"</span><span class="kw">></span>Todos<span class="kw"></a></span></span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a><span class="kw"><a</span> <span class="er">*ngIf</span><span class="ot">=</span><span class="st">"!hardcodedAuthService.isUserLoggedIn()"</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-link"</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/login"</span> <span class="er">routerLinkActive</span><span class="ot">=</span><span class="st">"active"</span><span class="kw">></span>Login<span class="kw"></a></span></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a><span class="kw"><a</span> <span class="er">*ngIf</span><span class="ot">=</span><span class="st">"hardcodedAuthService.isUserLoggedIn()"</span> <span class="er">class</span><span class="ot">=</span><span class="st">"nav-link"</span> <span class="er">routerLink</span><span class="ot">=</span><span class="st">"/logout"</span> <span class="er">routerLinkActive</span><span class="ot">=</span><span class="st">"active"</span><span class="kw">></span>Logout<span class="kw"></a></span></span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a>...</span></code></pre></div>
<p><strong><em>nav.component.ts</em></strong></p>
<div class="sourceCode" id="cb29"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="kw">constructor</span>(<span class="kw">public</span> hardcodedAuthService <span class="op">:</span> HardcodedAuthService) { }</span></code></pre></div>
<h3 id="routeguard-service">RouteGuard Service</h3>
<p><strong><em>route-guard.service.ts</em></strong></p>
<div class="sourceCode" id="cb30"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { Injectable } <span class="im">from</span> <span class="st">'@angular/core'</span><span class="op">;</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { ActivatedRouteSnapshot<span class="op">,</span> CanActivate<span class="op">,</span> Router<span class="op">,</span> RouterStateSnapshot } <span class="im">from</span> <span class="st">'@angular/router'</span><span class="op">;</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { Observable } <span class="im">from</span> <span class="st">'rxjs'</span><span class="op">;</span></span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { HardcodedAuthService } <span class="im">from</span> <span class="st">'./hardcoded-auth.service'</span><span class="op">;</span></span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a>@<span class="fu">Injectable</span>({ providedIn<span class="op">:</span> <span class="st">'root'</span><span class="op">,</span>})</span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> RouteGuardService <span class="kw">implements</span> CanActivate {</span>
<span id="cb30-8"><a href="#cb30-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(<span class="kw">private</span> hardcodedAuthService<span class="op">:</span> HardcodedAuthService<span class="op">,</span> <span class="kw">private</span> router<span class="op">:</span> Router) {}</span>
<span id="cb30-9"><a href="#cb30-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-10"><a href="#cb30-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">canActivate</span>(route<span class="op">:</span> ActivatedRouteSnapshot<span class="op">,</span> state<span class="op">:</span> RouterStateSnapshot) {</span>
<span id="cb30-11"><a href="#cb30-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="kw">this</span><span class="op">.</span><span class="at">hardcodedAuthService</span><span class="op">.</span><span class="fu">isUserLoggedIn</span>()) { <span class="cf">return</span> <span class="kw">true</span><span class="op">;</span> }</span>
<span id="cb30-12"><a href="#cb30-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>{</span>
<span id="cb30-13"><a href="#cb30-13" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">router</span><span class="op">.</span><span class="fu">navigate</span>([<span class="st">'/login'</span>])<span class="op">;</span></span>
<span id="cb30-14"><a href="#cb30-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">false</span><span class="op">;</span></span>
<span id="cb30-15"><a href="#cb30-15" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb30-16"><a href="#cb30-16" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb30-17"><a href="#cb30-17" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>app-routing.module.ts</em></strong></p>
<div class="sourceCode" id="cb31"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> routes<span class="op">:</span> Routes <span class="op">=</span> [</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">''</span><span class="op">,</span> redirectTo<span class="op">:</span> <span class="st">'/login'</span><span class="op">,</span> pathMatch<span class="op">:</span> <span class="st">'full'</span> }<span class="op">,</span></span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'login'</span><span class="op">,</span> component<span class="op">:</span> LoginComponent }<span class="op">,</span></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'welcome/:name'</span><span class="op">,</span> component<span class="op">:</span> WelcomeComponent<span class="op">,</span> canActivate<span class="op">:</span>[RouteGuardService] }<span class="op">,</span></span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'todos'</span><span class="op">,</span> component<span class="op">:</span> ListTodosComponent<span class="op">,</span> canActivate<span class="op">:</span>[RouteGuardService] }<span class="op">,</span></span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'logout'</span><span class="op">,</span> component<span class="op">:</span> LogoutComponent<span class="op">,</span> canActivate<span class="op">:</span>[RouteGuardService] }<span class="op">,</span></span>
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a> { path<span class="op">:</span> <span class="st">'**'</span><span class="op">,</span> component<span class="op">:</span> ErrorComponent }<span class="op">,</span></span>
<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a>]<span class="op">;</span></span></code></pre></div>
<h2 id="backend">Backend</h2>
<h4 id="basic-backend-setup">Basic Backend Setup</h4>
<p>Created a Spring Boot starter project TodoFullStack using below
dependencies:</p>
<pre><code>spring-boot-starter-web
spring-boot-starter-data-jpa
spring-boot-devtools
postgresql</code></pre>
<p><strong><em>backend.java</em></strong></p>
<div class="sourceCode" id="cb33"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="kw">package</span><span class="im"> com</span><span class="op">.</span><span class="im">swarna</span><span class="op">.</span><span class="im">todoFullStack</span><span class="op">;</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> <span class="kw">class</span> HelloWorldBean <span class="op">{</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> <span class="bu">String</span> message<span class="op">;</span></span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> <span class="fu">HelloWorldBean</span><span class="op">(</span><span class="bu">String</span> message<span class="op">)</span> <span class="op">{</span></span>
<span id="cb33-6"><a href="#cb33-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="fu">message</span> <span class="op">=</span> message<span class="op">;</span></span>
<span id="cb33-7"><a href="#cb33-7" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb33-8"><a href="#cb33-8" aria-hidden="true" tabindex="-1"></a> <span class="co">// Auto Generated Getter, Setter and ToString below ...</span></span>
<span id="cb33-9"><a href="#cb33-9" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p><strong><em>backend.java</em></strong> - Created 3 API</p>
<ul>
<li><code>/hello</code> - returns String hardcoded rsponse</li>
<li><code>/hello-bean</code> - returns new HelloWorldBean object which
has property “message”</li>
<li><code>/hello/path-variable/{name}</code> - prints path variable
also</li>
</ul>
<div class="sourceCode" id="cb34"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="kw">package</span><span class="im"> com</span><span class="op">.</span><span class="im">swarna</span><span class="op">.</span><span class="im">todoFullStack</span><span class="op">;</span></span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a><span class="co">// Allowing localhost:4200 so that backend can be called by frontend server</span></span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a><span class="at">@CrossOrigin</span><span class="op">(</span>origins <span class="op">=</span> <span class="st">"http://localhost:4200"</span><span class="op">)</span> </span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a><span class="at">@RestController</span></span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> <span class="kw">class</span> HelloController <span class="op">{</span></span>
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a> <span class="at">@GetMapping</span><span class="op">(</span><span class="st">"hello"</span><span class="op">)</span></span>
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> <span class="bu">String</span> <span class="fu">helloWorld</span><span class="op">()</span> <span class="op">{</span></span>
<span id="cb34-10"><a href="#cb34-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"Hello world"</span><span class="op">;</span></span>
<span id="cb34-11"><a href="#cb34-11" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb34-12"><a href="#cb34-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-13"><a href="#cb34-13" aria-hidden="true" tabindex="-1"></a> <span class="at">@GetMapping</span><span class="op">(</span><span class="st">"hello-bean"</span><span class="op">)</span> <span class="co">// Bean is converting into JSON and send to view.</span></span>
<span id="cb34-14"><a href="#cb34-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> HelloWorldBean <span class="fu">helloWorldBean</span><span class="op">()</span> <span class="op">{</span></span>
<span id="cb34-15"><a href="#cb34-15" aria-hidden="true" tabindex="-1"></a> <span class="co">// throw new RuntimeException("Some error occurred. Please contact helpdesk.");</span></span>
<span id="cb34-16"><a href="#cb34-16" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">new</span> <span class="fu">HelloWorldBean</span><span class="op">(</span><span class="st">"Hello World from HelloWorldBean backend"</span><span class="op">);</span> </span>
<span id="cb34-17"><a href="#cb34-17" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb34-18"><a href="#cb34-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-19"><a href="#cb34-19" aria-hidden="true" tabindex="-1"></a> <span class="at">@GetMapping</span><span class="op">(</span><span class="st">"hello/path-variable/{name}"</span><span class="op">)</span></span>
<span id="cb34-20"><a href="#cb34-20" aria-hidden="true" tabindex="-1"></a> <span class="kw">public</span> HelloWorldBean <span class="fu">helloWorldBeanWithPathVariable</span><span class="op">(</span><span class="at">@PathVariable</span> <span class="bu">String</span> name<span class="op">)</span> <span class="op">{</span></span>
<span id="cb34-21"><a href="#cb34-21" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">new</span> <span class="fu">HelloWorldBean</span><span class="op">(</span><span class="bu">String</span><span class="op">.</span><span class="fu">format</span><span class="op">(</span><span class="st">"Hello World from Path Variable, Username : </span><span class="sc">%s</span><span class="st">"</span><span class="op">,</span> name<span class="op">));</span></span>
<span id="cb34-22"><a href="#cb34-22" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb34-23"><a href="#cb34-23" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<h3 id="connnecting-backend-to-frontend">Connnecting Backend to
Frontend</h3>
<p><strong><em>app.module.ts</em></strong> - importing
HttpClientModule</p>
<div class="sourceCode" id="cb35"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { HttpClientModule } <span class="im">from</span> <span class="st">'@angular/common/http'</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a> imports<span class="op">:</span> [ <span class="op">...</span></span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a> HttpClientModule</span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a> <span class="op">...</span></span></code></pre></div>
<p><strong><em>welcome-data.service.ts</em></strong> - This will fetch
data from backend by REST-API call and share the response to angular
components.</p>
<div class="sourceCode" id="cb36"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { HttpClient } <span class="im">from</span> <span class="st">'@angular/common/http'</span><span class="op">;</span></span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { Injectable } <span class="im">from</span> <span class="st">'@angular/core'</span><span class="op">;</span></span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> HelloWorldBean {</span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(<span class="kw">public</span> message<span class="op">:</span> <span class="bu">String</span>) {}</span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a>@<span class="fu">Injectable</span>({</span>
<span id="cb36-9"><a href="#cb36-9" aria-hidden="true" tabindex="-1"></a> providedIn<span class="op">:</span> <span class="st">'root'</span><span class="op">,</span></span>
<span id="cb36-10"><a href="#cb36-10" aria-hidden="true" tabindex="-1"></a>})</span>
<span id="cb36-11"><a href="#cb36-11" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> WelcomeDataService {</span>
<span id="cb36-12"><a href="#cb36-12" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(<span class="kw">private</span> http<span class="op">:</span> HttpClient) {}</span>
<span id="cb36-13"><a href="#cb36-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-14"><a href="#cb36-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">executeHelloWorldBeanService</span>() {</span>
<span id="cb36-15"><a href="#cb36-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">this</span><span class="op">.</span><span class="at">http</span><span class="op">.</span><span class="fu">get</span><span class="op"><</span>HelloWorldBean<span class="op">></span>(<span class="st">'http://localhost:8080/hello-bean'</span>)<span class="op">;</span></span>
<span id="cb36-16"><a href="#cb36-16" aria-hidden="true" tabindex="-1"></a> <span class="co">// returns> Observable {source: Observable, operator: ƒ} and no call in network tab</span></span>
<span id="cb36-17"><a href="#cb36-17" aria-hidden="true" tabindex="-1"></a> <span class="co">// console.log('executeHelloWorldBean');</span></span>
<span id="cb36-18"><a href="#cb36-18" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb36-19"><a href="#cb36-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-20"><a href="#cb36-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">executeHelloWorldBeanServiceWithParameter</span>(name<span class="op">:</span> <span class="dt">any</span>) { <span class="co">// Fetching Path Variable from backend</span></span>
<span id="cb36-21"><a href="#cb36-21" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">this</span><span class="op">.</span><span class="at">http</span><span class="op">.</span><span class="fu">get</span><span class="op"><</span>HelloWorldBean<span class="op">></span>(<span class="vs">`http://localhost:8080/hello/path-variable/</span><span class="sc">${</span>name<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb36-22"><a href="#cb36-22" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb36-23"><a href="#cb36-23" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>welcome.component.ts</em></strong> - Added 2 methods to
handle success and error response, 1 method to fetch Welcome message
without parameter and 1 method to fetch Welcome message with
parameter</p>
<div class="sourceCode" id="cb37"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> WelcomeComponent <span class="kw">implements</span> OnInit {</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a> welcomeMessageFromService<span class="op">:</span> <span class="dt">string</span> <span class="op">|</span> <span class="dt">undefined</span><span class="op">;</span></span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a> <span class="op">...</span></span>
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">handleSuccessResponse</span>(response<span class="op">:</span> <span class="dt">any</span>) {</span>
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">welcomeMessageFromService</span> <span class="op">=</span> response<span class="op">.</span><span class="at">message</span><span class="op">;</span></span>
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(response<span class="op">.</span><span class="at">message</span>)<span class="op">;</span></span>
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">handleErrorResponse</span>(error<span class="op">:</span> <span class="dt">any</span>) {</span>
<span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">welcomeMessageFromService</span> <span class="op">=</span> error<span class="op">.</span><span class="at">error</span><span class="op">.</span><span class="at">message</span><span class="op">;</span></span>
<span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb37-11"><a href="#cb37-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-12"><a href="#cb37-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">getWelcomeMessage</span>() {</span>
<span id="cb37-13"><a href="#cb37-13" aria-hidden="true" tabindex="-1"></a> <span class="co">// console.log(this.welcomeDataService.executeHelloWorldBeanService());</span></span>
<span id="cb37-14"><a href="#cb37-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">welcomeDataService</span><span class="op">.</span><span class="fu">executeHelloWorldBeanService</span>()<span class="op">.</span><span class="fu">subscribe</span>(</span>
<span id="cb37-15"><a href="#cb37-15" aria-hidden="true" tabindex="-1"></a> (response) <span class="kw">=></span> <span class="kw">this</span><span class="op">.</span><span class="fu">handleSuccessResponse</span>(response)<span class="op">,</span></span>
<span id="cb37-16"><a href="#cb37-16" aria-hidden="true" tabindex="-1"></a> (error) <span class="kw">=></span> <span class="kw">this</span><span class="op">.</span><span class="fu">handleErrorResponse</span>(error)</span>
<span id="cb37-17"><a href="#cb37-17" aria-hidden="true" tabindex="-1"></a> )<span class="op">;</span></span>
<span id="cb37-18"><a href="#cb37-18" aria-hidden="true" tabindex="-1"></a> <span class="co">// console.log('Last line of getWelcomeMessage'); // this will be print before response because observable is asynchronous call</span></span>
<span id="cb37-19"><a href="#cb37-19" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb37-20"><a href="#cb37-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-21"><a href="#cb37-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">getWelcomeMessageWithParameter</span>() {</span>
<span id="cb37-22"><a href="#cb37-22" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">welcomeDataService</span></span>
<span id="cb37-23"><a href="#cb37-23" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span><span class="fu">executeHelloWorldBeanServiceWithParameter</span>(<span class="kw">this</span><span class="op">.</span><span class="at">name</span>)</span>
<span id="cb37-24"><a href="#cb37-24" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span><span class="fu">subscribe</span>(</span>
<span id="cb37-25"><a href="#cb37-25" aria-hidden="true" tabindex="-1"></a> (response) <span class="kw">=></span> <span class="kw">this</span><span class="op">.</span><span class="fu">handleSuccessResponse</span>(response)<span class="op">,</span></span>
<span id="cb37-26"><a href="#cb37-26" aria-hidden="true" tabindex="-1"></a> (error) <span class="kw">=></span> <span class="kw">this</span><span class="op">.</span><span class="fu">handleErrorResponse</span>(error)</span>
<span id="cb37-27"><a href="#cb37-27" aria-hidden="true" tabindex="-1"></a> )<span class="op">;</span></span>
<span id="cb37-28"><a href="#cb37-28" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb37-29"><a href="#cb37-29" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>welcome.component.html</em></strong> - created a button
that will fetch data from local backend server and show output message
in html page.</p>
<div class="sourceCode" id="cb38"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="op">...</span></span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a><span class="op"><</span>div <span class="kw">class</span><span class="op">=</span><span class="st">"container"</span><span class="op">></span></span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> Click here to <span class="kw">get</span> welcome message <span class="im">from</span> backend </span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a> <span class="op"><</span><span class="fu">button</span> (click)<span class="op">=</span><span class="st">"getWelcomeMessageWithParameter()"</span> <span class="kw">class</span><span class="op">=</span><span class="st">"btn btn-primary"</span><span class="op">></span>Get Welcome Message<span class="op"></</span>button<span class="op">></span></span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a><span class="op"></</span>div<span class="op">></span></span>
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a><span class="op"><</span>div <span class="kw">class</span><span class="op">=</span><span class="st">"container"</span> <span class="op">*</span>ngIf<span class="op">=</span><span class="st">"welcomeMessageFromService"</span><span class="op">></span></span>
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a> <span class="op"><</span>h3<span class="op">></span>Welcome Message <span class="op">:</span> <span class="op"></</span>h3<span class="op">></span></span>
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a> {{welcomeMessageFromService}}</span>
<span id="cb38-10"><a href="#cb38-10" aria-hidden="true" tabindex="-1"></a><span class="op"></</span>div<span class="op">></span></span></code></pre></div>
<h3 id="crud-operation">CRUD Operation</h3>
<table>
<colgroup>
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 27%" />
<col style="width: 49%" />
</colgroup>
<thead>
<tr class="header">
<th>Operation</th>
<th>Request Method</th>
<th>URI</th>
<th>Returns</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Read all todos</td>
<td>GET</td>
<td>/users/{username}/todos</td>
<td>200 OK with Todo List</td>
</tr>
<tr class="even">
<td>Read one todo</td>
<td>GET</td>
<td>/users/{username}/todos/{todo_id}</td>
<td>200 OK with one Todo</td>
</tr>
<tr class="odd">
<td>Create a todo</td>
<td>POST</td>
<td>/users/{username}/todos/</td>
<td>201 CREATED</td>
</tr>
<tr class="even">
<td>Update a todo</td>
<td>PUT</td>
<td>/users/{username}/todos/{todo_id}</td>
<td>200 OK with updated Todo</td>
</tr>
<tr class="odd">
<td>Delete a todo</td>
<td>DELETE</td>
<td>/users/{username}/todos/{todo_id}</td>
<td>204 NO CONTENT => for Successful Deletion,<br/>404 NOT FOUND
=> for Todo Not Found</td>
</tr>
</tbody>
</table>
<p><strong>VsCode Lombok issue :</strong></p>
<p>Adding Lombok in <strong><em>pom.xml</em></strong></p>
<div class="sourceCode" id="cb39"><pre
class="sourceCode xml"><code class="sourceCode xml"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><<span class="kw">dependency</span>></span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a> <<span class="kw">groupId</span>>org.projectlombok</<span class="kw">groupId</span>></span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a> <<span class="kw">artifactId</span>>lombok</<span class="kw">artifactId</span>></span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a> <<span class="kw">optional</span>>true</<span class="kw">optional</span>></span>
<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a></<span class="kw">dependency</span>></span></code></pre></div>
<p>If your project loads before installing this plugin
<code>Lombok Annotations Support for VS Code</code>, you can run this
command in vscode to reload the project.</p>
<p>Press <code>Command + shift + P</code> and execute:</p>
<div class="sourceCode" id="cb40"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>Java<span class="op">:</span> Clean Java language server workspace</span></code></pre></div>
<h5 id="backend-model-service-controller">Backend Model, Service,
Controller</h5>
<p><strong><em>Todo.java</em></strong></p>
<div class="sourceCode" id="cb41"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="at">@Data</span></span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a><span class="at">@NoArgsConstructor</span></span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="at">@AllArgsConstructor</span></span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a><span class="at">@Builder</span></span>
<span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> <span class="kw">class</span> Todo <span class="op">{</span></span>
<span id="cb41-6"><a href="#cb41-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">private</span> <span class="dt">long</span> id<span class="op">;</span></span>
<span id="cb41-7"><a href="#cb41-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">private</span> <span class="bu">String</span> username<span class="op">;</span></span>
<span id="cb41-8"><a href="#cb41-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">private</span> <span class="bu">String</span> description<span class="op">;</span></span>
<span id="cb41-9"><a href="#cb41-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">private</span> <span class="bu">Date</span> targetDate<span class="op">;</span></span>
<span id="cb41-10"><a href="#cb41-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">private</span> <span class="dt">boolean</span> isDone<span class="op">;</span></span>
<span id="cb41-11"><a href="#cb41-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p><strong><em>TodoHardCodedService.java</em></strong></p>
<div class="sourceCode" id="cb42"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="at">@Service</span></span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> <span class="kw">class</span> TodoHardCodedService <span class="op">{</span></span>
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">private</span> <span class="dt">static</span> <span class="bu">List</span><span class="op"><</span>Todo<span class="op">></span> todos <span class="op">=</span> <span class="kw">new</span> <span class="bu">ArrayList</span><span class="op"><>();</span></span>
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">private</span> <span class="dt">static</span> <span class="dt">int</span> idCounter <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a> <span class="dt">static</span> <span class="op">{</span></span>
<span id="cb42-7"><a href="#cb42-7" aria-hidden="true" tabindex="-1"></a> todos<span class="op">.</span><span class="fu">add</span><span class="op">(</span><span class="kw">new</span> <span class="fu">Todo</span><span class="op">(++</span>idCounter<span class="op">,</span> <span class="st">"user"</span><span class="op">,</span> <span class="st">"My first Todo"</span><span class="op">,</span> <span class="kw">new</span> <span class="bu">Date</span><span class="op">(),</span> <span class="kw">false</span><span class="op">));</span></span>
<span id="cb42-8"><a href="#cb42-8" aria-hidden="true" tabindex="-1"></a> todos<span class="op">.</span><span class="fu">add</span><span class="op">(</span><span class="kw">new</span> <span class="fu">Todo</span><span class="op">(++</span>idCounter<span class="op">,</span> <span class="st">"user"</span><span class="op">,</span> <span class="st">"Spring Boot microservices"</span><span class="op">,</span> <span class="kw">new</span> <span class="bu">Date</span><span class="op">(),</span> <span class="kw">true</span><span class="op">));</span></span>
<span id="cb42-9"><a href="#cb42-9" aria-hidden="true" tabindex="-1"></a> todos<span class="op">.</span><span class="fu">add</span><span class="op">(</span><span class="kw">new</span> <span class="fu">Todo</span><span class="op">(++</span>idCounter<span class="op">,</span> <span class="st">"user"</span><span class="op">,</span> <span class="st">"Angular"</span><span class="op">,</span> <span class="kw">new</span> <span class="bu">Date</span><span class="op">(),</span> <span class="kw">false</span><span class="op">));</span></span>
<span id="cb42-10"><a href="#cb42-10" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb42-11"><a href="#cb42-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p><strong><em>TodoController.java</em></strong></p>
<div class="sourceCode" id="cb43"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="at">@CrossOrigin</span><span class="op">(</span>origins <span class="op">=</span> <span class="st">"http://localhost:4200"</span><span class="op">)</span> <span class="co">// Allowing localhost:4200 so that backend can be called by frontend server</span></span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a><span class="at">@RestController</span></span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> <span class="kw">class</span> TodoController <span class="op">{</span></span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a> <span class="at">@Autowired</span></span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">private</span> TodoHardCodedService todoService<span class="op">;</span></span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<h3 id="read">READ</h3>
<h5 id="readtodos-backend">ReadTodos Backend</h5>
<p><strong><em>TodoHardCodedService.java</em></strong></p>
<div class="sourceCode" id="cb44"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> <span class="bu">List</span><span class="op"><</span>Todo<span class="op">></span> <span class="fu">getAllTodos</span><span class="op">()</span> <span class="op">{</span></span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> todos<span class="op">;</span></span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p><strong><em>TodoController.java</em></strong></p>
<div class="sourceCode" id="cb45"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="at">@GetMapping</span><span class="op">(</span><span class="st">"/users/{username}/todos"</span><span class="op">)</span></span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> <span class="bu">List</span><span class="op"><</span>Todo<span class="op">></span> <span class="fu">getAllTodos</span><span class="op">(</span><span class="at">@PathVariable</span> <span class="bu">String</span> username<span class="op">)</span> <span class="op">{</span></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> todoService<span class="op">.</span><span class="fu">getAllTodos</span><span class="op">();</span></span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a> <span class="co">// returns default 200 OK with Todo List</span></span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<h5 id="readtodos-frontend">ReadTodos FrontEnd</h5>
<p><strong><em>todo-data.service.ts</em></strong> - to get data from
backend</p>
<div class="sourceCode" id="cb46"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a>@<span class="fu">Injectable</span>({</span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a> providedIn<span class="op">:</span> <span class="st">'root'</span></span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a>})</span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> TodoDataService {</span>
<span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(<span class="kw">private</span> http<span class="op">:</span> HttpClient) { }</span>
<span id="cb46-6"><a href="#cb46-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">getAllTodos</span>(username<span class="op">:</span> <span class="dt">any</span>) {</span>
<span id="cb46-7"><a href="#cb46-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">this</span><span class="op">.</span><span class="at">http</span><span class="op">.</span><span class="fu">get</span><span class="op"><</span>Todo[]<span class="op">></span>(<span class="vs">`http://localhost:8080/users/</span><span class="sc">${</span>username<span class="sc">}</span><span class="vs">/todos`</span>)<span class="op">;</span></span>
<span id="cb46-8"><a href="#cb46-8" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb46-9"><a href="#cb46-9" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>list-todos.component.ts</em></strong> - to serve data
from data service to frontend</p>
<div class="sourceCode" id="cb47"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="im">export</span> <span class="kw">class</span> ListTodosComponent <span class="kw">implements</span> OnInit {</span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a> message<span class="op">:</span> <span class="dt">string</span> <span class="op">|</span> <span class="dt">undefined</span><span class="op">;</span></span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a> todos<span class="op">:</span> Todo[] <span class="op">=</span> []<span class="op">;</span></span>
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a> <span class="co">// [</span></span>
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a> <span class="co">// new Todo(1, 'My first Todo', true, new Date()),</span></span>
<span id="cb47-6"><a href="#cb47-6" aria-hidden="true" tabindex="-1"></a> <span class="co">// new Todo(1, 'Welcome to India', false, new Date()),</span></span>
<span id="cb47-7"><a href="#cb47-7" aria-hidden="true" tabindex="-1"></a> <span class="co">// new Todo(1, 'Angular project', true, new Date()),</span></span>
<span id="cb47-8"><a href="#cb47-8" aria-hidden="true" tabindex="-1"></a> <span class="co">// ];</span></span>
<span id="cb47-9"><a href="#cb47-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-10"><a href="#cb47-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span>(<span class="kw">private</span> todoService<span class="op">:</span> TodoDataService) {}</span>
<span id="cb47-11"><a href="#cb47-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-12"><a href="#cb47-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">ngOnInit</span>()<span class="op">:</span> <span class="dt">void</span> {</span>
<span id="cb47-13"><a href="#cb47-13" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="fu">refreshTodos</span>()<span class="op">;</span></span>
<span id="cb47-14"><a href="#cb47-14" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb47-15"><a href="#cb47-15" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb47-16"><a href="#cb47-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">refreshTodos</span>(){</span>
<span id="cb47-17"><a href="#cb47-17" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">todoService</span><span class="op">.</span><span class="fu">getAllTodos</span>(<span class="st">'user'</span>)<span class="op">.</span><span class="fu">subscribe</span>((response) <span class="kw">=></span> {</span>
<span id="cb47-18"><a href="#cb47-18" aria-hidden="true" tabindex="-1"></a> <span class="co">// console.log(response);</span></span>
<span id="cb47-19"><a href="#cb47-19" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">todos</span> <span class="op">=</span> response<span class="op">;</span></span>
<span id="cb47-20"><a href="#cb47-20" aria-hidden="true" tabindex="-1"></a> })<span class="op">;</span></span>
<span id="cb47-21"><a href="#cb47-21" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb47-22"><a href="#cb47-22" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<h3 id="delete">DELETE</h3>
<h5 id="deletetodo-backend">DeleteTodo Backend</h5>
<p><strong><em>TodoHardCodedService.java</em></strong></p>
<div class="sourceCode" id="cb48"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> Todo <span class="fu">findById</span><span class="op">(</span><span class="dt">long</span> id<span class="op">)</span> <span class="op">{</span></span>
<span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span><span class="op">(</span>Todo todo <span class="op">:</span> todos<span class="op">){</span></span>
<span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span><span class="op">(</span>todo<span class="op">.</span><span class="fu">getId</span><span class="op">()</span> <span class="op">==</span> id<span class="op">)</span> <span class="cf">return</span> todo<span class="op">;</span></span>
<span id="cb48-4"><a href="#cb48-4" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb48-5"><a href="#cb48-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">null</span><span class="op">;</span></span>
<span id="cb48-6"><a href="#cb48-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb48-7"><a href="#cb48-7" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> Todo <span class="fu">deleteById</span><span class="op">(</span><span class="dt">long</span> id<span class="op">){</span></span>
<span id="cb48-8"><a href="#cb48-8" aria-hidden="true" tabindex="-1"></a> Todo todo <span class="op">=</span> <span class="fu">findById</span><span class="op">(</span>id<span class="op">);</span></span>
<span id="cb48-9"><a href="#cb48-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-10"><a href="#cb48-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span><span class="op">(</span>todo <span class="op">==</span> <span class="kw">null</span><span class="op">)</span> <span class="cf">return</span> <span class="kw">null</span><span class="op">;</span></span>
<span id="cb48-11"><a href="#cb48-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span><span class="op">(</span>todos<span class="op">.</span><span class="fu">remove</span><span class="op">(</span>todo<span class="op">))</span> <span class="cf">return</span> todo<span class="op">;</span></span>
<span id="cb48-12"><a href="#cb48-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">null</span><span class="op">;</span></span>
<span id="cb48-13"><a href="#cb48-13" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p><strong><em>TodoController.java</em></strong></p>
<div class="sourceCode" id="cb49"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="at">@DeleteMapping</span><span class="op">(</span><span class="st">"/users/{username}/todos/{id}"</span><span class="op">)</span></span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> ResponseEntity<span class="op"><</span><span class="bu">Void</span><span class="op">></span> <span class="fu">deleteTodo</span><span class="op">(</span><span class="at">@PathVariable</span> <span class="bu">String</span> username<span class="op">,</span> <span class="at">@PathVariable</span> <span class="dt">long</span> id<span class="op">)</span> <span class="op">{</span></span>
<span id="cb49-3"><a href="#cb49-3" aria-hidden="true" tabindex="-1"></a> Todo todo <span class="op">=</span> todoService<span class="op">.</span><span class="fu">deleteById</span><span class="op">(</span>id<span class="op">);</span></span>
<span id="cb49-4"><a href="#cb49-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="op">(</span>todo <span class="op">!=</span> <span class="kw">null</span><span class="op">)</span> <span class="op">{</span></span>
<span id="cb49-5"><a href="#cb49-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> ResponseEntity<span class="op">.</span><span class="fu">noContent</span><span class="op">().</span><span class="fu">build</span><span class="op">();</span></span>
<span id="cb49-6"><a href="#cb49-6" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb49-7"><a href="#cb49-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> ResponseEntity<span class="op">.</span><span class="fu">notFound</span><span class="op">().</span><span class="fu">build</span><span class="op">();</span></span>
<span id="cb49-8"><a href="#cb49-8" aria-hidden="true" tabindex="-1"></a> <span class="co">// returns 204 no Content for successful deletion and returns 404 for not found</span></span>
<span id="cb49-9"><a href="#cb49-9" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<h5 id="deletetodo-frontend">DeleteTodo FrontEnd</h5>
<p><strong><em>todo-data.service.ts</em></strong> - to get data from
backend</p>
<div class="sourceCode" id="cb50"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">deleteTodo</span>(username<span class="op">:</span> <span class="dt">any</span><span class="op">,</span> id<span class="op">:</span> <span class="dt">number</span>) {</span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">this</span><span class="op">.</span><span class="at">http</span><span class="op">.</span><span class="fu">delete</span>(<span class="vs">`http://localhost:8080/users/</span><span class="sc">${</span>username<span class="sc">}</span><span class="vs">/todos/</span><span class="sc">${</span>id<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>list-todos.component.ts</em></strong> - to serve data
from data service to frontend</p>
<div class="sourceCode" id="cb51"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="fu">deleteTodo</span>(id<span class="op">:</span> <span class="dt">number</span>) {</span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Todo Deleted </span><span class="sc">${</span>id<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb51-3"><a href="#cb51-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">todoService</span><span class="op">.</span><span class="fu">deleteTodo</span>(<span class="st">'user'</span><span class="op">,</span> id)<span class="op">.</span><span class="fu">subscribe</span>((response) <span class="kw">=></span> {</span>
<span id="cb51-4"><a href="#cb51-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(response)<span class="op">;</span></span>
<span id="cb51-5"><a href="#cb51-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="at">message</span> <span class="op">=</span> <span class="vs">`Delete of Todo </span><span class="sc">${</span>id<span class="sc">}</span><span class="vs"> successful`</span><span class="op">;</span></span>
<span id="cb51-6"><a href="#cb51-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">this</span><span class="op">.</span><span class="fu">refreshTodos</span>()<span class="op">;</span></span>
<span id="cb51-7"><a href="#cb51-7" aria-hidden="true" tabindex="-1"></a> })<span class="op">;</span></span>
<span id="cb51-8"><a href="#cb51-8" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><strong><em>list-todos.component.html</em></strong></p>
<div class="sourceCode" id="cb52"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><h2></span>My Todo's<span class="kw"></h2></span></span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a><span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"alert alert-success"</span> <span class="er">*ngIf</span><span class="ot">=</span><span class="st">"message"</span><span class="kw">></span>{{message}}<span class="kw"></div></span></span>
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a><span class="kw"><div</span> <span class="er">class</span><span class="ot">=</span><span class="st">"container"</span><span class="kw">></span></span>
<span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a> <span class="kw"><table</span> <span class="er">class</span><span class="ot">=</span><span class="st">"table"</span><span class="kw">></span></span>
<span id="cb52-7"><a href="#cb52-7" aria-hidden="true" tabindex="-1"></a> <span class="kw"><thead></span></span>
<span id="cb52-8"><a href="#cb52-8" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tr></span></span>
<span id="cb52-9"><a href="#cb52-9" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Description<span class="kw"></th></span></span>
<span id="cb52-10"><a href="#cb52-10" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Date<span class="kw"></th></span></span>
<span id="cb52-11"><a href="#cb52-11" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Is Completed ?<span class="kw"></th></span></span>
<span id="cb52-12"><a href="#cb52-12" aria-hidden="true" tabindex="-1"></a> <span class="kw"><th></span>Delete<span class="kw"></th></span></span>
<span id="cb52-13"><a href="#cb52-13" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tr></span></span>
<span id="cb52-14"><a href="#cb52-14" aria-hidden="true" tabindex="-1"></a> <span class="kw"></thead></span></span>
<span id="cb52-15"><a href="#cb52-15" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tbody></span></span>
<span id="cb52-16"><a href="#cb52-16" aria-hidden="true" tabindex="-1"></a> <span class="kw"><tr</span> <span class="er">*ngFor</span><span class="ot">=</span><span class="st">"let todo of todos"</span><span class="kw">></span></span>
<span id="cb52-17"><a href="#cb52-17" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.description }}<span class="kw"></td></span></span>
<span id="cb52-18"><a href="#cb52-18" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.targetDate | date }}<span class="kw"></td></span></span>
<span id="cb52-19"><a href="#cb52-19" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td></span>{{ todo.done }}<span class="kw"></td></span></span>
<span id="cb52-20"><a href="#cb52-20" aria-hidden="true" tabindex="-1"></a> <span class="kw"><td><button</span> <span class="er">(click)</span><span class="ot">=</span><span class="st">"deleteTodo(todo.id)"</span> <span class="er">class</span><span class="ot">=</span><span class="st">"btn btn-danger"</span><span class="kw">></span>Delete<span class="kw"></button></td></span></span>
<span id="cb52-21"><a href="#cb52-21" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tr></span></span>
<span id="cb52-22"><a href="#cb52-22" aria-hidden="true" tabindex="-1"></a> <span class="kw"></tbody></span></span>
<span id="cb52-23"><a href="#cb52-23" aria-hidden="true" tabindex="-1"></a> <span class="kw"></table></span></span>
<span id="cb52-24"><a href="#cb52-24" aria-hidden="true" tabindex="-1"></a><span class="kw"></div></span></span></code></pre></div>
<h3 id="update">UPDATE</h3>
<h5 id="updatetodo-backend">UpdateTodo Backend</h5>
<p><strong><em>TodoHardCodedService.java</em></strong></p>
<div class="sourceCode" id="cb53"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> Todo <span class="fu">getTodo</span><span class="op">(</span><span class="dt">long</span> id<span class="op">)</span> <span class="op">{</span> <span class="co">// This data will be populated in update frontend screen</span></span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="fu">findById</span><span class="op">(</span>id<span class="op">);</span></span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-5"><a href="#cb53-5" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> Todo <span class="fu">save</span><span class="op">(</span>Todo todo<span class="op">)</span> <span class="op">{</span></span>
<span id="cb53-6"><a href="#cb53-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="op">(</span>todo<span class="op">.</span><span class="fu">getId</span><span class="op">()</span> <span class="op">==</span> <span class="op">-</span><span class="dv">1</span> <span class="op">||</span> todo<span class="op">.</span><span class="fu">getId</span><span class="op">()</span> <span class="op">==</span> <span class="dv">0</span><span class="op">)</span> <span class="op">{</span> <span class="co">// Saving Todo if not exist</span></span>
<span id="cb53-7"><a href="#cb53-7" aria-hidden="true" tabindex="-1"></a> todo<span class="op">.</span><span class="fu">setId</span><span class="op">(++</span>idCounter<span class="op">);</span></span>
<span id="cb53-8"><a href="#cb53-8" aria-hidden="true" tabindex="-1"></a> todos<span class="op">.</span><span class="fu">add</span><span class="op">(</span>todo<span class="op">);</span></span>
<span id="cb53-9"><a href="#cb53-9" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span> <span class="cf">else</span> <span class="op">{</span></span>
<span id="cb53-10"><a href="#cb53-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">deleteById</span><span class="op">(</span>todo<span class="op">.</span><span class="fu">getId</span><span class="op">());</span> <span class="co">// Deleting and adding Todo if already exists.</span></span>
<span id="cb53-11"><a href="#cb53-11" aria-hidden="true" tabindex="-1"></a> todos<span class="op">.</span><span class="fu">add</span><span class="op">(</span>todo<span class="op">);</span></span>
<span id="cb53-12"><a href="#cb53-12" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb53-13"><a href="#cb53-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> todo<span class="op">;</span></span>
<span id="cb53-14"><a href="#cb53-14" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p><strong><em>TodoController.java</em></strong></p>
<div class="sourceCode" id="cb54"><pre
class="sourceCode java"><code class="sourceCode java"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a><span class="at">@PutMapping</span><span class="op">(</span><span class="st">"/users/{username}/todos/{id}"</span><span class="op">)</span></span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> ResponseEntity<span class="op"><</span>Todo<span class="op">></span> <span class="fu">updateTodo</span><span class="op">(</span><span class="at">@PathVariable</span> <span class="bu">String</span> username<span class="op">,</span> <span class="at">@PathVariable</span> <span class="dt">long</span> id<span class="op">,</span></span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a> <span class="at">@RequestBody</span> Todo todo<span class="op">)</span> <span class="op">{</span></span>
<span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a> Todo todoUpdated <span class="op">=</span> todoService<span class="op">.</span><span class="fu">save</span><span class="op">(</span>todo<span class="op">);</span></span>
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">new</span> ResponseEntity<span class="op"><</span>Todo<span class="op">>(</span>todo<span class="op">,</span> HttpStatus<span class="op">.</span><span class="fu">OK</span><span class="op">);</span> </span>
<span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a> <span class="co">// returns 200 OK status with updated Todo</span></span>
<span id="cb54-7"><a href="#cb54-7" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<h5 id="updatetodo-frontend">UpdateTodo FrontEnd</h5>
<p>We need <strong>Todo Component</strong> to <strong>Update</strong> /
<strong>Add</strong> a new Todo in Todo List.
(<code>ng generate component todo</code>)</p>
<p><strong><em>app-routing.module.ts</em></strong> - to get data from
backend</p>
<div class="sourceCode" id="cb55"><pre
class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a>{ path<span class="op">:</span> <span class="st">'todos/:id'</span><span class="op">,</span> component<span class="op">:</span> TodoComponent<span class="op">,</span> canActivate<span class="op">:</span>[RouteGuardService] }<span class="op">,</span></span></code></pre></div>
<p><strong><em>todo-data.service.ts</em></strong> - to get data from
backend</p>
<div class="sourceCode" id="cb56"><pre