optimize tenant with saldo query

This commit is contained in:
Wolfgang Hottgenroth 2021-09-14 14:14:56 +02:00
parent 797cbb4b65
commit 76255efbe9
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
9 changed files with 138 additions and 9 deletions

View File

@ -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

View File

@ -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']

View File

@ -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, )
}
)
)
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": ()
}
)

View File

@ -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

View File

@ -156,3 +156,5 @@ components:
#end if
#end for
#end for
#include raw "./api/additional_components.yaml"

View File

@ -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<Saldo>(`${serviceBaseUrl}/v1/account/saldo/${id}`).toPromise()
}
async getTenantsWithSaldo(): Promise<Tenant_with_Saldo[]> {
this.messageService.add("ExtApiService: get tenants with saldo");
return this.http.get<Tenant_with_Saldo[]>(`${serviceBaseUrl}/v1/tenants/saldo`).toPromise()
}
}

View File

@ -2,4 +2,11 @@
export interface Saldo {
saldo: number
}
export interface Tenant_with_Saldo {
id: number
firstname: string
lastname: string
address1: string
saldo: number
}

View File

@ -22,6 +22,10 @@
<th mat-header-cell *matHeaderCellDef>Adresse 1</th>
<td mat-cell *matCellDef="let element">{{element.address1}}</td>
</ng-container>
<ng-container matColumnDef="saldo">
<th mat-header-cell *matHeaderCellDef>Saldo</th>
<td mat-cell *matCellDef="let element">{{element.saldo | number:'1.2-2'}} €</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" [routerLink]="['/tenant/', row.id]"></tr>
</table>

View File

@ -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<Tenant>
displayedColumns: string[] = ["lastname", "firstname", "address1"]
tenants: Tenant_with_Saldo[]
dataSource: MatTableDataSource<Tenant_with_Saldo>
displayedColumns: string[] = ["lastname", "firstname", "address1", "saldo"]
constructor(private tenantService: TenantService, private messageService: MessageService) { }
constructor(
private extApiService: ExtApiService,
private messageService: MessageService
) { }
async getTenants(): Promise<void> {
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<Tenant>(this.tenants)
this.dataSource = new MatTableDataSource<Tenant_with_Saldo>(this.tenants)
} catch (err) {
this.messageService.add(JSON.stringify(err, undefined, 4))
}