From 936f33d59ae8378fe203fcef1287456cd7649baa Mon Sep 17 00:00:00 2001 From: "kagbie3nn@mail.ru" Date: Fri, 31 May 2024 12:46:44 +0400 Subject: [PATCH] cot --- .../Implements/CostStorage.cs | 89 +++++++++++++++++++ .../ZooDataBaseImplement/Models/Cost.cs | 53 +++++++++++ .../ZooDataBaseImplement/ZooDatabase.cs | 3 +- 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 git/JurasicZoo/ZooDataBaseImplement/Implements/CostStorage.cs create mode 100644 git/JurasicZoo/ZooDataBaseImplement/Models/Cost.cs diff --git a/git/JurasicZoo/ZooDataBaseImplement/Implements/CostStorage.cs b/git/JurasicZoo/ZooDataBaseImplement/Implements/CostStorage.cs new file mode 100644 index 0000000..700a556 --- /dev/null +++ b/git/JurasicZoo/ZooDataBaseImplement/Implements/CostStorage.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZooContracts.BindingModels; +using ZooContracts.SearchModels; +using ZooContracts.StorageContracts; +using ZooContracts.ViewModels; +using ZooDataBaseImplement.Models; + +namespace ZooDataBaseImplement.Implements +{ + internal class CostStorage : ICostStorage + { + public List GetFullList() + { + using var context = new ZooDatabase(); + return context.Costs + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(CostSearchModel + model) + { + if (string.IsNullOrEmpty(model.CostName)) + { + return new(); + } + using var context = new ZooDatabase(); + return context.Costs + .Where(x => x.CostName.Contains(model.CostName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public CostViewModel? GetElement(CostSearchModel model) + { + if (string.IsNullOrEmpty(model.CostName) && !model.Id.HasValue) + { + return null; + } + using var context = new ZooDatabase(); + return context.Costs + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.CostName) && x.CostName == + model.CostName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public CostViewModel? Insert(CostBindingModel model) + { + var newCost = Cost.Create(model); + if (newCost == null) + { + return null; + } + using var context = new ZooDatabase(); + context.Costs.Add(newCost); + context.SaveChanges(); + return newCost.GetViewModel; + } + public CostViewModel? Update(CostBindingModel model) + { + using var context = new ZooDatabase(); + var component = context.Costs.FirstOrDefault(x => x.Id == + model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public CostViewModel? Delete(CostBindingModel model) + { + using var context = new ZooDatabase(); + var element = context.Costs.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Costs.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/git/JurasicZoo/ZooDataBaseImplement/Models/Cost.cs b/git/JurasicZoo/ZooDataBaseImplement/Models/Cost.cs new file mode 100644 index 0000000..645b730 --- /dev/null +++ b/git/JurasicZoo/ZooDataBaseImplement/Models/Cost.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZooContracts.BindingModels; +using ZooContracts.ViewModels; +using ZooDataModels.Models; + +namespace ZooDataBaseImplement.Models +{ + public class Cost : ICostModel + { + public int Id { get; set; } + [Required] + public string CostName { get; set; } = string.Empty; + [Required] + public double CostPrice { get; set; } + [ForeignKey("CostID")] + public virtual List Routes { get; set; } = new(); + public static Cost? Create(CostBindingModel model) + { + if (model == null) + { + return null; + } + return new Cost() + { + Id = model.Id, + CostName = model.CostName, + CostPrice = model.CostPrice, + }; + } + public void Update(CostBindingModel model) + { + if (model == null) + { + return; + } + CostName = model.CostName; + CostPrice = model.CostPrice; + } + public CostViewModel GetViewModel => new() + { + Id = Id, + CostName = CostName, + CostPrice = CostPrice, + }; + } +} + diff --git a/git/JurasicZoo/ZooDataBaseImplement/ZooDatabase.cs b/git/JurasicZoo/ZooDataBaseImplement/ZooDatabase.cs index fe8b626..7a05d97 100644 --- a/git/JurasicZoo/ZooDataBaseImplement/ZooDatabase.cs +++ b/git/JurasicZoo/ZooDataBaseImplement/ZooDatabase.cs @@ -25,6 +25,7 @@ namespace ZooDataBaseImplement public virtual DbSet Routes { set; get; } public virtual DbSet RoutePreserves { set; get; } public virtual DbSet Preserves { set; get; } - + public virtual DbSet Costs { set; get; } + } }