feat(persistance) : add DAO

This commit is contained in:
2026-01-17 15:01:16 -03:00
parent 8b462f98f6
commit fd763cc162
7 changed files with 310 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
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';
@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);
}
}
type ProductEstablishmentQueryResult = {
address: string;
barcode: string;
chain_id: number;
chain_image: string | null;
chain_name: string;
establishment_id: number;
id: number;
product_id: number;
product_image: string | null;
product_name: string;
};