2

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,
        );
    }
}
1
  • 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? Commented Jan 23, 2020 at 9:36

1 Answer 1

3

You could construct an array of Items before passing them into your invoice constructor.

export class InvoiceAdapter implements Adapter<Invoice> {
    adapt(item: any): Invoice {
        let array_of_items = item.items.map(item => {
            return new Item(item.id, item.invoice, etc ,etc)
        }
        return new Invoice (
            item.id,
            item.customer.id,
            item.customer.name,
            item.date,
            item.final_amount.total,
            array_of_items
        );
    }
}

If you want to use adapter.

export class InvoiceAdapter implements Adapter<Invoice> {
    adapt(item: any): Invoice {
        let array_of_items = item.items.map(item => {
            return ItemAdapter.adapt(item)
        }
        return new Invoice (
            item.id,
            item.customer.id,
            item.customer.name,
            item.date,
            item.final_amount.total,
            array_of_items
        );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.