Commit d57d3621 authored by hasan khaddour's avatar hasan khaddour

update services responsabilites

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