Commit a7ea84d5 authored by Almouhannad's avatar Almouhannad

(B) Add db seed

parent ec58ab00
using API.Options.Database; using API.Options.Database;
using API.SeedDatabaseHelper;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Persistence.Context; using Persistence.Context;
...@@ -61,6 +62,10 @@ builder.Services.AddSwaggerGen(); ...@@ -61,6 +62,10 @@ builder.Services.AddSwaggerGen();
var app = builder.Build(); var app = builder.Build();
#region Seed database
await SeedHelper.Seed(app);
#endregion
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
......
using Domain.Entities.Medicals.Medicines.MedicineFormValues;
using Domain.Entities.People.Doctors.Shared.Constants.DoctorStatusValues;
using Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues;
using Domain.Entities.People.Shared.GenderValues;
using Persistence.SeedDatabase;
namespace API.SeedDatabaseHelper;
public class SeedHelper
{
public static async Task Seed(IApplicationBuilder applicationBuilder)
{
using (var serviceScope = applicationBuilder.ApplicationServices.CreateScope())
{
var seedGenders = serviceScope.ServiceProvider.GetRequiredService<ISeed<Gender>>();
await seedGenders.Seed();
var seedDoctorStatuses = serviceScope.ServiceProvider.GetRequiredService<ISeed<DoctorStatus>>();
await seedDoctorStatuses.Seed();
var seedFamilyRoles = serviceScope.ServiceProvider.GetRequiredService<ISeed<FamilyRole>>();
await seedFamilyRoles.Seed();
var seedMedicineForms = serviceScope.ServiceProvider.GetRequiredService<ISeed<MedicineForm>>();
await seedMedicineForms.Seed();
}
}
}
...@@ -6,6 +6,7 @@ public static class MedicineForms ...@@ -6,6 +6,7 @@ public static class MedicineForms
{ {
#region Constant values #region Constant values
public static int Count => 2;
public static MedicineForm Tablet public static MedicineForm Tablet
{ {
get get
......
using Domain.Entities.People.Doctors.Shared; using Domain.Entities.People.Doctors.Shared;
using Domain.Entities.People.Doctors.Shared.Constants.DoctorStatusValues; using Domain.Entities.People.Doctors.Shared.Constants.DoctorStatusValues;
using Domain.Entities.People.Doctors.Shared.DoctorStatusValues;
using Domain.Entities.People.Shared; using Domain.Entities.People.Shared;
using Domain.Primitives; using Domain.Primitives;
using Domain.Shared; using Domain.Shared;
......
using Domain.Exceptions.InvalidValue; using Domain.Entities.People.Doctors.Shared.Constants.DoctorStatusValues;
using Domain.Exceptions.InvalidValue;
namespace Domain.Entities.People.Doctors.Shared.Constants.DoctorStatusValues; namespace Domain.Entities.People.Doctors.Shared.DoctorStatusValues;
public static class DoctorStatuses public static class DoctorStatuses
{ {
#region Constant values #region Constant values
public static int Count => 3;
public static DoctorStatus Available public static DoctorStatus Available
{ {
get get
...@@ -21,7 +24,7 @@ public static class DoctorStatuses ...@@ -21,7 +24,7 @@ public static class DoctorStatuses
{ {
get get
{ {
var result = DoctorStatus.Create("لديه مريض", 1); var result = DoctorStatus.Create("لديه مريض", 2);
if (result.IsFailure) if (result.IsFailure)
throw new InvalidValuesDomainException<DoctorStatus>(); throw new InvalidValuesDomainException<DoctorStatus>();
return result.Value; return result.Value;
...@@ -32,7 +35,7 @@ public static class DoctorStatuses ...@@ -32,7 +35,7 @@ public static class DoctorStatuses
{ {
get get
{ {
var result = DoctorStatus.Create("مشغول", 1); var result = DoctorStatus.Create("مشغول", 3);
if (result.IsFailure) if (result.IsFailure)
throw new InvalidValuesDomainException<DoctorStatus>(); throw new InvalidValuesDomainException<DoctorStatus>();
return result.Value; return result.Value;
......
...@@ -6,6 +6,7 @@ public static class FamilyRoles ...@@ -6,6 +6,7 @@ public static class FamilyRoles
{ {
#region Constant id values #region Constant id values
public static int Count => 4;
public static FamilyRole Husband public static FamilyRole Husband
{ {
get get
......
...@@ -6,6 +6,8 @@ public static class Genders ...@@ -6,6 +6,8 @@ public static class Genders
{ {
#region Constant values #region Constant values
public static int Count => 2;
public static Gender Male public static Gender Male
{ {
get get
......
using Domain.Entities.People.Doctors.Shared.Constants.DoctorStatusValues;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
namespace Persistence.SeedDatabase.DoctorStatuses;
public class SeedDoctorStatuses : ISeed<DoctorStatus>
{
#region Ctor DI
private readonly ClinicsDbContext _clinicsContext;
public SeedDoctorStatuses(ClinicsDbContext clinicsContext)
{
_clinicsContext = clinicsContext;
}
#endregion
public async Task Seed()
{
DbSet<DoctorStatus> doctorStatuses = _clinicsContext.Set<DoctorStatus>();
if (doctorStatuses.ToList().Count !=
Domain.Entities.People.Doctors.Shared.DoctorStatusValues.DoctorStatuses.Count)
{
doctorStatuses.RemoveRange(doctorStatuses.ToList());
doctorStatuses.Add
(Domain.Entities.People.Doctors.Shared.DoctorStatusValues.DoctorStatuses.Available);
doctorStatuses.Add
(Domain.Entities.People.Doctors.Shared.DoctorStatusValues.DoctorStatuses.Busy);
doctorStatuses.Add
(Domain.Entities.People.Doctors.Shared.DoctorStatusValues.DoctorStatuses.Working);
await _clinicsContext.SaveChangesAsync();
}
}
}
using Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
namespace Persistence.SeedDatabase.FamilyRoles;
public class SeedFamilyRoles : ISeed<FamilyRole>
{
#region Ctor DI
private readonly ClinicsDbContext _clinicsContext;
public SeedFamilyRoles(ClinicsDbContext clinicsContext)
{
_clinicsContext = clinicsContext;
}
#endregion
public async Task Seed()
{
DbSet<FamilyRole> familyRoles = _clinicsContext.Set<FamilyRole>();
if (familyRoles.ToList().Count !=
Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues.FamilyRoles.Count)
{
familyRoles.RemoveRange(familyRoles.ToList());
familyRoles.Add
(Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues.FamilyRoles.Husband);
familyRoles.Add
(Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues.FamilyRoles.Wife);
familyRoles.Add
(Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues.FamilyRoles.Son);
familyRoles.Add
(Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues.FamilyRoles.Daughter);
await _clinicsContext.SaveChangesAsync();
}
}
}
using Domain.Entities.People.Shared.GenderValues;
using Domain.UnitOfWork;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.SeedDatabase;
namespace Persistence.Seed.Genders;
public class SeedGenders : ISeed<Gender>
{
#region Ctor DI
private readonly ClinicsDbContext _clinicsContext;
public SeedGenders(ClinicsDbContext clinicsContext)
{
_clinicsContext = clinicsContext;
}
#endregion
public async Task Seed()
{
DbSet<Gender> Genders = _clinicsContext.Set<Gender>();
if (Genders.ToList().Count !=
Domain.Entities.People.Shared.GenderValues.Genders.Count)
{
Genders.RemoveRange(Genders.ToList());
Genders
.Add(Domain.Entities.People.Shared.GenderValues.Genders.Male);
Genders
.Add(Domain.Entities.People.Shared.GenderValues.Genders.Female);
await _clinicsContext.SaveChangesAsync();
}
}
}
using Domain.Primitives;
namespace Persistence.SeedDatabase;
public interface ISeed<TEntity> where TEntity : Entity
{
public Task Seed();
}
using Domain.Entities.Medicals.Medicines.MedicineFormValues;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
namespace Persistence.SeedDatabase.MedicineForms;
public class SeedMedicineForms : ISeed<MedicineForm>
{
#region Ctor DI
private readonly ClinicsDbContext _clinicsContext;
public SeedMedicineForms(ClinicsDbContext clinicsContext)
{
_clinicsContext = clinicsContext;
}
#endregion
public async Task Seed()
{
DbSet<MedicineForm> medicineForms = _clinicsContext.Set<MedicineForm>();
if (medicineForms.ToList().Count !=
Domain.Entities.Medicals.Medicines.MedicineFormValues.MedicineForms.Count)
{
medicineForms.RemoveRange(medicineForms.ToList());
medicineForms
.Add(Domain.Entities.Medicals.Medicines.MedicineFormValues.MedicineForms.Tablet);
medicineForms
.Add(Domain.Entities.Medicals.Medicines.MedicineFormValues.MedicineForms.Syrup);
await _clinicsContext.SaveChangesAsync();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment