import { Injectable } from '@angular/core'; import { Establishment } from '../models/Establishment'; import { Product } from '../models/Product'; import { ProductEstablishment } from '../models/ProductEstablisment'; import { Chain } from '../models/Chain'; import { DBProductEstablishment } from '../models/db/DBProductEstablishment'; import { ComposedDAO } from './ComposedDAO'; import { ProductEstablishmentQueryResult } from '../types/sqlite.type'; @Injectable({ providedIn: 'root', }) export class ProductEstablishmentDAO extends ComposedDAO< ProductEstablishment, DBProductEstablishment, ProductEstablishmentQueryResult > { constructor() { super( 'product_establishment', ` SELECT pe.id, pe.product_id, pe.establishment_id, p.barcode, p.image as product_image, p.name as product_name, e.address, e.chain_id, c.image as chain_image, c.name as chain_name FROM product_establishment pe JOIN product p ON p.id = pe.product_id JOIN establishment e ON e.id = pe.establishment_id JOIN chain c ON c.id = e.chain_id;`, 'pe', ); } protected override toDB(model: ProductEstablishment): DBProductEstablishment { if (!model.product.id) throw new Error('Product id is required'); if (!model.establishment.id) throw new Error('Establishment id is required'); return new DBProductEstablishment(model.product.id, model.establishment.id, model.id); } protected override fromDB(qR: ProductEstablishmentQueryResult): ProductEstablishment { const chain = new Chain(qR.chain_name, qR.chain_image, qR.chain_id); const establishment = new Establishment(chain, qR.address, qR.establishment_id); const product = new Product(qR.barcode, qR.product_name, qR.product_image, qR.product_id); return new ProductEstablishment(product, establishment, qR.id); } }