metadata.ts.twig 3.87 KB
Newer Older
1
2
{% extends '@NgModelGenerator/_layout.ts.twig' %}

3
{% import '@NgModelGenerator/_macros.ts.twig' as m %}
4

5
6
{% block content %}
{% autoescape false %}
7
import { Injectable, Provider } from '@angular/core';
8
import { HttpClient } from '@angular/common/http';
9
import {
10
11
    APIMeta,
    APIRepositoryRegistry,
12
13
14
    AbstractAPIService,
    AbstractResourceCache,
    IRIMetadata,
15
    LazyMetadataRegistry,
16
17
18
    ResourceMetadata,
    UUID,
} from 'irstea-ng-model';
19

20
21
22
23
24
import {
{% for repo in repositories %}
  {{ repo.usage }}{% if not loop.last %},{% endif %}
{%- endfor %}
} from './repositories';
25
26
27
28
29
import {
{% for repo in repositories %}
  {{ repo.resourceName }}{% if not loop.last %},{% endif %}
{%- endfor %}
} from './resources';
30
31

/**
32
 * Interface permettant de faire le lien entre le "@type", la ressource, les metadonnées et son repository.
33
 */
Guillaume Perréal's avatar
Guillaume Perréal committed
34
export interface AppAPI extends APIMeta {
35
  {%- for repo in repositories %}
36
  {{ repo.resourceName | objectKey }}: {
37
    resource: {{ repo.resourceName }};
38
    repository: {{ repo.usage }};
39
40
    metadata: {{ m.metadataType(repo) }};
    iriParameters: {{ m.iriParameterType(repo) }};
41
42
43
44
45
  };
{% endfor %}
}

/**
46
 * Metadonnées des ressources.
47
 */
48
@Injectable()
49
50
51
52
export class AppMetadata extends LazyMetadataRegistry<AppAPI> {
  public constructor() {
    super({
{%- for repo in repositories %}
53
54
  {%- set name = repo.resourceName -%}
  {%- set subTypes -%}
55
56
57
58
    {%- for type in repo.atTypes if type.name != name %}this.{{ type.name }}, {% endfor -%}
  {%- endset %}

    {{ name | objectKey }}: () => new {{ m.metadataType(repo) }}(
59
60
61
62
      {{ name | quoteString }},
      new IRIMetadata(
        /^{{ repo.iri.testPattern }}$/u,
        /^{{ repo.iri.capturePattern }}$/u,
63
64
65
        (
          {%- if repo.iri.parameters | length > 1 %}[{% endif -%}
          {%- for param in repo.iri.parameters -%}
66
67
            {{ param.name }}{% if not loop.last %}, {% endif -%}
          {%- endfor -%}
68
69
          {%- if repo.iri.parameters | length > 1 %}]{% endif -%}
        : {{ m.iriParameterType(repo) }}) => {{ repo.iri.usage }}
70
71
72
73
      ),
      [ {% if repo.resource.properties is defined -%}
        {%- for name, property in repo.resource.properties if not property.nullable %}{{ property.name | quoteString }}, {% endfor -%}
      {%- endif %}],
74
75
76
77
78
      {%- if subTypes %}

      [ {{ subTypes }} ]
      {%- endif %}

79
80
    ),
{%- endfor %}
81

82
83
  });
 };
84

85
{% for repo in repositories %}
86
  public get {{ repo.resourceName }}(): {{ m.metadataType(repo) }} { return this.getOrCreate({{ repo.resourceName | quoteString }}); }
87
{% endfor %}
88
89
90
}

/**
91
 * Repositories.
92
 */
93
@Injectable()
94
95
96
97
export class AppRepositories implements APIRepositoryRegistry<AppAPI> {
{% for repo in repositories %}
  public readonly {{ repo.resourceName }} = new {{ repo.name }}(this.metadata.{{ repo.resourceName }}, this.client, this.cache);
{% endfor %}
98
99

  public constructor(
100
    public readonly metadata: AppMetadata,
101
102
103
    private readonly cache: AbstractResourceCache,
    private readonly client: HttpClient
  ) {}
104

105
106
  public get<T extends keyof AppAPI>(type: T): AppAPI[T]['repository'] {
    return (this as any)[type];
107
  }
108
109
}

110
111
112
113
/**
 * Le service d'API
 */
@Injectable()
114
115
116
117
118
119
120
121
122
123
export class AppAPIService extends AbstractAPIService<AppAPI, AppMetadata, AppRepositories> {
  public constructor(
    metadata: AppMetadata,
    repositories: AppRepositories,
    cache: AbstractResourceCache,
    client: HttpClient
  ) {
    super(metadata, repositories, cache, client);
  }
}
124

125
/* Provider factories */
126
{% for repo in repositories %}
127
export function {{ repo.usage }}Factory(reps: AppRepositories): {{ repo.usage }} { return reps.{{ repo.resourceName }}; }
128
{% endfor %}
129
130

/**
131
 * Providers Angular
132
 */
133
export const PROVIDERS: Provider[] = [
134
135
136
  AppMetadata,
  AppRepositories,
  AppAPIService,
137
{% for repo in repositories %}
138
  { provide: {{ repo.usage  }}, useFactory: {{ repo.usage }}Factory, deps: [AppRepositories] },
139
{% endfor %}
140
];
141
142
143

{% endautoescape -%}
{% endblock content %}