diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs index 683cbfb..5f07919 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -2,6 +2,7 @@ using LawFirmContracts.SearchModels; using LawFirmContracts.StoragesContracts; using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement_.Models; using System; using System.Collections.Generic; using System.Linq; @@ -12,34 +13,89 @@ namespace LawFirmDatabaseImplement_.Implements { public class OrderStorage : IOrderStorage { - public OrderViewModel? Delete(OrderBindingModel model) - { - throw new NotImplementedException(); - } - - public OrderViewModel? GetElement(OrderSearchModel model) - { - throw new NotImplementedException(); - } - - public List GetFilteredList(OrderSearchModel model) - { - throw new NotImplementedException(); - } - public List GetFullList() { - throw new NotImplementedException(); + using var context = new LawFirmDatabase(); + List orderList = context.Orders + .Select(x => x.GetViewModel) + .ToList(); + foreach (var order in orderList) + { + string documentName = context.Documents + .SingleOrDefault(x => x.Id == order.DocumentId)?.DocumentName ?? string.Empty; + order.DocumentName = documentName; + } + return orderList; } - + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new LawFirmDatabase(); + List orderList = context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + foreach (var order in orderList) + { + string documentName = context.Documents + .SingleOrDefault(x => x.Id == order.DocumentId)?.DocumentName ?? string.Empty; + order.DocumentName = documentName; + } + return orderList; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Orders + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) { - throw new NotImplementedException(); + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new LawFirmDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; } - + public OrderViewModel? Update(OrderBindingModel model) { - throw new NotImplementedException(); + using var context = new LawFirmDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } } } diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs index 0bf7114..c12c882 100644 --- a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -14,7 +14,9 @@ namespace LawFirmDatabaseImplement_ { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=AbstractShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=фыва\SQLEXPRESS; + Initial Catalog=LawFirmDatabase;Integrated Security=True; + MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230509183111_InitialCreate.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230509183111_InitialCreate.Designer.cs new file mode 100644 index 0000000..2e17f21 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230509183111_InitialCreate.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using LawFirmDatabaseImplement_; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement_.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230509183111_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.DocumentComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentComponents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.DocumentComponent", b => + { + b.HasOne("LawFirmDatabaseImplement_.Models.Component", "Component") + .WithMany("DocumentComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement_.Models.Document", "Document") + .WithMany("Components") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement_.Models.Document", null) + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Component", b => + { + b.Navigation("DocumentComponents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Document", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230509183111_InitialCreate.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230509183111_InitialCreate.cs new file mode 100644 index 0000000..d5c3121 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230509183111_InitialCreate.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LawFirmDatabaseImplement_.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Order.cs b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs index 04516a3..6518ec3 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Order.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs @@ -4,6 +4,7 @@ using LawFirmDataModels.Enums; using LawFirmDataModels.Models; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,37 +14,70 @@ namespace LawFirmDatabaseImplement_.Models { public class Order : IOrderModel { - public int DocumentId { get; set; } - - public int Count { get; set; } - - public double Sum { get; set; } - - public OrderStatus Status { get; set; } - - public DateTime DateCreate { get; set; } - - public DateTime? DateImplement { get; set; } - public int Id { get; set; } + [Required] + public int DocumentId { get; set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } - public static Order? Create(XElement element) + public static Order? Create(OrderBindingModel model) { - return new Order(); - + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + DocumentId = model.DocumentId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public static Order Create(OrderViewModel model) + { + return new Order() + { + Id = model.Id, + DocumentId = model.DocumentId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; } - public void Update(OrderBindingModel? model) { - + if (model == null) + { + return; + } + Status = model.Status; + if (model.DateImplement != null) + { + DateImplement = model.DateImplement; + } } - public OrderViewModel GetViewModel => new() { - + Id = Id, + DocumentId = DocumentId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement }; - - public XElement GetXElement => new("Order"); - } }