public IActionResult Put(int id, [FromBody] MedicationPostView medicationPostView) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } CustomerMed customerMed = new CustomerMed { CustomerMedId = medicationPostView.CustomerMedId, MedicationId = medicationPostView.MedicationId, CustomerId = medicationPostView.CustomerId, Usage = medicationPostView.Usage, Frequency = medicationPostView.Frequency, Notes = medicationPostView.Notes, ShowOnPublicView = medicationPostView.ShowOnPublicView, CustUserName = medicationPostView.CustUserName }; Medication medication = new Medication { MedicationId = medicationPostView.MedicationId, GenericName = medicationPostView.GenericName, BrandName = medicationPostView.BrandName, Dosage = medicationPostView.Dosage, SideEffects = medicationPostView.SideEffects, DrugInteractions = medicationPostView.DrugInteractions }; _context.Entry(medication).State = EntityState.Modified; _context.Entry(customerMed).State = EntityState.Modified; try { _context.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!MedicationExists(medicationPostView.MedicationId)) { return(NotFound()); } else { throw; } } return(new StatusCodeResult(StatusCodes.Status204NoContent)); }
public IActionResult Post([FromBody] MedicationPostView medicationPostView) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // checks to see if current drug exists var currentMedication = (from a in _context.Medication where a.BrandName == medicationPostView.BrandName && a.Dosage == medicationPostView.Dosage select new Medication { MedicationId = a.MedicationId, GenericName = a.GenericName, BrandName = a.BrandName, Dosage = a.Dosage, SideEffects = a.SideEffects, DrugInteractions = a.DrugInteractions }); //if medication exists, it will use current drug to create new Customer Medication if (currentMedication.Count <Medication>() > 0) { CustomerMed existingCustomerMed = new CustomerMed { MedicationId = medicationPostView.MedicationId, CustomerId = medicationPostView.CustomerId, Usage = medicationPostView.Usage, Frequency = medicationPostView.Frequency, Notes = medicationPostView.Notes, ShowOnPublicView = medicationPostView.ShowOnPublicView, CustUserName = medicationPostView.CustUserName }; _context.CustomerMed.Add(existingCustomerMed); _context.SaveChanges(); return(Ok(medicationPostView)); } //otherwise create new medication else { Medication medication = new Medication { GenericName = medicationPostView.GenericName, BrandName = medicationPostView.BrandName, Dosage = medicationPostView.Dosage, SideEffects = medicationPostView.SideEffects, DrugInteractions = medicationPostView.DrugInteractions }; _context.Medication.Add(medication); _context.SaveChanges(); //get last medication added var newMed = currentMedication.Last(); //use that medication ID to create new customerMed CustomerMed customerMed = new CustomerMed { MedicationId = newMed.MedicationId, CustomerId = medicationPostView.CustomerId, Usage = medicationPostView.Usage, Frequency = medicationPostView.Frequency, Notes = medicationPostView.Notes, ShowOnPublicView = medicationPostView.ShowOnPublicView, CustUserName = medicationPostView.CustUserName }; _context.CustomerMed.Add(customerMed); try { _context.SaveChanges(); } catch (DbUpdateException) { if (CustomerMedExists(customerMed.CustomerMedId)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(Ok(medicationPostView)); } }