public async Task <IHttpActionResult> PostMedication(MedicationBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var medication = new Medication() { MedicationId = Guid.NewGuid(), GenericName = model.GenericName, Code = model.Code }; try { if (MedicationExists(medication) == false) { db.Medication.Add(medication); await db.SaveChangesAsync(); } else { return(BadRequest("The medication name or code already exists in the databae.")); } } catch (DbUpdateException) { throw; } return(Created("medications/" + medication.MedicationId, ToDto.MedicationToDto(medication))); }
public async Task <IHttpActionResult> GetMedications() { List <Medication> medications; medications = await db.Medication .Take(30) .ToListAsync(); var medicationsDto = new List <MedicationDto>(); foreach (var medication in medications) { medicationsDto.Add(ToDto.MedicationToDto(medication)); } return(Ok(medicationsDto)); }