From 3b7ca0973e6eb2e30e3a9dc8bee34bbb86c73432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 23 May 2024 16:29:35 +0200 Subject: [PATCH] Adapted the repositories to the controllers --- Accounting.BLI/IAccountRecords.cs | 13 +-- Accounting.BLR/AccountRecords.cs | 74 +++++++++++---- Accounting.BLR/Accounting.BLR.csproj | 4 + Accounting.BLR/ReadingIn.cs | 4 +- Accounting/Accounting.csproj | 1 + .../Controllers/AccountingController.cs | 92 ++++++------------- Accounting/Program.cs | 6 +- 7 files changed, 101 insertions(+), 93 deletions(-) diff --git a/Accounting.BLI/IAccountRecords.cs b/Accounting.BLI/IAccountRecords.cs index fcf9096..7f4418b 100644 --- a/Accounting.BLI/IAccountRecords.cs +++ b/Accounting.BLI/IAccountRecords.cs @@ -4,13 +4,14 @@ namespace Accounting.BLI { public interface IAccountRecords { - bool AddAccountRecord(AccountRecord record); - AccountRecord SaveAcountRecord(AccountRecord record); - bool DeleteAccountRecord(AccountRecord record); + Task AddAccountRecordAsync(AccountRecord record); + Task SaveAcountRecordAsync(AccountRecord record); + Task DeleteAccountRecordAsync(AccountRecord record); bool DeleteAllAccountRecords(); IEnumerable GetAllAccBetweenDates(DateTime dateFrom, DateTime dateTo); - IEnumerable GetAllAccounts(); - AccountRecord GetAccount(int id); - AccountRecord GetAccountByDateBelKonto(DateTime _date, double _belopp, string _konto); + Task> GetAllAccountsAsync(); + Task GetAccountAsync(int id); + Task GetAccountByDateBelKontoAsync(DateTime _date, double _belopp, string _konto); + Task> AddAccRecordsFromJsonAsync(string jsonFile); } } diff --git a/Accounting.BLR/AccountRecords.cs b/Accounting.BLR/AccountRecords.cs index fa5d5b1..3de1bdc 100644 --- a/Accounting.BLR/AccountRecords.cs +++ b/Accounting.BLR/AccountRecords.cs @@ -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 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 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 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 GetAllAccounts() + public async Task> GetAllAccountsAsync() { - return _dataContext.AccountRecords; + return await _dataContext.AccountRecords.ToListAsync(); } - public AccountRecord GetAccount(int id) + public async Task 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 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> AddAccRecordsFromJsonAsync(string jsonFile) + { + var jsonText = await System.IO.File.ReadAllTextAsync(jsonFile); + + var accRecords = JsonConvert.DeserializeObject>(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(); + } } } diff --git a/Accounting.BLR/Accounting.BLR.csproj b/Accounting.BLR/Accounting.BLR.csproj index 41c9ddd..2f68716 100644 --- a/Accounting.BLR/Accounting.BLR.csproj +++ b/Accounting.BLR/Accounting.BLR.csproj @@ -18,6 +18,10 @@ + + + + diff --git a/Accounting.BLR/ReadingIn.cs b/Accounting.BLR/ReadingIn.cs index c6d2b13..4ee7214 100644 --- a/Accounting.BLR/ReadingIn.cs +++ b/Accounting.BLR/ReadingIn.cs @@ -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 { }; }); diff --git a/Accounting/Accounting.csproj b/Accounting/Accounting.csproj index e7c0a4f..314c5cf 100644 --- a/Accounting/Accounting.csproj +++ b/Accounting/Accounting.csproj @@ -20,6 +20,7 @@ + diff --git a/Accounting/Controllers/AccountingController.cs b/Accounting/Controllers/AccountingController.cs index 390394c..87f304f 100644 --- a/Accounting/Controllers/AccountingController.cs +++ b/Accounting/Controllers/AccountingController.cs @@ -1,6 +1,5 @@ -using Accounting.DAL; +using Accounting.BLI; using Accounting.Models; -using Newtonsoft.Json; namespace Accounting.Controllers; @@ -9,40 +8,27 @@ namespace Accounting.Controllers; [ApiController] public class AccountingController : ControllerBase { - private readonly DataContext _context; + private readonly IAccountRecords _accountRecords; - public AccountingController(DataContext dataContext) + public AccountingController(IAccountRecords accountRecords) { - _context = dataContext; + _accountRecords = accountRecords; } [HttpGet] public async Task>> GetAllAccRecords() { - var accRecords = await _context.AccountRecords.ToListAsync(); + var accRecords = await _accountRecords.GetAllAccountsAsync(); return Ok(accRecords); - - - //var accRecords = new List{ - // new AccountRecord { - // Id = 1, - // Avisering = "Efaktura", - // Belopp = 250.50D, - // BetalDatum = DateTime.Parse("2024-05-30"), - // Konto = "1234-5678", - // Mottagare = "Tele2", - // Stored = DateTime.Now - // } - //}; } [HttpGet("{id}")] public async Task> GetAccountRecord(int id) { - var accRecord = await _context.AccountRecords.FindAsync(id); + var accRecord = await _accountRecords.GetAccountAsync(id); if (accRecord is null) return NotFound("AccountRecord not found"); @@ -50,45 +36,23 @@ public class AccountingController : ControllerBase } [HttpPost("Addrecord")] - public async Task>> AddAccRecord(AccountRecord accRecord) + public async Task AddAccRecord(AccountRecord accRecord) { - _context.AccountRecords.Add(accRecord); - await _context.SaveChangesAsync(); - - return Ok(await _context.AccountRecords.ToListAsync()); + var answ = await _accountRecords.AddAccountRecordAsync(accRecord); + return answ; } [HttpPost("AddAccRecordsFromJson/{jsonFile}")] public async Task>> AddAccRecordsFromJson(string jsonFile) { - var jsonText = await System.IO.File.ReadAllTextAsync(jsonFile); - var accRecords = JsonConvert.DeserializeObject>(jsonText); + var accRecords = await _accountRecords.AddAccRecordsFromJsonAsync(jsonFile); if (accRecords == null) - return BadRequest($"File not found : {jsonText}"); - - - using (var transaction = _context.Database.BeginTransaction()) - { - try - { - await _context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.AccountRecords ON"); - await _context.AccountRecords.AddRangeAsync(accRecords); - await _context.SaveChangesAsync(); - - await _context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.AccountRecords OFF"); - transaction.Commit(); - } - catch (Exception ex) - { - transaction.Rollback(); - return BadRequest($"Error in call {ex.Message}"); - } - - } - return Ok(await _context.AccountRecords.ToListAsync()); + return BadRequest($"Repository-Error see logfile!"); + else + return Ok(accRecords); } @@ -96,32 +60,28 @@ public class AccountingController : ControllerBase [HttpPut] public async Task>> UpdateAccRecord(AccountRecord updatedAccRecord) { - var accRecord = await _context.AccountRecords.FindAsync(updatedAccRecord.Id); + var accRecord = await _accountRecords.SaveAcountRecordAsync(updatedAccRecord); if (accRecord is null) return NotFound("Record not found"); - accRecord.Stored = updatedAccRecord.Stored; - accRecord.BetalDatum = updatedAccRecord.BetalDatum; - accRecord.Avisering = updatedAccRecord.Avisering; - accRecord.Mottagare = updatedAccRecord.Mottagare; - accRecord.Belopp = updatedAccRecord.Belopp; - accRecord.Konto = updatedAccRecord.Konto; - - await _context.SaveChangesAsync(); - - return Ok(await _context.AccountRecords.ToListAsync()); + return Ok(accRecord); } [HttpDelete] public async Task>> DeleteAccRecord(int id) { - var AccRecord = await _context.AccountRecords.FindAsync(id); - if (AccRecord is null) - return NotFound("AccRecord not found"); + var accRecord = await _accountRecords.GetAccountAsync(id); + if (accRecord is null) + return NotFound("AccountRecord not found"); - _context.AccountRecords.Remove(AccRecord); - await _context.SaveChangesAsync(); + var result = await _accountRecords.DeleteAccountRecordAsync(accRecord); + if (result == false) + return NotFound("Remove unsuccessful"); - return Ok(await _context.AccountRecords.ToListAsync()); + var records = await _accountRecords.GetAllAccountsAsync(); + if (records.ToList().Count < 1) + return BadRequest("Nothing left"); + + return Ok(records.ToList()); } } diff --git a/Accounting/Program.cs b/Accounting/Program.cs index 769d7b1..75028e3 100644 --- a/Accounting/Program.cs +++ b/Accounting/Program.cs @@ -1,4 +1,6 @@ +using Accounting.BLI; +using Accounting.BLR; using Accounting.DAL; var builder = WebApplication.CreateBuilder(args); @@ -15,7 +17,9 @@ builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); }); - +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); var app = builder.Build();