//TODO: create new sorting + UserEntrySortings public void CreateSorting(UserListSortingDto dto, IList <ProductDto> products) { int sortingId = _listSortingRepository.Create(ConvertDtoToDB(dto)); for (int x = 0; x < products.Count(); x++) { Nullable <int> prevEntryId = null; if (x > 0) { prevEntryId = products[x - 1].Id; } Nullable <int> nextEntryId = null; if (x < products.Count() - 1) { nextEntryId = products[x + 1].Id; } UserEntrySorting entrySorting = new UserEntrySorting { UserListSorting_Id = sortingId, ShoppingListEntry_Id = products[x].Id, PrevEntryId_Id = prevEntryId, NextEntryId_Id = nextEntryId }; _entrySortingRepository.Create(entrySorting); } ShoppingListService shoppingListService = new ShoppingListService(); ShoppingListDto shoppingList = new ShoppingListDto(); shoppingList.Id = dto.ShoppingList_Id; shoppingList.ChosenSortingId = sortingId; shoppingListService.Update(shoppingList); }
//TODO: (get) apply sorting by id + UserEntrySortings public IList <ProductDto> ApplyUserSorting(int sortingId, IList <ProductDto> products) { List <ProductDto> sortedList = new List <ProductDto>(); var nextEntry = _entrySortingRepository.GetFirstEntry(sortingId); //1. finding very first product foreach (ProductDto product in products) { if (nextEntry.ShoppingListEntry_Id == product.Id) { sortedList.Add(product); } } //2. getting each next product if (products.Count > 1) { for (int x = 0; x < products.Count(); x++) { UserEntrySorting entry = new UserEntrySorting(); if (nextEntry.NextEntryId_Id != null) { entry = _entrySortingRepository.GetByEntryId(sortingId, (int)nextEntry.NextEntryId_Id); } foreach (ProductDto product in products) { if (entry.ShoppingListEntry_Id == product.Id) { sortedList.Add(product); if (entry.NextEntryId_Id != null) { nextEntry = _entrySortingRepository.GetByEntryId(sortingId, (int)entry.NextEntryId_Id); } } } if (entry.NextEntryId_Id == null) { return(sortedList); } } } return(sortedList); }