Adapted the repositories to the controllers

This commit is contained in:
2024-05-23 16:29:35 +02:00
parent c7d4307f47
commit 3b7ca0973e
7 changed files with 101 additions and 93 deletions

View File

@ -1,7 +1,9 @@
using Accounting.BLI;
using Accounting.DAL;
using Accounting.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Accounting.BLR
{
@ -17,12 +19,12 @@ namespace Accounting.BLR
}
public bool AddAccountRecord(AccountRecord record)
public async Task<bool> AddAccountRecordAsync(AccountRecord record)
{
try
{
_dataContext.AccountRecords.Add(record);
_dataContext.SaveChanges();
await _dataContext.AccountRecords.AddAsync(record);
await _dataContext.SaveChangesAsync();
return true;
}
catch (Exception e)
@ -30,20 +32,19 @@ namespace Accounting.BLR
_logger.LogError("Error occured in AddAccountRecord :-->{iMessage}", e.Message);
}
return false;
}
public AccountRecord SaveAcountRecord(AccountRecord record)
public async Task<AccountRecord?> SaveAcountRecordAsync(AccountRecord record)
{
try
{
var entity = (from account in _dataContext.AccountRecords
where account.Id == record.Id
select account).FirstOrDefault();
var entity = await (from account in _dataContext.AccountRecords
where account.Id == record.Id
select account).FirstOrDefaultAsync();
if (entity == null)
{
entity = new AccountRecord();
_dataContext.AccountRecords.Add(entity);
await _dataContext.AccountRecords.AddAsync(entity);
}
else
{
@ -56,7 +57,7 @@ namespace Accounting.BLR
entity.Konto = record.Konto;
entity.Mottagare = record.Mottagare;
_dataContext.SaveChanges();
await _dataContext.SaveChangesAsync();
record.Id = entity.Id;
@ -70,12 +71,12 @@ namespace Accounting.BLR
}
public bool DeleteAccountRecord(AccountRecord record)
public async Task<bool> DeleteAccountRecordAsync(AccountRecord record)
{
try
{
_dataContext.AccountRecords.Remove(record);
_dataContext.SaveChanges();
await _dataContext.SaveChangesAsync();
return true;
}
catch (Exception e)
@ -119,21 +120,58 @@ namespace Accounting.BLR
return result;
}
public IEnumerable<AccountRecord> GetAllAccounts()
public async Task<IEnumerable<AccountRecord>> GetAllAccountsAsync()
{
return _dataContext.AccountRecords;
return await _dataContext.AccountRecords.ToListAsync();
}
public AccountRecord GetAccount(int id)
public async Task<AccountRecord> GetAccountAsync(int id)
{
var accountRec = _dataContext.AccountRecords.FirstOrDefault(a => a.Id == id);
var accountRec = await _dataContext.AccountRecords.FirstOrDefaultAsync(a => a.Id == id);
return accountRec;
}
public AccountRecord GetAccountByDateBelKonto(DateTime _date, double _belopp, string _konto)
public async Task<AccountRecord> GetAccountByDateBelKontoAsync(DateTime _date, double _belopp, string _konto)
{
var accountRec = _dataContext.AccountRecords.FirstOrDefault(a => a.BetalDatum == _date && a.Belopp == _belopp && a.Konto == _konto);
var accountRec = await _dataContext.AccountRecords.FirstOrDefaultAsync(a => a.BetalDatum == _date && a.Belopp == _belopp && a.Konto == _konto);
return accountRec;
}
public async Task<List<AccountRecord>> AddAccRecordsFromJsonAsync(string jsonFile)
{
var jsonText = await System.IO.File.ReadAllTextAsync(jsonFile);
var accRecords = JsonConvert.DeserializeObject<List<AccountRecord>>(jsonText);
if (accRecords == null)
{
_logger.LogError("Error occured in AddAccRecordsFromJsonAsync Deserialization unsuccessful");
return null;
}
using (var transaction = _dataContext.Database.BeginTransaction())
{
try
{
await _dataContext.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.AccountRecords ON");
await _dataContext.AccountRecords.AddRangeAsync(accRecords);
await _dataContext.SaveChangesAsync();
await _dataContext.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.AccountRecords OFF");
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
_logger.LogError("Error occured in AddAccRecordsFromJsonAsync transaction unsuccessful ({0})", ex.Message);
return null;
}
}
return await _dataContext.AccountRecords.ToListAsync();
}
}
}

View File

@ -18,6 +18,10 @@
</COMReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Accounting.BLI\Accounting.BLI.csproj" />
<ProjectReference Include="..\Accounting.DAL\Accounting.DAL.csproj" />

View File

@ -140,8 +140,8 @@ namespace Accounting.BLR
restab.ToList().ForEach(x =>
{
if (_accountRecords.GetAccountByDateBelKonto(x.BetalDatum, x.Belopp, x.Konto) == null)
_accountRecords.AddAccountRecord(x);
if (_accountRecords.GetAccountByDateBelKontoAsync(x.BetalDatum, x.Belopp, x.Konto) == null)
_accountRecords.AddAccountRecordAsync(x);
else { };
});