Skip to content

Commit

Permalink
edit customer
Browse files Browse the repository at this point in the history
  • Loading branch information
123niel committed Jul 23, 2021
1 parent 94d78b9 commit 9213552
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 35 deletions.
3 changes: 2 additions & 1 deletion client/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h1>Artikel</h1>
<th mat-header-cell *matHeaderCellDef>Bearbeiten</th>
<td mat-cell *matCellDef="let element">
<button mat-icon-button color="secondary" (click)="element.edit()">
<mat-icon>more_horiz</mat-icon>
<mat-icon>edit</mat-icon>
</button>
</td>
</ng-container>
Expand Down
17 changes: 10 additions & 7 deletions client/src/app/article/article-table/article-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -68,21 +68,24 @@ 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))
}
}
);
}

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)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<EditArticleDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: Article
) { }

constructor(public dialogRef: MatDialogRef<NewArticleDialogComponent>, @Inject(MAT_DIALOG_DATA) public data: Article) {
console.log(this);
}
close() {
this.dialogRef.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ <h1>Teilnehmer</h1>
</td>
</ng-container>

<ng-container matColumnDef="edit">
<th mat-header-cell *matHeaderCellDef>Bearbeiten</th>
<td mat-cell *matCellDef="let element">
<button mat-icon-button color="secondary" (click)="element.edit()">
<mat-icon>edit</mat-icon>
</button>
</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedCols"></tr>
<tr mat-row *matRowDef="let row; columns: displayedCols"></tr>
</table>
Expand Down
29 changes: 22 additions & 7 deletions client/src/app/customer/customer-table/customer-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TableDataModel> = new MatTableDataSource();

@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
Expand Down Expand Up @@ -59,6 +60,7 @@ export class CustomerTableComponent implements OnInit {
customer.details,
customer.group
),
edit: () => this.openEditDialog({ ...customer })
})))
).subscribe(data => {
this.tableData.data = data;
Expand All @@ -84,17 +86,30 @@ 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) {
this.dialog.open(CustomerDetailDialogComponent, {
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)
}
})
}
}
2 changes: 1 addition & 1 deletion client/src/app/customer/customer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ import { NewCustomerDialogComponent } from './new-customer-dialog/new-customer-d
CustomerDetailDialogComponent,
],
})
export class CustomerModule {}
export class CustomerModule { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<h1 mat-dialog-title>Teilnehmer bearbeiten</h1>
<div mat-dialog-content>
<mat-form-field>
<mat-label>Vorname</mat-label>
<input matInput [(ngModel)]="data.firstname">
</mat-form-field>
<mat-form-field>
<mat-label>Nachname</mat-label>
<input matInput [(ngModel)]="data.lastname">
</mat-form-field>
<mat-form-field>
<mat-label>Zelt</mat-label>
<input matInput [(ngModel)]="data.group">
</mat-form-field>
<mat-form-field>
<mat-label>Details</mat-label>
<textarea matInput [(ngModel)]="data.details"></textarea>
<mat-hint>Allergien, Unverträglichkeiten etc.</mat-hint>
</mat-form-field>
<mat-form-field>
<mat-label>Guthaben</mat-label>
<input matInput disabled autocomplete="off" placeholder="1.00" value="{{(data.credit / 100).toFixed(2)}}">
<span matSuffix>€&nbsp;</span>
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-button [mat-dialog-close]="">Abbrechen</button>
<button mat-button [mat-dialog-close]="data">Ok</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
textarea {
width: 100%;
height: 5rem;
}

.mat-dialog-content {
width: 250px;
}

mat-form-field {
width: 100%;
}
Original file line number Diff line number Diff line change
@@ -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<EditCustomerDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data
) { }

ngOnInit(): void {
}

}
8 changes: 8 additions & 0 deletions client/src/app/shared/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export class ApiService {
);
}

updateCustomer(id, firstname, lastname, details, group) {
return this.http.patch<Customer>(
this.apiUrl + '/customer/' + id,
{ firstname, lastname, details, group },
{ headers: this.headers }
)
}

addArticle(name, category, price): Observable<Article> {
return this.http.post<Article>(
this.apiUrl + '/article',
Expand Down
12 changes: 11 additions & 1 deletion client/src/app/shared/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ApiService } from './api.service';
providedIn: 'root'
})
export class CustomerService {

private customersSubject = new BehaviorSubject<Customer[]>([])
private selectedSubject = new BehaviorSubject<Customer>(null);
customers$ = this.customersSubject.asObservable();
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/api/routes/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
12 changes: 12 additions & 0 deletions src/dataproviders/database-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 5 additions & 7 deletions src/dataproviders/excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
]))
];

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9213552

Please sign in to comment.