Spara ny /ändrad införd

This commit is contained in:
2023-09-15 17:59:58 +02:00
parent 0c0533f9d5
commit ef14ce2aaa
4 changed files with 70 additions and 0 deletions

View File

@ -5,6 +5,7 @@ namespace WinFormDiApp.BLI
public interface IAccountRecordRepository public interface IAccountRecordRepository
{ {
bool AddAccountRecord(AccountRecord record); bool AddAccountRecord(AccountRecord record);
AccountRecord SaveAcountRecord(AccountRecord record);
bool DeleteAccountRecord(AccountRecord record); bool DeleteAccountRecord(AccountRecord record);
IEnumerable<AccountRecord> GetAllAccBetweenDates(DateTime dateFrom, DateTime dateTo); IEnumerable<AccountRecord> GetAllAccBetweenDates(DateTime dateFrom, DateTime dateTo);
IEnumerable<AccountRecord> GetAllAccounts(); IEnumerable<AccountRecord> GetAllAccounts();

View File

@ -35,6 +35,43 @@ public class AccountRecordRepository : IAccountRecordRepository
} }
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) public bool DeleteAccountRecord(AccountRecord record)
{ {
try try

View File

@ -199,6 +199,7 @@
btnSave.TabIndex = 19; btnSave.TabIndex = 19;
btnSave.Text = "Spara"; btnSave.Text = "Spara";
btnSave.UseVisualStyleBackColor = true; btnSave.UseVisualStyleBackColor = true;
btnSave.Click += btnSave_Click;
// //
// btnClose // btnClose
// //

View File

@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using WinFormDiApp.BL.Helpers; using WinFormDiApp.BL.Helpers;
using WinFormDiApp.BL.Models;
using WinFormDiApp.BLI; using WinFormDiApp.BLI;
namespace WinFormDiApp namespace WinFormDiApp
@ -65,5 +66,35 @@ namespace WinFormDiApp
} }
} }
} }
private void btnSave_Click(object sender, EventArgs e)
{
AccountRecord saveRecord = new AccountRecord();
if(rbEdit.Checked)
{
saveRecord.Id = Convert.ToInt32( txtId.Text);
saveRecord.Stored = DateTime.Parse(txtSaved.Text);
}
saveRecord.Konto = txtAccount.Text;
saveRecord.Belopp = Convert.ToDouble(txtAmount.Text);
saveRecord.Avisering = txtPayInfo.Text;
saveRecord.Mottagare = txtReceiver.Text;
saveRecord.BetalDatum =dtpPayDate.Value;
;
_recordRepository.SaveAcountRecord(saveRecord);
if (saveRecord != null)
{
rbEdit.Checked = true;
txtId.Text = saveRecord.Id.ToString();
txtAccount.Text = saveRecord.Konto.ToString();
txtAmount.Text = saveRecord.Belopp.ToString();
txtPayInfo.Text = saveRecord.Avisering.ToString();
txtReceiver.Text = saveRecord.Mottagare.ToString();
dtpPayDate.Value = saveRecord.BetalDatum;
txtSaved.Text = saveRecord.Stored.ToLongDateString();
}
}
} }
} }