Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        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));
        }