From 92135525d232380e56a7de810742823f00c25462 Mon Sep 17 00:00:00 2001 From: Niel Rohling Date: Fri, 23 Jul 2021 16:14:27 +0200 Subject: [PATCH] edit customer --- client/src/app/app.module.ts | 3 +- .../article-table.component.html | 2 +- .../article-table/article-table.component.ts | 17 ++++++----- .../edit-article-dialog.component.ts | 14 +++------ .../customer-table.component.html | 9 ++++++ .../customer-table.component.ts | 29 ++++++++++++++----- client/src/app/customer/customer.module.ts | 2 +- .../edit-customer-dialog.component.html | 29 +++++++++++++++++++ .../edit-customer-dialog.component.scss | 12 ++++++++ .../edit-customer-dialog.component.ts | 21 ++++++++++++++ client/src/app/shared/api.service.ts | 8 +++++ client/src/app/shared/customer.service.ts | 12 +++++++- src/api/routes/customer.ts | 7 +++++ src/dataproviders/database-adapter.ts | 12 ++++++++ src/dataproviders/excel.ts | 12 ++++---- 15 files changed, 154 insertions(+), 35 deletions(-) create mode 100644 client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.html create mode 100644 client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.scss create mode 100644 client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.ts diff --git a/client/src/app/app.module.ts b/client/src/app/app.module.ts index 136d6fb..fbcf4a3 100644 --- a/client/src/app/app.module.ts +++ b/client/src/app/app.module.ts @@ -11,12 +11,13 @@ import { CounterModule } from './counter/counter.module'; import { CustomerModule } from './customer/customer.module'; import { MaterialModule } from './shared/material.module'; import { ImportExportComponent } from './import-export/import-export.component'; +import { EditCustomerDialogComponent } from './customer/edit-customer-dialog/edit-customer-dialog.component'; registerLocaleData(localeDe); @NgModule({ - declarations: [AppComponent, ImportExportComponent], + declarations: [AppComponent, ImportExportComponent, EditCustomerDialogComponent], imports: [ BrowserModule, BrowserAnimationsModule, diff --git a/client/src/app/article/article-table/article-table.component.html b/client/src/app/article/article-table/article-table.component.html index 5515c3d..6a8e050 100644 --- a/client/src/app/article/article-table/article-table.component.html +++ b/client/src/app/article/article-table/article-table.component.html @@ -40,7 +40,7 @@

Artikel

Bearbeiten diff --git a/client/src/app/article/article-table/article-table.component.ts b/client/src/app/article/article-table/article-table.component.ts index dad04f5..eb722cd 100644 --- a/client/src/app/article/article-table/article-table.component.ts +++ b/client/src/app/article/article-table/article-table.component.ts @@ -52,7 +52,7 @@ export class ArticleTableComponent implements OnInit { price: article.price, disabled: article.disabled, toggle: () => this.articleService.toggle(article), - edit: () => this.openEditDialog(article) + edit: () => this.openEditDialog({ ...article }) })) ) ).subscribe(data => { @@ -68,9 +68,11 @@ export class ArticleTableComponent implements OnInit { this.dialog .open(NewArticleDialogComponent) .afterClosed() - .subscribe(({ name, category, price }) => { - const cents = parseFloat((price as string).replace(",", ".")) * 100 - this.articleService.addArticle(name, category, Math.floor(cents)) + .subscribe((data: { name: string, category: string, price: string }) => { + if (data !== undefined) { + const cents = parseFloat(data.price.replace(",", ".")) * 100 + this.articleService.addArticle(data.name, data.category, Math.floor(cents)) + } } ); } @@ -78,11 +80,12 @@ export class ArticleTableComponent implements OnInit { openEditDialog(article: Article) { this.dialog .open(EditArticleDialogComponent, { - data: article + data: article, }) .afterClosed() - .subscribe(({ id, name, category }) => { - this.articleService.editArticle(id, name, category) + .subscribe((data: { id: string, name: string, category: string }) => { + if (data !== undefined) + this.articleService.editArticle(data.id, data.name, data.category) }) } } diff --git a/client/src/app/article/edit-article-dialog/edit-article-dialog.component.ts b/client/src/app/article/edit-article-dialog/edit-article-dialog.component.ts index 8a2a4e5..0e9088e 100644 --- a/client/src/app/article/edit-article-dialog/edit-article-dialog.component.ts +++ b/client/src/app/article/edit-article-dialog/edit-article-dialog.component.ts @@ -10,17 +10,11 @@ import { NewArticleDialogComponent } from '../new-article-dialog/new-article-dia }) export class EditArticleDialogComponent implements OnInit { - // data: Article = { - // name: '', - // category: '', - // price: 0, - // id: '0', - // disabled: false - // }; + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: Article + ) { } - constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: Article) { - console.log(this); - } close() { this.dialogRef.close(); } diff --git a/client/src/app/customer/customer-table/customer-table.component.html b/client/src/app/customer/customer-table/customer-table.component.html index 3f5ffbc..13bfa0b 100644 --- a/client/src/app/customer/customer-table/customer-table.component.html +++ b/client/src/app/customer/customer-table/customer-table.component.html @@ -49,6 +49,15 @@

Teilnehmer

+ + Bearbeiten + + + + + diff --git a/client/src/app/customer/customer-table/customer-table.component.ts b/client/src/app/customer/customer-table/customer-table.component.ts index 3125294..8d8c215 100644 --- a/client/src/app/customer/customer-table/customer-table.component.ts +++ b/client/src/app/customer/customer-table/customer-table.component.ts @@ -12,6 +12,7 @@ import { Transaction } from 'src/app/models/Transaction'; import { MatPaginator } from '@angular/material/paginator'; import { MatSort } from '@angular/material/sort'; import { FormControl } from '@angular/forms'; +import { EditCustomerDialogComponent } from 'src/app/customer/edit-customer-dialog/edit-customer-dialog.component'; interface TableDataModel { firstname: string; @@ -29,7 +30,7 @@ interface TableDataModel { styleUrls: ['./customer-table.component.scss'], }) export class CustomerTableComponent implements OnInit { - displayedCols = ['firstname', 'lastname', 'group', 'credit', 'deposit', 'details']; + displayedCols = ['firstname', 'lastname', 'group', 'credit', 'deposit', 'details', 'edit']; tableData: MatTableDataSource = new MatTableDataSource(); @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator; @@ -59,6 +60,7 @@ export class CustomerTableComponent implements OnInit { customer.details, customer.group ), + edit: () => this.openEditDialog({ ...customer }) }))) ).subscribe(data => { this.tableData.data = data; @@ -84,12 +86,12 @@ export class CustomerTableComponent implements OnInit { openNewDialog(): void { this.dialog.open(NewCustomerDialogComponent) .afterClosed() - .subscribe(({ firstname, lastname, group, details, credit }) => { - const cents = parseFloat((credit as string).replace(",", ".")) * 100 - this.customerService.addCustomer(firstname, lastname, group, details, cents || 0) - } - ) - + .subscribe((data: { firstname: string, lastname: string, group: string, details: string, credit: string }) => { + if (data !== undefined) { + const cents = parseFloat(data.credit.replace(",", ".")) * 100 + this.customerService.addCustomer(data.firstname, data.lastname, data.group, data.details, cents || 0) + } + }); } showDetails(firstname, lastname, transactions, details, group) { @@ -97,4 +99,17 @@ export class CustomerTableComponent implements OnInit { data: { firstname, lastname, transactions, details, group }, }); } + + openEditDialog(customer: Customer) { + this.dialog + .open(EditCustomerDialogComponent, { + data: { ...customer, credit: this.customerService.calculateCredit(customer) } + }) + .afterClosed() + .subscribe((data: { id: string, firstname: string, lastname: string, group: string, details: string }) => { + if (data !== undefined) { + this.customerService.editCustomer(data.id, data.firstname, data.lastname, data.details, data.group) + } + }) + } } diff --git a/client/src/app/customer/customer.module.ts b/client/src/app/customer/customer.module.ts index 9014227..77b5d75 100644 --- a/client/src/app/customer/customer.module.ts +++ b/client/src/app/customer/customer.module.ts @@ -20,4 +20,4 @@ import { NewCustomerDialogComponent } from './new-customer-dialog/new-customer-d CustomerDetailDialogComponent, ], }) -export class CustomerModule {} +export class CustomerModule { } diff --git a/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.html b/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.html new file mode 100644 index 0000000..86dd42e --- /dev/null +++ b/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.html @@ -0,0 +1,29 @@ +

Teilnehmer bearbeiten

+
+ + Vorname + + + + Nachname + + + + Zelt + + + + Details + + Allergien, Unverträglichkeiten etc. + + + Guthaben + + €  + +
+
+ + +
diff --git a/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.scss b/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.scss new file mode 100644 index 0000000..54441e1 --- /dev/null +++ b/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.scss @@ -0,0 +1,12 @@ +textarea { + width: 100%; + height: 5rem; +} + +.mat-dialog-content { + width: 250px; +} + +mat-form-field { + width: 100%; +} diff --git a/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.ts b/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.ts new file mode 100644 index 0000000..1f92097 --- /dev/null +++ b/client/src/app/customer/edit-customer-dialog/edit-customer-dialog.component.ts @@ -0,0 +1,21 @@ +import { CurrencyPipe } from '@angular/common'; +import { Component, Inject, OnInit } from '@angular/core'; +import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; +import { CustomerService } from 'src/app/shared/customer.service'; + +@Component({ + selector: 'app-edit-customer-dialog', + templateUrl: './edit-customer-dialog.component.html', + styleUrls: ['./edit-customer-dialog.component.scss'] +}) +export class EditCustomerDialogComponent implements OnInit { + + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data + ) { } + + ngOnInit(): void { + } + +} diff --git a/client/src/app/shared/api.service.ts b/client/src/app/shared/api.service.ts index 316bc30..79c28c4 100644 --- a/client/src/app/shared/api.service.ts +++ b/client/src/app/shared/api.service.ts @@ -33,6 +33,14 @@ export class ApiService { ); } + updateCustomer(id, firstname, lastname, details, group) { + return this.http.patch( + this.apiUrl + '/customer/' + id, + { firstname, lastname, details, group }, + { headers: this.headers } + ) + } + addArticle(name, category, price): Observable
{ return this.http.post
( this.apiUrl + '/article', diff --git a/client/src/app/shared/customer.service.ts b/client/src/app/shared/customer.service.ts index 9d89749..72e5c33 100644 --- a/client/src/app/shared/customer.service.ts +++ b/client/src/app/shared/customer.service.ts @@ -8,7 +8,6 @@ import { ApiService } from './api.service'; providedIn: 'root' }) export class CustomerService { - private customersSubject = new BehaviorSubject([]) private selectedSubject = new BehaviorSubject(null); customers$ = this.customersSubject.asObservable(); @@ -51,6 +50,17 @@ export class CustomerService { this.selectedSubject.next(null); } + editCustomer(id: string, firstname: string, lastname: string, details: string, group: string) { + this.apiService.updateCustomer(id, firstname, lastname, details, group) + .subscribe(c => { + const oldCustomers = this.customersSubject.getValue(); + const newCustomers = [...oldCustomers]; + + newCustomers[c.id] = c; + this.customersSubject.next(newCustomers); + }) + } + calculateCredit(customer: Customer) { return customer.transactions.reduce((sum, transaction) => sum += transaction.deposit diff --git a/src/api/routes/customer.ts b/src/api/routes/customer.ts index 574a599..8821934 100644 --- a/src/api/routes/customer.ts +++ b/src/api/routes/customer.ts @@ -28,5 +28,12 @@ export const customerRouter = (db: DatabaseAdapter) => { res.json(customer); })); + router.patch('/:id', catchAsync(async (req: Request, res: Response) => { + const { firstname, lastname, details, group } = req.body; + const id = parseInt(req.params.id, 10); + const customer = db.updateCustomer(id, firstname, lastname, group, details); + res.json(customer); + })) + return router; }; diff --git a/src/dataproviders/database-adapter.ts b/src/dataproviders/database-adapter.ts index 473547b..a2138ba 100644 --- a/src/dataproviders/database-adapter.ts +++ b/src/dataproviders/database-adapter.ts @@ -65,6 +65,18 @@ export class DatabaseAdapter { return this.getCustomers().filter(c => c.id === id)[0]; } + updateCustomer(id: number, firstname: string, lastname: string, group: string, details: string): Customer { + const customer = this.getCustomerByID(id); + customer.firstname = firstname; + customer.lastname = lastname; + customer.details = details; + customer.group = group; + + const index = this.db.getIndex('/customers', id); + this.db.push(`/customers[${index}]`, customer, true) + return customer; + } + // Transactions getTransactions(): Transaction[] { return this.getCustomers().flatMap(c => c.transactions); diff --git a/src/dataproviders/excel.ts b/src/dataproviders/excel.ts index 877b0a2..6669b14 100644 --- a/src/dataproviders/excel.ts +++ b/src/dataproviders/excel.ts @@ -29,12 +29,11 @@ export class ExcelAdapter { ]; const articles = [ - ['Name', 'Price', 'Category (ct.)', 'Disabled'], + ['Name', 'Price (ct.)', 'Category'], ...(this.dbAdapter.getArticles()).map(article => ([ article.name, article.price, - article.category, - article.disabled + article.category ])) ]; @@ -72,13 +71,12 @@ export class ExcelAdapter { } - const articles = data.find(sheet => sheet.name === 'Articles')?.data.slice(1).filter(row => row.length === 4).map(row => { - const [name, price, category, disabled] = row; + const articles = data.find(sheet => sheet.name === 'Articles')?.data.slice(1).filter(row => row.length === 3).map(row => { + const [name, price, category] = row; if (typeof name !== 'string' || !name) throw new Error(name + ' is not a valid article name'); if (typeof price !== 'number') throw new Error(price + ' is not a valid price'); if (typeof category !== 'string') throw new Error(category + ' is not a valid category'); - if (typeof disabled !== 'boolean') throw new Error(disabled + ' is not a valid boolean value'); - return { name, price, category, disabled }; + return { name, price, category, disabled: false }; }); if (articles) {