Практически сделанные Storage у кассира.

This commit is contained in:
Programmist73 2023-04-01 22:54:15 +04:00
parent 732478fe84
commit 2e30751157
5 changed files with 326 additions and 6 deletions

View File

@ -2,6 +2,8 @@
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -14,30 +16,103 @@ namespace BankYouBankruptDatabaseImplement.Implements
{
public List<AccountViewModel> GetFullList()
{
throw new NotImplementedException();
using var context = new BankYouBancruptDatabase();
return context.Accounts
.Select(x => x.GetViewModel)
.ToList();
}
public List<AccountViewModel> GetFilteredList(AccountSearchModel model)
{
throw new NotImplementedException();
if (string.IsNullOrEmpty(model.AccountNumber))
{
return new();
}
using var context = new BankYouBancruptDatabase();
return context.Accounts
.Where(x => x.AccountNumber.Contains(model.AccountNumber))
.Select(x => x.GetViewModel)
.ToList();
}
public AccountViewModel? GetElement(AccountSearchModel model)
{
throw new NotImplementedException();
if (string.IsNullOrEmpty(model.AccountNumber) && !model.Id.HasValue)
{
return null;
}
using var context = new BankYouBancruptDatabase();
return context.Accounts
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.AccountNumber) && x.AccountNumber == model.AccountNumber) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public AccountViewModel? Insert(AccountBindingModel model)
{
throw new NotImplementedException();
using var context = new BankYouBancruptDatabase();
var newAccount = Account.Create(context, model);
if (newAccount == null)
{
return null;
}
context.Accounts.Add(newAccount);
context.SaveChanges();
return newAccount.GetViewModel;
}
public AccountViewModel? Update(AccountBindingModel model)
{
throw new NotImplementedException();
using var context = new BankYouBancruptDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var account = context.Accounts.FirstOrDefault(rec => rec.Id == model.Id);
if (account == null)
{
return null;
}
account.Update(model);
context.SaveChanges();
transaction.Commit();
return account.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public AccountViewModel? Delete(AccountBindingModel model)
{
throw new NotImplementedException();
using var context = new BankYouBancruptDatabase();
var element = context.Accounts
.Include(x => x.AccountNumber)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Accounts.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankYouBankruptDatabaseImplement.Implements
{
internal class CashWithdrawalStorage
{
}
}

View File

@ -0,0 +1,105 @@
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankYouBankruptDatabaseImplement.Implements
{
public class CashierStorage : ICashierStorage
{
public List<CashierViewModel> GetFullList()
{
using var context = new BankYouBancruptDatabase();
return context.Cashiers
.Select(x => x.GetViewModel)
.ToList();
}
public List<CashierViewModel> GetFilteredList(CashierSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new BankYouBancruptDatabase();
return context.Cashiers
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
public CashierViewModel? GetElement(CashierSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new BankYouBancruptDatabase();
return context.Cashiers
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public CashierViewModel? Insert(CashierBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var newCashier = Cashier.Create(context, model);
if (newCashier == null)
{
return null;
}
context.Cashiers.Add(newCashier);
context.SaveChanges();
return newCashier.GetViewModel;
}
public CashierViewModel? Update(CashierBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var cashier = context.Cashiers.FirstOrDefault(x => x.Id == model.Id);
if (cashier == null)
{
return null;
}
cashier.Update(model);
context.SaveChanges();
return cashier.GetViewModel;
}
public CashierViewModel? Delete(CashierBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var element = context.Cashiers.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cashiers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,122 @@
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankYouBankruptDatabaseImplement.Implements
{
public class MoneyTransferStorage : IMoneyTransferStorage
{
public List<MoneyTransferViewModel> GetFullList()
{
using var context = new BankYouBancruptDatabase();
return context.MoneyTransfers
.Select(x => x.GetViewModel)
.ToList();
}
public List<MoneyTransferViewModel> GetFilteredList(MoneyTransferSearchModel model)
{
if (model.AccountSenderId < 0)
{
return new();
}
using var context = new BankYouBancruptDatabase();
return context.MoneyTransfers
.Where(x => x.AccountSenderId == model.AccountSenderId)
.Select(x => x.GetViewModel)
.ToList();
}
public MoneyTransferViewModel? GetElement(MoneyTransferSearchModel model)
{
if (model.AccountSenderId < 0 && !model.Id.HasValue)
{
return null;
}
using var context = new BankYouBancruptDatabase();
return context.MoneyTransfers
.FirstOrDefault(x => (!(model.AccountSenderId < 0) && x.AccountSenderId == model.AccountSenderId
&& x.AccountPayeeId == model.AccountPayeeId && x.DateOperation == x.DateOperation
&& x.Sum == model.Sum) || (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public MoneyTransferViewModel? Insert(MoneyTransferBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var newMoneyTransfer = MoneyTransfer.Create(context, model);
if (newMoneyTransfer == null)
{
return null;
}
context.MoneyTransfers.Add(newMoneyTransfer);
context.SaveChanges();
return newMoneyTransfer.GetViewModel;
}
public MoneyTransferViewModel? Update(MoneyTransferBindingModel model)
{
using var context = new BankYouBancruptDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var moneyTransfer = context.MoneyTransfers.FirstOrDefault(rec => rec.Id == model.Id);
if (moneyTransfer == null)
{
return null;
}
moneyTransfer.Update(model);
context.SaveChanges();
transaction.Commit();
return moneyTransfer.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public MoneyTransferViewModel? Delete(MoneyTransferBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var element = context.MoneyTransfers
.Include(x => x.AccountPayeeId)
.Include(x => x.AccountSenderId)
.Include(x => x.Sum)
.Include(x => x.DateOperation)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.MoneyTransfers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -39,6 +39,12 @@ namespace BankYouBankruptDatabaseImplement.Models
};
}
public void Update(MoneyTransferBindingModel model)
{
Id = model.Id;
DateOperation = model.DateOperation;
}
public MoneyTransferViewModel GetViewModel => new()
{
Id = Id,