In angular, I am using the adapter pattern to adapt Http response to 'Invoice'. It's working but I can't figure out how to use this when one of the items inside is an array.
Here, 'item' in class Invoice is an array of 'Item', how to use adapter pattern in this case?
export class Invoice {
constructor(
public id: number,
public customerId: number,
public customer: string,
public date: string,
public finalAmount: number,
public items: Item[],
) {}
}
export interface Adapter<T> {
adapt(item: any): T;
}
@Injectable({
providedIn: 'root'
})
export class InvoiceAdapter implements Adapter<Invoice> {
adapt(item: any): Invoice {
return new Invoice (
item.id,
item.customer.id,
item.customer.name,
item.date,
item.final_amount.total,
item.items // how to adapt to 'Item'?
);
}
}
export class Item {
constructor(
public id: number,
public invoice: number,
public productId: number,
public product: string,
public size: number,
public quantity: number,
public amount: number,
) {}
}
export class ItemAdapter implements Adapter<Item> {
adapt(item: any): Item {
return new Item (
item.id,
item.invoice,
item.product.id,
item.product.name,
item.size,
item.quantity,
item.amount,
);
}
}
item.items.map(i => new Item(...));Why are you using these classes, which don't offer anything more than the original POJO, in the first place?