public void LogError(ExceptionLog error) { ////if the same error has been logged from the same ip less than 5 minutes ago, ////simply update the existing one. error.ExceptionDateTime = Helper.GetLocalDate(); System.DateTime timeFiveMinutesAgo = Helper.GetLocalDate().AddMinutes(-5); dapperContext.RollbackTransaction(); dapperContext.OpenIfNot(); var ex = context.Query <ExceptionLog>("Select * From ExceptionLog Where UserIPAddress = @UserIPAddress And ExceptionPage = @ExceptionPage And ExceptionType = @ExceptionType And ExceptionDatetime > @timeFiveMinutesAgo; ", new { UserIPAddress = error.UserIPAddress, ExceptionPage = error.ExceptionPage, ExceptionType = error.ExceptionType, timeFiveMinutesAgo = timeFiveMinutesAgo }).FirstOrDefault(); if (ex == null) { context.Insert <ExceptionLog>(error, excludeFieldList: new string[] { "ExceptionId" }); } else { var sql = new StringBuilder(); error.ExceptionId = ex.ExceptionId; sql.AppendLine("UPDATE ExceptionLog SET ExceptionDatetime = @ExceptionDatetime, ExceptionDetails = @ExceptionDetails, ExceptionMessage = @ExceptionMessage, LoggedInUser = @LoggedInUser, ExceptionVersion = @ExceptionVersion "); sql.AppendLine("Where ExceptionId = @ExceptionId; "); context.Execute(sql.ToString(), error); } dapperContext.CloseIfNot(); }
public int EditRole(RoleViewModel roleToEdit) { var dbVersion = GetRole(roleToEdit.CurrentRole.RoleId); AuditRepository ar = new AuditRepository(dapperContext); var t = dapperContext.GetTransaction(); var sql = "UPDATE Role SET RoleName = @RoleName, Description = @Description Where RoleId = @RoleId AND CONVERT(bigint,RowVersionNo) = @RowVersionNo2; "; var rowAffected = context.Execute(sql, roleToEdit.CurrentRole, transaction: t); if (rowAffected > 0) { var list = context.Query <Guid>("SELECT ModuleID FROM RoleModuleAccess WHERE RoleID = @RoleID; ", new { RoleID = roleToEdit.CurrentRole.RoleId }, transaction: t).ToList(); sql = "UPDATE RoleModuleAccess SET ViewAccess = @ViewAccess, CreateAccess = @CreateAccess, EditAccess = @EditAccess, DeleteAccess = @DeleteAccess, VerifyAccess = @VerifyAccess, MakeOrCheckAccess = @MakeOrCheckAccess WHERE RoleId = @RoleID AND ModuleID = @ModuleID; "; var temp = roleToEdit.ModuleAccessList.Where(m => (m.CreateAccess == true || m.DeleteAccess == true || m.EditAccess == true || m.ViewAccess == true || m.VerifyAccess == true || m.MakeOrCheckAccess == true) && list.Contains(m.ModuleId)).Select(s => s).ToList(); foreach (var m in temp) { m.RoleId = roleToEdit.CurrentRole.RoleId; context.Execute(sql, m, transaction: t); } temp = roleToEdit.ModuleAccessList.Where(m => (m.CreateAccess == true || m.DeleteAccess == true || m.EditAccess == true || m.ViewAccess == true || m.VerifyAccess == true || m.MakeOrCheckAccess == true) && !list.Contains(m.ModuleId)).Select(s => s).ToList(); foreach (var g in temp) { g.RoleId = roleToEdit.CurrentRole.RoleId; } var success = context.InsertMany <RoleModuleAccess>(temp, excludeFieldList: new string[] { "ModuleName" }, transaction: t); var tempId = roleToEdit.ModuleAccessList.Where(m => (m.CreateAccess == false && m.DeleteAccess == false && m.EditAccess == false && m.ViewAccess == false && m.VerifyAccess == false && m.MakeOrCheckAccess == false) && list.Contains(m.ModuleId)).Select(s => s.ModuleId).ToList(); if (tempId.Count > 0) { context.Execute("DELETE FROM RoleModuleAccess WHERE ModuleID IN @ModuleIDList AND RoleID = @RoleID; ", new { RoleID = roleToEdit.CurrentRole.RoleId, ModuleIDList = tempId }, transaction: t); } //List<string> moduleNames = new List<string>(); //foreach (var item in roleToEdit.ModuleAccessList) //{ // moduleNames.Add(item.ModuleName); //} ar.CreateAuditChange(dbVersion, roleToEdit, t, roleToEdit.CurrentRole.RoleName);// ,moduleNames.ToArray()); dapperContext.CommitTransaction(); } else { dapperContext.RollbackTransaction(); } return(rowAffected); }