Compare commits

...

3 Commits

7 changed files with 436 additions and 5 deletions

View File

@ -13,18 +13,18 @@ namespace CarCenterContracts.ViewModels
public int Id { get; set; } public int Id { get; set; }
[DisplayName("ФИО администратора")] [DisplayName("ФИО администратора")]
public string AdministratorFIO { get; } = string.Empty; public string AdministratorFIO { get; set; } = string.Empty;
[DisplayName("Телефон администратора")] [DisplayName("Телефон администратора")]
public string AdministratorNumber { get; } = string.Empty; public string AdministratorNumber { get; set; } = string.Empty;
[DisplayName("Логин администратора")] [DisplayName("Логин администратора")]
public string AdministratorLogin { get; } = string.Empty; public string AdministratorLogin { get; set; } = string.Empty;
[DisplayName("Mail администратора")] [DisplayName("Mail администратора")]
public string AdministratorEmail { get; } = string.Empty; public string AdministratorEmail { get; set; } = string.Empty;
[DisplayName("Пароль администратора")] [DisplayName("Пароль администратора")]
public string AdministratorPassword { get; } = string.Empty; public string AdministratorPassword { get; set; } = string.Empty;
} }
} }

View File

@ -0,0 +1,95 @@
using CarCenterDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
namespace CarCenterDataBaseImplement.Models
{
public class Administrator : IAdministratorModel
{
public int Id { get; private set; }
[Required]
public string AdministratorFIO { get; set; } = string.Empty;
[Required]
public string AdministratorEmail { get; set; } = string.Empty;
[Required]
public string AdministratorPassword { get; set; } = string.Empty;
[Required]
public string AdministratorLogin { get; set; } = string.Empty;
[Required]
public string AdministratorNumber { get; set; } = string.Empty;
[ForeignKey("AdministratorId")]
public virtual List<Equipment> Equipments { get; set; } = new();
[ForeignKey("AdministratorId")]
public virtual List<Car> Cars { get; set; } = new();
[ForeignKey("AdministratorId")]
public virtual List<Inspection> Inspections { get; set; } = new();
public static Administrator? Create(AdministratorBindingModel model)
{
if (model == null)
{
return null;
}
return new Administrator()
{
Id = model.Id,
AdministratorFIO = model.AdministratorFIO,
AdministratorEmail = model.AdministratorEmail,
AdministratorPassword = model.AdministratorPassword,
AdministratorLogin = model.AdministratorLogin,
AdministratorNumber = model.AdministratorNumber
};
}
public static Administrator Create(AdministratorViewModel model)
{
return new Administrator
{
Id = model.Id,
AdministratorFIO = model.AdministratorFIO,
AdministratorEmail = model.AdministratorEmail,
AdministratorPassword = model.AdministratorPassword,
AdministratorLogin = model.AdministratorLogin,
AdministratorNumber = model.AdministratorNumber
};
}
public void Update(AdministratorBindingModel model)
{
if (model == null)
{
return;
}
AdministratorFIO = model.AdministratorFIO;
AdministratorEmail = model.AdministratorEmail;
AdministratorPassword = model.AdministratorPassword;
AdministratorLogin = model.AdministratorLogin;
AdministratorNumber = model.AdministratorNumber;
}
public AdministratorViewModel GetViewModel => new()
{
Id = Id,
AdministratorFIO = AdministratorFIO,
AdministratorEmail = AdministratorEmail,
AdministratorPassword = AdministratorPassword,
AdministratorLogin = AdministratorLogin,
AdministratorNumber = AdministratorNumber
};
}
}

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CarCenterDataModels.Models;
using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
namespace CarCenterDataBaseImplement.Models
{
public class Car : ICarModel
{
public int Id { get; set; }
public int AdministratorId { get; set; }
[Required]
public string BrandCar { get; set; } = string.Empty;
[Required]
public string Model { get; set; } = string.Empty;
public virtual Administrator Administrator { get; set; }
[ForeignKey("CarId")]
public virtual List<EquipmentCar> EquipmentCars { get; set; } = new();
[ForeignKey("CarId")]
public virtual List<InspectionCar> InspectionCar { get; set; } = new();
public static Car? Create(CarBindingModel model)
{
if (model == null)
{
return null;
}
return new Car()
{
Id = model.Id,
BrandCar = model.BrandCar,
AdministratorId = model.AdministratorId,
Model= model.Model
};
}
public static Car Create(CarViewModel model)
{
return new Car
{
Id = model.Id,
BrandCar = model.BrandCar,
AdministratorId = model.AdministratorId,
Model = model.Model
};
}
public void Update(CarBindingModel model)
{
if (model == null)
{
return;
}
AdministratorId = model.AdministratorId;
BrandCar = model.BrandCar;
Model = model.Model;
}
public CarViewModel GetViewModel => new()
{
Id = Id,
BrandCar = BrandCar,
AdministratorId = AdministratorId,
Model = Model
};
}
}

View File

@ -0,0 +1,112 @@
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 CarCenterDataModels.Models;
using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
namespace CarCenterDataBaseImplement.Models
{
public class Equipment : IEquipmentModel
{
[Required]
public string EquipmentName { get; set; } = string.Empty;
[Required]
public double EquipmentPrice { get; set; }
public int AdministratorId { get; private set; }
public int? PreSaleWorkId { get; private set; }
public int Id { get; private set; }
public virtual Administrator Administrator { get; set; }
public virtual PreSaleWork? PreSaleWork { get; set; }
[ForeignKey("EquipmentId")]
public virtual List<EquipmentCar> Cars { get; set; }
private Dictionary<int, ICarModel> _equipmentCars = null;
[NotMapped]
public Dictionary<int, ICarModel> EquipmentCars
{
get
{
if (_equipmentCars == null)
{
using var context = new CarCenterDataBase();
_equipmentCars = Cars
.ToDictionary(x => x.CarId, x => (context.Cars
.FirstOrDefault(y => y.Id == x.CarId)! as ICarModel));
}
return _equipmentCars;
}
}
public static Equipment Create(CarCenterDataBase context, EquipmentBindingModel model)
{
return new Equipment()
{
Id = model.Id,
EquipmentName = model.EquipmentName,
EquipmentPrice = model.EquipmentPrice,
AdministratorId = model.AdministratorId,
PreSaleWorkId = model.PreSaleWorkId,
Cars = model.EquipmentCars.Select(x => new EquipmentCar
{
Car = context.Cars.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(EquipmentBindingModel model)
{
EquipmentName = model.EquipmentName;
EquipmentPrice = model.EquipmentPrice;
AdministratorId = model.AdministratorId;
PreSaleWorkId = model.PreSaleWorkId;
}
public EquipmentViewModel GetViewModel => new()
{
Id = Id,
EquipmentName = EquipmentName,
AdministratorId = AdministratorId,
PreSaleWorkId = PreSaleWorkId,
EquipmentPrice = EquipmentPrice,
EquipmentCars = EquipmentCars
};
public void UpdateCars(CarCenterDataBase context, EquipmentBindingModel model)
{
var equipmentCars = context.EquipmentCars.Where(rec => rec.RoomId == model.Id).ToList();
if (equipmentCars != null && equipmentCars.Any())
{
context.EquipmentCars.RemoveRange(equipmentCars.Where(rec => !model.EquipmentCars.ContainsKey(rec.CarId)));
context.SaveChanges();
foreach (var updateCar in equipmentCars)
{
model.EquipmentCars.Remove(updateCar.CarId);
}
context.SaveChanges();
}
var equipment = context.Equipments.First(x => x.Id == Id);
foreach (var ec in model.EquipmentCars)
{
context.EquipmentCars.Add(new EquipmentCar
{
Equipment = equipment,
Car = context.Cars.First(x => x.Id == ec.Key),
});
context.SaveChanges();
}
_equipmentCars = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterDataBaseImplement.Models
{
public class EquipmentCar
{
public int Id { get; set; }
public int EquipmentId { get; set; }
public int CarId { get; set; }
[Required]
public int Count { get; set; }
public virtual Equipment Equipment { get; set; } = new();
public virtual Car Car { get; set; } = new();
}
}

View File

@ -0,0 +1,106 @@
using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
using CarCenterDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterDataBaseImplement.Models
{
public class Inspection : IInspectionModel
{
public DateTime? InspectionDate { get; set; }
public int AdministratorId { get; private set; }
public int? EmployeeId { get; private set; }
public int Id { get; private set; }
public string InspectionName { get; set; } = string.Empty;
public virtual Administrator Administrator { get; set; }
public virtual Employee? Employee { get; set; }
[ForeignKey("InspectionId")]
public virtual List<InspectionCar> Cars { get; set; }
private Dictionary<int, ICarModel> _inspectionCars = null;
[NotMapped]
public Dictionary<int, ICarModel> InspectionCars
{
get
{
if (_inspectionCars == null)
{
using var context = new CarCenterDataBase();
_inspectionCars = Cars
.ToDictionary(x => x.CarId, x => (context.Cars
.FirstOrDefault(y => y.Id == x.CarId)! as ICarModel));
}
return _inspectionCars;
}
}
public static InspectionCars Create(CarCenterDataBase context, InspectionBindingModel model)
{
return new Inspection()
{
Id = model.Id,
EmployeeId = model.EmployeeId,
AdministratorId = model.AdministratorId,
InspectionName = model.InspectionName,
Cars = model.InspectionCars.Select(x => new InspectionCar
{
Car = context.Cars.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(InspectionBindingModel model)
{
EmployeeId = model.EmployeeId;
InspectionName = model.InspectionName;
InspectionDate = model.InspectionDate;
}
public InspectionViewModel GetViewModel => new()
{
Id = Id,
EmployeeId = EmployeeId,
AdministratorId = AdministratorId,
InspectionName = InspectionName,
InspectionDate = InspectionDate,
InspectionCars = InspectionCars,
EmplFIO = Employee?.EmployeeFIO
};
public void UpdateCars(CarCenterDataBase context, InspectionBindingModel model)
{
var inspectionCars = context.InspectionCars.Where(rec => rec.InspectionId == model.Id).ToList();
if (inspectionCars != null && inspectionCars.Any())
{
context.InspectionCars.RemoveRange(inspectionCars.Where(rec => !model.InspectionCars.ContainsKey(rec.CarId)));
context.SaveChanges();
foreach (var updateCar in inspectionCars)
{
model.InspectionCars.Remove(updateCar.CarId);
}
context.SaveChanges();
}
var inspection = context.Inspections.First(x => x.Id == Id);
foreach (var ic in model.InspectionCars)
{
context.InspectionCars.Add(new InspectionCar
{
Inspection = inspection,
Car = context.Cars.First(x => x.Id == ic.Key)
});
context.SaveChanges();
}
_inspectionCars = null;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterDataBaseImplement.Models
{
public class InspectionCar
{
public int Id { get; set; }
public int InspectionId { get; set; }
public int CarId { get; set; }
[Required]
public virtual Inspection Inspection { get; set; }
public virtual Car Car { get; set; }
}
}