cot
This commit is contained in:
parent
4a65ea5456
commit
936f33d59a
@ -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<CostViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ZooDatabase();
|
||||
return context.Costs
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<CostViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
53
git/JurasicZoo/ZooDataBaseImplement/Models/Cost.cs
Normal file
53
git/JurasicZoo/ZooDataBaseImplement/Models/Cost.cs
Normal file
@ -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<Route> 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ namespace ZooDataBaseImplement
|
||||
public virtual DbSet<Route> Routes { set; get; }
|
||||
public virtual DbSet<RoutePreserve> RoutePreserves { set; get; }
|
||||
public virtual DbSet<Preserve> Preserves { set; get; }
|
||||
|
||||
public virtual DbSet<Cost> Costs { set; get; }
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user