From 76255efbe9119731661f29dfa915ece1f838e88d Mon Sep 17 00:00:00 2001 From: Wolfgang Ludger Hottgenroth Date: Tue, 14 Sep 2021 14:14:56 +0200 Subject: [PATCH] optimize tenant with saldo query --- api/additional_components.yaml | 30 ++++++++++++ api/additional_endpoints.yaml | 16 +++++++ api/additional_methods.py | 14 +++++- api/openapi.yaml | 47 +++++++++++++++++++ api/openapi.yaml.tmpl | 2 + ui/hv2-ui/src/app/ext-data-object-service.ts | 7 ++- ui/hv2-ui/src/app/ext-data-objects.ts | 9 +++- .../app/my-tenants/my-tenants.component.html | 4 ++ .../app/my-tenants/my-tenants.component.ts | 18 ++++--- 9 files changed, 138 insertions(+), 9 deletions(-) create mode 100644 api/additional_components.yaml diff --git a/api/additional_components.yaml b/api/additional_components.yaml new file mode 100644 index 0000000..3a0648f --- /dev/null +++ b/api/additional_components.yaml @@ -0,0 +1,30 @@ +# ------------------------------------------------------------------- +# ATTENTION: This file will not be parsed by Cheetah +# Use plain openapi/yaml syntax, no Cheetah +# escaping +# ------------------------------------------------------------------- + + + + tenant_with_saldo: + description: tenant with saldo + type: object + properties: + id: + type: integer + salutation: + type: string + nullable: true + firstname: + type: string + nullable: true + lastname: + type: string + nullable: true + address1: + type: string + nullable: true + saldo: + type: number + nullable: true + \ No newline at end of file diff --git a/api/additional_endpoints.yaml b/api/additional_endpoints.yaml index d1c3735..6775990 100644 --- a/api/additional_endpoints.yaml +++ b/api/additional_endpoints.yaml @@ -72,4 +72,20 @@ type: number security: - jwt: ['secret'] + /v1/tenants/saldo: + get: + tags: [ "tenant", "account" ] + summary: Return tenant with saldo of the account + operationId: additional_methods.get_tenant_with_saldo + responses: + '200': + description: get_tenant_with_saldo + content: + 'application/json': + schema: + type: array + items: + $ref: '#/components/schemas/tenant_with_saldo' + security: + - jwt: ['secret'] diff --git a/api/additional_methods.py b/api/additional_methods.py index 0f8cf33..a2ca323 100644 --- a/api/additional_methods.py +++ b/api/additional_methods.py @@ -31,4 +31,16 @@ def get_account_saldo(user, token_info, accountId=None): "statement": "SELECT sum(amount) as saldo FROM account_entry_t WHERE account=%s", "params": (accountId, ) } - ) \ No newline at end of file + ) + +def get_tenant_with_saldo(user, token_info): + return dbGetMany(user, token_info, { + "statement": """ + SELECT t.firstname, t.lastname, t.address1, sum(a.amount) AS saldo + FROM tenant_t t, account_entry_t a + WHERE a.account = t.account + GROUP BY t.firstname, t.lastname, t.address1 + """, + "params": () + } + ) diff --git a/api/openapi.yaml b/api/openapi.yaml index 107850e..af89473 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -1493,6 +1493,22 @@ paths: type: number security: - jwt: ['secret'] + /v1/tenants/saldo: + get: + tags: [ "tenant", "account" ] + summary: Return tenant with saldo of the account + operationId: additional_methods.get_tenant_with_saldo + responses: + '200': + description: get_tenant_with_saldo + content: + 'application/json': + schema: + type: array + items: + $ref: '#/components/schemas/tenant_with_saldo' + security: + - jwt: ['secret'] components: @@ -1727,3 +1743,34 @@ components: type: integer note: type: string + +# ------------------------------------------------------------------- +# ATTENTION: This file will not be parsed by Cheetah +# Use plain openapi/yaml syntax, no Cheetah +# escaping +# ------------------------------------------------------------------- + + + + tenant_with_saldo: + description: tenant with saldo + type: object + properties: + id: + type: integer + salutation: + type: string + nullable: true + firstname: + type: string + nullable: true + lastname: + type: string + nullable: true + address1: + type: string + nullable: true + saldo: + type: number + nullable: true + \ No newline at end of file diff --git a/api/openapi.yaml.tmpl b/api/openapi.yaml.tmpl index 5cd1cc0..fb1c8f6 100644 --- a/api/openapi.yaml.tmpl +++ b/api/openapi.yaml.tmpl @@ -156,3 +156,5 @@ components: #end if #end for #end for + +#include raw "./api/additional_components.yaml" diff --git a/ui/hv2-ui/src/app/ext-data-object-service.ts b/ui/hv2-ui/src/app/ext-data-object-service.ts index 05549b9..5caf12d 100644 --- a/ui/hv2-ui/src/app/ext-data-object-service.ts +++ b/ui/hv2-ui/src/app/ext-data-object-service.ts @@ -7,7 +7,7 @@ import { serviceBaseUrl } from './config'; import { Fee, OverheadAdvance } from './data-objects'; -import { Saldo } from './ext-data-objects'; +import { Saldo, Tenant_with_Saldo } from './ext-data-objects'; @Injectable({ providedIn: 'root' }) @@ -28,4 +28,9 @@ export class ExtApiService { this.messageService.add(`ExtApiService: get saldo for account ${id}`); return this.http.get(`${serviceBaseUrl}/v1/account/saldo/${id}`).toPromise() } + + async getTenantsWithSaldo(): Promise { + this.messageService.add("ExtApiService: get tenants with saldo"); + return this.http.get(`${serviceBaseUrl}/v1/tenants/saldo`).toPromise() + } } diff --git a/ui/hv2-ui/src/app/ext-data-objects.ts b/ui/hv2-ui/src/app/ext-data-objects.ts index 69d4160..37d9aed 100644 --- a/ui/hv2-ui/src/app/ext-data-objects.ts +++ b/ui/hv2-ui/src/app/ext-data-objects.ts @@ -2,4 +2,11 @@ export interface Saldo { saldo: number } - \ No newline at end of file + +export interface Tenant_with_Saldo { + id: number + firstname: string + lastname: string + address1: string + saldo: number +} \ No newline at end of file diff --git a/ui/hv2-ui/src/app/my-tenants/my-tenants.component.html b/ui/hv2-ui/src/app/my-tenants/my-tenants.component.html index 56f49c1..03151f6 100644 --- a/ui/hv2-ui/src/app/my-tenants/my-tenants.component.html +++ b/ui/hv2-ui/src/app/my-tenants/my-tenants.component.html @@ -22,6 +22,10 @@ Adresse 1 {{element.address1}} + + Saldo + {{element.saldo | number:'1.2-2'}} € + diff --git a/ui/hv2-ui/src/app/my-tenants/my-tenants.component.ts b/ui/hv2-ui/src/app/my-tenants/my-tenants.component.ts index d8bcaed..4640a77 100644 --- a/ui/hv2-ui/src/app/my-tenants/my-tenants.component.ts +++ b/ui/hv2-ui/src/app/my-tenants/my-tenants.component.ts @@ -3,6 +3,9 @@ import { MessageService } from '../message.service'; import { TenantService } from '../data-object-service'; import { Tenant } from '../data-objects'; import { MatTableDataSource } from '@angular/material/table' +import { Tenant_with_Saldo } from '../ext-data-objects'; +import { ExtApiService } from '../ext-data-object-service'; + @Component({ selector: 'app-my-tenants', @@ -11,19 +14,22 @@ import { MatTableDataSource } from '@angular/material/table' }) export class MyTenantsComponent implements OnInit { - tenants: Tenant[] - dataSource: MatTableDataSource - displayedColumns: string[] = ["lastname", "firstname", "address1"] + tenants: Tenant_with_Saldo[] + dataSource: MatTableDataSource + displayedColumns: string[] = ["lastname", "firstname", "address1", "saldo"] - constructor(private tenantService: TenantService, private messageService: MessageService) { } + constructor( + private extApiService: ExtApiService, + private messageService: MessageService + ) { } async getTenants(): Promise { try { this.messageService.add("Trying to load tenants") - this.tenants = await this.tenantService.getTenants() + this.tenants = await this.extApiService.getTenantsWithSaldo() this.messageService.add("Tenants loaded") - this.dataSource = new MatTableDataSource(this.tenants) + this.dataSource = new MatTableDataSource(this.tenants) } catch (err) { this.messageService.add(JSON.stringify(err, undefined, 4)) }