public IHttpActionResult PostDrugModify(DrugDto drug) { string msg = ""; if (drug == null) { msg = "参数错误"; } var drugToUpdate = _context.Drugs.Find(drug.Id); //userToUpdate = Mapper.Map<UserDto, User>(user); drugToUpdate.Name = drug.Name; drugToUpdate.Price = drug.Price; try { _context.Entry(drugToUpdate).State = EntityState.Modified; _context.SaveChanges(); msg = "修改成功"; } catch (RetryLimitExceededException) { msg = "网络故障"; } var str = "{ \"Message\" : \"" + msg + "\" , \"" + "Data\" : \"" + "null" + "\" }"; return(Ok(str)); }
public IHttpActionResult PostDrugDelete(DrugDto drug) { string msg = ""; if (drug == null) { msg = "参数错误"; } var drugToDelete = _context.Drugs.Find(drug.Id); if (drugToDelete == null) { msg = "删除失败,该药品不存在"; } else { try { _context.Drugs.Remove(drugToDelete); _context.SaveChanges(); msg = "删除成功"; } catch (RetryLimitExceededException) { msg = "网络故障"; } } var str = "{ \"Message\" : \"" + msg + "\" , \"" + "Data\" : \"" + "null" + "\" }"; return(Ok(str)); }
/// <summary> /// Removes the specified item. /// </summary> /// <param name="item">The item to remove</param> public void Remove(DrugDto item) { Assert.IsNotNull(item, "item"); if (!this.CanRemove(item)) { throw new ReferencialIntegrityException(); } this.Remove <Drug>(item); }
/// <summary> /// Determines whether this instance can remove the specified drug dto. /// </summary> /// <param name="item">The item.</param> /// <returns> /// <c>true</c> if this instance can remove the specified drug dto; otherwise, <c>false</c>. /// </returns> public bool CanRemove(DrugDto item) { return((from t in this.Session.Query <Patient>() where t.PrescriptionDocuments .Where(e => e.Prescriptions .Where(p => p.Drug.Id == item.Id).Count() > 0) .Count() > 0 select t).Count() == 0); }
public void IsInvalid_Drug() { var item = new DrugDto() { Name = string.Empty, }; Assert.IsFalse(item.IsValid()); }
public async Task CreateDrugAsync_ShouldReturnBadRequest() { int count = base.UnitOfWork.Drugs.Get().Count(); DrugDto newDrug = new DrugDto(); DrugController.Validate(newDrug); var result = await DrugController.CreateDrugAsync(newDrug); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(InvalidModelStateResult)); }
public void IsValid_Drug() { var item = new DrugDto() { Name = Guid.NewGuid().ToString(), Tag = new TagDto(TagCategory.Appointment) { Id = 14 }, }; Assert.IsTrue(item.IsValid()); }
public async Task CreateDrugAsync_ShouldAddNewDrug() { int count = base.UnitOfWork.Drugs.Get().Count(); DrugDto newDrug = new DrugDto() { BrandName = "Advil", GenericName = "pain killer", NdcId = "1597845841", Price = 10, Strength = "500 mg" }; DrugController.Validate(newDrug); var result = await DrugController.CreateDrugAsync(newDrug) as CreatedNegotiatedContentResult <DrugDto>; int newCount = base.UnitOfWork.Drugs.Get().Count(); Assert.IsNotNull(result); Assert.AreEqual(count + 1, newCount); }
public async Task <IHttpActionResult> CreateDrugAsync(DrugDto drug) { try { if (ModelState.IsValid) { Drug dbDrug = new Drug { BrandName = drug.BrandName, GenericName = drug.GenericName, NdcId = drug.NdcId, Price = drug.Price, Strength = drug.Strength, CreatedBy = User.Identity.Name }; _unitOfWork.Drugs.Add(dbDrug); await _unitOfWork.CompleteAsync(); drug.Id = dbDrug.Id; _logger.Info($"New drug created with id {drug.Id}"); InvalidateCache(); return(Created(new Uri(Request.RequestUri + "/" + drug.Id), drug)); } else { _logger.Warn(string.Join(" | ", ModelState.Values.SelectMany(e => e.Errors.Take(1)).Select(e => e.ErrorMessage))); return(BadRequest(ModelState)); } } catch (Exception ex) { if (_logger.IsDebugEnabled) { _logger.Debug(ex); return(InternalServerError(ex)); } return(InternalServerError()); } }
public async Task UpdateDrugAsync_ShouldReturnDrugNotFound() { Drug drug = UnitOfWork.Drugs.Get(1); DrugDto updatedDrug = new DrugDto { BrandName = drug.BrandName, GenericName = drug.GenericName, Id = -1, NdcId = drug.NdcId, Price = drug.Price, Strength = drug.Strength }; DrugController.Validate(updatedDrug); var result = await DrugController.UpdateDrugAsync(updatedDrug); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
public async Task <IHttpActionResult> UpdateDrugAsync(DrugDto drug) { try { if (ModelState.IsValid) { Drug dbDrug = await _unitOfWork.Drugs.GetAsync(drug.Id); if (dbDrug == null) { _logger.Warn($"Invalid drugId: {drug.Id}"); return(NotFound()); } dbDrug.BrandName = drug.BrandName; dbDrug.GenericName = drug.GenericName; dbDrug.NdcId = drug.NdcId; dbDrug.Price = drug.Price; dbDrug.Strength = drug.Strength; dbDrug.UpdatedBy = User.Identity.Name; await _unitOfWork.CompleteAsync(); InvalidateCache(); return(Ok(drug)); } else { _logger.Warn(string.Join(" | ", ModelState.Values.SelectMany(e => e.Errors.Take(1)).Select(e => e.ErrorMessage))); return(BadRequest(ModelState)); } } catch (Exception ex) { if (_logger.IsDebugEnabled) { _logger.Debug(ex); return(InternalServerError(ex)); } return(InternalServerError()); } }
/// <summary> /// Create the specified item into the database /// </summary> /// <param name="item">The item to add in the database</param> public long Create(DrugDto item) { Assert.IsNotNull(item, "item"); var exist = (from i in this.Session.Query <Drug>() where i.Name.ToUpper() == item.Name.ToUpper() || i.Id == item.Id select i).ToList().Count() > 0; if (exist) { throw new ExistingItemException(); } var entity = Mapper.Map <DrugDto, Drug>(item); item.Id = (long)this.Session.Save(entity); return(item.Id); }
private DrugDto Clone(DrugDto drug) { return(new DrugDto() { Id = drug.Id, IsImported = drug.IsImported, Name = drug.Name, Notes = drug.Notes, Segretator = drug.Segretator, Tag = new TagDto(drug.Tag.Category) { Id = drug.Tag.Id, IsImported = drug.Tag.IsImported, Name = drug.Tag.Name, Notes = drug.Tag.Notes, Segretator = drug.Tag.Segretator, } }); }
public async Task UpdateDrugAsync_ShouldReturnUpdatedDrug() { Drug drug = UnitOfWork.Drugs.Get(1); DrugDto updatedDrug = new DrugDto { BrandName = "Updated brand name", GenericName = "Updated generic name", Id = drug.Id, NdcId = drug.NdcId, Price = drug.Price, Strength = drug.Strength }; DrugController.Validate(updatedDrug); var result = await DrugController.UpdateDrugAsync(updatedDrug) as OkNegotiatedContentResult <DrugDto>; Assert.IsNotNull(result); Assert.AreEqual(result.Content.BrandName, updatedDrug.BrandName); Assert.AreEqual(result.Content.GenericName, updatedDrug.GenericName); }
public async Task UpdateDrugAsync_ShouldReturnBadRequest() { Drug drug = UnitOfWork.Drugs.Get(1); DrugDto updatedDrug = new DrugDto { BrandName = string.Empty, GenericName = string.Empty, Id = drug.Id, NdcId = drug.NdcId, Price = drug.Price, Strength = drug.Strength }; //Validation happens when the posted data is bound to the view model. The view model is then passed into the controller. You are skipping part 1 and passing a view model straight into a controller. DrugController.Validate(updatedDrug); var result = await DrugController.UpdateDrugAsync(updatedDrug); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(InvalidModelStateResult)); }
public IHttpActionResult PostDrugAdd(DrugDto drug) { string msg = ""; if (drug == null) { msg = "参数错误"; } var drugToAdd = Mapper.Map <DrugDto, Drug>(drug); try { _context.Drugs.Add(drugToAdd); _context.SaveChanges(); msg = "添加成功"; } catch (RetryLimitExceededException) { msg = "网络故障"; } var str = "{ \"Message\" : \"" + msg + "\" , \"" + "Data\" : \"" + "null" + "\" }"; return(Ok(str)); }
public static void SetDrug(DependencyObject target, DrugDto value) { target.SetValue(DrugyProperty, value); }
/// <summary> /// Updates the specified drug. /// </summary> /// <param name="drug">The drug.</param> public void Update(DrugDto drug) { new Updator(this.Session).Update(drug); }
/// <summary> /// Removes the specified item. /// </summary> /// <param name="item">The item to remove</param> public void Remove(DrugDto item) { new Remover(this.Session).Remove(item); }
/// <summary> /// Create the specified item into the database /// </summary> /// <param name="item">The item to add in the database</param> /// <returns></returns> public long Create(DrugDto item) { return(new Creator(this.Session).Create(item)); }
/// <summary> /// Determines whether this instance can remove the specified drug dto. /// </summary> /// <param name="item">The item.</param> /// <returns> /// <c>true</c> if this instance can remove the specified drug dto; otherwise, <c>false</c>. /// </returns> public bool CanRemove(DrugDto item) { return(new Remover(this.Session).CanRemove(item)); }
/// <summary> /// Updates the specified drug. /// </summary> /// <param name="drug">The drug.</param> public void Update(DrugDto drug) { var entity = Mapper.Map <DrugDto, Drug>(drug); this.Session.Update(entity); }