public async Task <IActionResult> Edit([FromRoute] Guid id, [FromBody] MedicamentRequest request) { try { var medicament = await medicamentService.EditAsync(id, request); return(Ok(medicament)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task <IActionResult> Create([FromBody] MedicamentRequest request) { try { await medicamentService.CreateAsync(request); return(Ok()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task CreateAsync(MedicamentRequest request) { Medicament medicament = new Medicament(request.Title, request.Description, request.AmountInPack, request.ManufacturerId, request.MedicalProtocolId); var inBase = await applicationContext.Medicaments.FirstOrDefaultAsync(x => x.Title == request.Title && x.Description == request.Description); if (inBase != null) { throw new Exception(localizer["Medicament already exists."]); } await applicationContext.Medicaments.AddAsync(medicament); await applicationContext.SaveChangesAsync(); }
public async Task <Medicament> EditAsync(Guid id, MedicamentRequest request) { Medicament newMedicament = new Medicament(request.Title, request.Description, request.AmountInPack, request.ManufacturerId, request.MedicalProtocolId); Medicament medicament = await GetAsync(id); if (medicament == null) { throw new Exception(localizer["Medicament with this identifier doesn`t exist."]); } medicament = newMedicament; medicament.Id = id; applicationContext.Medicaments.Update(medicament); await applicationContext.SaveChangesAsync(); return(await GetAsync(medicament.Id)); }