Доделал часть сториджей

This commit is contained in:
Artyom_Yashin 2024-04-27 17:09:40 +04:00
parent 3f00c6f2f3
commit 3abfaa4880
6 changed files with 191 additions and 12 deletions

View File

@ -9,7 +9,8 @@ namespace BankContracts.SearchModels
public class OperationSearchModel
{
public int? Id { get; set; }
public DateTime? OperationTime { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public int? SenderCardId { get; set; }
public int? RecipientCardId { get; set; }
}

View File

@ -17,12 +17,15 @@ namespace BankDatabaseImplement.Implements
public List<CardViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Cards.Include(x => x.Client).Select(x => x.GetViewModel).ToList();
return context.Cards.Include(x => x.Client).Include(x => x.Account).Include(x => x.CardRequests)
.Include(x => x.SenderOperations).Include(x => x.RecipientOperations)
.Select(x => x.GetViewModel).ToList();
}
public List<CardViewModel> GetFilteredList(CardSearchModel model)
{
using var context = new BankDatabase();
return context.Cards.Include(x => x.Client)
return context.Cards.Include(x => x.Client).Include(x => x.Account).Include(x => x.CardRequests)
.Include(x => x.SenderOperations).Include(x => x.RecipientOperations)
.Where(x => (
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.ClientId.HasValue || x.ClientId == model.ClientId) &&
@ -33,9 +36,9 @@ namespace BankDatabaseImplement.Implements
}
public CardViewModel? GetElement(CardSearchModel model)
{
if (!model.Id.HasValue) return null;
using var context = new BankDatabase();
return context.Cards.Include(x => x.Client).FirstOrDefault(
return context.Cards.Include(x => x.Client).Include(x => x.Account).Include(x => x.CardRequests)
.Include(x => x.SenderOperations).Include(x => x.RecipientOperations).FirstOrDefault(
x => (model.Number != null && x.Number == model.Number)
)?.GetViewModel;
}
@ -54,7 +57,8 @@ namespace BankDatabaseImplement.Implements
public CardViewModel? Update(CardBindingModel model)
{
using var context = new BankDatabase();
var card = context.Cards.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id);
var card = context.Cards.Include(x => x.Client).Include(x => x.Account).Include(x => x.CardRequests)
.Include(x => x.SenderOperations).Include(x => x.RecipientOperations).FirstOrDefault(x => x.Id == model.Id);
if (card == null)
{
return null;
@ -66,7 +70,8 @@ namespace BankDatabaseImplement.Implements
public CardViewModel? Delete(CardBindingModel model)
{
using var context = new BankDatabase();
var card = context.Cards.Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id);
var card = context.Cards.Include(x => x.Client).Include(x => x.Account).Include(x => x.CardRequests)
.Include(x => x.SenderOperations).Include(x => x.RecipientOperations).FirstOrDefault(rec => rec.Id == model.Id);
if (card != null)
{
context.Cards.Remove(card);

View File

@ -3,6 +3,7 @@ using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -16,7 +17,7 @@ namespace BankDatabaseImplement.Implements
public List<ClientViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
return context.Clients.Include(x => x.Cards).Select(x => x.GetViewModel).ToList();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
@ -25,7 +26,7 @@ namespace BankDatabaseImplement.Implements
return new();
}
using var context = new BankDatabase();
return context.Clients
return context.Clients.Include(x => x.Cards)
.Where(x => string.IsNullOrEmpty(model.Fio) || x.Fio.Contains(model.Fio) &&
(string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) &&
(string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password)))
@ -38,7 +39,7 @@ namespace BankDatabaseImplement.Implements
return null;
}
using var context = new BankDatabase();
return context.Clients
return context.Clients.Include(x => x.Cards)
.FirstOrDefault(x => (string.IsNullOrEmpty(model.Fio) || x.Fio == model.Fio) &&
(!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) &&
(string.IsNullOrEmpty(model.Password) || x.Password == model.Password))
@ -71,7 +72,7 @@ namespace BankDatabaseImplement.Implements
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new BankDatabase();
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
var element = context.Clients.Include(x => x.Cards).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clients.Remove(element);

View File

@ -0,0 +1,78 @@
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
public class OperationStorage : IOperationStorage
{
public List<OperationViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Operations.Include(x => x.SenderCard).Include(x => x.RecipientCard)
.Select(x => x.GetViewModel).ToList();
}
public List<OperationViewModel> GetFilteredList(OperationSearchModel model)
{
using var context = new BankDatabase();
return context.Operations.Include(x => x.SenderCard).Include(x => x.RecipientCard)
.Where(x => (
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.DateFrom.HasValue || x.OperationTime >= model.DateFrom ) &&
(!model.DateTo.HasValue || x.OperationTime <= model.DateTo)
)).Select(x => x.GetViewModel).ToList();
}
public OperationViewModel? GetElement(OperationSearchModel model)
{
if (!model.Id.HasValue) return null;
using var context = new BankDatabase();
return context.Operations.Include(x => x.SenderCard).Include(x => x.RecipientCard)
.FirstOrDefault(x => ((model.Id.HasValue && x.Id == model.Id)
))?.GetViewModel;
}
public OperationViewModel? Insert(OperationBindingModel model)
{
var newOperation = Operation.Create(model);
if (newOperation == null)
{
return null;
}
using var context = new BankDatabase();
context.Operations.Add(newOperation);
context.SaveChanges();
return newOperation.GetViewModel;
}
public OperationViewModel? Update(OperationBindingModel model)
{
using var context = new BankDatabase();
var operation = context.Operations.Include(x => x.SenderCard).Include(x => x.RecipientCard).FirstOrDefault(x => x.Id == model.Id);
if (operation == null)
{
return null;
}
operation.Update(model);
context.SaveChanges();
return operation.GetViewModel;
}
public OperationViewModel? Delete(OperationBindingModel model)
{
using var context = new BankDatabase();
var operation = context.Operations.Include(x => x.SenderCard).Include(x => x.RecipientCard).FirstOrDefault(rec => rec.Id == model.Id);
if (operation != null)
{
context.Operations.Remove(operation);
context.SaveChanges();
return operation.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,93 @@
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
public class RequestStorage : IRequestStorage
{
public List<RequestViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Requests
.Include(x => x.Cards)
.ThenInclude(x => x.Card)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<RequestViewModel> GetFilteredList(RequestSearchModel model)
{
using var context = new BankDatabase();
return context.Requests.Include(x => x.Cards).ThenInclude(x => x.Card)
.Where(x => (model.Id.HasValue && x.Id == model.Id)).ToList()
.Select(x => x.GetViewModel).ToList();
}
public RequestViewModel? GetElement(RequestSearchModel model)
{
if (!model.Id.HasValue) return null;
using var context = new BankDatabase();
return context.Requests.Include(x => x.Cards).ThenInclude(x => x.Card)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public RequestViewModel? Insert(RequestBindingModel model)
{
using var context = new BankDatabase();
var newRequest = Request.Create(context, model);
if (newRequest == null)
{
return null;
}
context.Requests.Add(newRequest);
context.SaveChanges();
return newRequest.GetViewModel;
}
public RequestViewModel? Update(RequestBindingModel model)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Request = context.Requests.FirstOrDefault(rec =>
rec.Id == model.Id);
if (Request == null)
{
return null;
}
Request.Update(model);
context.SaveChanges();
Request.UpdateCards(context, model);
transaction.Commit();
return Request.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public RequestViewModel? Delete(RequestBindingModel model)
{
using var context = new BankDatabase();
var element = context.Requests
.Include(x => x.Cards)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Requests.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -1,4 +1,5 @@
using BankContracts.BindingModels;
using BankContracts.ViewModels;
using BankDataModels.Models;
using System;
using System.Collections.Generic;
@ -57,7 +58,7 @@ namespace BankDatabaseImplement.Models
Status = model.Status;
}
public RequestBindingModel GetViewModel => new()
public RequestViewModel GetViewModel => new()
{
Id = Id,
Sum = Sum,