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,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Accounting.Models\Accounting.Models.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,16 @@
using Accounting.Models;
namespace Accounting.BLI
{
public interface IAccountRecords
{
bool AddAccountRecord(AccountRecord record);
AccountRecord SaveAcountRecord(AccountRecord record);
bool DeleteAccountRecord(AccountRecord record);
bool DeleteAllAccountRecords();
IEnumerable<AccountRecord> GetAllAccBetweenDates(DateTime dateFrom, DateTime dateTo);
IEnumerable<AccountRecord> GetAllAccounts();
AccountRecord GetAccount(int id);
AccountRecord GetAccountByDateBelKonto(DateTime _date, double _belopp, string _konto);
}
}

View File

@ -0,0 +1,13 @@
namespace Accounting.BLI
{
public interface IExcellent
{
int Columns { get; set; }
string DataType { get; set; }
int Rows { get; set; }
void Dispose();
void ExcellentStart(string path, int Sheet);
string ReadCell(int i, int j);
}
}

View File

@ -0,0 +1,11 @@
using Accounting.Models;
namespace Accounting.BLI
{
public interface IReadingIn
{
bool ReadAndSaveInvoices(string fullFileName);
IEnumerable<AccountRecord> readXLS(string FilePath);
void Dispose();
}
}

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();
}
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Accounting.Models\Accounting.Models.csproj" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,7 @@
namespace Accounting.Data;
using Accounting.Models;
using Microsoft.EntityFrameworkCore;
namespace Accounting.DAL;
public class DataContext : DbContext
{

View File

@ -1,6 +1,6 @@
// <auto-generated />
using System;
using Accounting.Data;
using Accounting.DAL;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;

View File

@ -1,6 +1,6 @@
// <auto-generated />
using System;
using Accounting.Data;
using Accounting.DAL;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;

View File

@ -1,4 +1,5 @@
namespace Accounting.Entities;
using Accounting.Models.Common;
namespace Accounting.Models;
public class AccountRecord : BaseEntity
{

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,8 @@
using System.ComponentModel.DataAnnotations;
namespace Accounting.Models.Common;
public abstract class BaseEntity
{
[Key]
public int Id { get; set; }
}

View File

@ -13,8 +13,15 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Accounting.BLI\Accounting.BLI.csproj" />
<ProjectReference Include="..\Accounting.DAL\Accounting.DAL.csproj" />
<ProjectReference Include="..\Accounting.Models\Accounting.Models.csproj" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
using Accounting.Data;
using Accounting.DAL;
using Accounting.Models;
using Newtonsoft.Json;

View File

@ -1,7 +0,0 @@
namespace Accounting.Entities.Common;
public abstract class BaseEntity
{
[Key]
public int Id { get; set; }
}

View File

@ -1,8 +1,6 @@
global using Accounting.Entities;
global using Accounting.Entities.Common;

global using Microsoft.AspNetCore.Mvc;
global using Microsoft.EntityFrameworkCore;
global using System.ComponentModel.DataAnnotations;
namespace Accounting;

View File

@ -1,4 +1,5 @@
using Accounting.Data;
using Accounting.DAL;
var builder = WebApplication.CreateBuilder(args);
@ -14,6 +15,9 @@ builder.Services.AddDbContext<DataContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
var app = builder.Build();
// Configure the HTTP request pipeline.

5
Accounting/libman.json Normal file
View File

@ -0,0 +1,5 @@
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": []
}

View File

@ -3,7 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounting", "Accounting\Accounting.csproj", "{EA69CE3F-020D-482B-82DB-3A5141E67774}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Accounting", "Accounting\Accounting.csproj", "{EA69CE3F-020D-482B-82DB-3A5141E67774}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounting.BLI", "Accounting.BLI\Accounting.BLI.csproj", "{3C7E3952-2AE1-422D-9325-7E85D8D35F57}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounting.DAL", "Accounting.DAL\Accounting.DAL.csproj", "{AF338983-D9FF-4DC3-B418-F14E83036DEC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounting.Models", "Accounting.Models\Accounting.Models.csproj", "{42192805-C79D-447E-AE45-866FC4CF9DC5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounting.BLR", "Accounting.BLR\Accounting.BLR.csproj", "{F9F760C1-CE79-4B7F-A3D6-29EBA7D1D718}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +23,22 @@ Global
{EA69CE3F-020D-482B-82DB-3A5141E67774}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA69CE3F-020D-482B-82DB-3A5141E67774}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA69CE3F-020D-482B-82DB-3A5141E67774}.Release|Any CPU.Build.0 = Release|Any CPU
{3C7E3952-2AE1-422D-9325-7E85D8D35F57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C7E3952-2AE1-422D-9325-7E85D8D35F57}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C7E3952-2AE1-422D-9325-7E85D8D35F57}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C7E3952-2AE1-422D-9325-7E85D8D35F57}.Release|Any CPU.Build.0 = Release|Any CPU
{AF338983-D9FF-4DC3-B418-F14E83036DEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF338983-D9FF-4DC3-B418-F14E83036DEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF338983-D9FF-4DC3-B418-F14E83036DEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF338983-D9FF-4DC3-B418-F14E83036DEC}.Release|Any CPU.Build.0 = Release|Any CPU
{42192805-C79D-447E-AE45-866FC4CF9DC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{42192805-C79D-447E-AE45-866FC4CF9DC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42192805-C79D-447E-AE45-866FC4CF9DC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42192805-C79D-447E-AE45-866FC4CF9DC5}.Release|Any CPU.Build.0 = Release|Any CPU
{F9F760C1-CE79-4B7F-A3D6-29EBA7D1D718}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9F760C1-CE79-4B7F-A3D6-29EBA7D1D718}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9F760C1-CE79-4B7F-A3D6-29EBA7D1D718}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9F760C1-CE79-4B7F-A3D6-29EBA7D1D718}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE