Beispiel #1
0
        public async Task <CoreDataItem> CreateItemAsync(CoreDataItem item)
        {
            _context.CoreDataItems.Add(item);
            await _context.SaveChangesAsync();

            return(await GetItemAsync(item.Id));
        }
Beispiel #2
0
        public async Task CreateOrMergeItemAsync(ShoppingListItem item)
        {
            ShoppingListItem itemOnList = await GetItemByNameAsync(item.OwnerId, item.Name);

            // check if items could be merged
            if (itemOnList != null && (ValueUnit.TryParse(itemOnList.Quantity, out ValueUnit onlistValue) && ValueUnit.TryParse(item.Quantity, out ValueUnit coreDataValue)))
            {
                // merge items
                ValueUnit newValueUnit = ValueUnit.Add(onlistValue, coreDataValue);

                itemOnList.Quantity = newValueUnit.ToString();
            }
            else
            {
                // create new item
                _context.ShoppingListItems.Add(item);
            }
            await _context.SaveChangesAsync();
        }