/// <summary> /// Creates necessary database tables and seeds initial data. /// </summary> private async Task InitializeDatabase() { using (var context = new MeterReaderContext()) { DatabaseFacade db = context.Database; RelationalDatabaseCreator databaseCreator = (RelationalDatabaseCreator)db.GetService <IDatabaseCreator>(); try { databaseCreator.Create(); } catch (Exception e) { // Database already exists } try { databaseCreator.CreateTables(); } catch (Exception e) { // Tables already exist } using (StreamReader accountsCsv = new StreamReader(CUSTOMER_CSV_FILE_PATH)) { CustomerAccountCsvParser csvParser = new CustomerAccountCsvParser(); CsvParseResult <CustomerAccount> accounts = await csvParser.ParseCsvFileAsync(accountsCsv); CustomerAccountDbTableInterface accountsTable = new CustomerAccountDbTableInterface(); await accountsTable.InsertEntriesAsync(accounts.Data); } } }
/// <summary> /// Asynchronously inserts meter reading data into the database if it is unique otherwise ignores it. /// </summary> /// <param name="meterReadings">Meter reading data to insert.</param> /// <returns>DbResult detailing the result of the insert operation.</returns> public async Task <DbResult> InsertEntriesAsync(ICollection <MeterReading> meterReadings) { using (var context = new MeterReaderContext()) { IEnumerable <MeterReading> validReadings = RemoveInvalidReadings(meterReadings, context); try { foreach (MeterReading reading in validReadings) { reading.Id = GenerateMeterReadingId(reading); } int initialRowCount = context.MeterReadings.Count(); await context.BulkMergeAsync(validReadings); int finalRowCount = context.MeterReadings.Count(); int insertCount = finalRowCount - initialRowCount; return(new DbResult { InsertCount = insertCount, ErrorCount = meterReadings.Count - insertCount }); } catch (Exception e) { return(new DbResult { InsertCount = 0, ErrorCount = meterReadings.Count }); } } }
/// <summary> /// Removes all entries from the meter readings table. /// </summary> /// <returns>True if all entries were deleted else false.</returns> public async Task <bool> ClearTableAsync() { using (var context = new MeterReaderContext()) { await context.BulkDeleteAsync(context.MeterReadings); return(context.MeterReadings.Count() == 0); } }
/// <summary> /// Asynchronously inserts account data into the database if it is unique otherwise ignores it. /// </summary> /// <param name="accounts">Account data to insert.</param> /// <returns>DbResult detailing the result of the insert operation.</returns> public async Task <DbResult> InsertEntriesAsync(ICollection <CustomerAccount> accounts) { using (var context = new MeterReaderContext()) { int initialRowCount = context.Accounts.Count(); await context.BulkMergeAsync(accounts); int finalRowCount = context.Accounts.Count(); int insertCount = finalRowCount - initialRowCount; return(new DbResult { InsertCount = insertCount, ErrorCount = accounts.Count - insertCount }); } }
/// <summary> /// Returns the filtered subset of meter readings that have a valid Account ID. /// </summary> /// <param name="readings">Meter readings to filter.</param> /// <returns>Valid meter readings subset.</returns> private IEnumerable <MeterReading> RemoveInvalidReadings(ICollection <MeterReading> readings, MeterReaderContext context) { IEnumerable <string> actualAccountIds = context.Accounts.Select(account => account.Id); return(readings.Where(reading => actualAccountIds.Contains(reading.AccountId))); }