Införande av repository method

This commit is contained in:
2024-05-22 11:10:37 +02:00
parent 673387215d
commit c7d4307f47
23 changed files with 541 additions and 17 deletions

View File

@ -0,0 +1,139 @@
using Accounting.BLI;
using Accounting.DAL;
using Accounting.Models;
using Microsoft.Extensions.Logging;
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 bool AddAccountRecord(AccountRecord record)
{
try
{
_dataContext.AccountRecords.Add(record);
_dataContext.SaveChanges();
return true;
}
catch (Exception e)
{
_logger.LogError("Error occured in AddAccountRecord :-->{iMessage}", e.Message);
}
return false;
}
public AccountRecord SaveAcountRecord(AccountRecord record)
{
try
{
var entity = (from account in _dataContext.AccountRecords
where account.Id == record.Id
select account).FirstOrDefault();
if (entity == null)
{
entity = new AccountRecord();
_dataContext.AccountRecords.Add(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;
_dataContext.SaveChanges();
record.Id = entity.Id;
return entity;
}
catch (Exception e)
{
_logger.LogError("Error occured in SaveAccountRecord :-->{iMessage}", e.Message);
return null;
}
}
public bool DeleteAccountRecord(AccountRecord record)
{
try
{
_dataContext.AccountRecords.Remove(record);
_dataContext.SaveChanges();
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 IEnumerable<AccountRecord> GetAllAccounts()
{
return _dataContext.AccountRecords;
}
public AccountRecord GetAccount(int id)
{
var accountRec = _dataContext.AccountRecords.FirstOrDefault(a => a.Id == id);
return accountRec;
}
public AccountRecord GetAccountByDateBelKonto(DateTime _date, double _belopp, string _konto)
{
var accountRec = _dataContext.AccountRecords.FirstOrDefault(a => a.BetalDatum == _date && a.Belopp == _belopp && a.Konto == _konto);
return accountRec;
}
}
}

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<COMReference Include="Microsoft.Office.Interop.Excel">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>9</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>00020813-0000-0000-c000-000000000046</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Accounting.BLI\Accounting.BLI.csproj" />
<ProjectReference Include="..\Accounting.DAL\Accounting.DAL.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,61 @@
using Accounting.BLI;
using Microsoft.Extensions.Logging;
using Microsoft.Office.Interop.Excel;
using _Excel = Microsoft.Office.Interop.Excel;
namespace Accounting.BLR
{
public class Excellent : IDisposable, IExcellent
{
string path = string.Empty;
_Application excel = new _Excel.Application();
Workbook wb;
Worksheet ws;
private readonly ILogger<Excellent> _logger;
public int Columns { get; set; }
public int Rows { get; set; }
public string DataType { get; set; }
public Excellent(ILogger<Excellent> logger)
{
_logger = logger;
}
public void ExcellentStart(string path, int Sheet)
{
if (wb != null)
{
wb.Close();
}
this.path = path;
wb = excel.Workbooks.Open(path);
ws = wb.Worksheets[Sheet];
_Excel.Range usedRange = ws.UsedRange;
Columns = usedRange.Columns.Count;
Rows = usedRange.Rows.Count;
}
public string ReadCell(int i, int j)
{
i++;
j++;
if (ws.Cells[i, j].Value2 != null)
{
_Excel.Range cell = ws.Cells[i, j];
object value = cell.get_Value(Type.Missing);
if (value != null)
{
Type type = value.GetType();
DataType = type.Name;
//Console.WriteLine($"Cell ({i},{j}) contains value of type {DataType}");
}
return Convert.ToString(ws.Cells[i, j].Value2);
}
return string.Empty;
}
public void Dispose()
{
excel.Workbooks.Close();
}
}
}

169
Accounting.BLR/ReadingIn.cs Normal file
View File

@ -0,0 +1,169 @@
using Accounting.BLI;
using Accounting.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace Accounting.BLR
{
public class ReadingIn : IReadingIn, IDisposable
{
private readonly IConfiguration _configuration;
private readonly ILogger<ReadingIn> _logger;
private readonly IAccountRecords _accountRecords;
private readonly IExcellent _excellent;
public ReadingIn(
IConfiguration configuration,
ILogger<ReadingIn> logger,
IAccountRecords accountRecords,
IExcellent excellent)
{
_configuration = configuration;
_logger = logger;
_accountRecords = accountRecords;
_excellent = excellent;
}
public IEnumerable<AccountRecord> readXLS(string FilePath)
{
List<AccountRecord> records = new List<AccountRecord>();
AccountRecord? record = null;
FileInfo existingFile = new FileInfo(FilePath);
var fieldNr = 0;
_excellent.ExcellentStart(existingFile.FullName, 1); ;
//get the first worksheet in the workbook
int colCount = _excellent.Columns;
int rowCount = _excellent.Rows; //get row count
bool prt = false;
for (int row = 0; row <= rowCount; row++)
{
if (prt)
{
//Console.WriteLine();
record.Id = row;
_logger.LogInformation(record.ToString());
record.Id = 0;
records.Add(record);
}
prt = false;
for (int col = 0; col <= colCount; col++)
{
var x = _excellent.ReadCell(row, col);
if (_excellent.ReadCell(row, col) == null)
{
// Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null");
}
else
{
if (col == 0 && _excellent.DataType == "DateTime")
{
prt = true;
record = new AccountRecord();
fieldNr = 0;
}
else
{
if (col == 0)
{
col = colCount;
}
}
//Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
if (prt)
{
if (x == "") { }
else
{
fieldNr++;
// _logger.LogInformation($"{exObject.ReadCell(row, col).ToString().Trim()}/t");
switch (fieldNr)
{
case 1:
{
record.BetalDatum = DateTime.FromOADate(double.Parse(_excellent.ReadCell(row, col)));
break;
}
case 2:
{
record.Mottagare = _excellent.ReadCell(row, col).ToString().Trim();
break;
}
case 3:
{
record.Konto = _excellent.ReadCell(row, col).ToString().Trim();
if (record.Konto.ToLower().StartsWith("bg")
|| record.Konto.ToLower().StartsWith("pg")
|| record.Konto.ToLower().StartsWith("hs")) { }
else
{
prt = false;
col = colCount;
}
break;
}
case 4:
{
record.Belopp = double.Parse(_excellent.ReadCell(row, col).ToString().Trim());
break;
}
case 5:
{
record.Avisering = _excellent.ReadCell(row, col).ToString().Trim();
break;
}
}
}
}
}
}
}
return records;
}
public bool ReadAndSaveInvoices(string fullFileName)
{
var result = true;
var restab = readXLS(fullFileName);
if (restab != null)
{
//restab.ToList().ForEach(x =>
//{
// _logger.LogInformation(x.ToString());
//});
try
{
restab.ToList().ForEach(x =>
{
if (_accountRecords.GetAccountByDateBelKonto(x.BetalDatum, x.Belopp, x.Konto) == null)
_accountRecords.AddAccountRecord(x);
else { };
});
// restab.ToList().ForEach(x => { _accountRecordRepository.AddAccountRecord(x); });
}
catch (Exception ex)
{
_logger.LogError("MassUppdatering misslyckat: -->{iMessage}", ex.Message);
result = false;
}
}
return result;
}
public void Dispose()
{
_excellent.Dispose();
}
}
}