/// <summary> /// Creates the entity in the database /// </summary> /// <param name="performingUserId">User creating the entity</param> /// <param name="entity">Entity to be created</param> /// <returns>Created entity DTO</returns> protected override IServiceBundleDto Create(int performingUserId, IServiceBundleDto serviceBundle) { using (var context = new PrometheusContext()) { var bundle = context.ServiceBundles.Find(serviceBundle.Id); if (bundle != null) { throw new InvalidOperationException(string.Format("Service Bundle with ID {0} already exists.", serviceBundle.Id)); } var savedBundle = context.ServiceBundles.Add(ManualMapper.MapDtoToServiceBundle(serviceBundle)); context.SaveChanges(performingUserId); return(ManualMapper.MapServiceBundleToDto(savedBundle)); } }
/// <summary> /// Updates the entity in the database /// </summary> /// <param name="performingUserId">User updating the entity</param> /// <param name="entity">Entity to be updated</param> /// <returns>Updated entity DTO</returns> protected override IServiceBundleDto Update(int performingUserId, IServiceBundleDto serviceBundle) { using (var context = new PrometheusContext()) { if (!context.ServiceBundles.Any(x => x.Id == serviceBundle.Id)) { throw new InvalidOperationException("Service Bundle must exist in order to be updated."); } var updatedServiceBundle = ManualMapper.MapDtoToServiceBundle(serviceBundle); context.ServiceBundles.Attach(updatedServiceBundle); context.Entry(updatedServiceBundle).State = EntityState.Modified; context.SaveChanges(performingUserId); return(ManualMapper.MapServiceBundleToDto(updatedServiceBundle)); } }
private int CreateFakeServiceBundle(string name) { var fakeBundle = A.Fake <IServiceBundleDto>(); A.CallTo(() => fakeBundle.Name).Returns(name); using (var context = new PrometheusContext()) { var bundle = ManualMapper.MapDtoToServiceBundle(fakeBundle); context.ServiceBundles.Add(bundle); context.SaveChanges(); return(bundle.Id); } }