Upload files to 'STODataBaseImplement'
This commit is contained in:
parent
2ae45ed3cd
commit
6855bcd115
34
STODataBaseImplement/STODatabase.cs
Normal file
34
STODataBaseImplement/STODatabase.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.SqlServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DatabaseImplement;
|
||||
|
||||
namespace STODatabaseImplement
|
||||
{
|
||||
public class STODatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=STODataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Client> Clients { set; get; }
|
||||
public virtual DbSet<TechnicalWork> TechnicalWorks { set; get; }
|
||||
public virtual DbSet<Car> Cars { set; get; }
|
||||
public virtual DbSet<Service> Services { set; get; }
|
||||
public virtual DbSet<CarPart> CarParts { set; get; }
|
||||
public virtual DbSet<Work> Works { set; get; }
|
||||
|
||||
//public virtual DbSet<CarPartCar> CarPartCar { set; get; }
|
||||
public virtual DbSet<CarPartWork> CarPartWork { set; get; }
|
||||
public virtual DbSet<CarTechnicalWork> CarTechnicalWork { set; get; }
|
||||
public virtual DbSet<WorkTechnicalWork> WorkTechnicalWork { set; get; }
|
||||
}
|
||||
}
|
86
STODataBaseImplement/ServiceStorage.cs
Normal file
86
STODataBaseImplement/ServiceStorage.cs
Normal file
@ -0,0 +1,86 @@
|
||||
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 ServiceStorage : IServiceStorage
|
||||
{
|
||||
public List<ServiceViewModel> GetFullList()
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
return context.Services.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.Services.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public ServiceViewModel? GetElement(ServiceSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.Services.FirstOrDefault(x =>
|
||||
(x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public ServiceViewModel? Insert(ServiceBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
if (model == null)
|
||||
return null;
|
||||
var newService = Service.Create(context, model);
|
||||
if (newService == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Services.Add(newService);
|
||||
context.SaveChanges();
|
||||
return newService.GetViewModel;
|
||||
}
|
||||
|
||||
public ServiceViewModel? Update(ServiceBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var component = context.Services.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
|
||||
public ServiceViewModel? Delete(ServiceBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var element = context.Services.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Services.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
114
STODataBaseImplement/TechnicalWork.cs
Normal file
114
STODataBaseImplement/TechnicalWork.cs
Normal file
@ -0,0 +1,114 @@
|
||||
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.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class TechnicalWork: ITechnicalWorkModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
public string Description { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public DateTime Date { get; private set; } = DateTime.MinValue;
|
||||
|
||||
public Dictionary<int, (IWorkModel, int)> _Works = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IWorkModel, int)> TechnicalworkWorks
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_Works == null)
|
||||
{
|
||||
_Works = Works
|
||||
.ToDictionary(recPC => recPC.Id, recPC =>
|
||||
(recPC.Work as IWorkModel, recPC.Count));
|
||||
}
|
||||
return _Works;
|
||||
}
|
||||
}
|
||||
[ForeignKey("TechnicalWorkId")]
|
||||
public virtual List<WorkTechnicalWork> Works { get; set; } = new();
|
||||
|
||||
|
||||
public static TechnicalWork? Create(STODatabase context, TechnicalWorkBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new TechnicalWork()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Description = model.Description,
|
||||
Date = model.Date,
|
||||
Works = model.TechnicalworkWorks.Select(x => new
|
||||
WorkTechnicalWork
|
||||
{
|
||||
Work = context.Works.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(TechnicalWorkBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Id = model.Id;
|
||||
Name = model.Name;
|
||||
Description = model.Description;
|
||||
Date = model.Date;
|
||||
}
|
||||
public TechnicalWorkViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Description = Description,
|
||||
Date = Date
|
||||
};
|
||||
|
||||
public void UpdateWorks(STODatabase context, TechnicalWorkBindingModel model)
|
||||
{
|
||||
var works = context.WorkTechnicalWork.Where(rec =>
|
||||
rec.TechnicalWorkId == model.Id).ToList();
|
||||
if (works != null && TechnicalworkWorks.Count > 0)
|
||||
{
|
||||
context.WorkTechnicalWork.RemoveRange(works.Where(rec
|
||||
=> !model.TechnicalworkWorks.ContainsKey(rec.WorkId)));
|
||||
context.SaveChanges();
|
||||
foreach (var updateComponent in works)
|
||||
{
|
||||
updateComponent.Count =
|
||||
model.TechnicalworkWorks[updateComponent.WorkId].Item2;
|
||||
model.TechnicalworkWorks.Remove(updateComponent.WorkId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var manufacture = context.TechnicalWorks.First(x => x.Id == Id);
|
||||
foreach (var pc in model.TechnicalworkWorks)
|
||||
{
|
||||
context.WorkTechnicalWork.Add(new WorkTechnicalWork
|
||||
{
|
||||
TechnicalWork = manufacture,
|
||||
Work = context.Works.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_Works = null;
|
||||
}
|
||||
}
|
||||
}
|
110
STODataBaseImplement/TechnicalWorkStorage.cs
Normal file
110
STODataBaseImplement/TechnicalWorkStorage.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using STOContracts.BindingModels;
|
||||
using STOContracts.SearchModels;
|
||||
using STOContracts.StoragesContracts;
|
||||
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 TechnicalWorkStorage : ITechnicalWorkStorage
|
||||
{
|
||||
public List<TechnicalWorkViewModel> GetFullList()
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
return context.TechnicalWorks
|
||||
.Include(x => x.Works)
|
||||
.ThenInclude(x => x.Work)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<TechnicalWorkViewModel> GetFilteredList(TechnicalWorkSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.TechnicalWorks.Include(x => x.Works)
|
||||
.ThenInclude(x => x.Work)
|
||||
.Where(x => x.Name.Contains(model.Name))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public TechnicalWorkViewModel? GetElement(TechnicalWorkSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Name) &&
|
||||
!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.TechnicalWorks
|
||||
.Include(x => x.Works)
|
||||
.ThenInclude(x => x.Work)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) &&
|
||||
x.Name == model.Name) ||
|
||||
(model.Id.HasValue && x.Id ==
|
||||
model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public TechnicalWorkViewModel? Insert(TechnicalWorkBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var newTechnicalWork = TechnicalWork.Create(context, model);
|
||||
if (newTechnicalWork == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.TechnicalWorks.Add(newTechnicalWork);
|
||||
context.SaveChanges();
|
||||
return newTechnicalWork.GetViewModel;
|
||||
}
|
||||
public TechnicalWorkViewModel? Update(TechnicalWorkBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var car = context.TechnicalWorks.FirstOrDefault(rec =>
|
||||
rec.Id == model.Id);
|
||||
if (car == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
car.Update(model);
|
||||
context.SaveChanges();
|
||||
car.UpdateWorks(context, model);
|
||||
transaction.Commit();
|
||||
return car.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public TechnicalWorkViewModel? Delete(TechnicalWorkBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var element = context.TechnicalWorks
|
||||
.Include(x => x.Works)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.TechnicalWorks.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
110
STODataBaseImplement/Work.cs
Normal file
110
STODataBaseImplement/Work.cs
Normal file
@ -0,0 +1,110 @@
|
||||
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;
|
||||
using STODatabaseImplement;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class Work: IWorkModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
|
||||
public int Time_duration { get; private set; } = 0;
|
||||
public Dictionary<int, (ICarPartModel, int)> _CarParts = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (ICarPartModel, int)> CarParts
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_CarParts == null)
|
||||
{
|
||||
_CarParts = CarPartCar
|
||||
.ToDictionary(recPC => recPC.Id, recPC =>
|
||||
(recPC.CarPart as ICarPartModel, recPC.Count));
|
||||
}
|
||||
return _CarParts;
|
||||
}
|
||||
}
|
||||
[ForeignKey("WorkId")]
|
||||
public virtual List<CarPartWork> CarPartCar { get; set; } = new();
|
||||
|
||||
|
||||
public static Work? Create(STODatabase context, WorkBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Work()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Time_duration = model.Time_duration,
|
||||
CarPartCar = model.CarParts.Select(x => new
|
||||
CarPartWork
|
||||
{
|
||||
CarPart = context.CarParts.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(WorkBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Id = model.Id;
|
||||
Name = model.Name;
|
||||
Time_duration = model.Time_duration;
|
||||
}
|
||||
public WorkViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Time_duration = Time_duration
|
||||
};
|
||||
|
||||
public void UpdateCarParts(STODatabase context, WorkBindingModel model)
|
||||
{
|
||||
var parts = context.CarPartWork.Where(rec =>
|
||||
rec.WorkId == model.Id).ToList();
|
||||
if (parts != null && CarParts.Count > 0)
|
||||
{
|
||||
context.CarPartWork.RemoveRange(parts.Where(rec
|
||||
=> !model.CarParts.ContainsKey(rec.CarPartId)));
|
||||
context.SaveChanges();
|
||||
foreach (var updateComponent in parts)
|
||||
{
|
||||
updateComponent.Count =
|
||||
model.CarParts[updateComponent.CarPartId].Item2;
|
||||
model.CarParts.Remove(updateComponent.CarPartId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var manufacture = context.Works.First(x => x.Id == Id);
|
||||
foreach (var pc in model.CarParts)
|
||||
{
|
||||
context.CarPartWork.Add(new CarPartWork
|
||||
{
|
||||
Work = manufacture,
|
||||
CarPart = context.CarParts.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_CarParts = null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user