From 7c431ab7232a899a3e88401e4e753d062cd99ea2 Mon Sep 17 00:00:00 2001 From: Artyom_Yashin Date: Sat, 27 Apr 2024 15:07:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D1=87?= =?UTF-8?q?=D0=B0=D1=81=D1=82=D1=8C=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B5?= =?UTF-8?q?=D0=B9=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/RequestBindingModel.cs | 1 + .../ViewModels/RequestViewModel.cs | 2 + Bank/BankDataModels/Models/IRequestModel.cs | 4 +- Bank/BankDatabaseImplement/BankDatabase.cs | 9 +- .../Models/CardRequest.cs | 8 ++ .../BankDatabaseImplement/Models/Operation.cs | 3 +- Bank/BankDatabaseImplement/Models/Request.cs | 90 +++++++++++++++++++ 7 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 Bank/BankDatabaseImplement/Models/Request.cs diff --git a/Bank/BankContracts/BindingModels/RequestBindingModel.cs b/Bank/BankContracts/BindingModels/RequestBindingModel.cs index c923f68..654fba9 100644 --- a/Bank/BankContracts/BindingModels/RequestBindingModel.cs +++ b/Bank/BankContracts/BindingModels/RequestBindingModel.cs @@ -13,5 +13,6 @@ namespace BankContracts.BindingModels public int Sum { get; set; } public DateTime RequestTime { get; set; } public RequestStatus Status { get; set; } + public Dictionary CardRequests { get; set; } = new(); } } diff --git a/Bank/BankContracts/ViewModels/RequestViewModel.cs b/Bank/BankContracts/ViewModels/RequestViewModel.cs index 7bb5239..b871f43 100644 --- a/Bank/BankContracts/ViewModels/RequestViewModel.cs +++ b/Bank/BankContracts/ViewModels/RequestViewModel.cs @@ -18,5 +18,7 @@ namespace BankContracts.ViewModels public DateTime RequestTime { get; set; } [DisplayName("Статус заявки")] public RequestStatus Status { get; set; } + public Dictionary CardRequests { get; set; } = new(); + } } diff --git a/Bank/BankDataModels/Models/IRequestModel.cs b/Bank/BankDataModels/Models/IRequestModel.cs index 6f3043e..e4dd12b 100644 --- a/Bank/BankDataModels/Models/IRequestModel.cs +++ b/Bank/BankDataModels/Models/IRequestModel.cs @@ -11,5 +11,7 @@ namespace BankDataModels.Models int Sum { get; set; } DateTime RequestTime { get; set; } RequestStatus Status { get; set; } - } + Dictionary CardRequests { get; } + + } } diff --git a/Bank/BankDatabaseImplement/BankDatabase.cs b/Bank/BankDatabaseImplement/BankDatabase.cs index 8541955..bee8ae3 100644 --- a/Bank/BankDatabaseImplement/BankDatabase.cs +++ b/Bank/BankDatabaseImplement/BankDatabase.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using BankDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace BankDatabaseImplement { @@ -13,5 +14,11 @@ namespace BankDatabaseImplement } base.OnConfiguring(optionsBuilder); } + + public virtual DbSet Clients { get; set; } + public virtual DbSet Cards { get; set; } + public virtual DbSet Operations { get; set; } + public virtual DbSet Requests { get; set; } + public virtual DbSet CardRequests { get; set; } } } \ No newline at end of file diff --git a/Bank/BankDatabaseImplement/Models/CardRequest.cs b/Bank/BankDatabaseImplement/Models/CardRequest.cs index 06c6f04..119bb1b 100644 --- a/Bank/BankDatabaseImplement/Models/CardRequest.cs +++ b/Bank/BankDatabaseImplement/Models/CardRequest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,5 +9,12 @@ namespace BankDatabaseImplement.Models { public class CardRequest { + public int Id { get; set; } + [Required] + public int CardId { get; set; } + [Required] + public int RequestId { get; set; } + public virtual Card Card { get; set; } = new(); + public virtual Request Request { get; set; } = new(); } } diff --git a/Bank/BankDatabaseImplement/Models/Operation.cs b/Bank/BankDatabaseImplement/Models/Operation.cs index b74652c..3e5e2d4 100644 --- a/Bank/BankDatabaseImplement/Models/Operation.cs +++ b/Bank/BankDatabaseImplement/Models/Operation.cs @@ -1,6 +1,7 @@ using Azure; using BankContracts.BindingModels; using BankContracts.ViewModels; +using BankDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -10,7 +11,7 @@ using System.Threading.Tasks; namespace BankDatabaseImplement.Models { - public class Operation + public class Operation : IOperationModel { public int Id { get; set; } [Required] diff --git a/Bank/BankDatabaseImplement/Models/Request.cs b/Bank/BankDatabaseImplement/Models/Request.cs new file mode 100644 index 0000000..1064682 --- /dev/null +++ b/Bank/BankDatabaseImplement/Models/Request.cs @@ -0,0 +1,90 @@ +using BankContracts.BindingModels; +using BankDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Models +{ + public class Request : IRequestModel + { + public int Id { get; set; } + [Required] + public int Sum { get; set; } + [Required] + public DateTime RequestTime { get; set; } + [Required] + public RequestStatus Status { get; set; } + [ForeignKey("RequestId")] + public virtual List Cards { get; set; } = new(); + private Dictionary? _cardRequests { get; set; } = null; + [NotMapped] + public Dictionary CardRequests + { + get + { + if (_cardRequests == null) + { + _cardRequests = Cards.ToDictionary(recPC => recPC.CardId, recPC => recPC.Card as ICardModel); + } + return _cardRequests; + } + } + + public static Request Create(BankDatabase context, RequestBindingModel model) + { + return new Request + { + Id = model.Id, + Sum = model.Sum, + RequestTime = model.RequestTime, + Status = model.Status, + Cards = model.CardRequests.Select(x => new CardRequest + { + Card = context.Cards.First(y => y.Id == x.Key) + }).ToList() + }; + } + + public void Update(RequestBindingModel model) + { + Sum = model.Sum; + RequestTime = model.RequestTime; + Status = model.Status; + } + + public RequestBindingModel GetViewModel => new() + { + Id = Id, + Sum = Sum, + RequestTime = RequestTime, + Status = Status, + CardRequests = CardRequests + }; + + public void UpdateCards(BankDatabase context, RequestBindingModel model) + { + var CardRequests = context.CardRequests.Where(rec => rec.RequestId == model.Id).ToList(); + if (CardRequests != null && CardRequests.Count > 0) + { // удалили те, которых нет в модели + context.CardRequests.RemoveRange(CardRequests.Where(rec => !model.CardRequests.ContainsKey(rec.CardId))); + context.SaveChanges(); + } + var Request = context.Requests.First(x => x.Id == Id); + foreach (var request in model.CardRequests) + { + context.CardRequests.Add(new CardRequest + { + Request = Request, + Card = context.Cards.First(x => x.Id == request.Key), + }); + context.SaveChanges(); + } + _cardRequests = null; + } + } +} \ No newline at end of file