Example #1
0
        public static void Initialize(
            IPatientDbService patientDbService,
            IHttpClientFactory httpClientFactory)
        {
            HttpClient httpClient;

            httpClient = httpClientFactory.CreateClient();

            ICollection <Patient>        patients        = new List <Patient>();
            ICollection <PatientContact> patientContacts = new List <PatientContact>();

            JsonElement patientsElement = JsonDocument
                                          .Parse(GetRandomPatientData(httpClient).Result)
                                          .RootElement.GetProperty("results");

            foreach (JsonElement patientElement in patientsElement.EnumerateArray())
            {
                Patient patient = GetPatientFromJsonElement(patientElement);

                PatientContact patientContact = GetPatientContactFromJsonElement(patientElement, patient);

                patients.Add(patient);
                patientContacts.Add(patientContact);
            }

            patientDbService.AddPatientRange(patients.Cast <Patient>());
            patientDbService.AddPatientContactRange(patientContacts.Cast <PatientContact>());
        }
Example #2
0
        private static void TestDbConnection(
            ILogger <Program> logger,
            IPatientDbService patientDbService)
        {
            for (int i = 0; i < MaxRetries - 1; i++)
            {
                if (patientDbService.CanConnect())
                {
                    logger.LogInformation("Successfully connected to database.");
                    return;
                }

                logger.LogError("Cannot connect to database. Retrying connection.");
                System.Threading.Thread.Sleep(5000);
            }

            if (patientDbService.CanConnect())
            {
                logger.LogInformation("Successfully connected to database.");
                return;
            }
            else
            {
                logger.LogCritical("Failed to connect to database.");
            }
        }
 public PatientController(
     ILogger <PatientController> logger,
     IPatientDbService patientDbService)
 {
     _logger           = logger;
     _patientDbService = patientDbService;
 }
Example #4
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services => {
                ServiceDescriptor descriptor = services.SingleOrDefault(
                    d => d.ServiceType ==
                    typeof(DbContextOptions <PatientDbContext>));

                services.Remove(descriptor);

                services.AddDbContext <PatientDbContext>(options => {
                    options.UseInMemoryDatabase("InMemoryDbForTesting");
                });

                ServiceProvider serviceProvider = services.BuildServiceProvider();

                IConfiguration configuration = serviceProvider.GetRequiredService <IConfiguration>();

                switch (configuration.GetValue <int>("PATIENTSERVICE_ORM"))
                {
                case 2:
                    services.AddScoped <IPatientDbService, PatientDapperDbService>();
                    break;

                default:
                    services.AddScoped <IPatientDbService, PatientEfCoreDbService>();
                    break;
                }

                using (IServiceScope scope = serviceProvider.CreateScope()) {
                    IServiceProvider scopedProvider    = scope.ServiceProvider;
                    PatientDbContext patientDbContext  = scopedProvider.GetRequiredService <PatientDbContext>();
                    IPatientDbService patientDbService = scopedProvider.GetRequiredService <IPatientDbService>();
                    ILogger <CustomWebApplicationFactory <TStartup> > logger = scopedProvider
                                                                               .GetRequiredService <ILogger <CustomWebApplicationFactory <TStartup> > >();

                    patientDbContext.Database.EnsureDeleted();
                    patientDbContext.Database.EnsureCreated();

                    try {
                        TestUtilities.InitializeDb(patientDbService);
                    } catch (Exception ex) {
                        logger.LogError(ex, "An error occurred seeding the " +
                                        "database with test messages. Error: {Message}", ex.Message);
                    }
                }
            });
        }
Example #5
0
        private static void SeedDb(
            ILogger <Program> logger,
            IConfiguration configuration,
            IServiceProvider serviceProvider,
            IPatientDbService patientDbService,
            IWebHostEnvironment webHostEnvironment)
        {
            bool seedDb = false;

            if (webHostEnvironment.IsDevelopment())
            {
                if (Boolean.TryParse(configuration["PATIENTSERVICE_SEED_DB"], out seedDb) && seedDb)
                {
                    logger.LogInformation("Seeding database.");

                    SeedData.Initialize(patientDbService,
                                        serviceProvider.GetRequiredService <IHttpClientFactory>());
                }
            }
        }
Example #6
0
        public static void Main(string[] args)
        {
            IHost host = CreateHostBuilder(args).Build();

            using (IServiceScope serviceScope = host.Services.CreateScope()) {
                IServiceProvider serviceProvider = serviceScope.ServiceProvider;

                ILogger <Program>   logger             = serviceProvider.GetRequiredService <ILogger <Program> >();
                IConfiguration      configuration      = serviceProvider.GetRequiredService <IConfiguration>();
                IPatientDbService   patientDbService   = serviceProvider.GetRequiredService <IPatientDbService>();
                IWebHostEnvironment webHostEnvironment = serviceProvider.GetRequiredService <IWebHostEnvironment>();

                logger.LogInformation("Environment: " + webHostEnvironment.EnvironmentName);

                LogOrmConfig(logger, configuration);
                EnsureDbCreated(logger, patientDbService, webHostEnvironment);
                TestDbConnection(logger, patientDbService);
                SeedDb(logger, configuration, serviceProvider, patientDbService, webHostEnvironment);
            }

            host.Run();
        }
Example #7
0
 public PatientsController(IPatientDbService service)
 {
     _service = service;
 }
 public static void InitializeDb(IPatientDbService patientDbService)
 {
     patientDbService.AddPatient(Patient);
     patientDbService.AddPatientContact(PatientContact);
 }
Example #9
0
        private static void EnsureDbCreated(
            ILogger <Program> logger,
            IPatientDbService patientDbService,
            IWebHostEnvironment webHostEnvironment)
        {
            int  retries = 0;
            bool created = false;

            if (webHostEnvironment.IsDevelopment())
            {
                logger.LogInformation("Creating new database.");

                do
                {
                    try {
                        patientDbService.EnsureDeleted();
                        patientDbService.EnsureCreated();

                        created = true;
                    } catch (Exception e) {
                        if (retries < MaxRetries)
                        {
                            logger.LogError(e, "Connection to DBMS failed. Retrying connection.");
                            System.Threading.Thread.Sleep(5000);

                            retries++;
                        }
                        else
                        {
                            logger.LogCritical(e, "Failed to connect to create database.");
                            throw;
                        }
                    }
                } while (!created);
            }
            else
            {
                if (!patientDbService.CanConnect())
                {
                    logger.LogInformation("Cannot connect to database, Creating new database.");

                    do
                    {
                        try {
                            patientDbService.EnsureCreated();
                        } catch (Exception e) {
                            if (retries < MaxRetries)
                            {
                                logger.LogError(e, "Connection to DBMS failed. Retrying connection.");
                                System.Threading.Thread.Sleep(5000);

                                retries++;
                            }
                            else
                            {
                                logger.LogCritical(e, "Failed to connect to create database.");
                                throw;
                            }
                        }
                    } while (!created);
                }
            }
        }
 public PatientController(IPatientDbService service, IConfiguration configuration)
 {
     this._service      = service;
     this.Configuration = configuration;
 }