Exemplo n.º 1
0
        private void ValidateAndSetRealmShardCode(Realm realm, RealmShard shard)
        {
            var code    = shard.Name.GenerateSlug();
            var matches = realm.RealmShards
                          .Where(x =>
                                 x.Code == code ||
                                 x.Code.StartsWith($"{code}_"))
                          .ToList();

            if (matches.Count > 0)
            {
                code += $"_{matches.Count}";
            }

            shard.Code = code;
        }
Exemplo n.º 2
0
        private RealmUpdateResponse ProcessAndPersistRealmShards(Realm realm, List <RealmShardInputModel> shards)
        {
            var response      = new RealmUpdateResponse();
            var allFragments  = realm.Fragments.ToList();
            var currentShards = realm.RealmShards.ToList();

            var existingShards = shards
                                 .Where(x => x.Id.HasValue);

            var shardIds = existingShards
                           .Select(x => x.Id.Value);

            // Update or Remove shards
            foreach (var shard in currentShards)
            {
                if (shardIds.Contains(shard.Id))
                {
                    var update = existingShards.First(x => x.Id.Value == shard.Id);
                    if (string.IsNullOrEmpty(update.Name))
                    {
                        response.ErrorMessages.Add("Shard Name is required.");
                        return(response);
                    }

                    shard.Name      = update.Name;
                    shard.IsOrdered = update.IsOrdered;

                    var shardFragmentIds = update.EntryList.Select(x => x.FragmentId).ToList();
                    var entries          = shard.RealmShardEntries.ToList();

                    foreach (var frag in entries)
                    {
                        if (shardFragmentIds.Contains(frag.FragmentId))
                        {
                            frag.EntryOrder = shard.IsOrdered
                                ? shardFragmentIds.IndexOf(frag.FragmentId)
                                : (int?)null;

                            _realmDataService.SetToPersist(frag);
                        }
                        else
                        {
                            shard.RealmShardEntries.Remove(frag);
                        }
                    }

                    var newFragments = update.EntryList.Where(x =>
                                                              !shard.RealmShardEntries.Any(s => s.FragmentId == x.FragmentId));

                    foreach (var inputFrag in newFragments)
                    {
                        var newFragEntry = new RealmShardEntry
                        {
                            EntryOrder = shard.IsOrdered ? shardFragmentIds.IndexOf(inputFrag.FragmentId) : (int?)null,
                            FragmentId = inputFrag.FragmentId
                        };

                        _realmDataService.SetToPersist(newFragEntry);
                        shard.RealmShardEntries.Add(newFragEntry);
                    }

                    _realmDataService.SetToPersist(shard);
                }
                else
                {
                    realm.RealmShards.Remove(shard);
                }
            }

            var newShards = shards.Where(x => !x.Id.HasValue);

            // Add shards
            foreach (var input in newShards)
            {
                if (string.IsNullOrEmpty(input.Name))
                {
                    response.ErrorMessages.Add("Shard Name is required.");
                    return(response);
                }

                var newRealmShard = new RealmShard
                {
                    Name      = input.Name,
                    IsOrdered = input.IsOrdered,
                    RealmId   = realm.Id
                };

                ValidateAndSetRealmShardCode(realm, newRealmShard);

                var entryOrder = 0;
                foreach (var frag in input.EntryList)
                {
                    var entry = new RealmShardEntry
                    {
                        EntryOrder = newRealmShard.IsOrdered ? entryOrder++ : (int?)null,
                        FragmentId = frag.FragmentId,
                    };

                    _realmDataService.SetToPersist(entry);
                    newRealmShard.RealmShardEntries.Add(entry);
                }

                _realmDataService.SetToPersist(newRealmShard);
                realm.RealmShards.Add(newRealmShard);
            }

            return(response);
        }