Example #1
0
        public async Task InitItems()
        {
            var defaultItems = _initRepository.GetDefaultItems();
            var defaultFiles = _initRepository.GetDefaultFiles();

            using (var trans = _unitOfWork.BeginTransaction())
            {
                var userItems = await _itemRepository.GetTracking(await _itemRepository.GetIdByUser(_userContext.UserId));

                var sameItems = userItems.Join(defaultItems,
                                               item => item.Name.ToLower(),
                                               item => item.Name.ToLower(),
                                               (item, _) => item);

                _itemRepository.DeleteRange(sameItems);

                var userFiles = await _userFileRepository.GetTracking(await _userFileRepository.GetIdByUser(_userContext.UserId));

                var sameFiles = userFiles.Join(defaultFiles,
                                               f => f.FileName.ToLower(),
                                               f => f.FileName.ToLower(),
                                               (r, _) => r);

                _userFileRepository.DeleteRange(sameFiles);

                await _unitOfWork.SaveChangesAsync();

                await _initRepository.AddDefaultItems(_userContext.UserId);

                await _initRepository.AddDefaultFiles(_userContext.UserId);
                await CommitTransaction(trans);
            }
        }