Commit d57d3621 authored by hasan khaddour's avatar hasan khaddour

update services responsabilites

parent 60ef3ecb
...@@ -11,5 +11,7 @@ namespace ApplicationCore.Interfaces.IServices ...@@ -11,5 +11,7 @@ namespace ApplicationCore.Interfaces.IServices
public interface IIngredientService : IService<IngredientModel> public interface IIngredientService : IService<IngredientModel>
{ {
public void AddToMedicine(MedicineIngredientModel medicineIngredientModel); public void AddToMedicine(MedicineIngredientModel medicineIngredientModel);
public void RemoveFromMedicine(MedicineIngredientModel medicineIngredientModel);
} }
} }
...@@ -10,7 +10,7 @@ namespace ApplicationCore.Interfaces.IServices ...@@ -10,7 +10,7 @@ namespace ApplicationCore.Interfaces.IServices
{ {
public interface IMedicalStateService : IService<MedicalStateModel> public interface IMedicalStateService : IService<MedicalStateModel>
{ {
public IEnumerable<MedicalStateModel> GetAllPatientMedicalStates(int patientId); public Task<IEnumerable<MedicalStateModel>> GetAllPatientMedicalStates(int patientId);
public MedicalStateModel AddToPateint(int patientId , MedicalStateModel medicalState); public MedicalStateModel AddToPateint(int patientId , MedicalStateModel medicalState);
......
...@@ -12,9 +12,9 @@ namespace ApplicationCore.Interfaces.IServices ...@@ -12,9 +12,9 @@ namespace ApplicationCore.Interfaces.IServices
public interface IPatientService :IService<PatientModel> public interface IPatientService :IService<PatientModel>
{ {
public IEnumerable<MedicalStateModel> GetPatientMedicalStates(int patientId); public Task<IEnumerable<MedicalStateModel>> GetPatientMedicalStates(int patientId);
public Task<MedicalStateModel> GetMedicalStateDetails(int id); public Task<MedicalStateModel> GetMedicalStateDetails(int id);
public Task<Patient> GetByUserID(String id ); public Task<PatientModel> GetByUserEmail(String email );
// 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);
......
using ApplicationCore.DomainModel; using ApplicationCore.DomainModel;
using ApplicationCore.Interfaces; using ApplicationCore.Interfaces;
using ApplicationDomain.Abstraction; using ApplicationDomain.Abstraction;
using ApplicationDomain.Exceptions;
using ApplicationDomain.Entities; using ApplicationDomain.Entities;
using AutoMapper; using AutoMapper;
using System; using System;
...@@ -21,7 +22,7 @@ namespace ApplicationCore.Services ...@@ -21,7 +22,7 @@ namespace ApplicationCore.Services
public ServiceBase( public ServiceBase(
IUnitOfWork<TEntity> unitOfWork, IUnitOfWork<TEntity> unitOfWork,
IMapper mapper IMapper mapper
) )
{ {
_unitOfWork = unitOfWork; _unitOfWork = unitOfWork;
...@@ -39,7 +40,7 @@ namespace ApplicationCore.Services ...@@ -39,7 +40,7 @@ namespace ApplicationCore.Services
{ {
TEntity entity = _unitOfWork.Entity.Insert(_mapper.Map<TEntity>(model)); TEntity entity = _unitOfWork.Entity.Insert(_mapper.Map<TEntity>(model));
_unitOfWork.Save(); _unitOfWork.Commit();
return _mapper.Map<TModel>(entity); return _mapper.Map<TModel>(entity);
} }
...@@ -47,22 +48,26 @@ namespace ApplicationCore.Services ...@@ -47,22 +48,26 @@ namespace ApplicationCore.Services
{ {
TEntity entity = _unitOfWork.Entity.Update(_mapper.Map<TEntity>(model)); TEntity entity = _unitOfWork.Entity.Update(_mapper.Map<TEntity>(model));
_unitOfWork.Save(); _unitOfWork.Commit();
return _mapper.Map<TModel>(entity); return _mapper.Map<TModel>(entity);
} }
public async Task<TModel> GetDetails(int id) public async Task<TModel> GetDetails(int id)
{ {
return _mapper.Map<TModel>(await _unitOfWork.Entity.GetById(id, var model = await _unitOfWork.Entity.GetById(id,
_specification)); _specification);
if (model is null) {
throw new NotFoundException();
}
return _mapper.Map<TModel>(model);
} }
public void Delete(int id) public void Delete(int id)
{ {
_unitOfWork.Entity.Delete(id); _unitOfWork.Entity.Delete(id);
_unitOfWork.Save(); _unitOfWork.Commit();
} }
......
...@@ -16,9 +16,9 @@ namespace ApplicationCore.Services ...@@ -16,9 +16,9 @@ namespace ApplicationCore.Services
public class IngredientService : ServiceBase<Ingredient,IngredientModel> , IIngredientService public class IngredientService : ServiceBase<Ingredient,IngredientModel> , IIngredientService
{ {
public IngredientService( public IngredientService(
IUnitOfWork<Ingredient> ingredientUnitOfWork, IUnitOfWork<Ingredient> unitOfWork,
IMapper mapper IMapper mapper
):base(ingredientUnitOfWork,mapper) ):base(unitOfWork,mapper)
{ {
_specification = new IngredientWithMedicinesSpecification(); _specification = new IngredientWithMedicinesSpecification();
} }
...@@ -30,9 +30,17 @@ namespace ApplicationCore.Services ...@@ -30,9 +30,17 @@ namespace ApplicationCore.Services
medicine.MedicineIngredients.Add( medicine.MedicineIngredients.Add(
medicineIngredient medicineIngredient
); );
_unitOfWork.Entity.Update(medicine); _unitOfWork.Entity.Update(medicine);
_unitOfWork.Save(); _unitOfWork.Commit();
}
public async void RemoveFromMedicine(MedicineIngredientModel medicineIngredientModel)
{
var medicine = await _unitOfWork.Entity.GetById(medicineIngredientModel.IngredientId, _specification);
MedicineIngredient medicineIngredient = _mapper.Map<MedicineIngredient>(medicineIngredientModel);
var m =medicine.MedicineIngredients.Where(p => p.IngredientId == medicineIngredientModel.IngredientId).FirstOrDefault();
medicine.MedicineIngredients.Remove(m);
_unitOfWork.Entity.Update(medicine);
_unitOfWork.Commit();
} }
} }
......
...@@ -11,21 +11,14 @@ namespace ApplicationCore.Services ...@@ -11,21 +11,14 @@ namespace ApplicationCore.Services
{ {
public class MedicalStateService :ServiceBase<MedicalState ,MedicalStateModel>, IMedicalStateService public class MedicalStateService :ServiceBase<MedicalState ,MedicalStateModel>, IMedicalStateService
{ {
private readonly PatientService _patientService;
private readonly IUnitOfWork<Medicine> _medicineUnitOfWork;
private readonly MedicineWithIngredientsSpecification _medicineSpecification;
public MedicalStateService( public MedicalStateService(
IUnitOfWork<MedicalState> medicalUnitOfWork, IUnitOfWork<MedicalState> unitOfWork,
IUnitOfWork<Medicine> medicineUnitOfWork, IMapper Mapper
IUnitOfWork<Patient> patientUnitOfWork, ):base(unitOfWork,Mapper)
IMapper Mapper
):base(medicalUnitOfWork,Mapper)
{ {
_specification = new MedicalStateWithMedicinesSpecification(); _specification = new MedicalStateWithMedicinesSpecification();
_medicineUnitOfWork = medicineUnitOfWork;
_patientService = new PatientService(patientUnitOfWork,medicalUnitOfWork,Mapper);
_medicineSpecification = new MedicineWithIngredientsSpecification();
} }
public MedicalStateModel AddToPateint(int patientId , MedicalStateModel medicalStateModel) public MedicalStateModel AddToPateint(int patientId , MedicalStateModel medicalStateModel)
{ {
...@@ -34,14 +27,18 @@ namespace ApplicationCore.Services ...@@ -34,14 +27,18 @@ namespace ApplicationCore.Services
var im =medicalStateModel; var im =medicalStateModel;
var r = _unitOfWork.Entity.Insert(_mapper.Map<MedicalState>(im)); var r = _unitOfWork.Entity.Insert(_mapper.Map<MedicalState>(im));
_unitOfWork.Save(); _unitOfWork.Commit();
return _mapper.Map<MedicalStateModel>(r); return _mapper.Map<MedicalStateModel>(r);
} }
public IEnumerable<MedicalStateModel> GetAllPatientMedicalStates(int patientId) public async Task<IEnumerable<MedicalStateModel>> GetAllPatientMedicalStates(int patientId)
{ {
return _mapper.Map<IEnumerable<MedicalStateModel>>( _patientService.GetPatientMedicalStates(patientId)); return _mapper.Map<IEnumerable<MedicalStateModel>>(
await _unitOfWork.MedicalStates.GetByPatient(patientId, _specification)
);
} }
......
using ApplicationDomain.Entities; using ApplicationDomain.Entities;
using ApplicationCore.Interfaces;
using ApplicationCore.Interfaces.IServices; using ApplicationCore.Interfaces.IServices;
using ApplicationDomain.Abstraction; using ApplicationDomain.Abstraction;
using ApplicationDomain.Specification; using ApplicationDomain.Specification;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ApplicationCore.Mapper;
using AutoMapper; using AutoMapper;
using ApplicationCore.DomainModel; using ApplicationCore.DomainModel;
...@@ -16,17 +10,13 @@ namespace ApplicationCore.Services ...@@ -16,17 +10,13 @@ namespace ApplicationCore.Services
{ {
public class MedicineService : ServiceBase<Medicine,MedicineModel>,IMedicineService public class MedicineService : ServiceBase<Medicine,MedicineModel>,IMedicineService
{ {
private IUnitOfWork<MedicalState> _medicalStateUnitOfWork;
private MedicalStateWithMedicinesSpecification _medicalSpecification; private MedicalStateWithMedicinesSpecification _medicalSpecification;
public MedicineService( public MedicineService(
IUnitOfWork<Medicine> medicineUnitOfWork, IUnitOfWork<Medicine> medicineUnitOfWork,
IUnitOfWork<MedicalState> medicalStateUnitOfWork,
IMapper Mapper ) IMapper Mapper )
:base(medicineUnitOfWork , Mapper) :base(medicineUnitOfWork , Mapper)
{ {
_medicalStateUnitOfWork = medicalStateUnitOfWork;
_specification = new MedicineWithIngredientsSpecification(); _specification = new MedicineWithIngredientsSpecification();
_medicalSpecification = new MedicalStateWithMedicinesSpecification(); _medicalSpecification = new MedicalStateWithMedicinesSpecification();
} }
...@@ -50,23 +40,24 @@ namespace ApplicationCore.Services ...@@ -50,23 +40,24 @@ namespace ApplicationCore.Services
_unitOfWork.Entity.Update(medicine); _unitOfWork.Entity.Update(medicine);
_unitOfWork.Save(); _unitOfWork.Commit();
} }
public void RemoveFromMedicalState(MedicalStateMedicineModel medicalStateMedicineModel) public void RemoveFromMedicalState(MedicalStateMedicineModel medicalStateMedicineModel)
{ {
var m = _medicalStateUnitOfWork.Entity.GetById(medicalStateMedicineModel.MedicalStateId, _medicalSpecification).Result; var m = _unitOfWork.MedicalStates.GetById(medicalStateMedicineModel.MedicalStateId, _medicalSpecification).Result;
//// throw an exception //// throw an exception
//if (m.Medicines is null) //if (m.Medicines is null)
// m.Medicines = new List<Medicine>(); // m.Medicines = new List<Medicine>();
var d = _unitOfWork.Entity.GetById(medicalStateMedicineModel.MedicineId, _specification).Result; var d = _unitOfWork.Entity.GetById(medicalStateMedicineModel.MedicineId, _specification).Result;
m.Medicines.Remove(d); m.Medicines.Remove(d);
_medicalStateUnitOfWork.Entity.Update(m); _unitOfWork.MedicalStates.Update(m);
_medicalStateUnitOfWork.Save(); _unitOfWork.Commit();
} }
public MedicineModel GetMedicineIngredentisDetails(int medicineId) { public MedicineModel GetMedicineIngredentisDetails(int medicineId) {
return _mapper.Map<MedicineModel>(_unitOfWork.Entity return _mapper.Map<MedicineModel>(_unitOfWork.Entity
.GetById(medicineId , .GetById(medicineId ,
_specification)); _specification));
......
...@@ -16,35 +16,35 @@ namespace ApplicationCore.Services ...@@ -16,35 +16,35 @@ namespace ApplicationCore.Services
{ {
public class PatientService : ServiceBase<Patient ,PatientModel> , IPatientService public class PatientService : ServiceBase<Patient ,PatientModel> , IPatientService
{ {
private readonly IUnitOfWork<MedicalState> _medicalStateUnitOfWork;
private MedicalStateWithMedicinesSpecification _medicalStateSpecification; private MedicalStateWithMedicinesSpecification _medicalStateSpecification;
public PatientService( public PatientService(
IUnitOfWork<Patient> patientUnitOfWork, IUnitOfWork<Patient> patientUnitOfWork,
IUnitOfWork<MedicalState> medicalStateUnitOfWork,
IMapper mapper ) IMapper mapper )
:base(patientUnitOfWork , mapper) :base(patientUnitOfWork , mapper)
{ {
_medicalStateUnitOfWork = medicalStateUnitOfWork;
_specification = new PatientWithMedicinesSpecification(); _specification = new PatientWithMedicinesSpecification();
_medicalStateSpecification = new MedicalStateWithMedicinesSpecification(); _medicalStateSpecification = new MedicalStateWithMedicinesSpecification();
} }
public IEnumerable<MedicalStateModel> GetPatientMedicalStates(int patientId) {
return _mapper.Map<IEnumerable<MedicalStateModel>>( _unitOfWork.Entity public async Task<IEnumerable<MedicalStateModel>> GetPatientMedicalStates(int patientId) {
.GetById(
patientId,_specification
).Result.MedicalStates.AsEnumerable()); return _mapper.Map<IEnumerable<MedicalStateModel>>(
await _unitOfWork.MedicalStates
.GetByPatient(
patientId, _medicalStateSpecification
));
} }
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 _unitOfWork.MedicalStates.GetById(id,_medicalStateSpecification));
} }
public void AddMedicalState (int patientId, MedicalStateModel medicalState) { public void AddMedicalState (int patientId, MedicalStateModel medicalState) {
var ptient = _unitOfWork.Entity.GetById(patientId,_specification).Result; var ptient = _unitOfWork.Entity.GetById(patientId,_specification).Result;
...@@ -52,7 +52,7 @@ namespace ApplicationCore.Services ...@@ -52,7 +52,7 @@ namespace ApplicationCore.Services
ptient.MedicalStates.Add(_mapper.Map<MedicalState>(medicalState)); ptient.MedicalStates.Add(_mapper.Map<MedicalState>(medicalState));
_unitOfWork.Entity.Update(ptient); _unitOfWork.Entity.Update(ptient);
_unitOfWork.Save(); _unitOfWork.Commit();
} }
public bool PatientExists(int id) public bool PatientExists(int id)
...@@ -60,10 +60,10 @@ namespace ApplicationCore.Services ...@@ -60,10 +60,10 @@ namespace ApplicationCore.Services
return _unitOfWork.Entity.GetById(id) is null ? false : true; return _unitOfWork.Entity.GetById(id) is null ? false : true;
} }
public async Task<Patient> GetByUserID(string id) public async Task<PatientModel> GetByUserEmail(string email)
{ {
var ps = await _unitOfWork.Entity.GetAll(_specification); var ps = await _unitOfWork.Entity.GetAll(_specification);
return ps.Where(p => p.UserId == id).FirstOrDefault(); return _mapper.Map<PatientModel>(ps.Where(p => p.User.Email == email).FirstOrDefault());
} }
} }
......
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