Files
Accounting/Accounting.BLR/AccountRecords.cs

178 lines
6.1 KiB
C#

using Accounting.BLI;
using Accounting.DAL;
using Accounting.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Accounting.BLR
{
public class AccountRecords : IAccountRecords
{
private readonly DataContext _dataContext;
private readonly ILogger<AccountRecords> _logger;
public AccountRecords(DataContext dataContext, ILogger<AccountRecords> logger)
{
_dataContext = dataContext;
_logger = logger;
}
public async Task<bool> AddAccountRecordAsync(AccountRecord record)
{
try
{
await _dataContext.AccountRecords.AddAsync(record);
await _dataContext.SaveChangesAsync();
return true;
}
catch (Exception e)
{
_logger.LogError("Error occured in AddAccountRecord :-->{iMessage}", e.Message);
}
return false;
}
public async Task<AccountRecord?> SaveAcountRecordAsync(AccountRecord record)
{
try
{
var entity = await (from account in _dataContext.AccountRecords
where account.Id == record.Id
select account).FirstOrDefaultAsync();
if (entity == null)
{
entity = new AccountRecord();
await _dataContext.AccountRecords.AddAsync(entity);
}
else
{
entity.Stored = record.Stored;
}
entity.Avisering = record.Avisering;
entity.BetalDatum = record.BetalDatum;
entity.Belopp = record.Belopp;
entity.Konto = record.Konto;
entity.Mottagare = record.Mottagare;
await _dataContext.SaveChangesAsync();
record.Id = entity.Id;
return entity;
}
catch (Exception e)
{
_logger.LogError("Error occured in SaveAccountRecord :-->{iMessage}", e.Message);
return null;
}
}
public async Task<bool> DeleteAccountRecordAsync(AccountRecord record)
{
try
{
_dataContext.AccountRecords.Remove(record);
await _dataContext.SaveChangesAsync();
return true;
}
catch (Exception e)
{
_logger.LogError("Error occured in DeleteAccountRecord :-->{iMessage}", e.Message);
}
return false;
}
public bool DeleteAllAccountRecords()
{
try
{
var all = from c in _dataContext.AccountRecords select c;
_dataContext.AccountRecords.RemoveRange(all);
_dataContext.SaveChanges();
return true;
}
catch (Exception e)
{
_logger.LogError("Error occured in DeleteAllAccountRecord :-->{iMessage}", e.Message);
}
return false;
}
public IEnumerable<AccountRecord> GetAllAccBetweenDates(DateTime dateFrom, DateTime dateTo)
{
IEnumerable<AccountRecord> result = null;
result = (from acc in _dataContext.AccountRecords
where acc.BetalDatum > dateFrom && acc.BetalDatum < dateTo
orderby acc.Mottagare, acc.BetalDatum descending
select new AccountRecord
{
Id = acc.Id,
BetalDatum = acc.BetalDatum,
Mottagare = acc.Mottagare,
Avisering = acc.Avisering,
Belopp = acc.Belopp,
Konto = acc.Konto,
Stored = acc.Stored
}).ToList();
return result;
}
public async Task<IEnumerable<AccountRecord>> GetAllAccountsAsync()
{
return await _dataContext.AccountRecords.ToListAsync();
}
public async Task<AccountRecord> GetAccountAsync(int id)
{
var accountRec = await _dataContext.AccountRecords.FirstOrDefaultAsync(a => a.Id == id);
return accountRec;
}
public async Task<AccountRecord> GetAccountByDateBelKontoAsync(DateTime _date, double _belopp, string _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();
}
}
}