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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
//! Implementation of REST APIs.

/// Types Used in REST Messages
pub mod rest_types {
    include!("../../../openapi/types.rs");
}
use std::str::FromStr;

pub use rest_types::*;

use axum::{extract::Path, Extension, Json};
use hyper::StatusCode;
use svc_storage_client_grpc::prelude::*;

use super::structs::{Aircraft, AssetGroup, Operator, Vertipad, Vertiport};
use crate::grpc::client::GrpcClients;
use uuid::Uuid;

//===========================================================
// Helpers
//===========================================================

/// Check if a string is a valid UUID.
fn is_uuid(s: &str) -> bool {
    uuid::Uuid::try_parse(s).is_ok()
}

//===========================================================
// REST API Implementations
//===========================================================
/// Health check for load balancing
#[utoipa::path(
    get,
    path = "/health",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Service is healthy, all dependencies running."),
        (status = 503, description = "Service is unhealthy, one or more dependencies unavailable.")
    )
)]
pub async fn health_check(
    Extension(grpc_clients): Extension<GrpcClients>,
) -> Result<(), StatusCode> {
    rest_debug!("(health_check) entry.");

    let mut ok = true;

    if grpc_clients
        .storage
        .vertiport
        .is_ready(ReadyRequest {})
        .await
        .is_err()
    {
        let error_msg = "svc-storage vertiport unavailable.".to_string();
        rest_error!("(health_check) {}.", &error_msg);
        ok = false;
    }
    if grpc_clients
        .storage
        .vertipad
        .is_ready(ReadyRequest {})
        .await
        .is_err()
    {
        let error_msg = "svc-storage vertipad unavailable.".to_string();
        rest_error!("(health_check) {}.", &error_msg);
        ok = false;
    }
    if grpc_clients
        .storage
        .vehicle
        .is_ready(ReadyRequest {})
        .await
        .is_err()
    {
        let error_msg = "svc-storage vehicle unavailable.".to_string();
        rest_error!("(health_check) {}.", &error_msg);
        ok = false;
    }

    match ok {
        true => {
            rest_debug!("(health_check) healthy, all dependencies running.");
            Ok(())
        }
        false => {
            rest_error!("(health_check) unhealthy, 1+ dependencies down.");
            Err(StatusCode::SERVICE_UNAVAILABLE)
        }
    }
}

/// Get info about an operator by id.
#[utoipa::path(
    get,
    path = "/assets/operators/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Operator found in database", body = Operator),
        (status = 404, description = "Operator not found in database"),
        (status = 400, description = "Invalid operator id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Operator id"),
    )
)]
pub async fn get_operator(
    Extension(mut _grpc_clients): Extension<GrpcClients>,
    Path(operator_id): Path<String>,
) -> Result<Json<Operator>, (StatusCode, String)> {
    rest_info!("(get_operator) {}", operator_id);
    if !is_uuid(&operator_id) {
        return Err((StatusCode::BAD_REQUEST, "Invalid operator id".to_string()));
    }
    // Get Client
    // TODO(R4): let _client_option = grpc_clients.storage.get_client().await;
    // if client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(get_operator) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }
    // let mut client = client_option.unwrap();
    Ok(Json(Operator::random()))
}

#[utoipa::path(
    get,
    path = "/assets/demo/aircraft",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Assets successfully found", body = [Aircraft]),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
)]
/// Get all aircraft from the database.
pub async fn get_all_aircraft(
    Extension(grpc_clients): Extension<GrpcClients>,
) -> Result<Json<Vec<Aircraft>>, (StatusCode, String)> {
    rest_info!("(get_all_aircraft) entry.");
    let filter = AdvancedSearchFilter::search_is_not_null(String::from("deleted_at"));

    let vehicle_client = grpc_clients.storage.vehicle;
    let mut vehicles = match vehicle_client.search(filter.clone()).await {
        Ok(response) => response.into_inner().list,
        Err(e) => {
            let error_msg = "could not retrieve vehicles.".to_string();
            rest_error!("(get_all_aircraft) {}: {}.", error_msg, e);
            return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
        }
    };

    let mut assets = Vec::new();

    for vehicle in vehicles.drain(..) {
        let aircraft: Aircraft = match vehicle.try_into() {
            Ok(object) => object,
            Err(_) => {
                let error_msg = "could not convert VehicleObject to Aircraft.".to_string();
                rest_error!("(get_all_aircraft) {}", &error_msg);
                return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
            }
        };
        assets.push(aircraft);
    }

    Ok(Json(assets))
}

#[utoipa::path(
    get,
    path = "/assets/demo/vertiports",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Assets successfully found", body = [Vertiport]),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
)]
/// Get all vertiports from the database.
pub async fn get_all_vertiports(
    Extension(grpc_clients): Extension<GrpcClients>,
) -> Result<Json<Vec<Vertiport>>, (StatusCode, String)> {
    rest_info!("(get_all_vertiports) entry.");
    let filter = AdvancedSearchFilter::search_is_not_null(String::from("deleted_at"));

    let vertiport_client = grpc_clients.storage.vertiport;
    let mut vertiports = match vertiport_client.search(filter.clone()).await {
        Ok(response) => response.into_inner().list,
        Err(e) => {
            let error_msg = "could not retrieve vertiports.".to_string();
            rest_error!("(get_all_vertiports) {}: {}.", error_msg, e);
            return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
        }
    };

    let mut assets = Vec::new();

    for vertiport in vertiports.drain(..) {
        let vertiport: Vertiport = match vertiport.try_into() {
            Ok(object) => object,
            Err(_) => {
                let error_msg = "could not convert VertiportObject to Vertiport.".to_string();
                rest_error!("(get_all_vertiports) {}", &error_msg);
                return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
            }
        };
        assets.push(vertiport);
    }

    Ok(Json(assets))
}

#[utoipa::path(
    get,
    path = "/assets/demo/vertipads",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Assets successfully found", body = [Vertipad]),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
)]
/// Get all vertipads from the database.
pub async fn get_all_vertipads(
    Extension(grpc_clients): Extension<GrpcClients>,
) -> Result<Json<Vec<Vertipad>>, (StatusCode, String)> {
    rest_info!("(get_all_vertipads) entry.");
    let filter = AdvancedSearchFilter::search_is_not_null(String::from("deleted_at"));

    let vertipad_client = grpc_clients.storage.vertipad;
    let mut vertipads = match vertipad_client.search(filter.clone()).await {
        Ok(response) => response.into_inner().list,
        Err(e) => {
            let error_msg = "could not retrieve vertipads.".to_string();
            rest_error!("(get_all_vertipads) {}: {}.", error_msg, e);
            return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
        }
    };

    let mut assets = Vec::new();

    for vertipad in vertipads.drain(..) {
        let vertipad: Vertipad = match vertipad.try_into() {
            Ok(object) => object,
            Err(_) => {
                let error_msg = "could not convert VertipadObject to Vertipad.".to_string();
                rest_error!("(get_all_vertipads) {}", &error_msg);
                return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
            }
        };
        assets.push(vertipad);
    }

    Ok(Json(assets))
}

//-----------------------------------------------------------
// Get assets by operator
//-----------------------------------------------------------
#[utoipa::path(
    get,
    path = "/assets/operators/{id}/assets",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Assets found from database for operator {id}", body = [String]),
        (status = 404, description = "Operator not found in database"),
        (status = 400, description = "Invalid operator id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Operator id"),
    )
)]
/// Get all assets belonging to an operator.
pub async fn get_all_assets_by_operator(
    Extension(mut _grpc_clients): Extension<GrpcClients>,
    Path(operator_id): Path<String>,
) -> Result<Json<Vec<Uuid>>, (StatusCode, String)> {
    rest_info!("(get_all_assets_by_operator) {}", operator_id);
    if !is_uuid(&operator_id) {
        return Err((StatusCode::BAD_REQUEST, "Invalid operator id".to_string()));
    }
    // Get Client
    // let vertiport_client_option = grpc_clients.storage_vertiport.get_client().await;
    // let vertipad_client_option = grpc_clients.storage_vertipad.get_client().await;
    // if vertiport_client_option.is_none() || vertipad_client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(get_all_assets) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }

    // let request = SearchFilter {
    //     search_field: "".to_string(),
    //     search_value: "".to_string(),
    //     page_number: 1,
    //     results_per_page: 50,
    // });

    // let mut vertiport_client = vertiport_client_option.unwrap();
    // let mut vertipad_client = vertipad_client_option.unwrap();
    // let mut result = Vec::new();
    // // Get Vertiports
    // let vertiports = vertiport_client
    //     .get_all_with_filter(request)
    //     .await
    //     .map_err(|e| {
    //         rest_error!("(get_all_assets) Error getting vertiports: {}", e);
    //         (
    //             StatusCode::SERVICE_UNAVAILABLE,
    //             "Error getting vertiports".to_string(),
    //         )
    //     })?
    //     .into_inner()
    //     .vertiports;
    // TODO(R4)
    Ok(Json(vec![]))
}

/// Get all grouped assets belonging to an operator.
///
/// These are the assets NOT being delegated to or from this operator.
///
/// Returns a list of grouped asset ids.
#[utoipa::path(
    get,
    path = "/assets/operators/{id}/grouped",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Grouped assets found from database for operator {id}", body = [String]),
        (status = 404, description = "Operator not found in database"),
        (status = 400, description = "Invalid operator id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Operator id"),
    )
)]
pub async fn get_all_grouped_assets(
    Extension(mut _grpc_clients): Extension<GrpcClients>,
    Path(operator_id): Path<String>,
) -> Result<Json<Vec<Uuid>>, (StatusCode, String)> {
    rest_info!("(get_all_grouped_assets) {}", operator_id);
    if !is_uuid(&operator_id) {
        return Err((StatusCode::BAD_REQUEST, "Invalid operator id".to_string()));
    }
    // Get Client
    // let _client_option = grpc_clients.storage.get_client().await;
    // if client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(get_all_grouped_assets) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }
    // let mut client = client_option.unwrap();
    // TODO(R4)
    Ok(Json(vec![]))
}

/// Get all grouped assets delegated to an operator.
#[utoipa::path(
    get,
    path = "/assets/operators/{id}/grouped/delegated-to",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Grouped assets delegated to operator {id} found from database", body = [String]),
        (status = 404, description = "Operator not found in database"),
        (status = 400, description = "Invalid operator id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Operator id"),
    )
)]
pub async fn get_all_grouped_assets_delegated_to(
    Extension(mut _grpc_clients): Extension<GrpcClients>,
    Path(operator_id): Path<String>,
) -> Result<Json<Vec<Uuid>>, (StatusCode, String)> {
    rest_info!("(get_all_grouped_assets_delegated_to) {}", operator_id);
    if !is_uuid(&operator_id) {
        return Err((StatusCode::BAD_REQUEST, "Invalid operator id".to_string()));
    }
    // Get Client
    // let _client_option = grpc_clients.storage.get_client().await;
    // if client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(get_all_grouped_assets_delegated_to) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }
    // let mut client = client_option.unwrap();
    // TODO(R4)
    Ok(Json(vec![]))
}

/// Get all grouped assets delegated from an operator.
#[utoipa::path(
    get,
    path = "/assets/operators/{id}/grouped/delegated-from",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Grouped assets delegated from operator {id} found from database", body = [String]),
        (status = 404, description = "Operator not found in database"),
        (status = 400, description = "Invalid operator id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Operator id"),
    )
)]
pub async fn get_all_grouped_assets_delegated_from(
    Extension(mut _grpc_clients): Extension<GrpcClients>,
    Path(operator_id): Path<String>,
) -> Result<Json<Vec<Uuid>>, (StatusCode, String)> {
    rest_info!("(get_all_grouped_assets_delegated_from) {}", operator_id);
    if !is_uuid(&operator_id) {
        return Err((StatusCode::BAD_REQUEST, "Invalid operator id".to_string()));
    }
    // Get Client
    // let _client_option = grpc_clients.storage.get_client().await;
    // if client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(get_all_grouped_assets_delegated_from) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }
    // let mut client = client_option.unwrap();
    // TODO(R4)
    Ok(Json(vec![]))
}

//-----------------------------------------------------------
// Get assets by asset id
//-----------------------------------------------------------

/// Get an [`Aircraft`] by its id.
#[utoipa::path(
    get,
    path = "/assets/aircraft/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Aircraft {id} found from database", body = Aircraft),
        (status = 404, description = "Aircraft not found in database"),
        (status = 400, description = "Invalid aircraft id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Aircraft id"),
    )
)]
pub async fn get_aircraft_by_id(
    Extension(grpc_clients): Extension<GrpcClients>,
    Path(aircraft_id): Path<String>,
) -> Result<Json<Aircraft>, (StatusCode, String)> {
    rest_info!("(get_aircraft_by_id) entry [{}].", aircraft_id);
    if !is_uuid(&aircraft_id) {
        return Err((StatusCode::BAD_REQUEST, "Invalid aircraft id".to_string()));
    }

    let client = grpc_clients.storage.vehicle;
    match client
        .get_by_id(Id {
            id: aircraft_id.clone(),
        })
        .await
    {
        Ok(response) => {
            let vehicle = response.into_inner();
            let aircraft: Aircraft = match vehicle.try_into() {
                Ok(aircraft) => {
                    rest_info!("(get_aircraft_by_id) Aircraft found: {}", aircraft_id);
                    aircraft
                }
                Err(e) => {
                    let error_msg = format!("Error converting vehicle to aircraft: {}", e);
                    rest_error!("(get_aircraft_by_id) {}", &error_msg);
                    return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
                }
            };
            Ok(Json(aircraft))
        }
        Err(e) => {
            let error_msg = format!("Error getting aircraft from storage: {}", e);
            rest_error!("(get_aircraft_by_id) {}", &error_msg);
            Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg))
        }
    }
}

/// Get an [`Vertipad`] by its id.
#[utoipa::path(
    get,
    path = "/assets/vertipads/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Vertipad {id} found from database", body = Vertipad),
        (status = 404, description = "Vertipad not found in database"),
        (status = 400, description = "Invalid vertipad id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Vertipad id"),
    )
)]
pub async fn get_vertipad_by_id(
    Extension(grpc_clients): Extension<GrpcClients>,
    Path(vertipad_id): Path<String>,
) -> Result<Json<Vertipad>, (StatusCode, String)> {
    rest_info!("(get_vertipad_by_id) entry [{}].", vertipad_id);
    if !is_uuid(&vertipad_id) {
        return Err((StatusCode::BAD_REQUEST, "Invalid vertipad id".to_string()));
    }

    let client = grpc_clients.storage.vertipad;
    match client
        .get_by_id(Id {
            id: vertipad_id.clone(),
        })
        .await
    {
        Ok(response) => {
            let vertipad = response.into_inner();
            let vertipad: Vertipad = match vertipad.try_into() {
                Ok(vertipad) => {
                    rest_info!("(get_vertipad_by_id) Vertipad found: {}", vertipad_id);
                    vertipad
                }
                Err(e) => {
                    let error_msg = format!("Error converting vehicle to vertipad: {}", e);
                    rest_error!("(get_vertipad_by_id) {}", &error_msg);
                    return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
                }
            };
            Ok(Json(vertipad))
        }
        Err(e) => {
            let error_msg = format!("Error getting vertipad from storage: {}", e);
            rest_error!("(get_vertipad_by_id) {}", &error_msg);
            Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg))
        }
    }
}

/// Get an [`Vertiport`] by its id.
#[utoipa::path(
    get,
    path = "/assets/vertiports/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Vertiport {id} found from database", body = Vertiport),
        (status = 404, description = "Vertiport not found in database"),
        (status = 400, description = "Invalid vertiport id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Vertiport id"),
    )
)]
pub async fn get_vertiport_by_id(
    Extension(grpc_clients): Extension<GrpcClients>,
    Path(vertiport_id): Path<String>,
) -> Result<Json<Vertiport>, (StatusCode, String)> {
    rest_info!("(get_vertiport_by_id) entry [{}].", vertiport_id);
    if !is_uuid(&vertiport_id) {
        return Err((StatusCode::BAD_REQUEST, "Invalid vertiport id".to_string()));
    }

    let client = grpc_clients.storage.vertiport;
    match client
        .get_by_id(Id {
            id: vertiport_id.clone(),
        })
        .await
    {
        Ok(response) => {
            let vertiport = response.into_inner();
            let vertiport: Vertiport = match vertiport.try_into() {
                Ok(vertiport) => {
                    rest_info!("(get_vertiport_by_id) Vertiport found: {}", vertiport_id);
                    vertiport
                }
                Err(e) => {
                    let error_msg = format!("Error converting vehicle to vertiport: {}", e);
                    rest_error!("(get_vertiport_by_id) {}", &error_msg);
                    return Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg));
                }
            };
            Ok(Json(vertiport))
        }
        Err(e) => {
            let error_msg = format!("Error getting vertiport from storage: {}", e);
            rest_error!("(get_vertiport_by_id) {}", &error_msg);
            Err((StatusCode::INTERNAL_SERVER_ERROR, error_msg))
        }
    }
}

/// Get an [`AssetGroup`](crate::rest::structs::AssetGroup) by its id.
#[utoipa::path(
    get,
    path = "/assets/groups/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Asset group {id} found from database", body = AssetGroup),
        (status = 404, description = "Asset group not found in database"),
        (status = 400, description = "Invalid asset group id"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Asset group id"),
    )
)]
pub async fn get_asset_group_by_id(
    Extension(mut _grpc_clients): Extension<GrpcClients>,
    Path(asset_group_id): Path<String>,
) -> Result<Json<AssetGroup>, (StatusCode, String)> {
    rest_info!("(get_asset_group_by_id) {}", asset_group_id);
    if !is_uuid(&asset_group_id) {
        return Err((
            StatusCode::BAD_REQUEST,
            "Invalid asset group id".to_string(),
        ));
    }
    // Get Client
    // let _client_option = grpc_clients.storage.get_client().await;
    // if client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(get_asset_group_by_id) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }
    // let mut client = client_option.unwrap();

    // TODO(R4)
    Ok(Json(AssetGroup::random()))
}

//-----------------------------------------------------------
// Register assets
//-----------------------------------------------------------

/// Register an [`Aircraft`] in the database.
#[utoipa::path(
    post,
    path = "/assets/aircraft",
    tag = "svc-assets",
    request_body=vehicle::Data,
    responses(
        (status = 200, description = "Aircraft registered in database; a UUID is returned", body = String),
        (status = 422, description = "Request body is invalid format"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    )
)]
pub async fn register_aircraft(
    Extension(grpc_clients): Extension<GrpcClients>,
    Json(payload): Json<vehicle::Data>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(register_aircraft) entry.");
    rest_debug!("(register_aircraft) Payload: {:?}", &payload);

    let client = grpc_clients.storage.vehicle;

    match client.insert(payload).await {
        Ok(res) => {
            rest_info!("(register_aircraft) registration success.");
            rest_debug!("(register_aircraft) {:?}", res);
            let vehicle_obj = res.into_inner().object;
            if let Some(vehicle_obj) = vehicle_obj {
                rest_debug!(
                    "(register_aircraft) got new aircraft id: {}",
                    vehicle_obj.id
                );
                Ok(vehicle_obj.id)
            } else {
                Err((
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "(register_aircraft) could not insert vehicle.".to_string(),
                ))
            }
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

/// Register an [`Vertiport`] in the database.
#[utoipa::path(
    post,
    path = "/assets/vertiports",
    tag = "svc-assets",
    request_body=vertiport::Data,
    responses(
        (status = 200, description = "Vertiport registered in database; a UUID is returned", body = String),
        (status = 422, description = "Request body is invalid format"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    )
)]
pub async fn register_vertiport(
    Extension(grpc_clients): Extension<GrpcClients>,
    Json(payload): Json<vertiport::Data>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(register_vertiport) entry.");
    rest_debug!("(register_vertiport) Payload: {:?}", &payload);

    let client = grpc_clients.storage.vertiport;

    match client.insert(payload).await {
        Ok(res) => {
            rest_info!("(register_vertiport) registration success.");
            rest_debug!("(register_vertiport) {:?}", res);
            let vertiport_obj = res.into_inner().object;
            if let Some(vertiport_obj) = vertiport_obj {
                rest_debug!(
                    "(register_vertiport) got new vertiport id: {}",
                    vertiport_obj.id
                );
                Ok(vertiport_obj.id)
            } else {
                Err((
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "(register_vertiport) could not insert vertiport.".to_string(),
                ))
            }
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

/// Register an [`Vertipad`] in the database.
///
/// Also inserts the vertipad into the vertiport's vertipad list.
#[utoipa::path(
    post,
    path = "/assets/vertipads",
    tag = "svc-assets",
    request_body=vertipad::Data,
    responses(
        (status = 200, description = "Vertipad registered in database; a UUID is returned", body = String),
        (status = 422, description = "Request body is invalid format"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    )
)]
pub async fn register_vertipad(
    Extension(grpc_clients): Extension<GrpcClients>,
    Json(payload): Json<vertipad::Data>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(register_vertipad) entry.");
    rest_debug!("(register_vertipad) Payload: {:?}", &payload);

    let client = grpc_clients.storage.vertipad;

    match client.insert(payload).await {
        Ok(res) => {
            rest_info!("(register_vertipad) registration success.");
            rest_debug!("(register_vertipad) {:?}", res);
            let vertipad_obj = res.into_inner().object;
            if let Some(vertipad_obj) = vertipad_obj {
                rest_debug!(
                    "(register_vertipad) got new vertipad id: {}",
                    vertipad_obj.id
                );
                Ok(vertipad_obj.id)
            } else {
                Err((
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "(register_vertipad) could not insert vertipad.".to_string(),
                ))
            }
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

//-----------------------------------------------------------
// Group management
//-----------------------------------------------------------

/// Register an [`AssetGroup`](crate::rest::structs::AssetGroup) in the database.
#[utoipa::path(
    post,
    path = "/assets/groups",
    tag = "svc-assets",
    request_body=RegisterAssetGroupPayload,
    responses(
        (status = 200, description = "AssetGroup registered in database; a UUID is returned", body = String),
        (status = 422, description = "Request body is invalid format"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    )
)]
pub async fn register_asset_group(
    Extension(mut _grpc_clients): Extension<GrpcClients>,
    Json(payload): Json<RegisterAssetGroupPayload>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(register_asset_group) with payload: {:?}", &payload);

    let _asset_group = AssetGroup {
        id: Uuid::new_v4().to_string(),
        name: payload.name,
        owner: payload.owner,
        created_at: None,
        updated_at: None,
        delegatee: None,
        assets: payload.assets,
    };

    // Get Client
    // let _client_option = grpc_clients.storage.get_client().await;
    // if client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(get_asset_group_by_id) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }
    // let mut client = client_option.unwrap();

    // TODO(R4)
    Ok(_asset_group.id)
}

//-----------------------------------------------------------
// Asset Updates
//-----------------------------------------------------------

/// Update/modify an [`Aircraft`] in the database.
///
/// This will update the aircraft's information.
#[utoipa::path(
    put,
    path = "/assets/aircraft",
    tag = "svc-assets",
    request_body=UpdateAircraftPayload,
    responses(
        (status = 200, description = "Aircraft updated in database; a UUID is returned", body = String),
        (status = 422, description = "Request body is invalid format"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    )
)]
pub async fn update_aircraft(
    Extension(grpc_clients): Extension<GrpcClients>,
    Json(payload): Json<UpdateAircraftPayload>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(update_aircraft) entry [{}].", payload.id);
    rest_debug!("(update_aircraft) Payload: {:?}", &payload);

    let vehicle_id = payload.id.clone();
    let client = grpc_clients.storage.vehicle;

    let response = match client
        .get_by_id(Id {
            id: vehicle_id.clone(),
        })
        .await
    {
        Ok(res) => {
            rest_debug!("(update_aircraft) successfully got vehicle {:?}", res);
            res
        }
        Err(e) => {
            return Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string()));
        }
    };

    let vehicle = match response.into_inner().data {
        Some(data) => data,
        None => {
            return Err((StatusCode::NOT_FOUND, "Vehicle not found".to_string()));
        }
    };

    match client
        .update(vehicle::UpdateObject {
            id: vehicle_id.clone(),
            data: Some(vehicle::Data {
                hangar_id: payload.hangar_id,
                hangar_bay_id: payload.hangar_bay_id,
                vehicle_model_id: payload.vehicle_model_id.unwrap_or(vehicle.vehicle_model_id),
                serial_number: payload.serial_number.unwrap_or(vehicle.serial_number),
                registration_number: payload
                    .registration_number
                    .unwrap_or(vehicle.registration_number),
                description: payload.description.unwrap_or(vehicle.description),
                asset_group_id: payload.asset_group_id.unwrap_or(vehicle.asset_group_id),
                schedule: payload.schedule.unwrap_or(vehicle.schedule),
                last_maintenance: if let Some(last_maintenance) = payload.last_maintenance {
                    match last_maintenance {
                        Some(last_maintenance) => match Timestamp::from_str(&last_maintenance) {
                            Ok(time_stamp) => Some(time_stamp),
                            Err(e) => {
                                rest_error!("(update_aircraft) {}", &e.to_string());
                                return Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string()));
                            }
                        },
                        None => None,
                    }
                } else {
                    vehicle.last_maintenance
                },

                next_maintenance: if let Some(next_maintenance) = payload.next_maintenance {
                    match next_maintenance {
                        Some(next_maintenance) => match Timestamp::from_str(&next_maintenance) {
                            Ok(time_stamp) => Some(time_stamp),
                            Err(e) => {
                                rest_error!("(update_aircraft) {}", &e.to_string());
                                return Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string()));
                            }
                        },
                        None => None,
                    }
                } else {
                    vehicle.next_maintenance
                },
                created_at: None,
                updated_at: None,
            }),
            mask: Some(FieldMask {
                paths: payload.mask,
            }),
        })
        .await
    {
        Ok(res) => {
            rest_info!("(update_aircraft) successfully updated vehicle {:?}", res);
            Ok(vehicle_id.clone())
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

/// Update/modify a [`Vertiport`] in the database.
///
/// This will update the vertiport's information. It can also be used to
/// perform batch add/remove of vertipads.
#[utoipa::path(
    put,
    path = "/assets/vertiports",
    tag = "svc-assets",
    request_body=UpdateVertiportPayload,
    responses(
        (status = 200, description = "Vertiport updated in database; a UUID is returned", body = String),
        (status = 422, description = "Request body is invalid format"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    )
)]
pub async fn update_vertiport(
    Extension(grpc_clients): Extension<GrpcClients>,
    Json(payload): Json<UpdateVertiportPayload>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(update_vertiport) entry [{}].", payload.id);
    rest_debug!("(update_vertiport) Payload: {:?}", &payload);

    let client = grpc_clients.storage.vertiport;

    let response = match client
        .get_by_id(Id {
            id: payload.id.clone(),
        })
        .await
    {
        Ok(res) => {
            rest_info!("(update_vertiport) successfully got vertiport {:?}", res);
            res
        }
        Err(e) => {
            return Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string()));
        }
    };

    let vertiport = match response.into_inner().data {
        Some(data) => data,
        None => {
            return Err((StatusCode::NOT_FOUND, "Vertiport not found".to_string()));
        }
    };

    match client
        .update(vertiport::UpdateObject {
            id: payload.id.clone(),
            data: Some(vertiport::Data {
                name: payload.name.unwrap_or(vertiport.name),
                description: payload.description.unwrap_or(vertiport.description),
                geo_location: payload.geo_location.or(vertiport.geo_location),
                schedule: payload.schedule.unwrap_or(vertiport.schedule),
                created_at: None,
                updated_at: None,
            }),
            mask: Some(FieldMask {
                paths: payload.mask,
            }),
        })
        .await
    {
        Ok(res) => {
            rest_info!(
                "(update_vertiport) successfully updated vertiport {:?}",
                res
            );
            Ok(payload.id.clone())
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

/// Update/modify a [`Vertipad`] in the database.
#[utoipa::path(
    put,
    path = "/assets/vertipads",
    tag = "svc-assets",
    request_body=UpdateVertipadPayload,
    responses(
        (status = 200, description = "Vertipad updated in database; a UUID is returned", body = String),
        (status = 422, description = "Request body is invalid format"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    )
)]
pub async fn update_vertipad(
    Extension(grpc_clients): Extension<GrpcClients>,
    Json(payload): Json<UpdateVertipadPayload>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(update_vertipad) entry [{}].", payload.id);
    rest_debug!("(update_vertipad) Payload: {:?}", &payload);

    let client = grpc_clients.storage.vertipad;

    let response = match client
        .get_by_id(Id {
            id: payload.id.clone(),
        })
        .await
    {
        Ok(res) => {
            rest_info!("(update_vertipad) successfully got vertipad {:?}", res);
            res
        }
        Err(e) => {
            return Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string()));
        }
    };

    let vertipad = match response.into_inner().data {
        Some(data) => data,
        None => {
            return Err((StatusCode::NOT_FOUND, "Vertipad not found".to_string()));
        }
    };

    match client
        .update(vertipad::UpdateObject {
            id: payload.id.clone(),
            data: Some(vertipad::Data {
                name: payload.name.unwrap_or(vertipad.name),
                geo_location: payload.geo_location.or(vertipad.geo_location),
                enabled: payload.enabled.unwrap_or(vertipad.enabled),
                occupied: payload.occupied.unwrap_or(vertipad.occupied),
                schedule: payload.schedule.unwrap_or(vertipad.schedule),
                vertiport_id: payload.vertiport_id.unwrap_or(vertipad.vertiport_id),
                created_at: None,
                updated_at: None,
            }),
            mask: Some(FieldMask {
                paths: payload.mask,
            }),
        })
        .await
    {
        Ok(res) => {
            rest_info!("(update_vertipad) successfully updated vertipad {:?}", res);
            Ok(payload.id.clone())
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

/// Update/modify an [`AssetGroup`](crate::rest::structs::AssetGroup) in the database.
#[utoipa::path(
    put,
    path = "/assets/groups/{id}",
    tag = "svc-assets",
    request_body=AssetGroup,
    responses(
        (status = 200, description = "AssetGroup updated in database; a UUID is returned", body = String),
        (status = 422, description = "Request body is invalid format"),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "AssetGroup id"),
    )
)]
pub async fn update_asset_group(
    Extension(mut _grpc_clients): Extension<GrpcClients>,
    Json(payload): Json<AssetGroup>,
    Path(_id): Path<String>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(update_asset_group) with payload: {:?}", &payload);

    // Get Client
    // let _client_option = grpc_clients.storage.get_client().await;
    // if client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(get_asset_group_by_id) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }
    // let mut client = client_option.unwrap();

    // TODO(R4)
    Ok(payload.id)
}

//-----------------------------------------------------------
// Asset Deletion
//-----------------------------------------------------------

/// Remove a [`Aircraft`] from the database.
#[utoipa::path(
    delete,
    path = "/assets/aircraft/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Aircraft removed from database; a UUID is returned", body = String),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Aircraft id"),
    )
)]
pub async fn remove_aircraft(
    Extension(grpc_clients): Extension<GrpcClients>,
    Path(id): Path<String>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(remove_aircraft) entry [{}].", &id);

    let client = grpc_clients.storage.vehicle;

    match client.delete(Id { id: id.clone() }).await {
        Ok(res) => {
            rest_info!("(remove_aircraft) successfully removed aircraft {:?}", res);
            Ok(id)
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

/// Remove a [`Vertipad`] from the database.
#[utoipa::path(
    delete,
    path = "/assets/vertipads/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Vertipad removed from database; a UUID is returned", body = String),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Vertipad id"),
    )
)]
pub async fn remove_vertipad(
    Extension(grpc_clients): Extension<GrpcClients>,
    Path(id): Path<String>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(remove_vertipad) entry [{}].", &id);

    let client = grpc_clients.storage.vertipad;

    match client.delete(Id { id: id.clone() }).await {
        Ok(res) => {
            rest_info!("(remove_vertipad) successfully removed vertipad {:?}", res);
            Ok(id)
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

/// Remove a [`Vertiport`] from the database.
#[utoipa::path(
    delete,
    path = "/assets/vertiports/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "Vertiport removed from database; a UUID is returned", body = String),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "Vertiport id"),
    )
)]
pub async fn remove_vertiport(
    Extension(grpc_clients): Extension<GrpcClients>,
    Path(id): Path<String>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(remove_vertiport) entry [{}].", &id);

    let client = grpc_clients.storage.vertiport;

    match client.delete(Id { id: id.clone() }).await {
        Ok(res) => {
            rest_info!(
                "(remove_vertiport) successfully removed vertiport {:?}",
                res
            );
            Ok(id)
        }
        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

/// Remove an [`AssetGroup`](crate::rest::structs::AssetGroup) from the database.
#[utoipa::path(
    delete,
    path = "/assets/groups/{id}",
    tag = "svc-assets",
    responses(
        (status = 200, description = "AssetGroup removed from database; a UUID is returned", body = String),
        (status = 503, description = "Could not connect to other microservice dependencies")
    ),
    params(
        ("id" = String, Path, description = "AssetGroup id"),
    )
)]
pub async fn remove_asset_group(
    Extension(_grpc_clients): Extension<GrpcClients>,
    Path(_id): Path<String>,
) -> Result<String, (StatusCode, String)> {
    rest_info!("(remove_asset_group) with id: {:?}", &_id);

    // Get Client
    // let _client_option = grpc_clients.storage.get_client().await;
    // if client_option.is_none() {
    //     let error_msg = "svc-storage unavailable.".to_string();
    //     rest_error!("(remove_asset_group) {}", &error_msg);
    //     return Err((StatusCode::SERVICE_UNAVAILABLE, error_msg));
    // }
    // let mut client = client_option.unwrap();

    // TODO(R4)
    Ok(_id)
}