Пример #1
0
        public async Task <IActionResult> DeploySimulator(SimulatorDto simulatorDto)
        {
            logger.LogInformation("Deploying simulator...");
            bool simulatorExists = await this.simulatorFamilyRepository.SimulatorExists(simulatorDto.Identifier, simulatorDto.Version);

            if (simulatorExists)
            {
                return(Conflict($"Simulator(Identifier='{simulatorDto.Identifier}', Version={simulatorDto.Version}, NameLanguageKey='{simulatorDto.NameLanguageKey}' already exists."));
            }

            SimulatorFamily family = new SimulatorFamily
            {
                Identifier      = simulatorDto.SimulatorFamilyIdentifier,
                NameLanguageKey = simulatorDto.SimulatorFamilyNameLanguageKey
            };

            Simulator    simulator = this.mapper.Map <Simulator>(simulatorDto);
            List <Scene> scenes    = this.mapper.Map <IEnumerable <Scene> >(simulatorDto.Sections.SelectMany(x => x.Scenes)).ToList();

            scenes.ForEach(scene => { scene.SimulatorIdentifier = simulatorDto.Identifier; scene.SimulatorVersion = simulatorDto.Version; });

            using (IClientSessionHandle session = await this.clientWrapper.Client.StartSessionAsync(new ClientSessionOptions()))
            {
                try
                {
                    session.StartTransaction();
                    await this.sceneRepository.InsertMany(scenes, session);

                    foreach (var sectionDto in simulatorDto.Sections)
                    {
                        var section = simulator.SceneSections.FirstOrDefault(x => x.Identifier == sectionDto.Identifier);
                        foreach (var sceneOfSection in sectionDto.Scenes)
                        {
                            section.SceneIdentifiers.Add(scenes.FirstOrDefault(x => x.Identifier == sceneOfSection.Identifier).Id);
                        }
                    }

                    await this.simulatorFamilyRepository.UpsertAndAddSimulator(family, simulator, session);

                    await session.CommitTransactionAsync();
                }
                catch (Exception)
                {
                    await session.AbortTransactionAsync();

                    throw;
                }
            }


            logger.LogInformation("Deploying simulator finished");
            return(Ok());
        }
        public async Task Execute()
        {
            string connectionString = "mongodb+srv://mikino55:[email protected]/test?retryWrites=true&w=majority";

            MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
            //DB db = new DB(
            //    MongoClientSettings.FromConnectionString("mongodb+srv://mikino55:[email protected]/test?retryWrites=true&w=majority"),
            //    "VirtaMed");
            //var database = db.GetDatabase();
            //database.DropCollection("Person");

            MongoClientWrapper clientWrapper = new MongoClientWrapper(connectionString);
            //MongoClient client = new MongoClient(connectionString);
            //IMongoDatabase database = client.GetDatabase("VirtaMed");
            //database.CreateCollection("Person");
            //IMongoCollection<SimulatorFamily> collection = database.GetCollection<SimulatorFamily>("SimulatorFamily");
            SimulatorFamilyRepository repository = new SimulatorFamilyRepository(clientWrapper);

            //database.CreateCollection("SimulatorFamily");

            SimulatorFamily family = new SimulatorFamily
            {
                //Identifier = Guid.NewGuid(),
                Identifier      = Guid.Parse("177c20f1-e443-4646-af54-5850a481a8ee"),
                NameLanguageKey = "Arthros",
                Simulators      = new List <Simulator>
                {
                    new Simulator
                    {
                        Identifier      = Guid.Parse("d92b4ae1-680d-48e5-96ee-6acbb91d90cf"),
                        NameLanguageKey = "Arthros",
                        Version         = "1.0.0.0",
                        SceneSections   = new List <SceneSection>
                        {
                            new SceneSection
                            {
                                Identifier      = Guid.NewGuid(),
                                NameLanguageKey = "Section language key",
                            }
                        }
                    }
                }
            };
            //await repository.UpsertAndAddSimulator(family, null);
            //var result = family.Save();
            //collection.InsertOne(p);
        }