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)); }
private bool MedicationExists(Medication medication) { bool code_check = db.Medication.Count(e => e.Code == medication.Code) > 0; bool name_check = db.Medication.Count(e => e.GenericName == medication.GenericName) > 0; return (code_check || name_check); }
public static MedicationDto MedicationToDto(Medication medication) { var medicationDto = new MedicationDto(); medicationDto.InjectFrom(medication); return medicationDto; }