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

add generic service

parent e0d8a6d1
...@@ -7,8 +7,8 @@ using System.Threading.Tasks; ...@@ -7,8 +7,8 @@ using System.Threading.Tasks;
namespace ApplicationCore.DomainModel namespace ApplicationCore.DomainModel
{ {
public class DomainBase : EntityBase public class DomainBase
{ {
// public int Id { get; set; } public int Id { get; set; }
} }
} }
...@@ -8,6 +8,7 @@ namespace ApplicationCore.Interfaces ...@@ -8,6 +8,7 @@ namespace ApplicationCore.Interfaces
{ {
public interface IService<T> where T : class public interface IService<T> where T : class
{ {
public Task<IEnumerable<T>> GetAll();
public Task<T> GetDetails(int Id); public Task<T> GetDetails(int Id);
public void Delete(int Id); public void Delete(int Id);
public T Update(T tModel); public T Update(T tModel);
......
...@@ -10,7 +10,7 @@ namespace ApplicationCore.Interfaces.IServices ...@@ -10,7 +10,7 @@ namespace ApplicationCore.Interfaces.IServices
{ {
public interface IIngredientService : IService<IngredientModel> public interface IIngredientService : IService<IngredientModel>
{ {
public Task<IEnumerable<IngredientModel>> GetAllIngredients(); // public Task<IEnumerable<IngredientModel>> GetAllIngredients();
public void AddToMedicine(int ingredientId ,int medicineId , int ratio); public void AddToMedicine(int ingredientId ,int medicineId , int ratio);
} }
} }
...@@ -10,10 +10,8 @@ namespace ApplicationCore.Interfaces.IServices ...@@ -10,10 +10,8 @@ namespace ApplicationCore.Interfaces.IServices
{ {
public interface IMedicalStateService : IService<MedicalStateModel> public interface IMedicalStateService : IService<MedicalStateModel>
{ {
public Task<IEnumerable<MedicalStateModel>> GetAll();
public IEnumerable<MedicalStateModel> GetAllPatientMedicalStates(int patientId); public IEnumerable<MedicalStateModel> GetAllPatientMedicalStates(int patientId);
public MedicalStateModel Add(int patientId , MedicalStateModel medicalState); public MedicalStateModel AddMedicalStateToPateint(int patientId , MedicalStateModel medicalState);
public void AddMedicine(int medicalStateId, int medicineId); public void AddMedicine(int medicalStateId, int medicineId);
public void RemoveMedicine(int medicalStateId, int medicineId); public void RemoveMedicine(int medicalStateId, int medicineId);
......
...@@ -10,11 +10,10 @@ namespace ApplicationCore.Interfaces.IServices ...@@ -10,11 +10,10 @@ namespace ApplicationCore.Interfaces.IServices
{ {
public interface IMedicineService :IService<MedicineModel> public interface IMedicineService :IService<MedicineModel>
{ {
public Task<IEnumerable<MedicineModel>> GetAllMedicines(); // public Task<IEnumerable<MedicineModel>> GetAllMedicines();
// public void AddMedicine(MedicineModel medicine); // public void AddMedicine(MedicineModel medicine);
public void AddMedicineIngredient(int medicineId, IngredientModel ingredient); public void AddMedicineIngredient(int medicineId, IngredientModel ingredient);
public MedicineModel GetMedicineIngredentisDetails(int medicineId); public MedicineModel GetMedicineIngredentisDetails(int medicineId);
public void AddIngredient( int medicineId, int ratio, IngredientModel ingredient);
} }
} }
...@@ -14,10 +14,10 @@ namespace ApplicationCore.Interfaces.IServices ...@@ -14,10 +14,10 @@ namespace ApplicationCore.Interfaces.IServices
public IEnumerable<MedicalStateModel> GetPatientMedicalStates(int patientId); public IEnumerable<MedicalStateModel> GetPatientMedicalStates(int patientId);
public Task<MedicalStateModel> GetMedicalStateDetails(int id); public Task<MedicalStateModel> GetMedicalStateDetails(int id);
public Task<IEnumerable<PatientModel>>GetAll(); // public Task<IEnumerable<PatientModel>>GetAll();
public void AddMedicalState(int patientId, MedicalStateModel medicalState); public void AddMedicalState(int patientId, MedicalStateModel medicalState);
// public Patient GetDetails(int id); // public Patient GetDetails(int id);
public void Insert(PatientModel patient); // public void Insert(PatientModel patient);
// public void Update(Patient patient); // public void Update(Patient patient);
// public void Delete(int id); // public void Delete(int id);
public bool PatientExists(int id); public bool PatientExists(int id);
......
...@@ -9,6 +9,7 @@ namespace ApplicationCore.Mapper ...@@ -9,6 +9,7 @@ namespace ApplicationCore.Mapper
public class ObjectMapper :Profile public class ObjectMapper :Profile
{ {
public ObjectMapper() { public ObjectMapper() {
CreateMap<Medicine, MedicineModel>() CreateMap<Medicine, MedicineModel>()
.ForMember(dest => dest.Category, opt => opt.MapFrom(src => src.Category)) .ForMember(dest => dest.Category, opt => opt.MapFrom(src => src.Category))
.ForMember(dest => dest.MedicineType, opt => opt.MapFrom(src => src.MedicineType)) .ForMember(dest => dest.MedicineType, opt => opt.MapFrom(src => src.MedicineType))
...@@ -26,7 +27,18 @@ namespace ApplicationCore.Mapper ...@@ -26,7 +27,18 @@ namespace ApplicationCore.Mapper
CreateMap<PatientModel, Patient>().ReverseMap(); CreateMap<PatientModel, Patient>().ReverseMap();
CreateMap<Patient, PatientModel>().ReverseMap(); CreateMap<Patient, PatientModel>().ReverseMap();
CreateMap<Ingredient, IngredientModel>().ReverseMap(); CreateMap<Ingredient, IngredientModel>().ReverseMap();
CreateMap<MedicalState, MedicalStateModel>().ReverseMap(); CreateMap<MedicalState, MedicalStateModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.Patient, opt => opt.MapFrom(src => src.Patient))
.ForMember(dest => dest.Medicines, opt => opt.MapFrom(src => src.Medicines));
;
CreateMap<MedicalStateModel, MedicalState>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.Patient, opt => opt.MapFrom(src => src.Patient))
.ForMember(dest => dest.Medicines, opt => opt.MapFrom(src => src.Medicines))
.ForMember(s=>s.MedicalStateMedicines , op=>op.Ignore());
CreateMap<Category, CategoryModel>(); CreateMap<Category, CategoryModel>();
CreateMap<CategoryModel, Category>() CreateMap<CategoryModel, Category>()
.ForMember(dest => dest.Medicines, opt => opt.Ignore()) .ForMember(dest => dest.Medicines, opt => opt.Ignore())
...@@ -34,7 +46,8 @@ namespace ApplicationCore.Mapper ...@@ -34,7 +46,8 @@ namespace ApplicationCore.Mapper
CreateMap<MedicineType, MedicineTypeModel>().ReverseMap(); CreateMap<MedicineType, MedicineTypeModel>().ReverseMap();
CreateMap<MedicalStateMedicine, MedicalStateMedicineModel>().ReverseMap(); CreateMap<MedicalStateMedicine, MedicalStateMedicineModel>().ReverseMap();
CreateMap<DomainBase, EntityBase>().ReverseMap();
} }
} }
} }
using ApplicationCore.DomainModel;
using ApplicationCore.Interfaces;
using ApplicationDomain.Abstraction;
using ApplicationDomain.Entities;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationCore.Services
{
public class ServiceBase<T,TModel> : IService<TModel> where T : EntityBase where TModel : DomainBase
{
protected readonly IMapper _mapper;
protected readonly IUnitOfWork<T> _unitOfWork;
protected ISpecification<T> _specification;
public ServiceBase(
IUnitOfWork<T> unitOfWork,
IMapper mapper
)
{
_unitOfWork = unitOfWork;
_mapper = mapper;
}
public async Task<IEnumerable<TModel>> GetAll()
{
var r = await _unitOfWork.Entity.GetAll(
_specification
);
return _mapper.Map<IEnumerable<TModel>>(r);
}
public TModel Create(TModel model )
{
var ing = _unitOfWork.Entity.Insert(_mapper.Map<T>(model));
_unitOfWork.Save();
return _mapper.Map<TModel>(ing);
}
public TModel Update(TModel model)
{
var r = _unitOfWork.Entity.Update(_mapper.Map<T>(model));
_unitOfWork.Save();
return _mapper.Map<TModel>(r);
}
public async Task<TModel> GetDetails(int id)
{
return _mapper.Map<TModel>(await _unitOfWork.Entity.GetById(id,
_specification));
}
public void Delete(int id)
{
_unitOfWork.Entity.Delete(id);
_unitOfWork.Save();
}
}
}
...@@ -13,65 +13,24 @@ using AutoMapper; ...@@ -13,65 +13,24 @@ using AutoMapper;
namespace ApplicationCore.Services namespace ApplicationCore.Services
{ {
public class IngredientService :IIngredientService public class IngredientService : ServiceBase<Ingredient,IngredientModel> , IIngredientService
{ {
private readonly IMapper _mapper;
private readonly IUnitOfWork<Ingredient> _ingredientUnitOfWork;
private IngredientSpecification _IngredientSpecification;
public IngredientService( public IngredientService(
IUnitOfWork<Ingredient> ingredientUnitOfWork, IUnitOfWork<Ingredient> ingredientUnitOfWork,
IUnitOfWork<Medicine> medicineUnitOfWork,
IMapper mapper IMapper mapper
) ):base(ingredientUnitOfWork,mapper)
{
_mapper = mapper;
_ingredientUnitOfWork = ingredientUnitOfWork;
_IngredientSpecification = new IngredientSpecification();
}
public async Task<IEnumerable<IngredientModel>> GetAllIngredients()
{ {
return _mapper.Map<IEnumerable<IngredientModel>>(await _ingredientUnitOfWork.Entity.GetAll( _specification = new IngredientSpecification();
_IngredientSpecification
));
} }
public IngredientModel Create(IngredientModel ingredient)
{
var ing = _ingredientUnitOfWork.Entity.Insert(_mapper.Map<Ingredient>(ingredient));
_ingredientUnitOfWork.Save();
return _mapper.Map<IngredientModel>(ing);
}
public IngredientModel Update(IngredientModel ingredient)
{
var r = _ingredientUnitOfWork.Entity.Update(_mapper.Map<Ingredient>(ingredient));
_ingredientUnitOfWork.Save();
return _mapper.Map<IngredientModel>(r);
}
public void AddToMedicine(int ingredientId, int medicineId, int ratio) { public void AddToMedicine(int ingredientId, int medicineId, int ratio) {
var r = _ingredientUnitOfWork.Entity.GetById(ingredientId,_IngredientSpecification).Result; var r = _unitOfWork.Entity.GetById(ingredientId,_specification).Result;
r.MedicineIngredients.Add( r.MedicineIngredients.Add(
new MedicineIngredient { IngredientId = ingredientId , MedicineId=medicineId ,Ratio=ratio} new MedicineIngredient { IngredientId = ingredientId , MedicineId=medicineId ,Ratio=ratio}
); );
_ingredientUnitOfWork.Entity.Update(r); _unitOfWork.Entity.Update(r);
_ingredientUnitOfWork.Save(); _unitOfWork.Save();
}
public async Task<IngredientModel> GetDetails(int id)
{
return _mapper.Map<IngredientModel>(await _ingredientUnitOfWork.Entity.GetById(id,
_IngredientSpecification));
}
public void Delete(int id)
{
_ingredientUnitOfWork.Entity.Delete(id);
_ingredientUnitOfWork.Save();
} }
} }
......
...@@ -9,100 +9,65 @@ using System.Threading.Tasks; ...@@ -9,100 +9,65 @@ using System.Threading.Tasks;
namespace ApplicationCore.Services namespace ApplicationCore.Services
{ {
public class MedicalStateService : IMedicalStateService public class MedicalStateService :ServiceBase<MedicalState ,MedicalStateModel>, IMedicalStateService
{ {
private readonly IUnitOfWork<MedicalState> _MedicalStateUnitOfWork;
private readonly PatientService _patientService; private readonly PatientService _patientService;
private readonly IUnitOfWork<Medicine> _medicineUnitOfWork; private readonly IUnitOfWork<Medicine> _medicineUnitOfWork;
private readonly MedicalStateSpecification _MedicalStateSpecification;
private readonly MedicineIngredientSpecification _medicineSpecification; private readonly MedicineIngredientSpecification _medicineSpecification;
private readonly IMapper _mapper;
public MedicalStateService( public MedicalStateService(
IUnitOfWork<MedicalState> medicalUnitOfWork, IUnitOfWork<MedicalState> medicalUnitOfWork,
IUnitOfWork<Medicine> medicineUnitOfWork, IUnitOfWork<Medicine> medicineUnitOfWork,
IUnitOfWork<Patient> patientUnitOfWork, IUnitOfWork<Patient> patientUnitOfWork,
IMapper Mapper IMapper Mapper
) ):base(medicalUnitOfWork,Mapper)
{ {
_MedicalStateUnitOfWork = medicalUnitOfWork; _specification = new MedicalStateSpecification();
_MedicalStateSpecification = new MedicalStateSpecification();
_medicineUnitOfWork = medicineUnitOfWork; _medicineUnitOfWork = medicineUnitOfWork;
_patientService = new PatientService(patientUnitOfWork,medicalUnitOfWork,Mapper); _patientService = new PatientService(patientUnitOfWork,medicalUnitOfWork,Mapper);
_medicineSpecification = new MedicineIngredientSpecification(); _medicineSpecification = new MedicineIngredientSpecification();
_mapper = Mapper;
} }
public MedicalStateModel Add(int patientId , MedicalStateModel medicalStateModel) public MedicalStateModel AddMedicalStateToPateint(int patientId , MedicalStateModel medicalStateModel)
{ {
//var im = Create(medicalStateModel);
// _patientService.AddMedicalState(patientId ,_mapper.Map<MedicalState>( im));
medicalStateModel.PatientId = patientId; medicalStateModel.PatientId = patientId;
var im =medicalStateModel; var im =medicalStateModel;
var r = _MedicalStateUnitOfWork.Entity.Insert(_mapper.Map<MedicalState>(im)); var r = _unitOfWork.Entity.Insert(_mapper.Map<MedicalState>(im));
_MedicalStateUnitOfWork.Save(); _unitOfWork.Save();
return _mapper.Map<MedicalStateModel>(r); return _mapper.Map<MedicalStateModel>(r);
} }
public MedicalStateModel Create(MedicalStateModel medicalStateModel ) {
var medicalState = _mapper.Map<MedicalState>(medicalStateModel);
var e = _MedicalStateUnitOfWork.Entity.Insert(medicalState);
_MedicalStateUnitOfWork.Save();
return _mapper.Map<MedicalStateModel>(e);
}
public void AddMedicine(int MedicalStateId, int medicineId) public void AddMedicine(int MedicalStateId, int medicineId)
{ {
var m = _MedicalStateUnitOfWork.Entity.GetById(MedicalStateId, _MedicalStateSpecification).Result; var m = _unitOfWork.Entity.GetById(MedicalStateId, _specification).Result;
if (m.Medicines is null) if (m.Medicines is null)
m.Medicines = new List<Medicine>(); m.Medicines = new List<Medicine>();
var d = _medicineUnitOfWork.Entity.GetById(medicineId,_medicineSpecification ).Result; var d = _medicineUnitOfWork.Entity.GetById(medicineId,_medicineSpecification ).Result;
m.Medicines.Add(d ); m.Medicines.Add(d );
_MedicalStateUnitOfWork.Entity.Update(m); _unitOfWork.Entity.Update(m);
_MedicalStateUnitOfWork.Save(); _unitOfWork.Save();
} }
public void RemoveMedicine(int MedicalStateId, int medicineId) public void RemoveMedicine(int MedicalStateId, int medicineId)
{ {
var m = _MedicalStateUnitOfWork.Entity.GetById(MedicalStateId, _MedicalStateSpecification).Result; var m = _unitOfWork.Entity.GetById(MedicalStateId, _specification).Result;
if (m.Medicines is null) if (m.Medicines is null)
m.Medicines = new List<Medicine>(); m.Medicines = new List<Medicine>();
var d = _medicineUnitOfWork.Entity.GetById(medicineId, _medicineSpecification).Result; var d = _medicineUnitOfWork.Entity.GetById(medicineId, _medicineSpecification).Result;
m.Medicines.Remove(d); m.Medicines.Remove(d);
_MedicalStateUnitOfWork.Entity.Update(m); _unitOfWork.Entity.Update(m);
_MedicalStateUnitOfWork.Save(); _unitOfWork.Save();
} }
public void Delete(int id)
{
_MedicalStateUnitOfWork.Entity.Delete(id);
_MedicalStateUnitOfWork.Save();
}
public async Task<IEnumerable<MedicalStateModel>> GetAll()
{
return _mapper.Map<IEnumerable<MedicalStateModel>>(await _MedicalStateUnitOfWork.Entity.GetAll(_MedicalStateSpecification));
}
public IEnumerable<MedicalStateModel> GetAllPatientMedicalStates(int patientId) public IEnumerable<MedicalStateModel> GetAllPatientMedicalStates(int patientId)
{ {
return _mapper.Map<IEnumerable<MedicalStateModel>>( _patientService.GetPatientMedicalStates(patientId)); return _mapper.Map<IEnumerable<MedicalStateModel>>( _patientService.GetPatientMedicalStates(patientId));
} }
public async Task<MedicalStateModel> GetDetails(int MedicalStateId)
{
return _mapper.Map<MedicalStateModel>(await _patientService.GetMedicalStateDetails(MedicalStateId));
}
public MedicalStateModel Update(MedicalStateModel MedicalStateModel)
{
var MedicalState = _mapper.Map<MedicalState>(MedicalStateModel);
var r = _MedicalStateUnitOfWork.Entity.Update(MedicalState);
_MedicalStateUnitOfWork.Save();
return _mapper.Map<MedicalStateModel>(r);
}
} }
} }
...@@ -14,87 +14,51 @@ using ApplicationCore.DomainModel; ...@@ -14,87 +14,51 @@ using ApplicationCore.DomainModel;
namespace ApplicationCore.Services namespace ApplicationCore.Services
{ {
public class MedicineService : IMedicineService public class MedicineService : ServiceBase<Medicine,MedicineModel>,IMedicineService
{ {
private readonly IMapper _mapper;
private readonly IUnitOfWork<Medicine> _medicineUnitOfWork;
private MedicineIngredientSpecification _medicineIngredientSpecification;
public MedicineService( public MedicineService(
IUnitOfWork<Medicine> medicineUnitOfWork, IUnitOfWork<Medicine> medicineUnitOfWork,
IMapper medicineMapper ) IMapper Mapper )
:base(medicineUnitOfWork , Mapper)
{ {
_mapper = medicineMapper; _specification = new MedicineIngredientSpecification();
_medicineUnitOfWork = medicineUnitOfWork;
_medicineIngredientSpecification = new MedicineIngredientSpecification();
} }
public async Task<IEnumerable<MedicineModel>> GetAllMedicines() {
var m = _mapper.Map<IEnumerable<MedicineModel>>(await _medicineUnitOfWork.Entity.GetAll(
_medicineIngredientSpecification
));
return m ;
}
public MedicineModel Create (MedicineModel medicineModel) {
var r = _mapper.Map<MedicineModel>(new Medicine());
var rr = _mapper.Map<Medicine>(new MedicineModel());
var medicine = _mapper.Map<Medicine>(medicineModel);
var m = _medicineUnitOfWork.Entity.Insert(medicine);
_medicineUnitOfWork.Save();
return _mapper.Map<MedicineModel>(m);
}
public void AddMedicineIngredient(int medicineId ,IngredientModel ingredientModel ) { public void AddMedicineIngredient(int medicineId ,IngredientModel ingredientModel ) {
var s = _medicineUnitOfWork.Entity.GetById(medicineId,_medicineIngredientSpecification).Result; var s = _unitOfWork.Entity.GetById(medicineId,_specification).Result;
s.Ingredients.Add(_mapper.Map<Ingredient>(ingredientModel)); s.Ingredients.Add(_mapper.Map<Ingredient>(ingredientModel));
_medicineUnitOfWork.Entity.Update(s); _unitOfWork.Entity.Update(s);
_medicineUnitOfWork.Save(); _unitOfWork.Save();
}
public MedicineModel Update(MedicineModel medicineModel) {
var medicine = _mapper.Map<Medicine>(medicineModel);
var rm=_medicineUnitOfWork.Entity.Update(medicine);
_medicineUnitOfWork.Save();
var r =_mapper.Map<MedicineModel>(rm);
return r;
}
public async Task<MedicineModel> GetDetails(int id)
{
var medicine = _mapper.Map<MedicineModel>(await _medicineUnitOfWork.Entity.GetById(id, _medicineIngredientSpecification));
return medicine;
} }
public MedicineModel GetMedicineIngredentisDetails(int medicineId) { public MedicineModel GetMedicineIngredentisDetails(int medicineId) {
return _mapper.Map<MedicineModel>(_medicineUnitOfWork.Entity return _mapper.Map<MedicineModel>(_unitOfWork.Entity
.GetById(medicineId , .GetById(medicineId ,
_medicineIngredientSpecification)); _specification));
} }
public void AddIngredient(int medicineId, int ratio , IngredientModel ingredient) //public void AddIngredient(int medicineId, int ratio , IngredientModel ingredient)
{ //{
// var m = _mapper.Map<Medicine>(GetMedicineIngredentisDetails(medicineId)); // // var m = _mapper.Map<Medicine>(GetMedicineIngredentisDetails(medicineId));
var m = _medicineUnitOfWork.Entity.GetById(medicineId,_medicineIngredientSpecification).Result; // var m = _unitOfWork.Entity.GetById(medicineId,_specification).Result;
_medicineUnitOfWork.Save(); // _unitOfWork.Save();
if (ingredient.Id != 0) // if (ingredient.Id != 0)
foreach (var i in m.Ingredients) // foreach (var i in m.Ingredients)
{ // {
if (i.Id.Equals(ingredient.Id)) // if (i.Id.Equals(ingredient.Id))
return; // return;
} // }
m.AddIngredient(_mapper.Map<Ingredient>(ingredient), ratio); // m.AddIngredient(_mapper.Map<Ingredient>(ingredient), ratio);
_medicineUnitOfWork.Entity.Update(m); // _unitOfWork.Entity.Update(m);
_medicineUnitOfWork.Save(); // _unitOfWork.Save();
} //}
public void Delete(int id) {
_medicineUnitOfWork.Entity.Delete(id);
_medicineUnitOfWork.Save();
}
} }
} }
...@@ -14,88 +14,50 @@ using ApplicationCore.DomainModel; ...@@ -14,88 +14,50 @@ using ApplicationCore.DomainModel;
namespace ApplicationCore.Services namespace ApplicationCore.Services
{ {
public class PatientService : IPatientService public class PatientService : ServiceBase<Patient ,PatientModel> , IPatientService
{ {
private readonly IMapper _mapper;
private readonly IUnitOfWork<Patient> _patientUnitOfWork;
private readonly IUnitOfWork<MedicalState> _medicalStateUnitOfWork; private readonly IUnitOfWork<MedicalState> _medicalStateUnitOfWork;
private PatientMedicinesSpecification _patientMedicinesSpecification;
private MedicalStateSpecification _medicalStateSpecification; private MedicalStateSpecification _medicalStateSpecification;
public PatientService( public PatientService(
IUnitOfWork<Patient> patientUnitOfWork, IUnitOfWork<Patient> patientUnitOfWork,
IUnitOfWork<MedicalState> medicalStateUnitOfWork, IUnitOfWork<MedicalState> medicalStateUnitOfWork,
IMapper mapper ) IMapper mapper )
:base(patientUnitOfWork , mapper)
{ {
_mapper = mapper;
_patientUnitOfWork = patientUnitOfWork;
_medicalStateUnitOfWork = medicalStateUnitOfWork; _medicalStateUnitOfWork = medicalStateUnitOfWork;
_patientMedicinesSpecification = new PatientMedicinesSpecification(); _specification = new PatientMedicinesSpecification();
_medicalStateSpecification = new MedicalStateSpecification(); _medicalStateSpecification = new MedicalStateSpecification();
} }
public IEnumerable<MedicalStateModel> GetPatientMedicalStates(int patientId) { public IEnumerable<MedicalStateModel> GetPatientMedicalStates(int patientId) {
return _mapper.Map<IEnumerable<MedicalStateModel>>( _patientUnitOfWork.Entity return _mapper.Map<IEnumerable<MedicalStateModel>>( _unitOfWork.Entity
.GetById( .GetById(
patientId,_patientMedicinesSpecification patientId,_specification
).Result.MedicalStates.AsEnumerable()); ).Result.MedicalStates.AsEnumerable());
} }
public async Task< MedicalStateModel> GetMedicalStateDetails(int id) public async Task< MedicalStateModel> GetMedicalStateDetails(int id)
{ {
return _mapper.Map<MedicalStateModel>(await _medicalStateUnitOfWork.Entity.GetById(id,_medicalStateSpecification)); return _mapper.Map<MedicalStateModel>(await _medicalStateUnitOfWork.Entity.GetById(id,_medicalStateSpecification));
} }
public async Task<IEnumerable<PatientModel>> GetAll() {
return _mapper.Map < IEnumerable < PatientModel >> (await _patientUnitOfWork.Entity.GetAll(_patientMedicinesSpecification));
}
public void AddMedicalState (int patientId, MedicalStateModel medicalState) { public void AddMedicalState (int patientId, MedicalStateModel medicalState) {
var ptient = _patientUnitOfWork.Entity.GetById(patientId,_patientMedicinesSpecification).Result; var ptient = _unitOfWork.Entity.GetById(patientId,_specification).Result;
ptient.MedicalStates.Add(_mapper.Map<MedicalState>(medicalState)); ptient.MedicalStates.Add(_mapper.Map<MedicalState>(medicalState));
_patientUnitOfWork.Entity.Update(ptient); _unitOfWork.Entity.Update(ptient);
_patientUnitOfWork.Save(); _unitOfWork.Save();
}
public async Task<PatientModel> GetDetails(int id)
{
return _mapper.Map < PatientModel>(await _patientUnitOfWork.Entity.GetById(id, _patientMedicinesSpecification));
} }
public void Insert(PatientModel patient)
{
_patientUnitOfWork.Entity.Insert(_mapper.Map<Patient>(patient));
_patientUnitOfWork.Save();
}
public PatientModel Create(PatientModel patient) {
var p =_patientUnitOfWork.Entity.Insert(_mapper.Map<Patient>(patient));
return _mapper.Map<PatientModel>(p);
}
public PatientModel Update(PatientModel patient)
{
var p =_patientUnitOfWork.Entity.Update(_mapper.Map<Patient>(patient));
_patientUnitOfWork.Save();
return _mapper.Map < PatientModel > (p);
}
public void Delete(int id)
{
_patientUnitOfWork.Entity.Delete(id);
_patientUnitOfWork.Save();
}
public bool PatientExists(int id) public bool PatientExists(int id)
{ {
return _patientUnitOfWork.Entity.GetById(id) is null ? false : true; return _unitOfWork.Entity.GetById(id) is null ? false : true;
} }
......
...@@ -9,7 +9,7 @@ namespace ApplicationDomain.Abstraction ...@@ -9,7 +9,7 @@ namespace ApplicationDomain.Abstraction
{ {
public interface ISpecification<T> where T : class public interface ISpecification<T> where T : class
{ {
Expression<Func<T, bool>> Criteria { get; } Expression<Func<T, bool>> Criteria { get; set; }
List<Expression<Func<T, object>>> Includes { get; } List<Expression<Func<T, object>>> Includes { get; }
List<String> ThenInclude { get; } List<String> ThenInclude { get; }
Expression<Func<T, object>> OrderBy { get; } Expression<Func<T, object>> OrderBy { get; }
......
...@@ -13,8 +13,11 @@ namespace ApplicationDomain.Entities ...@@ -13,8 +13,11 @@ namespace ApplicationDomain.Entities
public String StateName { get; set; } public String StateName { get; set; }
public String StateDescription { get; set; } public String StateDescription { get; set; }
public DateTime PrescriptionTime { get; set; } public DateTime PrescriptionTime { get; set; }
#region Navigations
public ICollection<Medicine> Medicines { get; set; } public ICollection<Medicine> Medicines { get; set; }
public ICollection<MedicalStateMedicine> MedicalStateMedicines { get; set; } public ICollection<MedicalStateMedicine> MedicalStateMedicines { get; set; }
#endregion Navigations
} }
} }
...@@ -14,10 +14,17 @@ namespace ApplicationDomain.Entities ...@@ -14,10 +14,17 @@ namespace ApplicationDomain.Entities
public User User { get; set; } public User User { get; set; }
public String BIO { get; set; } public String BIO { get; set; }
#region Relations #region manage
public void AddMedicalState(MedicalState medicalState) {
MedicalStates.Add(medicalState);
}
#endregion manage
#region Navigations
public ICollection<MedicalState> MedicalStates { get; set; } public ICollection<MedicalState> MedicalStates { get; set; }
// public ICollection<Medicine> Medicines { get; set; }
// public ICollection<PatientMedicine> PatientMedicines { get; set; } #endregion Navigations
#endregion Relations
} }
} }
using ApplicationDomain.Abstraction;
using ApplicationDomain.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ApplicationDomain.Repositories
{
public interface IMedicalStateRepository : IGenericRepository<MedicalState>
{
public Task<IEnumerable<MedicalState>> GetByPatient(int patientId, ISpecification<MedicalState> specification);
}
}
using ApplicationDomain.Entities; using ApplicationDomain.Abstraction;
using ApplicationDomain.Entities;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -9,6 +10,7 @@ namespace ApplicationDomain.Repositories ...@@ -9,6 +10,7 @@ namespace ApplicationDomain.Repositories
{ {
public interface IMedicineRepository : IGenericRepository<Medicine> public interface IMedicineRepository : IGenericRepository<Medicine>
{ {
public Medicine AddIngredient { get; set; } public Task<IEnumerable<Medicine>> GetByMedicalState(int medicalStateId );
} }
} }
...@@ -14,7 +14,7 @@ namespace ApplicationDomain.Specification.BaseSpecification ...@@ -14,7 +14,7 @@ namespace ApplicationDomain.Specification.BaseSpecification
{ {
Criteria = criteria; Criteria = criteria;
} }
public Expression<Func<T, bool>> Criteria { get; } public Expression<Func<T, bool>> Criteria { get; set; }
public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>(); public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>();
public List<String> ThenInclude { get; } = new List<String>(); public List<String> ThenInclude { get; } = new List<String>();
public Expression<Func<T, object>> OrderBy { get; private set; } public Expression<Func<T, object>> OrderBy { get; private set; }
......
...@@ -3,6 +3,7 @@ using ApplicationDomain.Specification.BaseSpecification; ...@@ -3,6 +3,7 @@ using ApplicationDomain.Specification.BaseSpecification;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -11,9 +12,10 @@ namespace ApplicationDomain.Specification ...@@ -11,9 +12,10 @@ namespace ApplicationDomain.Specification
public class MedicalStateSpecification : BaseSpecification<MedicalState> public class MedicalStateSpecification : BaseSpecification<MedicalState>
{ {
public MedicalStateSpecification() public MedicalStateSpecification(Expression<Func<MedicalState, bool>> criteria =null)
:base(criteria)
{ {
AddInclude(p => p.MedicalStateMedicines); AddInclude(p => p.MedicalStateMedicines);
AddInclude(p => p.Medicines); AddInclude(p => p.Medicines);
AddInclude(p => p.Patient); AddInclude(p => p.Patient);
......
...@@ -15,7 +15,7 @@ namespace Infrastructure.Repository ...@@ -15,7 +15,7 @@ namespace Infrastructure.Repository
public class GenericRepository<T> : IGenericRepository<T> where T : EntityBase public class GenericRepository<T> : IGenericRepository<T> where T : EntityBase
{ {
protected readonly DbContext _context; protected readonly DbContext _context;
private DbSet<T> _table; protected DbSet<T> _table;
public GenericRepository(DbContext context) public GenericRepository(DbContext context)
{ {
_context = context; _context = context;
......
using ApplicationDomain.Abstraction;
using ApplicationDomain.Entities;
using ApplicationDomain.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Infrastructure.Repository
{
public class MedicalStateRepository : GenericRepository<MedicalState>, IMedicalStateRepository
{
public MedicalStateRepository(DbContext dbContext) : base(dbContext)
{
}
public Task<IEnumerable<MedicalState>> GetByPatient(int patientId , ISpecification<MedicalState> specification)
{
specification.Criteria = p => p.PatientId == patientId;
return GetAll(specification);
}
}
}
using ApplicationDomain.Entities; using ApplicationDomain.Entities;
using ApplicationDomain.Repositories;
using ApplicationDomain.Specification;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
...@@ -8,11 +10,19 @@ using System.Threading.Tasks; ...@@ -8,11 +10,19 @@ using System.Threading.Tasks;
namespace Infrastructure.Repository namespace Infrastructure.Repository
{ {
public class MedicineRepository : GenericRepository<Medicine> public class MedicineRepository : GenericRepository<Medicine> ,IMedicineRepository
{ {
public MedicineRepository(DbContext dbContext) : base(dbContext) public MedicineRepository(DbContext dbContext) : base(dbContext)
{ {
} }
public Task<IEnumerable<Medicine>> GetByMedicalState(int medicalStateId)
{
var spec = new MedicineIngredientSpecification();
spec.Criteria = p => p.MedicalStates.All(s=>s.Id ==medicalStateId );
return GetAll(spec);
}
} }
} }
991ccf053c0d53df7ecd75be411e20caece31f5d 35cbfd0c81bb22b7edd15e31ee71ff49ee100adb
...@@ -6,11 +6,12 @@ using Microsoft.AspNetCore.Mvc; ...@@ -6,11 +6,12 @@ using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using ApplicationCore.DomainModel;
namespace WebPresentation.Controllers namespace WebPresentation.Controllers
{ {
[Authorize] [Authorize]
public abstract class BaseController<T> : Controller where T : EntityBase public abstract class BaseController<T> : Controller where T : DomainBase
{ {
protected readonly UserManager<User> _userManager; protected readonly UserManager<User> _userManager;
protected readonly IService<T> _service; protected readonly IService<T> _service;
......
...@@ -28,7 +28,7 @@ namespace WebPresentation.Controllers ...@@ -28,7 +28,7 @@ namespace WebPresentation.Controllers
public IActionResult Index() public IActionResult Index()
{ {
var s = _ingredientService.GetAllIngredients().Result; var s = _ingredientService.GetAll().Result;
return View(s); return View(s);
} }
......
...@@ -63,7 +63,7 @@ namespace WebPresentation.Controllers ...@@ -63,7 +63,7 @@ namespace WebPresentation.Controllers
.FirstOrDefault().Id; .FirstOrDefault().Id;
if (medicalState.PrescriptionTime == DateTime.MinValue ) if (medicalState.PrescriptionTime == DateTime.MinValue )
medicalState.PrescriptionTime = DateTime.Now; medicalState.PrescriptionTime = DateTime.Now;
var n= ((IMedicalStateService)_service).Add(p,medicalState); var n= ((IMedicalStateService)_service).AddMedicalStateToPateint(p,medicalState);
return RedirectToAction("Details", "MedicalState" , new { Id =n.Id }); return RedirectToAction("Details", "MedicalState" , new { Id =n.Id });
} }
...@@ -74,7 +74,7 @@ namespace WebPresentation.Controllers ...@@ -74,7 +74,7 @@ namespace WebPresentation.Controllers
[HttpGet] [HttpGet]
public IActionResult AddMedicine(int id) public IActionResult AddMedicine(int id)
{ {
var all = _medicineService.GetAllMedicines(); var all = _medicineService.GetAll();
ViewBag.MedicalStateId = id; ViewBag.MedicalStateId = id;
return View(all); return View(all);
} }
......
...@@ -36,7 +36,7 @@ namespace WebPresentation.Controllers ...@@ -36,7 +36,7 @@ namespace WebPresentation.Controllers
[Authorize(Roles = "Admin")] [Authorize(Roles = "Admin")]
public IActionResult Index() public IActionResult Index()
{ {
var s = _medicineService.GetAllMedicines().Result; var s = _medicineService.GetAll().Result;
return View(s); return View(s);
...@@ -67,7 +67,7 @@ namespace WebPresentation.Controllers ...@@ -67,7 +67,7 @@ namespace WebPresentation.Controllers
[Authorize(Roles = "Admin")] [Authorize(Roles = "Admin")]
public IActionResult AddIngredints(int id ) { public IActionResult AddIngredints(int id ) {
var s = _ingredientService.GetAllIngredients().Result; var s = _ingredientService.GetAll().Result;
ViewBag.MedicineId = id; ViewBag.MedicineId = id;
return View(s); return View(s);
...@@ -101,7 +101,7 @@ namespace WebPresentation.Controllers ...@@ -101,7 +101,7 @@ namespace WebPresentation.Controllers
[HttpGet] [HttpGet]
public JsonResult GetMedicines() public JsonResult GetMedicines()
{ {
var all = _medicineService.GetAllMedicines().Result; var all = _medicineService.GetAll().Result;
return new JsonResult(all); return new JsonResult(all);
......
using ApplicationCore.DomainModel;
using ApplicationDomain.Entities;
using AutoMapper;
using WebPresentation.ViewModels;
namespace ApplicationCore.Mapper
{
public class ObjectMapper : Profile
{
public ObjectMapper()
{
CreateMap<MedicineViewModel, MedicineModel>()
.ForMember(dest => dest.Category, opt => opt.MapFrom(src => src.Category))
.ForMember(dest => dest.MedicineType, opt => opt.MapFrom(src => src.MedicineType))
.ForMember(dest => dest.Ingredients, opt => opt.MapFrom(src => src.Ingredients))
.ForMember(dest => dest.MedicineIngredients, opt => opt.MapFrom(src => src.MedicineIngredients));
;
CreateMap<MedicineModel, MedicineViewModel>()
.ForMember(de => de.Ingredients, o => o.MapFrom(s => s.Ingredients))
.ForMember(de => de.MedicineIngredients, o => o.MapFrom(s => s.MedicineIngredients))
.ForMember(de => de.MedicineType, o => o.MapFrom(s => s.MedicineType))
.ForMember(de => de.Category, o => o.MapFrom(s => s.Category.Name))
;
CreateMap<PatientModel, PatientViewModel>().ReverseMap();
CreateMap<Ingredient, IngredientModel>().ReverseMap();
CreateMap<MedicalStateViewModel, MedicalStateModel>().ReverseMap();
CreateMap<DomainBase, BaseViewModel>().ReverseMap();
}
}
}
...@@ -15,6 +15,7 @@ using Microsoft.Extensions.Hosting; ...@@ -15,6 +15,7 @@ using Microsoft.Extensions.Hosting;
using AutoMapper; using AutoMapper;
using ApplicationDomain.Abstraction; using ApplicationDomain.Abstraction;
using ApplicationDomain.Repositories; using ApplicationDomain.Repositories;
using ApplicationCore.DomainModel;
namespace WebPresentation namespace WebPresentation
{ {
...@@ -33,7 +34,7 @@ namespace WebPresentation ...@@ -33,7 +34,7 @@ namespace WebPresentation
services.AddScoped<DbContext, MedicDbContext>(); services.AddScoped<DbContext, MedicDbContext>();
services.AddScoped<Mapper>(); services.AddScoped<Mapper>();
services.AddAutoMapper(typeof(ApplicationCore.Mapper.ObjectMapper)); services.AddAutoMapper(typeof(ApplicationCore.Mapper.ObjectMapper));
#region ADD Scoped Repository #region ADD Scoped Repository
services.AddScoped(typeof(IUnitOfWork<>),typeof(UnitOfWork<>)); services.AddScoped(typeof(IUnitOfWork<>),typeof(UnitOfWork<>));
......
...@@ -3,7 +3,7 @@ using System.Collections.Generic; ...@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace WebPresentation.View_Models namespace WebPresentation.ViewModels
{ {
public class BaseViewModel public class BaseViewModel
{ {
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebPresentation.ViewModels
{
public class IngredientViewModel : BaseViewModel
{
public String Name { get; set; }
public String Description { get; set; }
}
}
...@@ -4,14 +4,14 @@ using System.Linq; ...@@ -4,14 +4,14 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ApplicationDomain.Entities namespace WebPresentation.ViewModels
{ {
public class PatientMedicine : EntityBase public class MedicineIngredientViewModel : BaseViewModel
{ {
public DateTime PrescripDate { get; set; } public int Ratio { get; set; }
public int MedicineId { get; set; } public int MedicineId { get; set; }
public int PatientId { get; set; } public int IngredientId { get; set; }
public Medicine Medicine { get; set; }
public Patient Patient { get; set; } public IngredientViewModel Ingredient { get; set; }
} }
} }
using ApplicationDomain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebPresentation.ViewModels
{
public class MedicineViewModel : BaseViewModel
{
public String TradeName { get; set; }
public String ScintificName { get; set; }
public String ManufactureName { get; set; }
public String SideEffect { get; set; }
public String Description { get; set; }
public int Price { get; set; }
public String Image { get; set; }
public int Dosage { get; set; }
public String Category { get; set; }
public String MedicineType { get; set; }
public ICollection<IngredientViewModel> Ingredients { get; set; }
public ICollection<MedicineIngredientViewModel> MedicineIngredients { get; set; }
}
}
...@@ -4,14 +4,16 @@ using System.Linq; ...@@ -4,14 +4,16 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ApplicationDomain.Entities namespace WebPresentation.ViewModels
{ {
public class PatientMedicine : EntityBase public class MedicalStateViewModel : BaseViewModel
{ {
public DateTime PrescripDate { get; set; }
public int MedicineId { get; set; }
public int PatientId { get; set; } public int PatientId { get; set; }
public Medicine Medicine { get; set; } public PatientViewModel Patient { get; set; }
public Patient Patient { get; set; } public String StateName { get; set; }
public String StateDescription { get; set; }
public DateTime PrescriptionTime { get; set; }
public ICollection<MedicineViewModel> Medicines { get; set; }
} }
} }
using ApplicationDomain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebPresentation.ViewModels
{
public class PatientViewModel : BaseViewModel
{
public String UserId { get; set; }
public User User { get; set; }
public String BIO { get; set; }
#region Navigations
public ICollection<MedicalStateViewModel> MedicalStates { get; set; }
#endregion Navigations
}
}
bbdf2a2e112d3a2fddb1de1a8d25d860a967c842 0f87441b6f32c7123a80c471b0eaca934bec84ff
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