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> <mat-label>IBAN</mat-label>
<input matInput name="iban" [(ngModel)]="tenant.iban"/> <input matInput name="iban" [(ngModel)]="tenant.iban"/>
</mat-form-field> </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> </div>
<button type="submit" mat-raised-button color="primary">Speichern</button> <button type="submit" mat-raised-button color="primary">Speichern</button>
</form> </form>

View File

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