private async Task GetMedications(int id)
        {
            Medications = await _context.Medications.ToListAsync();

            var medicationDistributions = await _context.MedicationDistributions.ToListAsync();

            MedicationUnits = await _context.MedicationUnits.ToListAsync();

            var medicationIds = medicationDistributions.Where(x => x.SubjectId == id).Select(x => x.MedicationId).ToList();

            Medications = Medications.Where(x => medicationIds.Contains(x.ID)).ToList();
        }
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Subject = await _context.Subjects.FirstOrDefaultAsync(m => m.ID == id);

            if (Subject == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            SubjectBuy.RequestDate = DateTime.Now.Date;

            await GetMedications((int)id);

            Medication = Medications.Where(x => x.Name.Trim().ToUpper().Equals(SubjectBuy.MedicationName.Trim().ToUpper())).First();
            var _medicationUnits = MedicationUnits.Where(x => x.MedicationId == Medication.ID).ToList();

            _medicationUnits = _medicationUnits.Where(x => x.SiteId == Subject.SiteId).ToList();

            int quantity = _medicationUnits.Sum(x => x.Quantity);

            if (quantity < SubjectBuy.Quantity)
            {
                QuantityError = "The specified quantity is not available!";
                return(Page());
            }
            else
            {
                QuantityError = null;
            }

            quantity = SubjectBuy.Quantity;

            int counter = 0;

            while (quantity > 0)
            {
                if (quantity > _medicationUnits[counter].Quantity)
                {
                    quantity -= _medicationUnits[counter].Quantity;
                    _medicationUnits[counter].Quantity = 0;
                }
                else
                {
                    _medicationUnits[counter].Quantity -= quantity;
                    quantity = 0;
                }

                counter++;
            }

            foreach (var medicationUnit in _medicationUnits)
            {
                _context.Attach(medicationUnit).State = EntityState.Modified;
            }

            _context.SubjectBuys.Add(SubjectBuy);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SubjectExists(Subject.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }