// Gets an order from a file based off of order date and order number public GetOrderResponse GetOrder(string orderDate, int orderNumber) { bool _foundMatch = false; // Sets the Orders list in the class to the orders loaded from the file GetOrderResponse response = new GetOrderResponse { Orders = _orderRepository.LoadOrders(orderDate), Order = new Order() }; // Edit order and Remove order use the GetOrder method, so, I'm storing the loaded orders in a List // to keep from reopening the file when using EditOrder and RemoveOrder _loadedOrders = response.Orders; // Checks that the date provided is a valid date if (!DateHandler.IsValidDate(orderDate)) { response.Success = false; response.Code = ErrorCode.NotAValidDate; response.IsValidDate = false; return(response); } // Checks that the date is in the future (dates not in the future can't be edited or removed) if (!DateHandler.IsFutureDate(orderDate)) { response.Success = false; response.Code = ErrorCode.NotAFutureDate; response.IsFutureDate = false; return(response); } // Checks if a file exists with the given order date if (!File.Exists(Paths.ordersFolderFilePath + orderDate + ".txt")) { response.Success = false; response.Code = ErrorCode.CouldNotFindFile; return(response); } // Looks for an order matching the order number if file isn't empty if (response.Orders != null) { foreach (var o in response.Orders) { if (o.Number == orderNumber) { response.Order = o; response.Success = true; _foundMatch = true; break; } } } else { response.Success = false; response.Code = ErrorCode.FileIsEmpty; return(response); } // If no match was found, return if (!_foundMatch) { response.Success = false; response.Code = ErrorCode.OrderNumberDoesNotExist; return(response); } return(response); }
// Takes the neccessary information and creates an order with it. public ValidateAddOrderResponse ValidateAddOrder(string orderDate, string customerName, string state, string productType, decimal area) { ValidateAddOrderResponse response = new ValidateAddOrderResponse { Orders = _orderRepository.LoadOrders(orderDate), // LoadOrders takes the order date and attempts to find and open a file with the same order date, then assigns it to the response's order list. Order = new Order() }; if (response.Orders != null && response.Orders.Any(x => x.Number == -1)) //LoadOrders returns null if the file does not exist. { response.Success = false; response.Code = ErrorCode.CorruptFile; return(response); } if (response.Orders == null || response.Orders.Count() == 0) // LoadOrders returns null if the file does not exist. { response.Order.Number = 1; } else { response.Order.Number = response.Orders.Last().Number + 1; // Sets the order number to 1 greater than the last order number } // Checks if the date is a valid date if (!DateHandler.IsValidDate(orderDate)) { response.Success = false; response.Code = ErrorCode.NotAValidDate; return(response); } // Checks that the date is in the future if (!DateHandler.IsFutureDate(orderDate)) { response.Success = false; response.Code = ErrorCode.NotAFutureDate; return(response); } // Checks that the customer name is valid if (!IsValidCustomerName(customerName)) { response.Success = false; response.Code = ErrorCode.NameIsInvalid; return(response); } // STATES is an array with every state's abbreviations in it, this will take the given state and run it through the array looking for a match. // This is done so that if the user makes a simple typo (ie ih instead of oh), it will let them know that they entered an abbreviation that isn't // a state abbreviation. So, mainly, it's just for more precise error messaging. for (int i = 0; i < ImportantValues.STATES.Length; i++) { if (state == ImportantValues.STATES[i]) { break; } // If it gets to the last iteration and it still hasn't found a match, the given state abbreviation is invalid. if (i + 1 == ImportantValues.STATES.Length) { response.Success = false; response.Code = ErrorCode.InvalidState; return(response); } } // Get all the information from the state and then validate the information. _state = GetStateInfo(state); _validation = ValidateState(_state.Abbreviation); if (_validation != ErrorCode.NoError) { response.Success = false; response.Code = _validation; return(response); } // Get all the information from the product and then validate the information. _product = GetProductInfo(productType); _validation = ValidateProduct(_product); if (_validation != ErrorCode.NoError) { response.Success = false; response.Code = _validation; return(response); } // Checks that the area isn't less than the min. if (area < ImportantValues.MIN_AREA) { response.Success = false; response.Code = ErrorCode.AreaTooSmall; return(response); } // Assigns all the information to the order and performs calculations on it. response.Order.CustomerName = customerName; response.Order.State = _state.Abbreviation; response.Order.TaxRate = _state.TaxRate; response.Order.ProductType = _product.ProductName; response.Order.Area = area; response.Order.CostPerSquareFoot = _product.CostPerSquareFoot; response.Order.LaborCostPerSquareFoot = _product.LaborCostPerSquareFoot; response.Order = PerformCalculation(response.Order); response.Success = true; return(response); }