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

add spcification

parent 68b48a2c
...@@ -4,13 +4,6 @@ ...@@ -4,13 +4,6 @@
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="Interfaces\IServices\" />
<Folder Include="Services\Ingredient\" />
<Folder Include="Specification\" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.17" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
......
...@@ -13,5 +13,6 @@ namespace ApplicationCore.Entities ...@@ -13,5 +13,6 @@ namespace ApplicationCore.Entities
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; } public int Id { get; set; }
} }
} }
...@@ -5,8 +5,10 @@ namespace ApplicationCore.Entities ...@@ -5,8 +5,10 @@ namespace ApplicationCore.Entities
public class User :IdentityUser public class User :IdentityUser
{ {
public DateTime CreationTime { get; set; } public DateTime CreationTime { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
public String Avatar { get; set; }
public int PatientId { get; set; }
public Patient Patient { get; set; } public Patient Patient { get; set; }
} }
} }
...@@ -8,9 +8,9 @@ namespace ApplicationCore.Entities ...@@ -8,9 +8,9 @@ namespace ApplicationCore.Entities
{ {
public class Patient : EntityBase public class Patient : EntityBase
{ {
public String FirstName { get; set; }
public String LastName { get; set; }
public String Avatar { get; set; } public String UserId { get; set; }
public User User { get; set; } public User User { get; set; }
public String BIO { get; set; } public String BIO { get; set; }
public ICollection<Medicine> Medicines { get; set; } public ICollection<Medicine> Medicines { get; set; }
......
using System; using ApplicationCore.Interfaces.ISpecification;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
...@@ -12,9 +13,9 @@ namespace ApplicationCore.Interfaces ...@@ -12,9 +13,9 @@ namespace ApplicationCore.Interfaces
public T Update(T entities); public T Update(T entities);
public T Insert(T entities); public T Insert(T entities);
public T GetById(int id, params Expression<Func<T, object>>[] includeProperties); public T GetById(int id, ISpecification<T>? specification = null );
public void Delete(int id); public void Delete(int id);
public IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includeProperties); public IEnumerable<T> GetAll(ISpecification<T> specification);
} }
} }
using ApplicationCore.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Interfaces.IServices
{
public interface IIngredientService
{
public IEnumerable<Ingredient> GetAllIngredients();
public void AddIngredient(Ingredient ingredient);
public Ingredient Update(Ingredient ingredient);
public Ingredient GetIngredientDetails(int id);
public void Delete(int id);
}
}
using ApplicationCore.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Interfaces.IServices
{
public interface IMedicineService
{
public IEnumerable<Medicine> GetAllMedicines();
public void AddMedicine(Medicine medicine);
public void AddMedicineIngredient(int medicineId, Ingredient ingredient);
public Medicine Update(Medicine medicine);
public Medicine GetMedicineDetails(int id);
public Medicine GetMedicineIngredentisDetails(int medicineId);
public void AddIngredient(int medicineId, int ratio, Ingredient ingredient);
public void Delete(int id);
}
}
using ApplicationCore.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Interfaces.IServices
{
public interface IPatientService
{
public IEnumerable<Medicine> GetPatientMedicines(int patientId);
public Medicine GetMedicineDetails(int id, params Expression<Func<Medicine, object>>[] includeProperties);
public IEnumerable<Patient> GetAll(params Expression<Func<Patient, object>>[] includeProperties);
public void AddMedicine(int patientId, Medicine medicine);
public Patient GetById(int id, params Expression<Func<Patient, object>>[] includeProperties);
public void Insert(Patient owner);
public void Update(Patient owner);
public void Delete(int id);
public bool PatientExists(int id);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Interfaces.ISpecification
{
public interface ISpecification<T> where T : class
{
Expression<Func<T, bool>> Criteria { get; }
List<Expression<Func<T, object>>> Includes { get; }
List<String> ThenInclude { get; }
Expression<Func<T, object>> OrderBy { get; }
Expression<Func<T, object>> OrderByDescending { get; }
}
}
using ApplicationCore.Entities; using ApplicationCore.Entities;
using ApplicationCore.Interfaces; using ApplicationCore.Interfaces;
using ApplicationCore.Specification;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -11,15 +12,17 @@ namespace ApplicationCore.Services.IngredientService ...@@ -11,15 +12,17 @@ namespace ApplicationCore.Services.IngredientService
public class IngredientService public class IngredientService
{ {
private readonly IUnitOfWork<Ingredient> _ingredientUnitOfWork; private readonly IUnitOfWork<Ingredient> _ingredientUnitOfWork;
private IngredientSpecification _IngredientSpecification;
public IngredientService(IUnitOfWork<Ingredient> ingredientUnitOfWork) public IngredientService(IUnitOfWork<Ingredient> ingredientUnitOfWork)
{ {
_ingredientUnitOfWork = ingredientUnitOfWork; _ingredientUnitOfWork = ingredientUnitOfWork;
_IngredientSpecification = new IngredientSpecification();
} }
public IEnumerable<Ingredient> GetAllIngredients() public IEnumerable<Ingredient> GetAllIngredients()
{ {
return _ingredientUnitOfWork.Entity.GetAll( return _ingredientUnitOfWork.Entity.GetAll(
p=>p.MedicineIngredients _IngredientSpecification
); );
} }
public void AddIngredient(Ingredient ingredient) public void AddIngredient(Ingredient ingredient)
...@@ -41,9 +44,7 @@ namespace ApplicationCore.Services.IngredientService ...@@ -41,9 +44,7 @@ namespace ApplicationCore.Services.IngredientService
{ {
return _ingredientUnitOfWork.Entity.GetById(id, return _ingredientUnitOfWork.Entity.GetById(id,
i => i.MedicineIngredients, _IngredientSpecification);
i => i.Medicines
);
} }
public void Delete(int id) public void Delete(int id)
......
using ApplicationCore.Entities; using ApplicationCore.Entities;
using ApplicationCore.Interfaces; using ApplicationCore.Interfaces;
using ApplicationCore.Specification;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -11,16 +12,15 @@ namespace ApplicationCore.Services.MedicineService ...@@ -11,16 +12,15 @@ namespace ApplicationCore.Services.MedicineService
public class MedicineService public class MedicineService
{ {
private readonly IUnitOfWork<Medicine> _medicineUnitOfWork; private readonly IUnitOfWork<Medicine> _medicineUnitOfWork;
private MedicineIngredientSpecification _medicineIngredientSpecification;
public MedicineService(IUnitOfWork<Medicine> medicineUnitOfWork) public MedicineService(IUnitOfWork<Medicine> medicineUnitOfWork)
{ {
_medicineUnitOfWork = medicineUnitOfWork; _medicineUnitOfWork = medicineUnitOfWork;
_medicineIngredientSpecification = new MedicineIngredientSpecification();
} }
public IEnumerable<Medicine> GetAllMedicines() { public IEnumerable<Medicine> GetAllMedicines() {
return _medicineUnitOfWork.Entity.GetAll( return _medicineUnitOfWork.Entity.GetAll(
p => p.Category _medicineIngredientSpecification
, p => p.Ingredients
, p => p.Patients
); );
} }
public void AddMedicine(Medicine medicine) { public void AddMedicine(Medicine medicine) {
...@@ -31,7 +31,7 @@ namespace ApplicationCore.Services.MedicineService ...@@ -31,7 +31,7 @@ namespace ApplicationCore.Services.MedicineService
} }
public void AddMedicineIngredient(int medicineId ,Ingredient ingredient ) { public void AddMedicineIngredient(int medicineId ,Ingredient ingredient ) {
var s =_medicineUnitOfWork.Entity.GetById(medicineId, p => p.Ingredients); var s =_medicineUnitOfWork.Entity.GetById(medicineId,_medicineIngredientSpecification);
s.Ingredients.Add(ingredient); s.Ingredients.Add(ingredient);
_medicineUnitOfWork.Entity.Update(s); _medicineUnitOfWork.Entity.Update(s);
_medicineUnitOfWork.Save(); _medicineUnitOfWork.Save();
...@@ -47,19 +47,23 @@ namespace ApplicationCore.Services.MedicineService ...@@ -47,19 +47,23 @@ namespace ApplicationCore.Services.MedicineService
public Medicine GetMedicineDetails(int id) public Medicine GetMedicineDetails(int id)
{ {
return _medicineUnitOfWork.Entity.GetById(id , i => i.MedicineIngredients , i => i.Ingredients,c => c.Category ); return _medicineUnitOfWork.Entity.GetById(id ,_medicineIngredientSpecification );
} }
public Medicine GetMedicineIngredentisDetails(int medicineId) { public Medicine GetMedicineIngredentisDetails(int medicineId) {
return _medicineUnitOfWork.Entity return _medicineUnitOfWork.Entity
.GetById(medicineId , .GetById(medicineId ,
i => i.MedicineIngredients, _medicineIngredientSpecification);
i => i.Ingredients,
c => c.Category);
} }
public void AddIngredient(int medicineId, int ratio ,Ingredient ingredient) { public void AddIngredient(int medicineId, int ratio ,Ingredient ingredient) {
var m = GetMedicineIngredentisDetails(medicineId); var m = GetMedicineIngredentisDetails(medicineId);
if(ingredient.Id!= 0 )
foreach (var i in m.Ingredients) {
if (i.Id.Equals(ingredient.Id))
return;
}
m.AddIngredient(ingredient ,ratio ); m.AddIngredient(ingredient ,ratio );
_medicineUnitOfWork.Entity.Update(m); _medicineUnitOfWork.Entity.Update(m);
_medicineUnitOfWork.Save(); _medicineUnitOfWork.Save();
......
using ApplicationCore.Entities; using ApplicationCore.Entities;
using ApplicationCore.Interfaces; using ApplicationCore.Interfaces;
using ApplicationCore.Interfaces.IServices;
using ApplicationCore.Specification;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -9,44 +11,64 @@ using System.Threading.Tasks; ...@@ -9,44 +11,64 @@ using System.Threading.Tasks;
namespace ApplicationCore.Services.PatientService namespace ApplicationCore.Services.PatientService
{ {
public class PatientService public class PatientService : IPatientService
{ {
private readonly IUnitOfWork<Patient> _patientUnitOfWork; private readonly IUnitOfWork<Patient> _patientUnitOfWork;
private readonly IUnitOfWork<Medicine> _medicineUnitOfWork; private readonly IUnitOfWork<Medicine> _medicineUnitOfWork;
private PatientMedicinesSpecification _patientMedicinesSpecification;
private MedicineIngredientSpecification _medicineIngredientSpecification;
public PatientService(IUnitOfWork<Patient> patientUnitOfWork, IUnitOfWork<Medicine> medicineUnitOfWork) public PatientService(IUnitOfWork<Patient> patientUnitOfWork, IUnitOfWork<Medicine> medicineUnitOfWork)
{ {
_patientUnitOfWork = patientUnitOfWork; _patientUnitOfWork = patientUnitOfWork;
_medicineUnitOfWork = medicineUnitOfWork; _medicineUnitOfWork = medicineUnitOfWork;
_patientMedicinesSpecification = new PatientMedicinesSpecification();
_medicineIngredientSpecification = new MedicineIngredientSpecification();
} }
public IEnumerable<Medicine> GetPatientMedicines(int patientId) { public IEnumerable<Medicine> GetPatientMedicines(int patientId) {
return _patientUnitOfWork.Entity.GetById(patientId, p => p.Medicines).Medicines.AsEnumerable(); return _patientUnitOfWork.Entity
.GetById(
patientId,_patientMedicinesSpecification
).Medicines.AsEnumerable();
} }
public Medicine GetMedicineDetails(int id, params Expression<Func<Medicine, object>>[] includeProperties) public Medicine GetMedicineDetails(int id, params Expression<Func<Medicine, object>>[] includeProperties)
{ {
return _medicineUnitOfWork.Entity.GetById(id,includeProperties); return _medicineUnitOfWork.Entity.GetById(id,_medicineIngredientSpecification);
} }
public IEnumerable<Patient> getAll(params Expression<Func<Patient, object>>[] includeProperties) { public IEnumerable<Patient> GetAll(params Expression<Func<Patient, object>>[] includeProperties) {
return _patientUnitOfWork.Entity.GetAll(includeProperties); return _patientUnitOfWork.Entity.GetAll(_patientMedicinesSpecification);
} }
public void AddMedicine(int patientId, Medicine medicine) { public void AddMedicine(int patientId, Medicine medicine) {
var ptient = _patientUnitOfWork.Entity.GetById(patientId); var ptient = _patientUnitOfWork.Entity.GetById(patientId,_patientMedicinesSpecification);
if (medicine.Id != 0)
ptient.Medicines.Add(medicine); foreach (var i in ptient.Medicines)
{
if (i.Id.Equals(medicine.Id))
return;
}
ptient.PatientMedicines
.Add(new PatientMedicine{
Medicine = medicine,
Patient =ptient ,
PrescripDate=DateTime.Now
});
_patientUnitOfWork.Entity.Update(ptient); _patientUnitOfWork.Entity.Update(ptient);
_patientUnitOfWork.Save(); _patientUnitOfWork.Save();
} }
public Patient getById(int id, params Expression<Func<Patient, object>>[] includeProperties) public Patient GetById(int id, params Expression<Func<Patient, object>>[] includeProperties)
{ {
return _patientUnitOfWork.Entity.GetById(id, includeProperties); return _patientUnitOfWork.Entity.GetById(id, _patientMedicinesSpecification);
} }
public void insert(Patient owner) public void Insert(Patient owner)
{ {
_patientUnitOfWork.Entity.Insert(owner); _patientUnitOfWork.Entity.Insert(owner);
...@@ -54,21 +76,21 @@ namespace ApplicationCore.Services.PatientService ...@@ -54,21 +76,21 @@ namespace ApplicationCore.Services.PatientService
_patientUnitOfWork.Save(); _patientUnitOfWork.Save();
} }
public void update(Patient owner) public void Update(Patient owner)
{ {
_patientUnitOfWork.Entity.Update(owner); _patientUnitOfWork.Entity.Update(owner);
_patientUnitOfWork.Save(); _patientUnitOfWork.Save();
} }
public void delete(int id) public void Delete(int id)
{ {
_patientUnitOfWork.Entity.Delete(id); _patientUnitOfWork.Entity.Delete(id);
_patientUnitOfWork.Save(); _patientUnitOfWork.Save();
} }
public bool ownerExists(int id) public bool PatientExists(int id)
{ {
return _patientUnitOfWork.Entity.GetById(id) is null ? false : true; return _patientUnitOfWork.Entity.GetById(id) is null ? false : true;
} }
......
using ApplicationCore.Interfaces.ISpecification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Specification.BaseSpecification
{
public class BaseSpecification<T> : ISpecification<T> where T :class
{
protected BaseSpecification(Expression<Func<T, bool>> criteria = null )
{
Criteria = criteria;
}
public Expression<Func<T, bool>> Criteria { get; }
public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>();
public List<String> ThenInclude { get; } = new List<String>();
public Expression<Func<T, object>> OrderBy { get; private set; }
public Expression<Func<T, object>> OrderByDescending { get; private set; }
protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
{
Includes.Add(includeExpression);
}
protected virtual void AddThenInclude(String include)
{
ThenInclude.Add(include);
}
protected virtual void ApplyOrderBy(Expression<Func<T, object>> orderByExpression)
{
OrderBy = orderByExpression;
}
protected virtual void ApplyOrderByDescending(Expression<Func<T, object>> orderByDescendingExpression)
{
OrderByDescending = orderByDescendingExpression;
}
}
}
using ApplicationCore.Entities;
using ApplicationCore.Specification.BaseSpecification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Specification
{
public class IngredientSpecification : BaseSpecification<Ingredient>
{
public IngredientSpecification()
{
AddInclude(p => p.MedicineIngredients);
AddInclude(p => p.Medicines);
}
}
}
using ApplicationCore.Entities;
using ApplicationCore.Specification.BaseSpecification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Specification
{
public class MedicineIngredientSpecification : BaseSpecification<Medicine>
{
public MedicineIngredientSpecification( )
{
AddInclude(p => p.MedicineIngredients);
AddInclude(p => p.Category);
AddInclude(p => p.MedicineType);
AddInclude(p => p.Patients);
AddInclude(p => p.Ingredients);
AddThenInclude("MedicineIngredients.Ingredient");
}
}
}
using ApplicationCore.Entities;
using ApplicationCore.Specification.BaseSpecification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Specification
{
public class PatientMedicinesSpecification : BaseSpecification<Patient>
{
public PatientMedicinesSpecification()
{
AddInclude(p => p.Medicines);
AddInclude(p => p.User);
AddInclude(p=> p.PatientMedicines);
}
}
}
...@@ -5,7 +5,7 @@ using System.Linq; ...@@ -5,7 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace cleanArchWebApp.ApplicationCore.ViewModels namespace ApplicationCore.ViewModel
{ {
public class LoginInuptModel public class LoginInuptModel
{ {
...@@ -20,6 +20,6 @@ namespace cleanArchWebApp.ApplicationCore.ViewModels ...@@ -20,6 +20,6 @@ namespace cleanArchWebApp.ApplicationCore.ViewModels
[Display(Name = "Remember me?")] [Display(Name = "Remember me?")]
public bool RememberMe { get; set; } public bool RememberMe { get; set; }
public String ReturnUrl { get; set; }
} }
} }
...@@ -6,7 +6,7 @@ using System.Linq; ...@@ -6,7 +6,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace cleanArchWebApp.ApplicationCore.Aggregate namespace ApplicationCore.ViewModel
{ {
public class RegisterationInputModel public class RegisterationInputModel
{ {
...@@ -14,6 +14,14 @@ namespace cleanArchWebApp.ApplicationCore.Aggregate ...@@ -14,6 +14,14 @@ namespace cleanArchWebApp.ApplicationCore.Aggregate
[EmailAddress] [EmailAddress]
[Display(Name = "Email")] [Display(Name = "Email")]
public string Email { get; set; } public string Email { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string Avatar { get; set; }
[Required] [Required]
public Patient Patient { get; set; } public Patient Patient { get; set; }
[Required] [Required]
...@@ -26,5 +34,6 @@ namespace cleanArchWebApp.ApplicationCore.Aggregate ...@@ -26,5 +34,6 @@ namespace cleanArchWebApp.ApplicationCore.Aggregate
[Display(Name = "Confirm password")] [Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; } public string ConfirmPassword { get; set; }
public string ReturnUrl { get; set; }
} }
} }
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