UNPKG

e-commercee

Version:

This package contains a backend of what would be the logic of a e-commercee software, the architecture used is made in 3 layers

173 lines 7.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LArticle = void 0; const FactoryData_1 = require("../../data/FactoryData"); const logicexception_1 = require("../../shared/exceptions/logicexception"); const LCategory_1 = require("./LCategory"); class LArticle { constructor() { } static getInstance() { if (!LArticle.instancia) { LArticle.instancia = new LArticle(); } return LArticle.instancia; } //Validations //************************************ validateBarCode(barcode) { if (barcode.trim() === "") { throw new logicexception_1.LogicException("The barcode cannot be empty"); } } async validateAddArticle(dtart) { this.validateBarCode(dtart.barcode); if (dtart.category == null) { throw new logicexception_1.LogicException("The Category is null"); } var searobjcat = await LCategory_1.LCategory.getInstance().getCategory(dtart.category.name); if (searobjcat == null) { throw new logicexception_1.LogicException("That Category does not exists in the system"); } var objart = await this.getArticle(dtart.barcode); if (objart != null) { throw new logicexception_1.LogicException("That Article already exists in the system"); } if (dtart == null) { throw new logicexception_1.LogicException("The Article is empty "); } if (dtart.description.trim() === "") { throw new logicexception_1.LogicException("The description cannot be empty"); } if (dtart.img.trim() === "") { throw new logicexception_1.LogicException("The img cannot be empty"); } if (!(dtart.img.trim().match(/\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$/))) { throw new logicexception_1.LogicException("Only images files are allowed"); } if (dtart.name.trim() === "") { throw new logicexception_1.LogicException("The name cannot be empty"); } if (dtart.price < 1) { throw new logicexception_1.LogicException("The price must be greater than 0"); } if (dtart.stock < 1) { throw new logicexception_1.LogicException("The stock must be greater than 0"); } } async validateUpdateArticle(dtart) { this.validateBarCode(dtart.barcode); if (dtart.category == null) { throw new logicexception_1.LogicException("The Category is null"); } var searobjcat = await LCategory_1.LCategory.getInstance().getCategory(dtart.category.name); if (searobjcat == null) { throw new logicexception_1.LogicException("That Category does not exists in the system"); } var objart = await this.getArticle(dtart.barcode); if (objart == null) { throw new logicexception_1.LogicException("That Article does not exists in the system"); } if (dtart == null) { throw new logicexception_1.LogicException("The Article is empty "); } if (dtart.description.trim() === "") { throw new logicexception_1.LogicException("The description cannot be empty"); } if (dtart.img.trim() === "") { throw new logicexception_1.LogicException("The img cannot be empty"); } if (!(dtart.img.trim().match(/\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$/))) { throw new logicexception_1.LogicException("Only images files are allowed"); } if (dtart.name.trim() === "") { throw new logicexception_1.LogicException("The name cannot be empty"); } if (dtart.price < 1) { throw new logicexception_1.LogicException("The price must be greater than 0"); } if (dtart.stock < 1) { throw new logicexception_1.LogicException("The stock must be greater than 0"); } } async validateDeleteArticle(dtart) { this.validateBarCode(dtart.barcode); let sobjart = await this.getArticle(dtart.barcode); if (dtart == null) { throw new logicexception_1.LogicException("The Article is empty "); } if (sobjart == null) { throw new logicexception_1.LogicException("That Article does not exists in the system"); } } validatequantity(quantity) { if (quantity < 1) { throw new logicexception_1.LogicException("The quantity must be greater than 0"); } } //*********************************************** */ //Functions async getArticle(barcode) { this.validateBarCode(barcode); var sarticle = await FactoryData_1.FactoryData.getDArticle().getArticle(barcode); return sarticle; } async addArticle(dtart) { await this.validateAddArticle(dtart); FactoryData_1.FactoryData.getDArticle().addArticle(dtart); } async updateArticle(dtart) { await this.validateUpdateArticle(dtart); FactoryData_1.FactoryData.getDArticle().updateArticle(dtart); } async deleteArticle(dtart) { await this.validateDeleteArticle(dtart); FactoryData_1.FactoryData.getDArticle().deleteArticle(dtart); } async registerStock(barcode, quantity) { this.validatequantity(quantity); var searcharticle = await this.getArticle(barcode); if (searcharticle == null) { throw new logicexception_1.LogicException("That Article does not exists in the system"); } searcharticle.stock += quantity; FactoryData_1.FactoryData.getDArticle().updateStock(searcharticle); } async deStock(barcode, quantity) { this.validatequantity(quantity); var searcharticle = await this.getArticle(barcode); if (searcharticle == null) { throw new logicexception_1.LogicException("That Article does not exists in the system"); } searcharticle.stock -= quantity; FactoryData_1.FactoryData.getDArticle().updateStock(searcharticle); } async getArticlesByNameLetter(expression) { if (expression === undefined) { return this.getArticles(); } var listexp = await FactoryData_1.FactoryData.getDArticle().getArticlesByNameLetter(expression); return listexp; } async getArticles() { var list = await FactoryData_1.FactoryData.getDArticle().getArticles(); return list; } async orderArticlesbyPrice() { var list = await FactoryData_1.FactoryData.getDArticle().orderArticlesbyPrice(); return list; } async orderArticlesbyCategory() { var list = await FactoryData_1.FactoryData.getDArticle().orderArticlesbyCategory(); return list; } async filterArticlesbyCategory(namecategory) { var searchcat = await LCategory_1.LCategory.getInstance().getCategory(namecategory); if (searchcat == null) { throw new logicexception_1.LogicException("That Category does not exists in the system"); } var list = await FactoryData_1.FactoryData.getDArticle().filterArticlesbyCategory(namecategory); return list; } } exports.LArticle = LArticle; //# sourceMappingURL=LArticle.js.map