public ActionResult DeleteAll(List <int> items) { PropertyRepository ir = new PropertyRepository(); List <Property> myList = new List <Property>(); try { if (ir.MyConnection.State == System.Data.ConnectionState.Closed) { ir.MyConnection.Open(); ir.MyTransaction = ir.MyConnection.BeginTransaction(); //pending until committed } foreach (int id in items) { ir.DeleteProperty(id); } if (items.Count > 1) { ViewBag.Success = "Items were deleted successfully"; } else { ViewBag.Success = "Item was deleted successfully"; } ir.MyTransaction.Commit(); //committed //this doesnt need to participate in transaction cos its not changing anything in database, its just getting. myList = ir.GetProperty(); return(View("Index", myList)); } catch (Exception ex) { ir.MyTransaction.Rollback(); if (items.Count > 1) { ViewBag.Error = ex + " - (Error occurred. Items were not deleted)"; } else { ViewBag.Error = ex + " - (Error occurred. Item was not deleted)"; } myList = ir.GetProperty(); return(View("Index", myList)); } finally { if (ir.MyConnection.State == System.Data.ConnectionState.Open) { ir.MyConnection.Close(); } } }
public void GetPropertySuccess() { //Arrange var propertyRepository = new PropertyRepository(_container); var dbContext = _serviceProvider.GetRequiredService <DeviserDbContext>(); var properties = TestDataRepository.GetProperties(); var property = properties.First(); propertyRepository.CreateProperty(property); //Act var result = propertyRepository.GetProperty(property.Id); //Assert Assert.NotNull(result); Assert.NotEqual(result.Id, Guid.Empty); Assert.True(!string.IsNullOrEmpty(result.Name)); Assert.True(!string.IsNullOrEmpty(result.Label)); Assert.True(result.CreatedDate > DateTime.MinValue); Assert.True(result.LastModifiedDate > DateTime.MinValue); //Clean dbContext.Property.RemoveRange(dbContext.Property); }
public ActionResult RefreshCache() { //1. Load items from cache RedisRepository rr = new RedisRepository(); var items = rr.LoadItems(); //2. hash the serialized value of items PropertyRepository pr = new PropertyRepository(); pr.MyConnection.Open(); var itemsFromDb = pr.GetProperty(); pr.MyConnection.Close(); if (items == null) { rr.StoreItems(itemsFromDb); return(Content("done")); } //3. compare the digest produced with the digest produced earlier while stored in application variable if (rr.HashValue(JsonConvert.SerializeObject(items)) != rr.HashValue(JsonConvert.SerializeObject(itemsFromDb))) { //4. if they do not match; storeincache method and re-produce a new hashcode rr.StoreItems(itemsFromDb); return(Content("done")); } return(Content("properties were not updated since they are still the same")); }
public ActionResult StoreInRedis() { PropertyRepository ir = new PropertyRepository(); try { if (ir.MyConnection.State == System.Data.ConnectionState.Closed) { ir.MyConnection.Open(); } var list = ir.GetProperty(); //foreach (var item in list) //{ // var name = ir.GetPropertyName(item.Name); // item.Name = name; //} RedisRepository rr = new RedisRepository(); return(View("Index", list)); } catch (Exception ex) { ViewBag.Error = ex + " - (Error occurred. Items were not deleted)"; return(View(new List <Property>())); } finally { if (ir.MyConnection.State == System.Data.ConnectionState.Open) { ir.MyConnection.Close(); } } }
public ActionResult DeleteItems(List <int> items) { if (items.Count > 0) { PropertyRepository pr = new PropertyRepository(); List <Property> myList = new List <Property>(); try { if (pr.MyConnection.State == System.Data.ConnectionState.Closed) { pr.MyConnection.Open(); pr.MyTransaction = pr.MyConnection.BeginTransaction(); } //delete all items foreach (int id in items) { pr.DeleteProperty(id); } pr.MyTransaction.Commit(); if (items.Count == 1) { ViewBag.Success = "Property was deleted"; } else { ViewBag.Success = "Properties were deleted"; } } catch (Exception ex) { pr.MyTransaction.Rollback(); //log exception LoggingRepository.ReportError(ex); ViewBag.Error = "Error occurred. Nothing was deleted; Try again later"; } finally { myList = pr.GetProperty().ToList(); //to refresh the now updated list if (pr.MyConnection.State == System.Data.ConnectionState.Open) { pr.MyConnection.Close(); } } return(View("Index", myList)); } return(RedirectToAction("Index")); }
public void GetPropertyFail() { //Arrange var propertyRepository = new PropertyRepository(_container); var dbContext = _serviceProvider.GetRequiredService <DeviserDbContext>(); var properties = TestDataRepository.GetProperties(); //Act var result = propertyRepository.GetProperty(Guid.NewGuid()); //Assert Assert.Null(result); //Clean dbContext.Property.RemoveRange(dbContext.Property); }
public ActionResult Index() { PropertyRepository pr = new PropertyRepository(); try { //1. Load items from cache RedisRepository rr = new RedisRepository(); var items = rr.LoadItems(); //2. hash the serialized value of items pr.MyConnection.Open(); var itemsFromDb = pr.GetProperty(); pr.MyConnection.Close(); if (items == null) { rr.StoreItems(itemsFromDb); items = rr.LoadItems(); } //3. compare the digest produced with the digest produced earlier while stored in application variable if (rr.HashValue(JsonConvert.SerializeObject(items)) != rr.HashValue(JsonConvert.SerializeObject(itemsFromDb))) { //4. if they do not match //storeincache method and re-produce a new hashcode rr.StoreItems(itemsFromDb); items = rr.LoadItems(); } return(View(items)); } catch (Exception ex) { LoggingRepository.ReportError(ex); ViewBag.Error = ex + " - (Error occurred while querying items)"; return(View(new List <Property>())); } finally { if (pr.MyConnection.State == System.Data.ConnectionState.Open) { pr.MyConnection.Close(); } } }
public PropertyResponse GetProperty(int id) { var property = PropertyRepository.GetProperty(id); return(_mapper.Map <PropertyResponse>(property)); }