Private/Angular/Component/Crud/New-NgCrudComponentTsToString.ps1

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
Function New-NgCrudComponentTsToString([TableInfo]$tableInfo, [string]$returnToUrl) {


<##########################################################################################>
return @"

import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

import { StringService } from '../service/string.service';
import { $($tableInfo.tableCapitalCamel) } from '../model/$($tableInfo.tableLowerKebab)';
import { $($tableInfo.tableCapitalCamel)Service } from '../service/$($tableInfo.tableLowerKebab).service';
import { ValidationHelperService } from 'box-turtle-ng';
import { ValidationMetaDataClass, ValidationMetaDataProperty } from 'box-turtle-ng';

@Component({
    selector: '$($tableInfo.tableLowerKebab)',
    templateUrl: './$($tableInfo.tableLowerKebab).component.html',
    styleUrls: ['./$($tableInfo.tableLowerKebab).component.css']
})
export class $($tableInfo.tableCapitalCamel)Component implements OnInit {
    private $($tableInfo.tableLowerCamel): $($tableInfo.tableCapitalCamel);
    private $($tableInfo.tableLowerCamel)Form: FormGroup;
    private validationMessages = {};
    private formErrors = {};
    private formMetaData: any = {};
    private title: string = "";
    private roles: any = {};

    constructor(
        private $($tableInfo.tableLowerCamel)Service: $($tableInfo.tableCapitalCamel)Service,
        private route: ActivatedRoute,
        private router: Router,
        private fb: FormBuilder,
        private validationHelperService: ValidationHelperService,
        private stringService: StringService
    ) {
    }

    ngOnInit(): void {
        this.route.params
            .switchMap(
            (params: Params) => {
                let $($tableInfo.tableLowerCamel)Promise: Promise<$($tableInfo.tableCapitalCamel)>;

                let id: number = params['id'] as number;
                if (id && (id > 0)) {
                    $($tableInfo.tableLowerCamel)Promise = this.$($tableInfo.tableLowerCamel)Service.get(id);
                } else {
                    $($tableInfo.tableLowerCamel)Promise = new Promise((resolve, reject) => { resolve(new $($tableInfo.tableCapitalCamel)()) });
                }
                return $($tableInfo.tableLowerCamel)Promise;
            }
            )
            .subscribe($($tableInfo.tableLowerCamel) => {
                this.$($tableInfo.tableLowerCamel) = $($tableInfo.tableLowerCamel);
                this.title = ($($tableInfo.tableLowerCamel).$($tableInfo.tableLowerCamel)Id ? "Edit" : "New") + " $($tableInfo.tablePretty)";
                this.buildForm();
            }
            );
    }

    buildForm(): void {
        // Use the current object and it's property decorators to generate all the angular validation metadata
        let formBuilderGroupData = {};
        this.validationHelperService.inferValidationMetadataFromPropertyDecorators($($tableInfo.tableCapitalCamel).name, this.$($tableInfo.tableLowerCamel),
            this.formErrors, this.validationMessages, formBuilderGroupData, this.formMetaData);
        this.$($tableInfo.tableLowerCamel)Form = this.fb.group(formBuilderGroupData);

        this.$($tableInfo.tableLowerCamel)Form.valueChanges
            .subscribe(data => this.onValueChanged(data));

        this.onValueChanged(); // (re)set validation message now
    }

    onValueChanged(data?: any): void {
        this.validationHelperService.copyValidationErrorsToLocalAssocArray(this.$($tableInfo.tableLowerCamel)Form, this.formErrors, this.validationMessages);
    }

    save(): void {
        this.$($tableInfo.tableLowerCamel) = this.$($tableInfo.tableLowerCamel)Form.value;
        if (this.$($tableInfo.tableLowerCamel).$($tableInfo.tableLowerCamel)Id > 0) {
            this.$($tableInfo.tableLowerCamel)Service.update(this.$($tableInfo.tableLowerCamel))
                .then(() => { this.router.navigate(['$($returnToUrl)']); });
        } else {
            this.$($tableInfo.tableLowerCamel)Service.create(this.$($tableInfo.tableLowerCamel))
                .then(() => { this.router.navigate(['$($returnToUrl)']); });
        }
    }

    delete(): void {
        this.$($tableInfo.tableLowerCamel)Service.delete(this.$($tableInfo.tableLowerCamel).$($tableInfo.tableLowerCamel)Id)
            .then(() => this.router.navigate(['$($returnToUrl)']));
    }

    goBack(): void {
        this.router.navigate(['$($returnToUrl)']);
    }
}

"@

<##########################################################################################>
}