добавление бд в процессе

This commit is contained in:
Татьяна Артамонова 2023-05-13 15:11:42 +04:00
parent 32ddb232e5
commit 1698a08f96
10 changed files with 322 additions and 1 deletions

View File

@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySalonContracts", "..\
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySalonBusinessLogic", "..\BeautySalonBusinessLogic\BeautySalonBusinessLogic.csproj", "{EC867077-D21F-4D12-9BD5-16244A3D119C}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySalonBusinessLogic", "..\BeautySalonBusinessLogic\BeautySalonBusinessLogic.csproj", "{EC867077-D21F-4D12-9BD5-16244A3D119C}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySalonDatabaseImplement", "..\BeautySalonDatabaseImplement\BeautySalonDatabaseImplement.csproj", "{BD110484-1896-428D-962D-A0EAE2AAD41E}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -33,6 +35,10 @@ Global
{EC867077-D21F-4D12-9BD5-16244A3D119C}.Debug|Any CPU.Build.0 = Debug|Any CPU {EC867077-D21F-4D12-9BD5-16244A3D119C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC867077-D21F-4D12-9BD5-16244A3D119C}.Release|Any CPU.ActiveCfg = Release|Any CPU {EC867077-D21F-4D12-9BD5-16244A3D119C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC867077-D21F-4D12-9BD5-16244A3D119C}.Release|Any CPU.Build.0 = Release|Any CPU {EC867077-D21F-4D12-9BD5-16244A3D119C}.Release|Any CPU.Build.0 = Release|Any CPU
{BD110484-1896-428D-962D-A0EAE2AAD41E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD110484-1896-428D-962D-A0EAE2AAD41E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD110484-1896-428D-962D-A0EAE2AAD41E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD110484-1896-428D-962D-A0EAE2AAD41E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -0,0 +1,22 @@
using BeautySalonDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BeautySalonDatabaseImplement
{
public class BeautySalonDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-IHH1ICP\SQLEXPRESS;Initial Catalog=BeautySalonDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Master> Masters { set; get; }
public virtual DbSet<Service> Services { set; get; }
public virtual DbSet<MasterService> MasterServices { set; get; }
public virtual DbSet<Visit> Visits { set; get; }
public virtual DbSet<Client> Clients { set; get; }
}
}

View File

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BeautySalonDataModels\BeautySalonDataModels.csproj" />
<ProjectReference Include="..\BeauySalonContracts\BeautySalonContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,53 @@
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.ViewModels;
using BeautySalonDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace BeautySalonDatabaseImplement.Models
{
public class Client : IClientModel
{
public int Id { get; private set; }
[Required]
public string ClientFIO { get; private set; } = string.Empty;
[Required]
public string PhoneNumber { get; private set; } = string.Empty;
public static Client? Create(ClientBindingModel model)
{
if (model == null)
{
return null;
}
return new Client()
{
Id = model.Id,
ClientFIO = model.ClientFIO,
PhoneNumber = model.PhoneNumber
};
}
public static Client Create(ClientViewModel model)
{
return new Client
{
Id = model.Id,
ClientFIO = model.ClientFIO,
PhoneNumber = model.PhoneNumber
};
}
public void Update(ClientBindingModel model)
{
if (model == null)
{
return;
}
ClientFIO = model.ClientFIO;
PhoneNumber = model.PhoneNumber;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
ClientFIO = ClientFIO,
PhoneNumber = PhoneNumber
};
}
}

View File

@ -0,0 +1,92 @@
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.ViewModels;
using BeautySalonDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BeautySalonDatabaseImplement.Models
{
public class Master : IMasterModel
{
public int Id { get; private set; }
[Required]
public string MasterFIO { get; set; } = string.Empty;
[Required]
public string Specialization { get; set; } = string.Empty;
private Dictionary<int, (IServiceModel, int)>? _masterServices = null;
[NotMapped]
public Dictionary<int, (IServiceModel, int)> MasterServices
{
get
{
if (_masterServices == null)
{
_masterServices = Services
.ToDictionary(recPC => recPC.MasterId, recPC =>
(recPC.Service as IServiceModel, recPC.Count));
}
return _masterServices;
}
}
[ForeignKey("MasterId")]
public virtual List<MasterService> Services { get; set; } = new();
[ForeignKey("MasterId")]
public virtual List<Visit> Visits { get; set; } = new();
public static Master Create(BeautySalonDatabase context, MasterBindingModel model)
{
return new Master
{
Id = model.Id,
MasterFIO = model.MasterFIO,
Specialization = model.Specialization,
Services = model.MasterServices.Select(x => new MasterService
{
Service = context.Services.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(MasterBindingModel model)
{
MasterFIO = model.MasterFIO;
Specialization = model.Specialization;
}
public MasterViewModel GetViewModel => new()
{
Id = Id,
MasterFIO = MasterFIO,
Specialization = Specialization,
MasterServices = MasterServices
};
public void UpdateServices(BeautySalonDatabase context, MasterBindingModel model)
{
var masterServices = context.MasterServices.Where(rec => rec.MasterId == model.Id).ToList();
if (masterServices != null && masterServices.Count > 0)
{ // удалили те, которых нет в модели
context.MasterServices.RemoveRange(masterServices.Where(rec
=> !model.MasterServices.ContainsKey(rec.ServiceId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateService in masterServices)
{
updateService.Count =
model.MasterServices[updateService.ServiceId].Item2;
model.MasterServices.Remove(updateService.ServiceId);
}
context.SaveChanges();
}
var master = context.Masters.First(x => x.Id == Id);
foreach (var pc in model.MasterServices)
{
context.MasterServices.Add(new MasterService
{
Master = master,
Service = context.Services.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_masterServices = null;
}
}
}

View File

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace BeautySalonDatabaseImplement.Models
{
public class MasterService
{
public int Id { get; set; }
[Required]
public int MasterId { get; set; }
[Required]
public int ServiceId { get; set; }
[Required]
public int Count { get; set; }
public virtual Service Service { get; set; } = new();
public virtual Master Master { get; set; } = new();
}
}

View File

@ -0,0 +1,56 @@
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.ViewModels;
using BeautySalonDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BeautySalonDatabaseImplement.Models
{
public class Service : IServiceModel
{
public int Id { get; private set; }
[Required]
public string ServiceName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ServiceId")]
public virtual List<MasterService> MasterServices { get; set; } = new();
public static Service? Create(ServiceBindingModel model)
{
if (model == null)
{
return null;
}
return new Service()
{
Id = model.Id,
ServiceName = model.ServiceName,
Cost = model.Cost
};
}
public static Service Create(ServiceViewModel model)
{
return new Service
{
Id = model.Id,
ServiceName = model.ServiceName,
Cost = model.Cost
};
}
public void Update(ServiceBindingModel model)
{
if (model == null)
{
return;
}
ServiceName = model.ServiceName;
Cost = model.Cost;
}
public ServiceViewModel GetViewModel => new()
{
Id = Id,
ServiceName = ServiceName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,49 @@
using BeautySalonContracts.BindingModels;
using BeautySalonDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace BeautySalonDatabaseImplement.Models
{
public class Visit : IVisitModel
{
public int Id { get; private set; }
[Required]
public DateTime DateOfVisit { get; private set; }
[Required]
public int ClientId { get; private set; }
public int MasterId { get; private set; }
public int ServiceId { get; private set; }
public string ClientFIO { get; set; } = string.Empty;
public string MasterFIO { get; set; } = string.Empty;
public string ServiceName { get; set; } = string.Empty;
[Required]
public double Sum { get; private set; }
[Required]
public int Count { get; private set; }
public virtual Client Client { get; set; }
public virtual Master Master { get; set; }
public virtual Service Service { get; set; }
public static Visit? Create(VisitBindingModel? model)
{
if (model == null)
{
return null;
}
return new Visit()
{
Id = model.Id,
MasterId = model.MasterId,
ClientId = model.ClientId,
ServiceId = model.ServiceId,
MasterFIO = model.MasterFIO,
ClientFIO = model.ClientFIO,
ServiceName = model.ServiceName,
Count = model.Count,
Sum = model.Sum,
DateOfVisit = model.DateOfVisit
};
}
}
}

View File

@ -6,6 +6,7 @@ namespace BeautySalonContracts.BindingModels
{ {
public int Id { get; set; } public int Id { get; set; }
public string MasterFIO { get; set; } = string.Empty; public string MasterFIO { get; set; } = string.Empty;
public string Specialization { get; set; } = string.Empty; public string Specialization { get; set; } = string.Empty;
public Dictionary<int, (IServiceModel, int)> MasterServices { get; set; } = new();
} }
} }

View File

@ -9,6 +9,9 @@ namespace BeautySalonContracts.BindingModels
public int ClientId { get; set; } public int ClientId { get; set; }
public int MasterId { get; set; } public int MasterId { get; set; }
public int ServiceId { get; set; } public int ServiceId { get; set; }
public string ClientFIO { get; set; } = string.Empty;
public string MasterFIO { get; set; } = string.Empty;
public string ServiceName { get; set; } = string.Empty;
public double Sum { get; set; } public double Sum { get; set; }
public int Count { get; set; } public int Count { get; set; }
} }