Införande av repository method
This commit is contained in:
139
Accounting.BLR/AccountRecords.cs
Normal file
139
Accounting.BLR/AccountRecords.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user