Upload files to 'STODataBaseImplement'
This commit is contained in:
parent
f6ea7423c2
commit
e22e0cc6d9
128
STODataBaseImplement/Car.cs
Normal file
128
STODataBaseImplement/Car.cs
Normal file
@ -0,0 +1,128 @@
|
||||
using STOContracts.BindingModels;
|
||||
using STOContracts.ViewModels;
|
||||
using STODatabaseImplement;
|
||||
using STODataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class Car : ICarModel
|
||||
{
|
||||
public int Id { get ; private set; }
|
||||
|
||||
[Required]
|
||||
public string CarNumber { get; private set; } = string.Empty;
|
||||
|
||||
public string CarBrand { get; private set; } = string.Empty;
|
||||
|
||||
public string CarModel { get; private set; } = string.Empty;
|
||||
|
||||
public DateTime Year { get; private set; } = DateTime.MinValue;
|
||||
[ForeignKey("CarId")]
|
||||
public virtual List<Service> Services { get; private set; } = new();
|
||||
|
||||
[Required]
|
||||
public int ClientId { get; private set; }
|
||||
public virtual Client Client { get; set; } = new();
|
||||
|
||||
public Dictionary<int, (ITechnicalWorkModel, int)> _CarTechnicalWorks = null;
|
||||
[ForeignKey("CarId")]
|
||||
public virtual List<CarTechnicalWork> TechnicalWorks { get; set; } = new();
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (ITechnicalWorkModel, int)> CarTechnicalWorks
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_CarTechnicalWorks == null)
|
||||
{
|
||||
_CarTechnicalWorks = TechnicalWorks
|
||||
.ToDictionary(recPC => recPC.Id, recPC =>
|
||||
(recPC.TechnicalWork as ITechnicalWorkModel, recPC.Count));
|
||||
}
|
||||
return _CarTechnicalWorks;
|
||||
}
|
||||
}
|
||||
|
||||
public static Car Create(STODatabase context, CarBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Car()
|
||||
{
|
||||
ClientId = model.ClientId,
|
||||
Client = context.Clients.First(x => x.Id == model.ClientId),
|
||||
Id = model.Id,
|
||||
CarNumber = model.CarNumber,
|
||||
CarBrand = model.CarBrand,
|
||||
CarModel = model.CarModel,
|
||||
TechnicalWorks = model.CarTechnicalWorks.Select(x => new
|
||||
CarTechnicalWork
|
||||
{
|
||||
TechnicalWork = context.TechnicalWorks.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(CarBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
CarNumber = model.CarNumber;
|
||||
CarBrand = model.CarBrand;
|
||||
CarModel = model.CarModel;
|
||||
Year = model.Year;
|
||||
}
|
||||
public CarViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
CarNumber = CarNumber,
|
||||
CarBrand = CarBrand,
|
||||
CarModel = CarModel,
|
||||
Year = Year
|
||||
};
|
||||
|
||||
public void UpdateTechnicalWorks(STODatabase context, CarBindingModel model)
|
||||
{
|
||||
var technicalWorks = context.CarTechnicalWork.Where(rec =>
|
||||
rec.CarId == model.Id).ToList();
|
||||
if (technicalWorks != null && CarTechnicalWorks.Count > 0)
|
||||
{
|
||||
context.CarTechnicalWork.RemoveRange(technicalWorks.Where(rec
|
||||
=> !model.CarTechnicalWorks.ContainsKey(rec.TechnicalWorkId)));
|
||||
context.SaveChanges();
|
||||
foreach (var updateComponent in technicalWorks)
|
||||
{
|
||||
updateComponent.Count =
|
||||
model.CarTechnicalWorks[updateComponent.TechnicalWorkId].Item2;
|
||||
model.CarTechnicalWorks.Remove(updateComponent.TechnicalWorkId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var manufacture = context.Cars.First(x => x.Id == Id);
|
||||
foreach (var pc in model.CarTechnicalWorks)
|
||||
{
|
||||
context.CarTechnicalWork.Add(new CarTechnicalWork
|
||||
{
|
||||
Car = manufacture,
|
||||
TechnicalWork = context.TechnicalWorks.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_CarTechnicalWorks = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
52
STODataBaseImplement/CarPart.cs
Normal file
52
STODataBaseImplement/CarPart.cs
Normal file
@ -0,0 +1,52 @@
|
||||
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 STODataModels;
|
||||
using STOContracts.BindingModels;
|
||||
using STOContracts.ViewModels;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class CarPart: ICarPartModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string CarPartName { get; private set; } = string.Empty;
|
||||
|
||||
public double Cost { get; private set; } = 0;
|
||||
public static CarPart? Create(CarPartBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new CarPart()
|
||||
{
|
||||
Id = model.Id,
|
||||
CarPartName = model.CarPartName,
|
||||
Cost = model.Cost,
|
||||
};
|
||||
}
|
||||
public void Update(CarPartBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Id = model.Id;
|
||||
CarPartName = model.CarPartName;
|
||||
Cost = model.Cost;
|
||||
}
|
||||
public CarPartViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
CarPartName = CarPartName,
|
||||
Cost = Cost
|
||||
};
|
||||
}
|
||||
}
|
83
STODataBaseImplement/CarPartStorage.cs
Normal file
83
STODataBaseImplement/CarPartStorage.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using STOContracts.BindingModels;
|
||||
using STOContracts.SearchModels;
|
||||
using STOContracts.StorageContracts;
|
||||
using STOContracts.ViewModels;
|
||||
using STODatabaseImplement;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class CarPartStorage : ICarPartStorage
|
||||
{
|
||||
public List<CarPartViewModel> GetFullList()
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
return context.CarParts.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<CarPartViewModel> GetFilteredList(CarPartSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.CarParts.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public CarPartViewModel? GetElement(CarPartSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.CarParts.FirstOrDefault(x =>
|
||||
(x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public CarPartViewModel? Insert(CarPartBindingModel model)
|
||||
{
|
||||
var newCarPart = CarPart.Create(model);
|
||||
if (newCarPart == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
context.CarParts.Add(newCarPart);
|
||||
context.SaveChanges();
|
||||
return newCarPart.GetViewModel;
|
||||
}
|
||||
|
||||
public CarPartViewModel? Update(CarPartBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var component = context.CarParts.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
|
||||
public CarPartViewModel? Delete(CarPartBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var element = context.CarParts.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.CarParts.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
22
STODataBaseImplement/CarPartWork.cs
Normal file
22
STODataBaseImplement/CarPartWork.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class CarPartWork
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int CarPartId { get; set; }
|
||||
[Required]
|
||||
public int WorkId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Work Work { get; set; } = new();
|
||||
public virtual CarPart CarPart { get; set; } = new();
|
||||
}
|
||||
}
|
109
STODataBaseImplement/CarStorage.cs
Normal file
109
STODataBaseImplement/CarStorage.cs
Normal file
@ -0,0 +1,109 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using STOContracts.BindingModels;
|
||||
using STOContracts.SearchModels;
|
||||
using STOContracts.StorageContracts;
|
||||
using STOContracts.ViewModels;
|
||||
using STODatabaseImplement;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class CarStorage : ICarStorage
|
||||
{
|
||||
public List<CarViewModel> GetFullList()
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
return context.Cars
|
||||
.Include(x => x.TechnicalWorks)
|
||||
.ThenInclude(x => x.TechnicalWork)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<CarViewModel> GetFilteredList(CarSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.CarNumber))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.Cars.Include(x => x.TechnicalWorks)
|
||||
.ThenInclude(x => x.TechnicalWork)
|
||||
.Where(x => x.CarNumber.Contains(model.CarNumber))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public CarViewModel? GetElement(CarSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.CarNumber) &&
|
||||
!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.Cars
|
||||
.Include(x => x.TechnicalWorks)
|
||||
.ThenInclude(x => x.TechnicalWork)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CarNumber) &&
|
||||
x.CarNumber == model.CarNumber) ||
|
||||
(model.Id.HasValue && x.Id ==
|
||||
model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public CarViewModel? Insert(CarBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var newCar = Car.Create(context, model);
|
||||
if (newCar == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Cars.Add(newCar);
|
||||
context.SaveChanges();
|
||||
return newCar.GetViewModel;
|
||||
}
|
||||
public CarViewModel? Update(CarBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var car = context.Cars.FirstOrDefault(rec =>
|
||||
rec.Id == model.Id);
|
||||
if (car == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
car.Update(model);
|
||||
context.SaveChanges();
|
||||
car.UpdateTechnicalWorks(context, model);
|
||||
transaction.Commit();
|
||||
return car.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public CarViewModel? Delete(CarBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var element = context.Cars
|
||||
.Include(x => x.TechnicalWorks)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Cars.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user