From 3f00c6f2f382458c0b2312d6c288b1be39731691 Mon Sep 17 00:00:00 2001 From: Artyom_Yashin Date: Sat, 27 Apr 2024 15:58:47 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8=20=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0,=20=D0=BD=D0=B0?= =?UTF-8?q?=D1=87=D0=B0=D0=BB=20=D1=81=D1=82=D0=BE=D1=80=D0=B5=D0=B4=D0=B6?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BankDatabaseImplement.csproj | 4 - .../Implements/CardStorage.cs | 79 +++++++++++++++++ .../Implements/ClientStorage.cs | 84 +++++++++++++++++++ Bank/BankDatabaseImplement/Models/Card.cs | 5 ++ .../BankDatabaseImplement/Models/Operation.cs | 5 +- 5 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 Bank/BankDatabaseImplement/Implements/CardStorage.cs create mode 100644 Bank/BankDatabaseImplement/Implements/ClientStorage.cs diff --git a/Bank/BankDatabaseImplement/BankDatabaseImplement.csproj b/Bank/BankDatabaseImplement/BankDatabaseImplement.csproj index ca2d4cb..d50f256 100644 --- a/Bank/BankDatabaseImplement/BankDatabaseImplement.csproj +++ b/Bank/BankDatabaseImplement/BankDatabaseImplement.csproj @@ -19,8 +19,4 @@ - - - - diff --git a/Bank/BankDatabaseImplement/Implements/CardStorage.cs b/Bank/BankDatabaseImplement/Implements/CardStorage.cs new file mode 100644 index 0000000..dfe977d --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/CardStorage.cs @@ -0,0 +1,79 @@ +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 CardStorage : ICardStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + return context.Cards.Include(x => x.Client).Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(CardSearchModel model) + { + using var context = new BankDatabase(); + return context.Cards.Include(x => x.Client) + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) && + (!model.ClientId.HasValue || x.ClientId == model.ClientId) && + (model.Number == null || x.Number == model.Number) + )) + .Select(x => x.GetViewModel) + .ToList(); + } + public CardViewModel? GetElement(CardSearchModel model) + { + if (!model.Id.HasValue) return null; + using var context = new BankDatabase(); + return context.Cards.Include(x => x.Client).FirstOrDefault( + x => (model.Number != null && x.Number == model.Number) + )?.GetViewModel; + } + public CardViewModel? Insert(CardBindingModel model) + { + var newCard = Card.Create(model); + if (newCard == null) + { + return null; + } + using var context = new BankDatabase(); + context.Cards.Add(newCard); + context.SaveChanges(); + return newCard.GetViewModel; + } + public CardViewModel? Update(CardBindingModel model) + { + using var context = new BankDatabase(); + var card = context.Cards.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id); + if (card == null) + { + return null; + } + card.Update(model); + context.SaveChanges(); + return card.GetViewModel; + } + public CardViewModel? Delete(CardBindingModel model) + { + using var context = new BankDatabase(); + var card = context.Cards.Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id); + if (card != null) + { + context.Cards.Remove(card); + context.SaveChanges(); + return card.GetViewModel; + } + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/ClientStorage.cs b/Bank/BankDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..d476224 --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,84 @@ +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + return context.Clients.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Fio) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)) + { + return new(); + } + using var context = new BankDatabase(); + return context.Clients + .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))) + .Select(x => x.GetViewModel).ToList(); + } + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Fio) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new BankDatabase(); + return context.Clients + .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)) + ?.GetViewModel; + } + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new BankDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new BankDatabase(); + var client = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + return client.GetViewModel; + } + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Models/Card.cs b/Bank/BankDatabaseImplement/Models/Card.cs index 05bba95..e9f2c32 100644 --- a/Bank/BankDatabaseImplement/Models/Card.cs +++ b/Bank/BankDatabaseImplement/Models/Card.cs @@ -13,6 +13,7 @@ namespace BankDatabaseImplement.Models { public class Card : ICardModel { + //убрать интерфейс public int Id { get; set; } [Required] public string Number { get; set; } = string.Empty; @@ -26,8 +27,10 @@ namespace BankDatabaseImplement.Models public DateOnly ExpirationDate { get; set; } [Required] public int ClientId { get; set; } + public virtual Client? Client { get; private set; } [Required] public int? AccountId { get; set; } = null; + public virtual IAccountModel? Account { get; private set; } [ForeignKey("CardId")] public virtual List CardRequests { get; set; } = new(); [ForeignKey("SenderCardId")] @@ -73,6 +76,8 @@ namespace BankDatabaseImplement.Models ExpirationDate = ExpirationDate, ClientId = ClientId, AccountId = AccountId, + ClientName = Client?.Fio ?? string.Empty, + AccountNumber = Account?.Number ?? string.Empty, }; } } diff --git a/Bank/BankDatabaseImplement/Models/Operation.cs b/Bank/BankDatabaseImplement/Models/Operation.cs index 3e5e2d4..4bd68e9 100644 --- a/Bank/BankDatabaseImplement/Models/Operation.cs +++ b/Bank/BankDatabaseImplement/Models/Operation.cs @@ -18,10 +18,11 @@ namespace BankDatabaseImplement.Models public int Sum { get; set; } [Required] public DateTime OperationTime { get; set; } - [Required] public int? SenderCardId { get; set; } = null; + public virtual Card? SenderCard { get; private set; } [Required] public int RecipientCardId { get; set; } + public virtual Card? RecipientCard { get; private set; } public static Operation? Create(OperationBindingModel model) { @@ -52,6 +53,8 @@ namespace BankDatabaseImplement.Models OperationTime = OperationTime, SenderCardId = SenderCardId, RecipientCardId = RecipientCardId, + SenderCardNumber = SenderCard?.Number ?? string.Empty, + RecipientCardNumber = RecipientCard?.Number ?? string.Empty, }; } } From 3abfaa4880499b4085d4db977c8e9dbba6ed79c0 Mon Sep 17 00:00:00 2001 From: Artyom_Yashin Date: Sat, 27 Apr 2024 17:09:40 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB?= =?UTF-8?q?=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D1=81=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=B4=D0=B6=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/OperationSearchModel.cs | 3 +- .../Implements/CardStorage.cs | 17 ++-- .../Implements/ClientStorage.cs | 9 +- .../Implements/OperationStorage.cs | 78 ++++++++++++++++ .../Implements/RequestStorage.cs | 93 +++++++++++++++++++ Bank/BankDatabaseImplement/Models/Request.cs | 3 +- 6 files changed, 191 insertions(+), 12 deletions(-) create mode 100644 Bank/BankDatabaseImplement/Implements/OperationStorage.cs create mode 100644 Bank/BankDatabaseImplement/Implements/RequestStorage.cs diff --git a/Bank/BankContracts/SearchModels/OperationSearchModel.cs b/Bank/BankContracts/SearchModels/OperationSearchModel.cs index 2ba7ad9..8aecfa4 100644 --- a/Bank/BankContracts/SearchModels/OperationSearchModel.cs +++ b/Bank/BankContracts/SearchModels/OperationSearchModel.cs @@ -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; } } diff --git a/Bank/BankDatabaseImplement/Implements/CardStorage.cs b/Bank/BankDatabaseImplement/Implements/CardStorage.cs index dfe977d..c537e21 100644 --- a/Bank/BankDatabaseImplement/Implements/CardStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/CardStorage.cs @@ -17,12 +17,15 @@ namespace BankDatabaseImplement.Implements public List 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 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); diff --git a/Bank/BankDatabaseImplement/Implements/ClientStorage.cs b/Bank/BankDatabaseImplement/Implements/ClientStorage.cs index d476224..b38a22b 100644 --- a/Bank/BankDatabaseImplement/Implements/ClientStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/ClientStorage.cs @@ -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 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 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); diff --git a/Bank/BankDatabaseImplement/Implements/OperationStorage.cs b/Bank/BankDatabaseImplement/Implements/OperationStorage.cs new file mode 100644 index 0000000..9273b47 --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/OperationStorage.cs @@ -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 GetFullList() + { + using var context = new BankDatabase(); + return context.Operations.Include(x => x.SenderCard).Include(x => x.RecipientCard) + .Select(x => x.GetViewModel).ToList(); + } + public List 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; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/RequestStorage.cs b/Bank/BankDatabaseImplement/Implements/RequestStorage.cs new file mode 100644 index 0000000..331211a --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/RequestStorage.cs @@ -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 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 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; + } + } +} diff --git a/Bank/BankDatabaseImplement/Models/Request.cs b/Bank/BankDatabaseImplement/Models/Request.cs index 1064682..fadc8e9 100644 --- a/Bank/BankDatabaseImplement/Models/Request.cs +++ b/Bank/BankDatabaseImplement/Models/Request.cs @@ -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,