/// <summary> /// Get or create a new city for the specified code. /// This is a recursive function so that it can generate a new city with a unique code. /// It has a limitation of which the code must be 4 characters and therefore it can only handle 9 variations (i.e. BRe1, BRe2...) /// </summary> /// <param name="code"></param> /// <param name="name"></param> /// <param name="attempts"></param> /// <returns></returns> private Entity.City GetOrCreateCity(string code, string name, int attempts = 1) { if (String.IsNullOrWhiteSpace(code)) { throw new ArgumentException($"Argument '{nameof(code)}' is required."); } if (String.IsNullOrWhiteSpace(name)) { throw new ArgumentException($"Argument '{nameof(name)}' is required."); } var city = _cities.FirstOrDefault(c => String.Compare(c.Code, code, true) == 0); // If City doesn't exist, create it. if (city == null) { city = new Entity.City(code, name); _pimsAdminService.City.Add(city); _cities.Add(city); _logger.LogDebug($"Adding city '{code}' - '{name}'."); } else { // Generate a new city code. code = $"{new string(code.Take(3).ToArray())}{attempts}"; // Check if the code is unique. city = GetOrCreateCity(code, name, ++attempts); } return(city); }
/// <summary> /// Create a new instance of an Address. /// </summary> /// <param name="id"></param> /// <param name="address1"></param> /// <param name="address2"></param> /// <param name="city"></param> /// <param name="province"></param> /// <param name="postal"></param> /// <returns></returns> public static Entity.Address CreateAddress(int id, string address1, string address2, Entity.City city, Entity.Province province, string postal) { return(new Entity.Address(address1, address2, city, province, postal) { Id = id, RowVersion = new byte[] { 12, 13, 14 } }); }