From db3fb1c9f439c14125cf2b9ca866b4821bca7098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=9F=D1=83=D1=82=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=D0=BD?= Date: Sun, 23 Apr 2023 16:48:49 +0400 Subject: [PATCH] DatabaseImplement --- .../PrecastConcretePlant/Program.cs | 2 + .../Client.cs | 3 ++ .../MessageInfo.cs | 54 +++++++++++++++++++ .../MessageInfoStorage.cs | 51 ++++++++++++++++++ .../PrecastConcretePlantDatabase.cs | 1 + 5 files changed, 111 insertions(+) create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/MessageInfo.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/MessageInfoStorage.cs diff --git a/PrecastConcretePlant/PrecastConcretePlant/Program.cs b/PrecastConcretePlant/PrecastConcretePlant/Program.cs index bd6e9fd..92a0b82 100644 --- a/PrecastConcretePlant/PrecastConcretePlant/Program.cs +++ b/PrecastConcretePlant/PrecastConcretePlant/Program.cs @@ -44,6 +44,7 @@ namespace PrecastConcretePlant services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -52,6 +53,7 @@ namespace PrecastConcretePlant services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Client.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Client.cs index b754a28..5f06395 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Client.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Client.cs @@ -23,6 +23,9 @@ namespace PrecastConcretePlantDatabaseImplement [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("ClientId")] + public virtual List Messages { get; set; } = new(); + public static Client? Create(ClientBindingModel model) { if (model == null) diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/MessageInfo.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/MessageInfo.cs new file mode 100644 index 0000000..400aec0 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/MessageInfo.cs @@ -0,0 +1,54 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels; +using System.ComponentModel.DataAnnotations; + + +namespace PrecastConcretePlantDatabaseImplement +{ + public class MessageInfo : IMessageInfoModel + { + [Key] + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public Client? Client { get; private set; } + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/MessageInfoStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..4d3fb08 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/MessageInfoStorage.cs @@ -0,0 +1,51 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContract; +using PrecastConcretePlantContracts.ViewModels; + +namespace PrecastConcretePlantDatabaseImplement +{ + public class MessageInfoStorage : IMessageInfoStorage + { + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + using var context = new PrecastConcretePlantDatabase(); + if (model.MessageId != null) + { + return context.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + using var context = new PrecastConcretePlantDatabase(); + return context.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new PrecastConcretePlantDatabase(); + return context.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + var newMessage = MessageInfo.Create(model); + if (newMessage == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId))) + { + return null; + } + context.Messages.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs index 87edd3d..52cf400 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs @@ -25,5 +25,6 @@ namespace PrecastConcretePlantDatabaseImplement public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } + public virtual DbSet Messages { set; get; } } }