예제 #1
0
        private async Task InitializeInMemoryRepositoryFromJson <TEntity>(MagusTtkContext dataContext, IReadOnlyRepository <TEntity> repo, string jsonFilePath, CancellationToken cancellationToken = default)
            where TEntity : IHasUniqueCode
        {
            string jsonString = await fileContentResolver.ReadFileAsTextAsync(jsonFilePath, cancellationToken);

            if (string.IsNullOrWhiteSpace(jsonString))
            {
                throw new ArgumentException($"Json file '{jsonFilePath}' not found.");
            }

            var entitiesFromJson = JsonDeserializer.DeserializeFromJsonString <TEntity[]>(jsonString);

            if ((entitiesFromJson == null) || (entitiesFromJson.Length == 0))
            {
                throw new ArgumentException($"Could not parse entities of type '{typeof(TEntity)}' from Json file '{jsonFilePath}'.");
            }

            HashSet <string> codeHashes = new HashSet <string>();
            var editableRepo            = repo.AsEditableRepository();

            foreach (var entity in entitiesFromJson)
            {
                // hiányzó Code
                if (string.IsNullOrWhiteSpace(entity.Code))
                {
                    throw new ArgumentException($"Unique code of '{typeof(TEntity)}' in file '{jsonFilePath}' is empty.");
                }

                // ha nem tudja hozzáadni, akkor már volt ilyen kóddal
                if (!codeHashes.Add(entity.Code))
                {
                    throw new ArgumentException($"Code '{entity.Code}' in file '{jsonFilePath}' is a duplicate.");
                }

                await editableRepo.Add(entity, cancellationToken);
            }
        }