public async Task <IActionResult> PostPrescription([FromBody] Prescription prescription) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _prescriptionRepository.CreateAsync(prescription); foreach (var medicinePrescription in prescription.MedicinePrescriptions) { Medicine medicine = await _medicineRepository.FindAsync(medicinePrescription.MedicineId); if (medicinePrescription.Quantity <= medicine.Stock) { medicine.Stock -= medicinePrescription.Quantity; _medicineRepository.Update(medicine); } else { return(BadRequest(ModelState)); } } await _prescriptionRepository.SaveChangesAsync(); return(CreatedAtAction("GetPrescription", new { id = prescription.Id }, prescription)); }
public async Task AddPrescriptionAsync(Prescription prescription) => await _prescriptionRepository.CreateAsync(prescription);