public async Task UpdateRowAsync(ItemCategoryLookupView updateVeiwEntity) { if (await IsDuplicate(updateVeiwEntity)) { _gridSettings.PopUpRef.ShowNotification(PopUpAndLogNotification.NotificationType.Error, $"Category Name: {updateVeiwEntity.CategoryName} - already exists, cannot be updated", "Exists already"); } else { if (IsValid(updateVeiwEntity)) { ItemCategoryLookup updatedItemCategoryLookup = GetItemFromView(updateVeiwEntity); // update and check for errors if (await UpdateItemAsync(updateVeiwEntity) != AppUnitOfWork.CONST_WASERROR) { if ((updateVeiwEntity.HasWooCategoryMap) && (await UpdateWooCategoryMap(updateVeiwEntity) == AppUnitOfWork.CONST_WASERROR)) { _gridSettings.PopUpRef.ShowNotification(PopUpAndLogNotification.NotificationType.Error, $"WooCategory map for Item: {updateVeiwEntity.CategoryName} - {_appUnitOfWork.GetErrorMessage()}", "Error updating"); } //else // PopUpRef.ShowNotification(PopUpAndLogNotification.NotificationType.Success, $"Category: {updatedItem.CategoryName} was updated."); } } else { string pMessage = $"Category Item {updateVeiwEntity.CategoryName} cannot be parent and child."; _gridSettings.PopUpRef.ShowNotification(PopUpAndLogNotification.NotificationType.Error, pMessage, "Error updating"); } } await LoadAllViewItems(); // reload the list so the latest item is displayed }
public async Task <Guid> AddOrGetIDItemCategoryLookup(ProductCategory sourcePC, List <WooItemWithParent> sourceCategoriesWithParents) { IAppRepository <ItemCategoryLookup> _itemCategoryLookupRepository = _AppUnitOfWork.Repository <ItemCategoryLookup>(); ItemCategoryLookup _itemCategoryLookup = await _itemCategoryLookupRepository.FindFirstAsync(ic => ic.CategoryName == sourcePC.name); if (_itemCategoryLookup == null) { ItemCategoryLookup _newItemCategoryLookup = new ItemCategoryLookup { CategoryName = sourcePC.name, // Null;? ParentCategoryId = Guid.Empty, UsedForPrediction = (sourcePC.parent == null) || (sourcePC.parent == 0), Notes = $"Imported Woo Category ID {sourcePC.id}" }; if (sourcePC.parent > 0) { sourceCategoriesWithParents.Add(new WooItemWithParent { ChildId = (int)sourcePC.id, ParentId = (int)sourcePC.parent }); } await _itemCategoryLookupRepository.AddAsync(_newItemCategoryLookup); return(_newItemCategoryLookup.ItemCategoryLookupId); } else { return(Guid.Empty); } }
public async Task <int> UpdateItemAsync(ItemCategoryLookup updateItem) { int _recsUpdted = 0; IAppRepository <ItemCategoryLookup> _ItemCategoryLookupRepository = _appUnitOfWork.Repository <ItemCategoryLookup>(); // first check it exists - it could have been deleted ItemCategoryLookup pUpdatedLookup = await _ItemCategoryLookupRepository.GetByIdAsync(updateItem.ItemCategoryLookupId); if (pUpdatedLookup == null) { _gridSettings.PopUpRef.ShowNotification(PopUpAndLogNotification.NotificationType.Error, $"Category: {updateItem.CategoryName} is no longer found, was it deleted?"); return(AppUnitOfWork.CONST_WASERROR); } else { pUpdatedLookup.CategoryName = updateItem.CategoryName; pUpdatedLookup.ParentCategoryId = (updateItem.ParentCategoryId == Guid.Empty) ? null : updateItem.ParentCategoryId; pUpdatedLookup.UsedForPrediction = updateItem.UsedForPrediction; pUpdatedLookup.Notes = updateItem.Notes; _recsUpdted = await _ItemCategoryLookupRepository.UpdateAsync(pUpdatedLookup); if (_recsUpdted == AppUnitOfWork.CONST_WASERROR) { _gridSettings.PopUpRef.ShowNotification(PopUpAndLogNotification.NotificationType.Error, $"{updateItem.CategoryName} - {_appUnitOfWork.GetErrorMessage()}", "Error adding Category"); } } return(_recsUpdted); }
public async Task <bool> IsDuplicate(ItemCategoryLookup targetEntity) { // check if does not exist in the list already (they edited it and it is the same name as another. Only a max of one should exists IAppRepository <ItemCategoryLookup> _ItemCategoryLookupRepository = _appUnitOfWork.Repository <ItemCategoryLookup>(); var _exists = (await _ItemCategoryLookupRepository.GetByAsync(ml => ml.CategoryName == targetEntity.CategoryName)).ToList(); return((_exists != null) && (_exists.Count > 1)); }
public ItemCategoryLookup GetItemFromView(ItemCategoryLookupView fromVeiwEntity) { ItemCategoryLookup _newItemCategoryLookup = new ItemCategoryLookup { ItemCategoryLookupId = fromVeiwEntity.ItemCategoryLookupId, CategoryName = fromVeiwEntity.CategoryName, UsedForPrediction = fromVeiwEntity.UsedForPrediction, ParentCategoryId = (fromVeiwEntity.ParentCategoryId == Guid.Empty) ? null : fromVeiwEntity.ParentCategoryId, Notes = fromVeiwEntity.Notes, }; return(_newItemCategoryLookup); }
async Task <Guid> AddOrUpdateItemCategoryLookup(ProductCategory sourcePC, Guid sourceWooMappedItemCategoryLookupId, List <WooItemWithParent> sourceCategoriesWithParents) { Guid _itemCategoryLookupId = Guid.Empty; IAppRepository <ItemCategoryLookup> _itemCategoryLookupRepository = _AppUnitOfWork.Repository <ItemCategoryLookup>(); // check if the category exists ItemCategoryLookup _itemCategoryLookup = await _itemCategoryLookupRepository.FindFirstAsync(ic => ic.ItemCategoryLookupId == sourceWooMappedItemCategoryLookupId); if (_itemCategoryLookup != null) { _itemCategoryLookupId = await UpdateItemCategoryLookup(sourcePC, _itemCategoryLookup, sourceCategoriesWithParents); } else { _itemCategoryLookupId = await AddOrGetIDItemCategoryLookup(sourcePC, sourceCategoriesWithParents); } return(_itemCategoryLookupId); }
/// <summary> /// Using the item sent assign the categories that the WooProduct has. Remember that we do not affect ItemId since the EF core /// </summary> /// <param name="Item">The Product Item t add too</param> /// <param name="WooProduct">The Woo Product Source</param> /// <returns></returns> async Task <Item> AssignWooProductCategory(Item currItem, Product currWooProd) { bool _isCatUpdate = currItem.ItemCategories != null; if (currItem != null) { // if we get here then we have an item and it has an id IAppRepository <ItemCategoryLookup> _itemCategoryLookupRepo = _AppUnitOfWork.Repository <ItemCategoryLookup>(); ItemCategoryLookup _itemCategoryLookup = null; bool _SetPrimary = true; foreach (var cat in currWooProd.categories) { Guid CategoryId = await GetCategoryById((int)cat.id); _itemCategoryLookup = await _itemCategoryLookupRepo.FindFirstAsync(ic => ic.ItemCategoryLookupId == CategoryId); if (_itemCategoryLookup != null) { ///////// check if exists if this is not a new item if ((_isCatUpdate) && (currItem.ItemCategories.Exists(ic => ic.ItemCategoryLookupId == _itemCategoryLookup.ItemCategoryLookupId))) { // this attribute exists so just update it - nothing to do here at the moment. } else { // add an item to the linked list. Since it is a new item the FK must be guid.empty for EF Core to work if (currItem.ItemCategories == null) { currItem.ItemCategories = new List <ItemCategory>(); } currItem.ItemCategories.Add(new ItemCategory { ItemCategoryLookupId = _itemCategoryLookup.ItemCategoryLookupId }); if (_SetPrimary) // assume the first in the list is the primary, as we have no other way of knowing. { currItem.PrimaryItemCategoryLookupId = _itemCategoryLookup.ItemCategoryLookupId; _SetPrimary = false; } } } } } return(currItem); // return the currItem after we have manipulated it. }
async Task <Guid> UpdateItemCategoryLookup(ProductCategory updatedPC, ItemCategoryLookup updatedItemCategoryLookup, List <WooItemWithParent> sourceAttribsWithParents) { IAppRepository <ItemCategoryLookup> _itemCategoryLookupRepository = _AppUnitOfWork.Repository <ItemCategoryLookup>(); updatedItemCategoryLookup.CategoryName = updatedPC.name; updatedItemCategoryLookup.UsedForPrediction = (updatedPC.parent == null) || (updatedPC.parent == 0); // do not set parent Id here since it could cause database problems - if it is already null then it will be updated later. // pItemCategoryLookup.ParentCategoryId = Guid.Empty; /// need to find the ParentId if it exists - or need to say it does not exists so that we can look later? if (updatedPC.parent > 0) { sourceAttribsWithParents.Add(new WooItemWithParent { ChildId = (int)updatedPC.id, ParentId = (int)updatedPC.parent }); } updatedItemCategoryLookup.Notes = $"Updated Woo Category ID {updatedPC.id}"; bool _success = await _itemCategoryLookupRepository.UpdateAsync(updatedItemCategoryLookup) != AppUnitOfWork.CONST_WASERROR; return((_success) ? updatedItemCategoryLookup.ItemCategoryLookupId : Guid.Empty); }
async Task <bool> SetParentCategory(Guid sourceChildWooCategoryId, Guid sourceParentWooCategoryId) { // using the GUIDs of the category id update the parent of that record bool _IsUpdated = false; if ((sourceChildWooCategoryId != Guid.Empty) && (sourceParentWooCategoryId != Guid.Empty)) { IAppRepository <ItemCategoryLookup> _itemCategoryLookupRepository = _AppUnitOfWork.Repository <ItemCategoryLookup>(); ItemCategoryLookup _itemCategoryLookup = await _itemCategoryLookupRepository.FindFirstAsync(ic => ic.ItemCategoryLookupId == sourceChildWooCategoryId); if (_itemCategoryLookup == null) { _IsUpdated = false; } else { // found so update _itemCategoryLookup.ParentCategoryId = sourceParentWooCategoryId; _IsUpdated = (await _itemCategoryLookupRepository.UpdateAsync(_itemCategoryLookup)) != AppUnitOfWork.CONST_WASERROR; } } return(_IsUpdated); }
public bool IsValid(ItemCategoryLookup checkEntity) { return(checkEntity.ParentCategoryId != checkEntity.ItemCategoryLookupId); }