public MedicineDto UpdateMedicine(MedicineDto medicineDto)
        {
            bool isValid = MedicineValidator.IsValid(medicineDto);

            if (!isValid)
            {
                throw new ValidationException("Validation for object failed");
            }


            var medicine = MedicineMapper.ToEntity(medicineDto);

            var savedMedicine = _medicinesRepository.GetAll().First(m => m.Id == medicineDto.Id);

            if (savedMedicine == null)
            {
                throw new NotFoundException("Medicines", medicineDto.Id);
            }

            savedMedicine.ModifiedOn = DateTime.Now;
            savedMedicine.FullName   = medicineDto.FullName;
            savedMedicine.Brand      = medicineDto.Brand;
            savedMedicine.Notes      = medicineDto.Notes;
            savedMedicine.Price      = medicineDto.Price;
            savedMedicine.Quantity   = medicineDto.Quantity;
            savedMedicine.ExpiryDate = medicineDto.ExpiryDate;

            _medicinesRepository.UpdateMedicine(savedMedicine);

            return(MedicineMapper.ToDto(savedMedicine));
        }
        public MedicineDto AddMedicine(MedicineDto medicineDto)
        {
            bool isValid = MedicineValidator.IsValid(medicineDto);

            if (!isValid)
            {
                throw new ValidationException("Validation for object failed");
            }

            var medicine = MedicineMapper.ToEntity(medicineDto);

            medicine.Id        = (_medicinesRepository.GetAll().OrderByDescending(_ => _.Id).FirstOrDefault()?.Id ?? 0) + 1;
            medicine.CreatedOn = DateTime.Now;
            _medicinesRepository.AddMedicine(medicine);

            return(MedicineMapper.ToDto(medicine));
        }
 public IEnumerable <MedicineDto> GetAllMedicines()
 {
     return(_medicinesRepository.GetAll()
            .Select(_ => MedicineMapper.ToDto(_)).OrderBy(_ => _.Id));
 }
Beispiel #4
0
 public MedicineService()
 {
     medicineRepository = MedicineRepository.GetInstance();
     medicineMapper     = new MedicineMapper();
 }