private void AddPartInvoiceToRepository(string stockCode, int quantity, int customerId) { PartInvoice _PartInvoice = new PartInvoice { StockCode = stockCode, Quantity = quantity, CustomerID = customerId }; PartInvoiceRepository.Add(_PartInvoice); }
public CreatePartInvoiceResult CreatePartInvoice(string stockCode, int quantity, string customerName) { // This method calls to external services (Database, Webservice). It is better to have try catch to handle exceptions. try { // Changed: Add validation for customerName. if (string.IsNullOrEmpty(stockCode) || string.IsNullOrEmpty(customerName)) { return(new CreatePartInvoiceResult(false)); } if (quantity <= 0) { return(new CreatePartInvoiceResult(false)); } Customer _Customer = __CustomerRepositoryDB.GetByName(customerName); // Changed: Validate Customer in case of customer not found. if (_Customer == null || _Customer.ID <= 0) { return(new CreatePartInvoiceResult(false)); } int _Availability = __IPartAvailabilityService.GetAvailability(stockCode); if (_Availability <= 0) { return(new CreatePartInvoiceResult(false)); } PartInvoice _PartInvoice = new PartInvoice { StockCode = stockCode, Quantity = quantity, CustomerID = _Customer.ID }; __PartInvoiceRepositoryDB.Add(_PartInvoice); return(new CreatePartInvoiceResult(true)); } catch (Exception) { // Logging return(new CreatePartInvoiceResult(false)); } }
public CreatePartInvoiceResult CreatePartInvoice(string stockCode, int quantity, string customerName) { //validate inputs if (IsStockCodeValid(stockCode) && IsQuantityValid(quantity) && IsCustomerValid(customerName)) { if (IsAvailable(stockCode)) { PartInvoice _PartInvoice = new PartInvoice { StockCode = stockCode, Quantity = quantity, CustomerID = _Customer.ID }; _PartInvoiceRepository.Add(_PartInvoice); return(new CreatePartInvoiceResult(true)); } return(new CreatePartInvoiceResult(false)); } return(new CreatePartInvoiceResult(false)); }