extend form to handle inserts for tenant

This commit is contained in:
Wolfgang Hottgenroth 2021-08-31 17:26:12 +02:00
parent 4f8f0fde9d
commit 5099e4ae63
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
2 changed files with 27 additions and 3 deletions

View File

@ -60,6 +60,15 @@
<mat-label>IBAN</mat-label>
<input matInput name="iban" [(ngModel)]="tenant.iban"/>
</mat-form-field>
</div><div>
<mat-form-field appearance="outline">
<mat-label>Account ID</mat-label>
<input matInput name="account_id" [readonly]="true" [ngModel]="account.id"/>
</mat-form-field>
<mat-form-field appearance="outline">
<mat-label>Account Description</mat-label>
<input matInput name="account_desc" [readonly]="true" [ngModel]="account.description"/>
</mat-form-field>
</div>
<button type="submit" mat-raised-button color="primary">Speichern</button>
</form>

View File

@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { TenantService } from '../data-object-service';
import { Tenant } from '../data-objects';
import { AccountService, TenantService } from '../data-object-service';
import { Account, Tenant } from '../data-objects';
import { MessageService } from '../message.service';
import { Location } from '@angular/common'
@ -28,9 +28,15 @@ export class TenantDetailsComponent implements OnInit {
account: 0
}
account: Account = {
id: 0,
description: ''
}
constructor(
private tenantService: TenantService,
private accountService: AccountService,
private messageService: MessageService,
private route: ActivatedRoute,
private location: Location
@ -41,17 +47,26 @@ export class TenantDetailsComponent implements OnInit {
const id = +this.route.snapshot.paramMap.get('id')
if (id != 0) {
this.tenant = await this.tenantService.getTenant(id)
this.account = await this.accountService.getAccount(this.tenant.account)
}
} catch (err) {
this.messageService.add(JSON.stringify(err, undefined, 4))
}
}
saveTenant() {
async saveTenant() {
this.messageService.add("saveTenant")
this.messageService.add(JSON.stringify(this.tenant, undefined, 4))
if (this.tenant.id == 0) {
this.messageService.add("about to insert new tenant")
this.account = {
"id": 0,
"description": `account_${this.tenant.firstname}_${this.tenant.lastname}`
}
this.account = await this.accountService.postAccount(this.account)
this.tenant.account = this.account.id
this.tenant = await this.tenantService.postTenant(this.tenant)
this.messageService.add(`Successfully added account with id ${this.account.id} and tenant with id ${this.tenant.id}`)
} else {
this.messageService.add("about to update existing tenant")
}