コード例 #1
0
        public IActionResult AddThesaurus(
            [FromRoute] string database,
            [FromBody] ThesaurusBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ICadmusRepository repository = _repositoryProvider.CreateRepository(database);
            Thesaurus         thesaurus  = new Thesaurus(model.Id);

            foreach (ThesaurusEntryBindingModel entry in model.Entries)
            {
                thesaurus.AddEntry(new ThesaurusEntry(entry.Id, entry.Value));
            }
            repository.AddThesaurus(thesaurus);

            return(CreatedAtRoute("GetThesaurus", new
            {
                database,
                id = thesaurus.Id
            }, thesaurus));
        }
コード例 #2
0
        public Task Run()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("REPLACE APPARATUS THESAURI\n");
            Console.ResetColor();
            Console.WriteLine(
                $"Input file: {_inputPath}" +
                $"Database: {_dbName}\n");

            ILoggerFactory loggerFactory = new LoggerFactory();

            loggerFactory.AddSerilog(Log.Logger);
            Log.Logger.Information("REPLACE APPARATUS THESAURI");

            using Stream input = new FileStream(_inputPath, FileMode.Open,
                                                FileAccess.Read, FileShare.Read);

            JsonDocument doc = JsonDocument.Parse(input, new JsonDocumentOptions
            {
                AllowTrailingCommas = true
            });
            ICadmusRepository repository =
                _repositoryProvider.CreateRepository(_dbName);
            string msg;

            // 1) remove all the apparatus- thesauri
            int delCount = 0;

            foreach (string id in repository.GetThesaurusIds(new ThesaurusFilter
            {
                PageSize = 0,
                Id = "apparatus-"
            }))
            {
                if (!id.StartsWith("apparatus-"))
                {
                    continue;
                }

                delCount++;
                msg = "Deleting " + id;
                Console.WriteLine(msg);
                Log.Logger.Information(msg);

                if (!_isDry)
                {
                    repository.DeleteThesaurus(id);
                }
            }

            // 2) insert new apparatus- thesauri
            // this is the thesauri JSON file generated by import thesauri,
            // having a root array with each thesaurus as an item
            int addCount = 0;

            foreach (JsonElement el in doc.RootElement.EnumerateArray())
            {
                addCount++;
                Thesaurus thesaurus = ReadThesaurus(el);
                msg = "Adding thesaurus " + thesaurus.Id;
                Console.WriteLine(msg);
                Log.Logger.Information(msg);
                if (!_isDry)
                {
                    repository.AddThesaurus(thesaurus);
                }
            }

            Console.WriteLine($"Completed: deleted {delCount}, added {addCount}");
            return(Task.CompletedTask);
        }