Commit 2f7b996d authored by hasan khaddour's avatar hasan khaddour

fix s

parent 2ce70ae7
...@@ -15,15 +15,15 @@ namespace Infrastructure.Data ...@@ -15,15 +15,15 @@ namespace Infrastructure.Data
public MedicDbContext(DbContextOptions<MedicDbContext> options) public MedicDbContext(DbContextOptions<MedicDbContext> options)
: base(options) : base(options)
{ {
} }
public DbSet<Patient> Patients { get; set; } public DbSet<Patient> Patients { get; set; }
public DbSet<Medicine> Medicines { get; set; } public DbSet<Medicine> Medicines { get; set; }
public DbSet<Ingredient> Ingredients { get; set; } public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<Category> Categories { get; set; } public DbSet<Category> Categories { get; set; }
public DbSet<MedicineType> MedicineTypes { get; set; } public DbSet<MedicineType> MedicineTypes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
optionsBuilder.UseSqlServer("Data Source=DESKTOP-TI6EF1L\\SQLEXPRESS;Initial Catalog=portfoilo;Integrated Security=True"); optionsBuilder.UseSqlServer("Data Source=DESKTOP-TI6EF1L\\SQLEXPRESS;Initial Catalog=portfoilo;Integrated Security=True");
...@@ -70,44 +70,115 @@ namespace Infrastructure.Data ...@@ -70,44 +70,115 @@ namespace Infrastructure.Data
.HasOne(o => o.MedicineType); .HasOne(o => o.MedicineType);
modelBuilder.Entity<User>().Property(e => e.Id).HasMaxLength(200); modelBuilder.Entity<User>().Property(e => e.Id).HasMaxLength(200);
modelBuilder.Entity<IdentityRole>().Property(e => e.Id).HasMaxLength(200); modelBuilder.Entity<IdentityRole>().Property(e => e.Id).HasMaxLength(200);
// Seed(modelBuilder);
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
Seed(modelBuilder);
} }
public void Seed(ModelBuilder modelBuilder) { public void Seed(ModelBuilder modelBuilder) {
var P = new Patient #region Roles
modelBuilder.Entity<IdentityRole>().HasData(
new IdentityRole
{
Name = "Admin",
NormalizedName = "Admin",
Id = "1-2-1"
},
new IdentityRole
{
Name = "patient",
NormalizedName = "patient",
Id = "1"
}
);
modelBuilder.Entity<IdentityUserRole<String>>()
.HasData( new IdentityUserRole<String>
{
UserId = "1",
RoleId = "1-2-1"
}, new IdentityUserRole<String>
{
UserId = "2",
RoleId = "1"
}
);
#endregion Roles
#region User
PasswordHasher<User> ph = new PasswordHasher<User>();
var admin = new User
{
Id = "1",
FirstName = "Hasan",
LastName = "Kh",
Avatar = "avatar.jpg",
Email = "hasan@b",
UserName="Hasan.Bahjat",
NormalizedEmail="hasan@b",
NormalizedUserName= "Hasan.Bahjat",
CreationTime = DateTime.Now
};
var PatientAccount = new User
{ {
Id = 1, Id = "2",
FirstName = "Hasasn", FirstName = "Hasan",
LastName = "Khaddour", LastName = "Khaddour",
Avatar = "avatr.png" Avatar = "avatar1.jpg",
Email = "hasan.bahjat@mail.y",
UserName = "Hasan.Khaddour",
NormalizedEmail = "hasan@b",
NormalizedUserName = "Hasan.khaddour",
CreationTime = DateTime.Now
}; };
modelBuilder.Entity<Patient>().HasData(P);
var appUser = new User
{
Id = "123-1213",
Email = "hasan@b",
EmailConfirmed = true,
UserName = "frankofoedu@gmail.com",
NormalizedUserName = "FRANKOFOEDU@GMAIL.COM",
PatientId = 1
admin.PasswordHash = ph.HashPassword(admin, "123@Aa");
PatientAccount.PasswordHash = ph.HashPassword(PatientAccount, "123@Aa");
modelBuilder.Entity<User>()
.HasData(
admin,
PatientAccount
);
#endregion User
#region Patients
var Patient = new Patient {
Id=1,
BIO = "a Patient ",
UserId = PatientAccount.Id,
}; };
//set user password modelBuilder.Entity<Patient>().HasData(
//appUser.Patient = P; Patient
PasswordHasher<User> ph = new PasswordHasher<User>(); );
appUser.PasswordHash = ph.HashPassword(appUser, "123@Aa");
//seed user #endregion Patients
modelBuilder.Entity<User>().HasData(appUser); #region Medicines
var med = new Medicine
{
Id=-1,
Name = "Augmentine",
Image="med1.png",
Dosage = 12,
Price = 2500,
};
var c = new Category { Id = 1, Name = "Augmentine" };
modelBuilder.Entity<Category>().HasData(c);
// med.Category = c;
modelBuilder.Entity<Medicine>().HasData(med);
#endregion Medicines
} }
} }
} }
...@@ -21,4 +21,8 @@ ...@@ -21,4 +21,8 @@
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" /> <ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project> </Project>
using Microsoft.EntityFrameworkCore.Migrations;
namespace Infrastructure.Migrations
{
public partial class addMedicineImage : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Image",
table: "Medicines",
type: "nvarchar(max)",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Image",
table: "Medicines");
}
}
}
...@@ -33,6 +33,13 @@ namespace Infrastructure.Migrations ...@@ -33,6 +33,13 @@ namespace Infrastructure.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Categories"); b.ToTable("Categories");
b.HasData(
new
{
Id = 1,
Name = "Augmentine"
});
}); });
modelBuilder.Entity("ApplicationCore.Entities.Ingredient", b => modelBuilder.Entity("ApplicationCore.Entities.Ingredient", b =>
...@@ -88,6 +95,16 @@ namespace Infrastructure.Migrations ...@@ -88,6 +95,16 @@ namespace Infrastructure.Migrations
b.HasIndex("MedicineTypeId"); b.HasIndex("MedicineTypeId");
b.ToTable("Medicines"); b.ToTable("Medicines");
b.HasData(
new
{
Id = -1,
Dosage = 12,
Image = "med1.png",
Name = "Augmentine",
Price = 2500
});
}); });
modelBuilder.Entity("ApplicationCore.Entities.MedicineIngredient", b => modelBuilder.Entity("ApplicationCore.Entities.MedicineIngredient", b =>
...@@ -137,21 +154,27 @@ namespace Infrastructure.Migrations ...@@ -137,21 +154,27 @@ namespace Infrastructure.Migrations
.HasColumnType("int") .HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Avatar")
.HasColumnType("nvarchar(max)");
b.Property<string>("BIO") b.Property<string>("BIO")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.Property<string>("FirstName") b.Property<string>("UserId")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(200)");
b.Property<string>("LastName")
.HasColumnType("nvarchar(max)");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("UserId")
.IsUnique()
.HasFilter("[UserId] IS NOT NULL");
b.ToTable("Patients"); b.ToTable("Patients");
b.HasData(
new
{
Id = 1,
BIO = "a Patient ",
UserId = "2"
});
}); });
modelBuilder.Entity("ApplicationCore.Entities.PatientMedicine", b => modelBuilder.Entity("ApplicationCore.Entities.PatientMedicine", b =>
...@@ -188,6 +211,9 @@ namespace Infrastructure.Migrations ...@@ -188,6 +211,9 @@ namespace Infrastructure.Migrations
b.Property<int>("AccessFailedCount") b.Property<int>("AccessFailedCount")
.HasColumnType("int"); .HasColumnType("int");
b.Property<string>("Avatar")
.HasColumnType("nvarchar(max)");
b.Property<string>("ConcurrencyStamp") b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken() .IsConcurrencyToken()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
...@@ -202,6 +228,12 @@ namespace Infrastructure.Migrations ...@@ -202,6 +228,12 @@ namespace Infrastructure.Migrations
b.Property<bool>("EmailConfirmed") b.Property<bool>("EmailConfirmed")
.HasColumnType("bit"); .HasColumnType("bit");
b.Property<string>("FirstName")
.HasColumnType("nvarchar(max)");
b.Property<string>("LastName")
.HasColumnType("nvarchar(max)");
b.Property<bool>("LockoutEnabled") b.Property<bool>("LockoutEnabled")
.HasColumnType("bit"); .HasColumnType("bit");
...@@ -219,9 +251,6 @@ namespace Infrastructure.Migrations ...@@ -219,9 +251,6 @@ namespace Infrastructure.Migrations
b.Property<string>("PasswordHash") b.Property<string>("PasswordHash")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.Property<int>("PatientId")
.HasColumnType("int");
b.Property<string>("PhoneNumber") b.Property<string>("PhoneNumber")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
...@@ -248,10 +277,49 @@ namespace Infrastructure.Migrations ...@@ -248,10 +277,49 @@ namespace Infrastructure.Migrations
.HasDatabaseName("UserNameIndex") .HasDatabaseName("UserNameIndex")
.HasFilter("[NormalizedUserName] IS NOT NULL"); .HasFilter("[NormalizedUserName] IS NOT NULL");
b.HasIndex("PatientId")
.IsUnique();
b.ToTable("AspNetUsers"); b.ToTable("AspNetUsers");
b.HasData(
new
{
Id = "1",
AccessFailedCount = 0,
Avatar = "avatar.jpg",
ConcurrencyStamp = "9122e783-9528-4e0a-b15d-82345f6f0418",
CreationTime = new DateTime(2024, 5, 31, 17, 38, 21, 491, DateTimeKind.Local).AddTicks(4784),
Email = "hasan@b",
EmailConfirmed = false,
FirstName = "Hasan",
LastName = "Kh",
LockoutEnabled = false,
NormalizedEmail = "hasan@b",
NormalizedUserName = "Hasan.Bahjat",
PasswordHash = "AQAAAAEAACcQAAAAEJwazDmiC7dJqkx0ZQHpN6lKVdLp1MlBxIx4e5ZcqF+gkiJTfUb/OJOI6LYXXw8o2A==",
PhoneNumberConfirmed = false,
SecurityStamp = "3fe96d52-7dbf-46c8-b9ff-2bf6819fbeac",
TwoFactorEnabled = false,
UserName = "Hasan.Bahjat"
},
new
{
Id = "2",
AccessFailedCount = 0,
Avatar = "avatar1.jpg",
ConcurrencyStamp = "6ef31db3-4cc6-40c1-9082-3dca1d055c9a",
CreationTime = new DateTime(2024, 5, 31, 17, 38, 21, 508, DateTimeKind.Local).AddTicks(9726),
Email = "hasan.bahjat@mail.y",
EmailConfirmed = false,
FirstName = "Hasan",
LastName = "Khaddour",
LockoutEnabled = false,
NormalizedEmail = "hasan@b",
NormalizedUserName = "Hasan.khaddour",
PasswordHash = "AQAAAAEAACcQAAAAEDWozHY53JhRnMlp3wyKL2X6zVG8Rgd8AQSP7OHCui5ObaKA6p2dpcg6EVjXzMc99Q==",
PhoneNumberConfirmed = false,
SecurityStamp = "df27679f-e318-49b0-af02-2ebd01b2ef40",
TwoFactorEnabled = false,
UserName = "Hasan.Khaddour"
});
}); });
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
...@@ -280,6 +348,22 @@ namespace Infrastructure.Migrations ...@@ -280,6 +348,22 @@ namespace Infrastructure.Migrations
.HasFilter("[NormalizedName] IS NOT NULL"); .HasFilter("[NormalizedName] IS NOT NULL");
b.ToTable("AspNetRoles"); b.ToTable("AspNetRoles");
b.HasData(
new
{
Id = "1-2-1",
ConcurrencyStamp = "e90561ea-fc91-4e43-9090-6967145f6f5b",
Name = "Admin",
NormalizedName = "Admin"
},
new
{
Id = "1",
ConcurrencyStamp = "28134bcc-f733-40be-8d2e-bface932cc86",
Name = "patient",
NormalizedName = "patient"
});
}); });
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
...@@ -365,6 +449,18 @@ namespace Infrastructure.Migrations ...@@ -365,6 +449,18 @@ namespace Infrastructure.Migrations
b.HasIndex("RoleId"); b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles"); b.ToTable("AspNetUserRoles");
b.HasData(
new
{
UserId = "1",
RoleId = "1-2-1"
},
new
{
UserId = "2",
RoleId = "1"
});
}); });
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
...@@ -420,6 +516,15 @@ namespace Infrastructure.Migrations ...@@ -420,6 +516,15 @@ namespace Infrastructure.Migrations
b.Navigation("Medicine"); b.Navigation("Medicine");
}); });
modelBuilder.Entity("ApplicationCore.Entities.Patient", b =>
{
b.HasOne("ApplicationCore.Entities.User", "User")
.WithOne("Patient")
.HasForeignKey("ApplicationCore.Entities.Patient", "UserId");
b.Navigation("User");
});
modelBuilder.Entity("ApplicationCore.Entities.PatientMedicine", b => modelBuilder.Entity("ApplicationCore.Entities.PatientMedicine", b =>
{ {
b.HasOne("ApplicationCore.Entities.Medicine", "Medicine") b.HasOne("ApplicationCore.Entities.Medicine", "Medicine")
...@@ -439,17 +544,6 @@ namespace Infrastructure.Migrations ...@@ -439,17 +544,6 @@ namespace Infrastructure.Migrations
b.Navigation("Patient"); b.Navigation("Patient");
}); });
modelBuilder.Entity("ApplicationCore.Entities.User", b =>
{
b.HasOne("ApplicationCore.Entities.Patient", "Patient")
.WithOne("User")
.HasForeignKey("ApplicationCore.Entities.User", "PatientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Patient");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{ {
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null)
...@@ -526,8 +620,11 @@ namespace Infrastructure.Migrations ...@@ -526,8 +620,11 @@ namespace Infrastructure.Migrations
modelBuilder.Entity("ApplicationCore.Entities.Patient", b => modelBuilder.Entity("ApplicationCore.Entities.Patient", b =>
{ {
b.Navigation("PatientMedicines"); b.Navigation("PatientMedicines");
});
b.Navigation("User"); modelBuilder.Entity("ApplicationCore.Entities.User", b =>
{
b.Navigation("Patient");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
......
using ApplicationCore.Entities; using ApplicationCore.Entities;
using ApplicationCore.Interfaces; using ApplicationCore.Interfaces;
using ApplicationCore.Interfaces.ISpecification;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
...@@ -25,30 +26,25 @@ namespace Infrastructure.Repository ...@@ -25,30 +26,25 @@ namespace Infrastructure.Repository
_table.Remove(entity); _table.Remove(entity);
} }
public IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includeProperties) public IEnumerable<T> GetAll(ISpecification<T> specification)
{ {
{
IQueryable<T> queryable = _table; IQueryable<T> queryable = _table;
foreach (var includeProperty in includeProperties) queryable = GetQuery(queryable, specification);
{
queryable = queryable.Include(includeProperty);
}
return queryable.AsEnumerable(); return queryable.AsEnumerable();
}
} }
public T GetById(int id, params Expression<Func<T, object>>[] includeProperties) public T GetById(int id, ISpecification<T>? specification)
{ {
IQueryable<T> query = _table; IQueryable<T> query = _table;
if (includeProperties is not null) if(specification != null )
foreach (var includeProperty in includeProperties) query = GetQuery(query, specification);
{
query = query.Include(includeProperty); return query.Where(p => p.Id == id).FirstOrDefault();
}
return query.Where(p => p.Id == id).First();
} }
public T Insert(T entity) public T Insert(T entity)
...@@ -63,5 +59,35 @@ namespace Infrastructure.Repository ...@@ -63,5 +59,35 @@ namespace Infrastructure.Repository
return e; return e;
} }
public IQueryable<T> GetQuery(IQueryable<T> inputQuery, ISpecification<T> specification)
{
IQueryable<T> query = inputQuery;
// modify the IQueryable using the specification's criteria expression
if (specification.Criteria != null)
{
query = query.Where(specification.Criteria);
}
// Includes all expression-based includes
query = specification.Includes.Aggregate(query,
(current, include) => current.Include(include));
// Include any string-based include statements
query = specification.ThenInclude.Aggregate(query,
(current, include) => current.Include(include));
// Apply ordering if expressions are set
if (specification.OrderBy != null)
{
query = query.OrderBy(specification.OrderBy);
}
else if (specification.OrderByDescending != null)
{
query = query.OrderByDescending(specification.OrderByDescending);
}
return query;
}
} }
} }
16668239acb0b8ba1b3fb234af1679c89ab8ecd0 86b4a36b27ca89275e50461b3805a94292ff7dbf
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