/// <summary> /// Remove a dynamic rule from the Database. /// </summary> /// <param name="dynamic">The dynamic rule to remove</param> /// <returns>true if the rule is successfully deleted and false otherwise.</returns> public bool DeleteDynamicRule(DyanamicRule dynamic) { bool value = false; try { if (dynamic.RuleId != 0) { using (var context = new UTSDatabaseEntities()) { var saved = context.DyanamicRules.Find(dynamic.RuleId); context.DyanamicRules.Remove(saved); context.SaveChanges(); value = true; return(value); } } else { return(value); } } catch (NullReferenceException) { return(false); } }
public void DeleteDynamicRule_InvalidRuleId_ReturnsFalse() { //Arrange Configure dynamicRuleTest = new Configure(); DyanamicRule dynamic = new DyanamicRule { RuleId = 0 }; //Act bool isRuleDeleted = dynamicRuleTest.DeleteDynamicRule(dynamic); //Assert Assert.IsFalse(isRuleDeleted, "Rule does not exsist"); }
/// <summary> /// This fnuction approves or rejects dynamic rules as requested /// </summary> /// <param name="dyrule"> Is a refernece of Dynamic rules table which helps to access the values stored in the variables of dynamic rule and /// stores the value entered by a user and other values.</param> /// <param name="selectedValue">The value selected as approved or rejected</param> /// <returns>true if the chnages are done nd saved , false otherwise</returns> public bool ApproveRejectRule(DyanamicRule dyrule, string selectedValue) { using (var usersDbContext = new UsersEntities()) { bool isSaved = false; using (var context = new UTSDatabaseEntities()) { DyanamicRule storedRule = context.DyanamicRules.Find(dyrule.RuleId); storedRule.Status = selectedValue == "Approve" ? (RulesStatus.Approved).ToString() : (RulesStatus.Rejected).ToString(); context.SaveChanges(); isSaved = true; } return(isSaved); } }
public void DeleteDynamicRule_ValidRuleIdInput_ReturnsTrue() { //Arrange Configure dynamicRuleTest = new Configure(); DyanamicRule dynamic = new DyanamicRule { RuleId = 16 // add rule id here on the final day. }; //Act bool isRuleDeleted = dynamicRuleTest.DeleteDynamicRule(dynamic); //Assert Assert.IsTrue(isRuleDeleted, "Rule Deleted."); }
public void SaveDynamicRule_QueryOrResponseIsNull_returnsFalse() { //Arrange Configure dynamicRuleTest = new Configure(); DyanamicRule dynamic = new DyanamicRule { Query = null, Response = null }; //Act bool isRuleSaved = dynamicRuleTest.SaveDynamicRule(dynamic, "*****@*****.**"); //Assert Assert.IsFalse(isRuleSaved, "Rule Not Saved"); }
public void SaveDynamicRule_QueryAlreadyExsists_returnsFalse() { //Arrange Configure dynamicRuleTest = new Configure(); DyanamicRule dynamic = new DyanamicRule() { Query = "What will happen in Week [Keyword] ", Response = "hi" }; //Act bool isRuleSaved = dynamicRuleTest.SaveDynamicRule(dynamic, "*****@*****.**"); //Assert Assert.IsFalse(isRuleSaved, "Query Already exsists."); }
/// <summary> /// This funtion represts the logic to edit an exsiting dynamic rule which is requested by the user. /// </summary> /// <param name="dynamic"> Is a refernece of Dynamicrule which helps to access the values stored in the variables of dynamic rule and /// stores the value entered by a user and other values.</param> /// <param name="loggedinUser">The session email of the logged in user (user.identity.name)</param> /// <returns>true if the rule is editted and flase when the rule can not be editted</returns> public bool EditDynamicRule(DyanamicRule dynamic, string loggedinUser) { using (var usersDbContext = new UsersEntities()) { string userid = (from row in usersDbContext.AspNetUsers where row.UserName == loggedinUser select row.Id).First(); bool isSaved = false; if (dynamic.Query == "" && dynamic.Response == "") { return(isSaved); } else if (dynamic.Query == null && dynamic.Response == null) { return(isSaved); } else { using (var context = new UTSDatabaseEntities()) { var query_search = (from row in context.DyanamicRules.ToList() where row.Query == dynamic.Query select row.Query).FirstOrDefault(); if (query_search.ToLower() == dynamic.Query.ToLower()) { return(isSaved); } else { var stored = context.DyanamicRules.Find(dynamic.RuleId); var oldvalue = stored; stored.Status = RulesStatus.Updated.ToString(); stored.Query = dynamic.Query; stored.Response = dynamic.Response; stored.Keyword = dynamic.Keyword; stored.Match = dynamic.Match; stored.LastUpdatedby = loggedinUser; context.Entry(oldvalue).CurrentValues.SetValues(stored); context.SaveChanges(); isSaved = true; return(isSaved); } } } } }
public ActionResult Create([Bind(Include = "Query, Response, Keyword, Match")] DyanamicRule dynamic) { List <SelectListItem> columnList = new List <SelectListItem>(); if (!ModelState.IsValid) { Create(); return(View(dynamic)); } bool rule = configure.SaveDynamicRule(dynamic, User.Identity.Name); if (rule == true) { return(RedirectToAction("Index")); } else { ViewBag.Error = "This Query Already Exists."; Create(); return(View()); } }
/// <summary> /// This funtion represts the logic to save a dynamic rule which is requested by the user. /// </summary> /// <param name="dynamic"> Is a refernece of Dynamicrule which helps to access th values stored in the variables of dynamic rule and /// stores the value entered by a user and other values.</param> /// <param name="loggedinUser">The session email of the logged in user (user.identity.name)</param> /// <returns>Returns true when the rule is saved and false when the rule is not saved or already exsits.</returns> public bool SaveDynamicRule(DyanamicRule dynamic, string loggedinUser) { using (var usersDbContext = new UsersEntities()) { string userid = (from row in usersDbContext.AspNetUsers where row.UserName == loggedinUser select row.Id).First(); bool isSaved = false; if (dynamic.Query == "" && dynamic.Response == "") { return(isSaved); } else { using (var context = new UTSDatabaseEntities()) { var query = (from row in context.DyanamicRules.ToList() where row.Query == dynamic.Query select row.Query).FirstOrDefault(); //Checking if the query already exsist if (query == dynamic.Query) { return(isSaved); } dynamic.Type = "Dynamic"; dynamic.Createdby = loggedinUser; dynamic.LastUpdatedby = loggedinUser; dynamic.UserId = userid; dynamic.Status = RulesStatus.Created.ToString(); context.DyanamicRules.Add(dynamic); context.SaveChanges(); isSaved = true; } } return(isSaved); } }
public ActionResult Edit(DyanamicRule dynamic) { if (!ModelState.IsValid) { List <SelectListItem> columnList = new List <SelectListItem>(); var columns = typeof(TopicInformationViewModel).GetProperties() .Select(property => property.Name) .ToList(); columnList = columns.Select(x => new SelectListItem() { Value = x, Text = x }).ToList(); ViewBag.TopicInformationList = columnList; return(View(dynamic)); } bool rule = configure.SaveDynamicRule(dynamic, User.Identity.Name); if (rule == true) { return(RedirectToAction("Index")); } else { ViewBag.Error = "This Query Already Exists."; List <SelectListItem> columnList = new List <SelectListItem>(); var columns = typeof(TopicInformationViewModel).GetProperties() .Select(property => property.Name) .ToList(); columnList = columns.Select(x => new SelectListItem() { Value = x, Text = x }).ToList(); ViewBag.TopicInformationList = columnList; return(View()); } }