public async Task <IActionResult> PutMedication(int id, MedicationDTO medication) { if (id != medication.Id) { return(BadRequest()); } var dbMed = await _context.Medications.FirstOrDefaultAsync(m => m.Id == id); dbMed.name = medication.Name; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MedicationExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public static MedicationEN Convert(MedicationDTO dto) { MedicationEN newinstance = null; try { if (dto != null) { newinstance = new MedicationEN(); if (dto.CareActivity_oid != -1) { MoSIoTGenNHibernate.CAD.MosIoT.ICareActivityCAD careActivityCAD = new MoSIoTGenNHibernate.CAD.MosIoT.CareActivityCAD(); newinstance.CareActivity = careActivityCAD.ReadOIDDefault(dto.CareActivity_oid); } newinstance.ProductReference = dto.ProductReference; newinstance.Name = dto.Name; newinstance.Manufacturer = dto.Manufacturer; newinstance.Description = dto.Description; newinstance.Dosage = dto.Dosage; newinstance.Form = dto.Form; newinstance.MedicationCode = dto.MedicationCode; } } catch (Exception ex) { throw ex; } return(newinstance); }
public async Task <ServiceResult> CreateMedication([FromBody] MedicationDTO medication) { if (!ModelState.IsValid) { var result = new ServiceResult(); result.Errors = ModelState.SelectMany(x => x.Value.Errors.Select(t => t.ErrorMessage)).ToList(); } return(await _medicationService.CreateMedicationAsync(medication)); }
public async Task <ActionResult <MedicationDTO> > PostMedication(MedicationDTO medication) { var med = new Medication { name = medication.Name }; _context.Medications.Add(med); await _context.SaveChangesAsync(); medication.Id = med.Id; return(CreatedAtAction("GetMedication", new { id = med.Id }, medication)); }
public async Task <bool> UpdateMedication(MedicationDTO medication) { try { await _medicationsClient.PutMedicationAsync(medication.Id, medication); return(true); } catch (Exception ex) { Debug.WriteLine("Couldn't update medication"); Debug.WriteLine(ex.Message); return(false); } }
public async Task <int> AddMedication(MedicationDTO medication) { try { var result = await _medicationsClient.PostMedicationAsync(medication); return(result.Id); } catch (Exception ex) { Debug.WriteLine("Couldn't add medication"); Debug.WriteLine(ex.Message); return(0); } }
public async Task <ActionResult <MedicationDTO> > GetMedication(int id) { var medication = await _context.Medications.FindAsync(id); var medDTO = new MedicationDTO { Id = medication.Id, Name = medication.name }; if (medication == null) { return(NotFound()); } return(medDTO); }
public async Task <ActionResult <MedicationDTO> > DeleteMedication(int id) { var medication = await _context.Medications.FindAsync(id); if (medication == null) { return(NotFound()); } var dto = new MedicationDTO { Id = id, Name = medication.name }; _context.Medications.Remove(medication); await _context.SaveChangesAsync(); return(dto); }
public async Task <ServiceResult> CreateMedicationAsync(MedicationDTO medicationDTO) { var medication = new Medication { Name = medicationDTO.Name, Quantity = medicationDTO.Quantity, CreationDate = DateTime.Now }; await _context.Medications.AddAsync(medication); var saved = await _context.SaveChangesAsync(); if (saved > 0) { return(new ServiceResult()); } return(new ServiceResult { Errors = new List <string> { "Error while creating a new Medication!" } }); }
public HttpResponseMessage Modify(int idMedication, [FromBody] MedicationDTO dto) { // CAD, CEN, returnValue MedicationRESTCAD medicationRESTCAD = null; MedicationCEN medicationCEN = null; MedicationDTOA returnValue = null; // HTTP response HttpResponseMessage response = null; string uri = null; try { SessionInitializeTransaction(); medicationRESTCAD = new MedicationRESTCAD(session); medicationCEN = new MedicationCEN(medicationRESTCAD); // Modify medicationCEN.Modify(idMedication, dto.Name , dto.Manufacturer , dto.Description , dto.Dosage , dto.Form , dto.MedicationCode ); // Return modified object returnValue = MedicationAssembler.Convert(medicationRESTCAD.ReadOIDDefault(idMedication), session); SessionCommit(); } catch (Exception e) { SessionRollBack(); if (e.GetType() == typeof(HttpResponseException)) { throw e; } else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto")) { throw new HttpResponseException(HttpStatusCode.Forbidden); } else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.DataLayerException)) { throw new HttpResponseException(HttpStatusCode.BadRequest); } else { throw new HttpResponseException(HttpStatusCode.InternalServerError); } } finally { SessionClose(); } // Return 404 - Not found if (returnValue == null) { return(this.Request.CreateResponse(HttpStatusCode.NotFound)); } // Return 200 - OK else { response = this.Request.CreateResponse(HttpStatusCode.OK, returnValue); return(response); } }
public HttpResponseMessage New_([FromBody] MedicationDTO dto) { // CAD, CEN, returnValue, returnOID MedicationRESTCAD medicationRESTCAD = null; MedicationCEN medicationCEN = null; MedicationDTOA returnValue = null; int returnOID = -1; // HTTP response HttpResponseMessage response = null; string uri = null; try { SessionInitializeTransaction(); medicationRESTCAD = new MedicationRESTCAD(session); medicationCEN = new MedicationCEN(medicationRESTCAD); // Create returnOID = medicationCEN.New_( //Atributo OID: p_careActivity // attr.estaRelacionado: true dto.CareActivity_oid // association role , dto.ProductReference //Atributo Primitivo: p_productReference , dto.Name //Atributo Primitivo: p_name , dto.Manufacturer //Atributo Primitivo: p_manufacturer , dto.Description //Atributo Primitivo: p_description , dto.Dosage //Atributo Primitivo: p_dosage , dto.Form //Atributo Primitivo: p_form , dto.MedicationCode //Atributo Primitivo: p_medicationCode ); SessionCommit(); // Convert return returnValue = MedicationAssembler.Convert(medicationRESTCAD.ReadOIDDefault(returnOID), session); } catch (Exception e) { SessionRollBack(); if (e.GetType() == typeof(HttpResponseException)) { throw e; } else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto")) { throw new HttpResponseException(HttpStatusCode.Forbidden); } else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.DataLayerException)) { throw new HttpResponseException(HttpStatusCode.BadRequest); } else { throw new HttpResponseException(HttpStatusCode.InternalServerError); } } finally { SessionClose(); } // Return 201 - Created response = this.Request.CreateResponse(HttpStatusCode.Created, returnValue); // Location Header /* * Dictionary<string, object> routeValues = new Dictionary<string, object>(); * * // TODO: y rolPaths * routeValues.Add("id", returnOID); * * uri = Url.Link("GetOIDMedication", routeValues); * response.Headers.Location = new Uri(uri); */ return(response); }