public void TerritoryInternalRecordsAreCreatedCorrectly() { Assert.That(_territoryRepositoryService.GetTerritoriesCount(), Is.EqualTo(0)); var tir = new TerritoryInternalRecord { Name = "test" }; Assert.That(_territoryRepositoryService.GetTerritoriesCount(), Is.EqualTo(0)); PopulateTable(6); Assert.That(_territoryRepositoryService.GetTerritoriesCount(), Is.EqualTo(6)); var created = _territoryRepositoryService.GetTerritories().ToArray(); Assert.That(created.Length, Is.EqualTo(6)); var sameObjects = true; for (int i = 0; i < created.Length; i++) { sameObjects &= created[i].Name == "Name" + i.ToString(); //this also verifies that Names are trimmed } Assert.That(sameObjects); }
public void Delete(TerritoryInternalRecord tir) { if (tir != null) { Delete(tir.Id); } }
public TerritoryInternalRecord Update(TerritoryInternalRecord tir) { ValidateTir(tir); tir.Name = tir.Name.Trim(); if (GetSameNameIds(tir).Any()) { throw new TerritoryInternalDuplicateException(T("A territory with the same name already exists.")); } _territoryInternalRecord.Update(tir); return(tir.CreateSafeDuplicate()); }
/// <summary> /// Validates a TerritoryInternalRecord parameter, throwing ArgumentExceptions if it fails. /// </summary> /// <param name="tir">A TerritoryInternalRecord to validate.</param> private void ValidateTir(TerritoryInternalRecord tir) { if (tir == null) { throw new ArgumentNullException("TerritoryInternalRecord"); } if (string.IsNullOrWhiteSpace(tir.Name)) { throw new ArgumentNullException("Name"); } }
private IEnumerable <int> GetSameNameIds(TerritoryInternalRecord tir) { var name = tir.Name.Trim(); try { return(_territoryInternalRecord.Table .Where(x => x.Name == name && (tir.Id == 0 || tir.Id != x.Id)) //can have same name as its own self .ToList() //force execution of the query so it can fail in sqlCE .Select(x => x.Id)); } catch (Exception) { //sqlCE doe not support using strings properly when their length is such that the column //in the record is of type ntext. var tirs = _territoryInternalRecord.Fetch(x => x.Name.StartsWith(name) && x.Name.EndsWith(name)); return(tirs .ToList() //force execution so that Linq happens on in-memory objects .Where(x => x.Name == name && (tir.Id == 0 || tir.Id != x.Id)) .Select(x => x.Id)); } }
public void AssignInternalRecord(TerritoryPart territory, TerritoryInternalRecord internalRecord) { TerritoriesUtilities.ValidateArgument(territory, nameof(territory)); if (internalRecord == null || _territoriesRepositoryService.GetTerritoryInternal(internalRecord.Id) == null) { throw new ArgumentNullException(nameof(internalRecord)); } // check that the internal record does not exist yet in the same hierarchy var hierarchyRecord = territory.Record.Hierarchy; if (hierarchyRecord != null) { if (hierarchyRecord .Territories .Where(tpr => tpr.Id != territory.Record.Id) // exclude current territory .Select(tpr => tpr.TerritoryInternalRecord) .Any(tir => tir.Id == internalRecord.Id)) { throw new TerritoryInternalDuplicateException(T("The selected territory is already assigned in the current hierarchy.")); } } territory.Record.TerritoryInternalRecord = internalRecord; }
public ActionResult AddTerritoryInternalPost() { if (!_authorizer.Authorize(TerritoriesPermissions.ManageInternalTerritories)) { return(new HttpUnauthorizedResult()); } var tir = new TerritoryInternalRecord(); if (!TryUpdateModel(tir, _territoryIncludeProperties)) { _transactionManager.Cancel(); return(View(tir)); } try { _territoryRepositoryService.AddTerritory(tir); } catch (Exception ex) { AddModelError("", ex.Message); return(View(tir)); } return(RedirectToAction("TerritoriesIndex")); }