protected virtual ObservableChangeTracker GetChangeTracker(IRepository repository) { var retVal = new ObservableChangeTracker { RemoveAction = x => repository.Remove(x), AddAction = x => repository.Add(x) }; return retVal; }
public override string Import(string catalogId, string propertySetId, ImportItem[] systemValues, ImportItem[] customValues, IRepository repository) { var _error = string.Empty; _repository = (ICatalogRepository)repository; var action = GetAction(systemValues.First(x => x.Name == "Action").Value); switch (action) { case ImportAction.Insert: var group = systemValues.First(y => y.Name == "GroupName").Value; var target = systemValues.First(y => y.Name == "TargetId").Value; var source = systemValues.First(y => y.Name == "ItemId").Value; var sourceItems = _repository.Items.Where(x => x.ItemId == source || x.Code == source).ToList(); Item sourceItem; //aa: below condition checks if more than 1 item found - catalogId should be provided and item of the catalog selected, otherwise return error if (sourceItems.Count() > 1) { var sourceCatName = systemValues.First(y => y.Name == "SourceCatalogId").Value; if (!string.IsNullOrEmpty(sourceCatName)) catalogId = _repository.Catalogs.Where(cat => cat.Name == sourceCatName).First().CatalogId; if (!string.IsNullOrEmpty(catalogId)) sourceItem = sourceItems.FirstOrDefault(x => x.CatalogId == catalogId); else { var catNames = string.Empty; sourceItems.ForEach(x => catNames = catNames + x.CatalogId + ", "); _error = string.Format(notUniqueSourceError, source, catNames); return _error; } } //aa: if 1 item found set it to sourceItem and go further else { sourceItem = sourceItems.FirstOrDefault(); } var targetItems = _repository.Items.Where(x => x.ItemId == target || x.Code == target).ToList(); Item targetItem; //aa: below condition checks if more than 1 item found - catalogId should be provided and item of the catalog selected, otherwise return error if (targetItems.Count() > 1) { var targetCatName = systemValues.First(y => y.Name == "TargetCatalogId").Value; if (!string.IsNullOrEmpty(targetCatName)) catalogId = _repository.Catalogs.Where(cat => cat.Name == targetCatName).First().CatalogId; if (!string.IsNullOrEmpty(catalogId)) targetItem = targetItems.FirstOrDefault(x => x.CatalogId == catalogId); else { var catNames = string.Empty; targetItems.ForEach(x => catNames = catNames + x.CatalogId + ", "); _error = string.Format(notUniqueTargetError, target, catNames); return _error; } } //aa: if 1 item found set it to sourceItem and go further else { targetItem = targetItems.FirstOrDefault(); } if (!string.IsNullOrEmpty(group) && targetItem != null && sourceItem != null) { var associationGroup = _repository.Associations.Where(x => x.AssociationGroup.Name == group && x.ItemId == targetItem.ItemId).SingleOrDefault(); string groupId; if (associationGroup == null) { var addGroup = new AssociationGroup() { ItemId = targetItem.ItemId, Name = group }; _repository.Add(addGroup); groupId = addGroup.AssociationGroupId; } else { groupId = associationGroup.AssociationGroupId; } var addItem = InitializeItem(null, systemValues); ((Association)addItem).AssociationGroupId = groupId; ((Association)addItem).ItemId = sourceItem.ItemId; _repository.Add(addItem); } else { _error = "Not all required data provided"; } break; case ImportAction.InsertAndReplace: var itemR = systemValues.FirstOrDefault(y => y.Name == "AssociationId"); if (itemR != null) { var originalItem = _repository.Associations.Where(x => x.ItemId == itemR.Value).SingleOrDefault(); if (originalItem != null) repository.Remove(originalItem); } var replaceItem = InitializeItem(null, systemValues); repository.Add(replaceItem); break; case ImportAction.Update: var itemU = systemValues.FirstOrDefault(y => y.Name == "AssociationId"); if (itemU != null) { var origItem = _repository.Associations.Where(x => x.ItemId == itemU.Value).SingleOrDefault(); if (origItem != null) { InitializeItem(origItem, systemValues); _repository.Update(origItem); } } break; case ImportAction.Delete: var itemD = systemValues.FirstOrDefault(y => y.Name == "AssociationId"); if (itemD != null) { var deleteItem = _repository.Associations.Where(x => x.ItemId == itemD.Value).SingleOrDefault(); if (deleteItem != null) _repository.Remove(deleteItem); } break; } return _error; }
private ObservableChangeTracker GetChangeTracker(IRepository repository, CustomerOrderEntity customerOrderEntity) { var retVal = new ObservableChangeTracker { RemoveAction = (x) => { repository.Remove(x); }, AddAction = (x) => { var address = x as AddressEntity; var shipment = x as ShipmentEntity; var lineItem = x as LineItemEntity; if (address != null) { address.CustomerOrder = customerOrderEntity; } if (shipment != null) { foreach (var shipmentItem in shipment.Items) { var orderLineItem = customerOrderEntity.Items.FirstOrDefault(item => item.Id == shipmentItem.Id); if (orderLineItem != null) { orderLineItem.Shipment = shipment; } else { shipmentItem.CustomerOrder = customerOrderEntity; } } shipment.Items = new NullCollection<LineItemEntity>(); } if (lineItem != null) { lineItem.CustomerOrder = customerOrderEntity; lineItem.CustomerOrderId = customerOrderEntity.Id; } repository.Add(x); } }; return retVal; }
// TODO: let's do content negotiation ourselves unless there is a nice // way to do it in Nancy like there seems to be for most things public CountryModule(IRepository<Domain.Country> countryRepository, CountryFactory countryFactory) : base("/countries") { Get["/"] = parameters => { var countryResources = countryRepository.FindAll() .Select(c => new Country {id = c.Id.ToString(), Name = c.Name}); var resource = countryResources.ToArray(); return Response.AsJson(resource); }; Get["/{id}"] = parameters => { var country = countryRepository.GetById((int)parameters.id); if (country == null) return HttpStatusCode.NotFound; var resource = new Country {id = country.Id.ToString(), Name = country.Name}; return Response.AsJson(resource); }; Post["/"] = parameters => { var countryResource = this.Bind<Country>(blacklistedProperties:"id"); var validationResult = this.Validate(countryResource); if (! validationResult.IsValid) { var response = Response.AsJson(validationResult.Errors); response.StatusCode = HttpStatusCode.BadRequest; return response; } var country = countryFactory.CreateCountry(countryResource.Name); countryRepository.Add(country); return Response.AsRedirect("/countries/"+country.Id); }; Put["/{id}"] = parameters => { var country = countryRepository.GetById((int) parameters.id); if (country == null) return HttpStatusCode.NotFound; // this correct for a put? should probably be some error // we don't actually support updates to countries! //country.Name = parameters.Name; return Response.AsRedirect("/countries/" + country.Id); }; Delete["/{id}"] = parameters => { var country = countryRepository.GetById((int)parameters.id); if (country == null) return HttpStatusCode.NotFound; // this correct for a put? should probably be some error countryRepository.Remove(country); return HttpStatusCode.OK; }; }