diff --git a/src/app/_services/data.service.ts b/src/app/_services/data.service.ts index c586aec..75563ff 100644 --- a/src/app/_services/data.service.ts +++ b/src/app/_services/data.service.ts @@ -25,6 +25,10 @@ export class DataService { return this.httpClient.get(environment.backendUrl + '/data').toPromise(); } + getCount(): Promise { + return this.httpClient.get(environment.backendUrl + '/data/count').toPromise(); + } + add(data: Data): Promise { return this.httpClient.post(environment.backendUrl + '/data', data).toPromise(); } diff --git a/src/app/data/add/add.component.ts b/src/app/data/add/add.component.ts index 126392b..d8e4335 100644 --- a/src/app/data/add/add.component.ts +++ b/src/app/data/add/add.component.ts @@ -12,6 +12,7 @@ import {isNameExists} from '../../_validators/uniq-name.validator'; styleUrls: ['./add.component.scss'] }) export class AddComponent implements OnInit { + dataCount: string; form: FormGroup; inProgress = false; type: FormControl = new FormControl('', Validators.required); @@ -39,6 +40,9 @@ export class AddComponent implements OnInit { } ngOnInit() { + this.dataService.getCount().then(data => { + this.dataCount = data + }) } back() { @@ -47,12 +51,19 @@ export class AddComponent implements OnInit { onSubmit() { this.inProgress = true; - const data = this.form.value; - data.name = this.getFormattedName(); - this.dataService.add(data).then(() => { - this.alertService.success('Data added successfully'); - this.router.navigate(['/data/' + data.name]); - }).finally(() => this.inProgress = false); + + if(+this.dataCount > 100) { + this.alertService.success('You cannot have more than 100 records!'); + this.router.navigate(['/data']); + this.inProgress = false; + } else { + const data = this.form.value; + data.name = this.getFormattedName(); + this.dataService.add(data).then(() => { + this.alertService.success('Data added successfully'); + this.router.navigate(['/data/' + data.name]); + }).finally(() => this.inProgress = false); + } } onTypeChange() {