From d3461d969742efe48fc79e47e2d07d18680808e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4=20=D0=9C=D0=B0=D0=BB?= =?UTF-8?q?=D0=B0=D1=84=D0=B5=D0=B5=D0=B2?= Date: Sun, 5 May 2024 16:47:03 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B1=D0=BE=D0=B1=D1=80=D1=8B=20=D0=B2=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/MessageInfoLogic.cs | 51 ++++++++++++ .../Implements/MessageInfoStorage.cs | 59 ++++++++++++++ .../JewelryStoreDatabase.cs | 1 + ....cs => 20240505124647_message.Designer.cs} | 45 ++++++++++- ...ialCreate.cs => 20240505124647_message.cs} | 31 ++++++- .../JewelryStoreDatabaseModelSnapshot.cs | 41 ++++++++++ .../Models/Client.cs | 4 +- .../Models/MessageInfo.cs | 54 +++++++++++++ .../DataListSingleton.cs | 2 + .../Implements/MessageInfoStorage.cs | 73 +++++++++++++++++ .../Models/MessageInfo.cs | 51 ++++++++++++ .../DataFileSingleton.cs | 5 ++ .../Implements/MessageInfoStorage.cs | 58 ++++++++++++++ .../Models/MessageInfo.cs | 80 +++++++++++++++++++ 14 files changed, 551 insertions(+), 4 deletions(-) create mode 100644 JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/MessageInfoLogic.cs create mode 100644 JewelryStore/JewelryStoreDatabaseImplement/Implements/MessageInfoStorage.cs rename JewelryStore/JewelryStoreDatabaseImplement/Migrations/{20240421174803_InitialCreate.Designer.cs => 20240505124647_message.Designer.cs} (85%) rename JewelryStore/JewelryStoreDatabaseImplement/Migrations/{20240421174803_InitialCreate.cs => 20240505124647_message.cs} (85%) create mode 100644 JewelryStore/JewelryStoreDatabaseImplement/Models/MessageInfo.cs create mode 100644 JewelryStore/JewelryStoreListImplement/Implements/MessageInfoStorage.cs create mode 100644 JewelryStore/JewelryStoreListImplement/Models/MessageInfo.cs create mode 100644 JewelryStore/JewerlyStoreFileImplement/Implements/MessageInfoStorage.cs create mode 100644 JewelryStore/JewerlyStoreFileImplement/Models/MessageInfo.cs diff --git a/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/MessageInfoLogic.cs new file mode 100644 index 0000000..6e306da --- /dev/null +++ b/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -0,0 +1,51 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.BusinessLogicsContracts; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreBusinessLogic.BusinessLogics +{ + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + + private readonly IMessageInfoStorage _messageStorage; + + public MessageInfoLogic(IMessageInfoStorage messageStorage, ILogger logger) + { + _messageStorage = messageStorage; + _logger = logger; + } + public bool Create(MessageInfoBindingModel model) + { + if (_messageStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public List? ReadList(MessageInfoSearchModel? model) + { + _logger.LogInformation("ReadList. ClientID:{ClientID}. ID:{ ID}", model?.ClientId, model?.MessageId); + + var list = model == null ? _messageStorage.GetFullList() : _messageStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + } +} diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Implements/MessageInfoStorage.cs b/JewelryStore/JewelryStoreDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..a229bef --- /dev/null +++ b/JewelryStore/JewelryStoreDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,59 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + using var context = new JewelryStoreDatabase(); + return context.Messages.Include(x => x.Client).FirstOrDefault(x => x.MessageId.Equals(model.MessageId))?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return context.Messages.Include(x => x.Client).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new JewelryStoreDatabase(); + return context.Messages.Include(x => x.Client).Select(x => x.GetViewModel).ToList(); + } + + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new JewelryStoreDatabase(); + model.ClientId = context.Clients.FirstOrDefault(x => x.Email.Equals(model.SenderName))?.Id; + var message = MessageInfo.Create(model); + if (message == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId))) + { + return null; + } + context.Messages.Add(message); + context.SaveChanges(); + return message.GetViewModel; + + } + } +} diff --git a/JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs b/JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs index e7acd6b..aff0387 100644 --- a/JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs +++ b/JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs @@ -26,5 +26,6 @@ namespace JewelryStoreDatabaseImplement public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } + public virtual DbSet Messages { get; set; } } } diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240421174803_InitialCreate.Designer.cs b/JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240505124647_message.Designer.cs similarity index 85% rename from JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240421174803_InitialCreate.Designer.cs rename to JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240505124647_message.Designer.cs index fe1e87e..ba53537 100644 --- a/JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240421174803_InitialCreate.Designer.cs +++ b/JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240505124647_message.Designer.cs @@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace JewelryStoreDatabaseImplement.Migrations { [DbContext(typeof(JewelryStoreDatabase))] - [Migration("20240421174803_InitialCreate")] - partial class InitialCreate + [Migration("20240505124647_message")] + partial class message { protected override void BuildTargetModel(ModelBuilder modelBuilder) { @@ -142,6 +142,36 @@ namespace JewelryStoreDatabaseImplement.Migrations b.ToTable("JewelComponents"); }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("text"); + + b.Property("Body") + .IsRequired() + .HasColumnType("text"); + + b.Property("ClientId") + .HasColumnType("integer"); + + b.Property("DateDelivery") + .HasColumnType("timestamp without time zone"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("Messages"); + }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -204,6 +234,15 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Navigation("Jewel"); }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Client", "Client") + .WithMany("Messages") + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => { b.HasOne("JewelryStoreDatabaseImplement.Models.Client", "Client") @@ -231,6 +270,8 @@ namespace JewelryStoreDatabaseImplement.Migrations modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Client", b => { + b.Navigation("Messages"); + b.Navigation("Orders"); }); diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240421174803_InitialCreate.cs b/JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240505124647_message.cs similarity index 85% rename from JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240421174803_InitialCreate.cs rename to JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240505124647_message.cs index b454e6c..4b0b385 100644 --- a/JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240421174803_InitialCreate.cs +++ b/JewelryStore/JewelryStoreDatabaseImplement/Migrations/20240505124647_message.cs @@ -6,7 +6,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace JewelryStoreDatabaseImplement.Migrations { - public partial class InitialCreate : Migration + public partial class message : Migration { protected override void Up(MigrationBuilder migrationBuilder) { @@ -69,6 +69,27 @@ namespace JewelryStoreDatabaseImplement.Migrations table.PrimaryKey("PK_Jewels", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Messages", + columns: table => new + { + MessageId = table.Column(type: "text", nullable: false), + ClientId = table.Column(type: "integer", nullable: true), + SenderName = table.Column(type: "text", nullable: false), + DateDelivery = table.Column(type: "timestamp without time zone", nullable: false), + Subject = table.Column(type: "text", nullable: false), + Body = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Messages", x => x.MessageId); + table.ForeignKey( + name: "FK_Messages_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id"); + }); + migrationBuilder.CreateTable( name: "JewelComponents", columns: table => new @@ -143,6 +164,11 @@ namespace JewelryStoreDatabaseImplement.Migrations table: "JewelComponents", column: "JewelId"); + migrationBuilder.CreateIndex( + name: "IX_Messages_ClientId", + table: "Messages", + column: "ClientId"); + migrationBuilder.CreateIndex( name: "IX_Orders_ClientId", table: "Orders", @@ -164,6 +190,9 @@ namespace JewelryStoreDatabaseImplement.Migrations migrationBuilder.DropTable( name: "JewelComponents"); + migrationBuilder.DropTable( + name: "Messages"); + migrationBuilder.DropTable( name: "Orders"); diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDatabaseModelSnapshot.cs b/JewelryStore/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDatabaseModelSnapshot.cs index 8121de2..135e8dd 100644 --- a/JewelryStore/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDatabaseModelSnapshot.cs +++ b/JewelryStore/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDatabaseModelSnapshot.cs @@ -140,6 +140,36 @@ namespace JewelryStoreDatabaseImplement.Migrations b.ToTable("JewelComponents"); }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("text"); + + b.Property("Body") + .IsRequired() + .HasColumnType("text"); + + b.Property("ClientId") + .HasColumnType("integer"); + + b.Property("DateDelivery") + .HasColumnType("timestamp without time zone"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("Messages"); + }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -202,6 +232,15 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Navigation("Jewel"); }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Client", "Client") + .WithMany("Messages") + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => { b.HasOne("JewelryStoreDatabaseImplement.Models.Client", "Client") @@ -229,6 +268,8 @@ namespace JewelryStoreDatabaseImplement.Migrations modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Client", b => { + b.Navigation("Messages"); + b.Navigation("Orders"); }); diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Models/Client.cs b/JewelryStore/JewelryStoreDatabaseImplement/Models/Client.cs index 7ea2a8a..994b2c5 100644 --- a/JewelryStore/JewelryStoreDatabaseImplement/Models/Client.cs +++ b/JewelryStore/JewelryStoreDatabaseImplement/Models/Client.cs @@ -20,8 +20,10 @@ namespace JewelryStoreDatabaseImplement.Models public string Email { get; set; } = string.Empty; [Required] public string Password { get; set; } = string.Empty; + [ForeignKey("ClientId")] + public virtual List Messages { get; set; } = new(); - [ForeignKey("ClientId")] + [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); public static Client? Create(ClientBindingModel model) diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Models/MessageInfo.cs b/JewelryStore/JewelryStoreDatabaseImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..7eb816d --- /dev/null +++ b/JewelryStore/JewelryStoreDatabaseImplement/Models/MessageInfo.cs @@ -0,0 +1,54 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewerlyStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + [Key] + public string MessageId { get; private set; } = string.Empty; + public int? ClientId { get; private set; } + [Required] + public string SenderName { get; private set; } = string.Empty; + [Required] + public DateTime DateDelivery { get; private set; } + [Required] + public string Subject { get; private set; } = string.Empty; + [Required] + public string Body { get; private set; } = string.Empty; + public virtual Client? Client { get; private set; } + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new MessageInfo + { + MessageId = model.MessageId, + ClientId = model.ClientId, + SenderName = model.SenderName, + Body = model.Body, + DateDelivery = model.DateDelivery, + Subject = model.Subject, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + ClientId = ClientId, + SenderName = SenderName, + Body = Body, + DateDelivery = DateDelivery, + Subject = Subject, + }; + } +} diff --git a/JewelryStore/JewelryStoreListImplement/DataListSingleton.cs b/JewelryStore/JewelryStoreListImplement/DataListSingleton.cs index 9455a98..322f622 100644 --- a/JewelryStore/JewelryStoreListImplement/DataListSingleton.cs +++ b/JewelryStore/JewelryStoreListImplement/DataListSingleton.cs @@ -15,6 +15,7 @@ namespace JewelryStoreListImplement public List Jewels { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() { @@ -23,6 +24,7 @@ namespace JewelryStoreListImplement Jewels = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() diff --git a/JewelryStore/JewelryStoreListImplement/Implements/MessageInfoStorage.cs b/JewelryStore/JewelryStoreListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..1a60a62 --- /dev/null +++ b/JewelryStore/JewelryStoreListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,73 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage(DataListSingleton source) + { + _source = DataListSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + + foreach (var msg in _source.Messages) + { + if (msg.MessageId.Equals(model.MessageId)) + { + return msg.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + var list = new List(); + foreach (var msg in _source.Messages) + { + if (msg.ClientId.HasValue && model.ClientId == msg.ClientId) + { + list.Add(msg.GetViewModel); + } + } + return list; + } + + public List GetFullList() + { + var list = new List(); + foreach (var msg in _source.Messages) + { + list.Add(msg.GetViewModel); + } + return list; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + } +} diff --git a/JewelryStore/JewelryStoreListImplement/Models/MessageInfo.cs b/JewelryStore/JewelryStoreListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..1e8040a --- /dev/null +++ b/JewelryStore/JewelryStoreListImplement/Models/MessageInfo.cs @@ -0,0 +1,51 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewerlyStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreListImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + public string SenderName { get; set; } = string.Empty; + + public DateTime DateDelivery { get; set; } + + public string Subject { get; set; } = string.Empty; + + public string Body { get; set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel? model) + { + if (model == null) + { + return null; + } + return new() + { + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + Subject = model.Subject, + Body = model.Body, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + Subject = Subject, + Body = Body + }; + } +} diff --git a/JewelryStore/JewerlyStoreFileImplement/DataFileSingleton.cs b/JewelryStore/JewerlyStoreFileImplement/DataFileSingleton.cs index 2a55139..b5e637f 100644 --- a/JewelryStore/JewerlyStoreFileImplement/DataFileSingleton.cs +++ b/JewelryStore/JewerlyStoreFileImplement/DataFileSingleton.cs @@ -17,11 +17,13 @@ namespace JewerlyStoreFileImplement private readonly string JewelFileName = "Jewel.xml"; private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageInfoFileName = "MessageInfo.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Jewels { get; private set; } public List Clients { get; private set; } public List Implementers { get; private set; } + public List Messages { get; private set; } public static DataFileSingleton GetInstance() { @@ -42,6 +44,8 @@ namespace JewerlyStoreFileImplement public void SaveImplementers() => SaveData(Implementers, OrderFileName, "Implementers", x => x.GetXElement); + public void SaveMessages() => SaveData(Messages, MessageInfoFileName, "Messages", x => x.GetXElement); + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; @@ -49,6 +53,7 @@ namespace JewerlyStoreFileImplement Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; + Messages = LoadData(MessageInfoFileName, "MessageInfo", x => MessageInfo.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/JewelryStore/JewerlyStoreFileImplement/Implements/MessageInfoStorage.cs b/JewelryStore/JewerlyStoreFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..8b19588 --- /dev/null +++ b/JewelryStore/JewerlyStoreFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,58 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewerlyStoreFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewerlyStoreFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + return _source.Messages.FirstOrDefault(x => x.MessageId.Equals(model.MessageId))?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + return _source.Messages.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return _source.Messages.Select(x => x.GetViewModel).ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + _source.SaveMessages(); + return newMessage.GetViewModel; + } + } +} diff --git a/JewelryStore/JewerlyStoreFileImplement/Models/MessageInfo.cs b/JewelryStore/JewerlyStoreFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..d7b1d89 --- /dev/null +++ b/JewelryStore/JewerlyStoreFileImplement/Models/MessageInfo.cs @@ -0,0 +1,80 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewerlyStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace JewerlyStoreFileImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + public string SenderName { get; set; } = string.Empty; + + public DateTime DateDelivery { get; set; } + + public string Subject { get; set; } = string.Empty; + + public string Body { get; set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel? model) + { + if (model == null) + { + return null; + } + return new MessageInfo() + { + MessageId = model.MessageId, + ClientId = model.ClientId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + Subject = model.Subject, + Body = model.Body, + + }; + } + + public static MessageInfo? Create(XElement element) + { + if (element == null) + { + return null; + } + return new MessageInfo() + { + MessageId = element.Attribute("MessageId")!.Value, + ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), + SenderName = element.Element("SenderName")!.Value, + DateDelivery = Convert.ToDateTime(element.Element("DateDelivery")!.Value), + Subject = element.Attribute("Subject")!.Value, + Body = element.Attribute("Body")!.Value, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + ClientId = ClientId, + SenderName = SenderName, + DateDelivery = DateDelivery, + Subject = Subject, + Body = Body + }; + + public XElement GetXElement => new("MessageInfo", new XAttribute("MessageId", MessageId), + new XElement("ClientId", ClientId.ToString()), + new XElement("SenderName", SenderName), + new XElement("DateDelivery", DateDelivery), + new XElement("Subject", Subject), + new XElement("Body", Body), + new XElement("SenderName", SenderName)); + } +}