Using the adapter pattern in Angular, I have successfully adapted Http response to an 'Invoice' object. However, I am facing a challenge when one of the properties inside the 'Item' is an array.
In this scenario, the 'items' property in the 'Invoice' class is an array of 'Item'. How can I implement the adapter pattern to handle this situation?
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,
);
}
}