public static RSR AddTeamUser(string sIdentity, string sTenant, string sTeamName, string sDivision, string sEmail, bool fAddTeam) { Sql sql = null; Sql.OpenConnection(out sql, Startup._sResourceConnString); RSR rsr = RSR.Failed("unknown"); try { sIdentity = Sql.Sqlify(sIdentity); sTeamName = Sql.Sqlify(sTeamName); sDivision = Sql.Sqlify(sDivision); Guid tenant = Guid.Parse(sTenant); // first, check to see if the team exists int c = sql.NExecuteScalar($"select Count(*) from rwllteams where TeamName='{sTeamName}'"); if (c != 1 && !fAddTeam) { throw new Exception($"team {sTeamName} does not exist and we were not asked to add the team"); } sql.BeginTransaction(); if (c != 1) { // add the team string sInsertTeam = $"INSERT INTO rwllteams (TeamName, Division, PW, DateCreated, DateUpdated, FieldsReleaseCount, CagesReleaseCount, ReleasedFieldsToday, ReleasedFieldsDate, ReleasedCagesToday, ReleasedCagesDate, Email1, Email2)" + $"VALUES ('{sTeamName}', '{sDivision}', '', '{DateTime.Now}', '{DateTime.Now}', 0, 0, 0, '1/1/2013', 0, '1/1/2013', '{sEmail}', '')"; rsr = RSR.FromSR(Sql.ExecuteNonQuery(sql, sInsertTeam, null)); if (!rsr.Succeeded) { throw new Exception($"{rsr.Reason} ({sInsertTeam})"); } } // now we have a team; insert the user into the table rsr = InsertAuthUser(sIdentity, sTeamName, tenant, sql); sql.Commit(); } catch (Exception exc) { rsr = RSR.Failed(exc); } finally { if (sql.InTransaction) { sql.Rollback(); } sql.Close(); } return(rsr); }
private static RSR InsertAuthUser(string sIdentity, string sTeamName, Guid tenant, Sql sql) { RSR rsr; string sInsertUser = $"INSERT INTO rwllauth (PrimaryIdentity, Tenant, TeamID) VALUES ('{sIdentity}', '{tenant}', '{sTeamName}')"; rsr = RSR.FromSR(Sql.ExecuteNonQuery(sql, sInsertUser, null)); if (!rsr.Succeeded) { throw new Exception($"{rsr.Reason} ({sInsertUser})"); } return(rsr); }
/* G E T C S V */ /*---------------------------------------------------------------------------- * %%Function: GetCsv * %%Qualified: RwpSvc.Practice:Teams.GetCsv * %%Contact: rlittle * * ----------------------------------------------------------------------------*/ public RSR GetCsv(Stream stm) { CsvTeams csvt = new CsvTeams(); SqlWhere sw = new SqlWhere(); TextWriter tw = new StreamWriter(stm); sw.AddAliases(RwpTeam.s_mpAliases); m_plrwpt = new List <RwpTeam>(); // StringBuilder sb = new StringBuilder(4096); RSR sr = RSR.FromSR(Sql.ExecuteQuery(sw.GetWhere(RwpTeam.s_sSqlQueryString), this, Startup._sResourceConnString)); tw.WriteLine(csvt.Header()); foreach (RwpTeam rwpt in m_plrwpt) { tw.WriteLine(csvt.CsvMake(rwpt)); } tw.Flush(); return(RSR.Success()); }
public static RSR ClearTeams() { return(RSR.FromSR(Sql.ExecuteNonQuery("DELETE FROM rwllteams WHERE TeamName <> 'Administrator'", Startup._sResourceConnString))); }
/* I M P O R T C S V */ /*---------------------------------------------------------------------------- * %%Function: ImportCsv * %%Qualified: RwpSvc.Practice:Teams.ImportCsv * %%Contact: rlittle * * ----------------------------------------------------------------------------*/ public static RSR ImportCsv(Stream stm) { RSR sr; Sql sql; CsvTeams csv = new CsvTeams(); Random rnd = new Random(System.Environment.TickCount); TextReader tr = new StreamReader(stm); // process each line sr = RSR.FromSR(Sql.OpenConnection(out sql, Startup._sResourceConnString)); if (!sr.Result) { return(sr); } sr = RSR.FromSR(sql.BeginTransaction()); if (!sr.Result) { return(sr); } bool fHeadingRead = false; string sLine; int iLine = 0; RwpTeam rwpt; bool fAdd; List <string> plsDiff; try { while ((sLine = tr.ReadLine()) != null) { iLine++; if (sLine.Length < 2) { continue; } if (!fHeadingRead) { sr = csv.ReadHeaderFromString(sLine); if (!sr.Result) { throw new Exception(String.Format("Failed to make heading at line {0}: {1}", iLine - 1, sr.Reason)); } fHeadingRead = true; continue; } sr = csv.LoadRwptFromCsv(sLine, sql, out rwpt, out fAdd, out plsDiff); if (!sr.Result) { throw new Exception(String.Format("Failed to process line {0}: {1}", iLine - 1, sr.Reason)); } if (rwpt.Name == "") { continue; } // at this point, rwpt is a fully loaded team; check for errors and generate a passowrd if necessary bool fTeamExists = false; bool fAuthExists = false; sr = rwpt.Preflight(sql, out fTeamExists, out fAuthExists); if (!sr.Result) { throw new Exception(String.Format("Failed to preflight line {0}: {1}", iLine - 1, sr.Reason)); } if (!fTeamExists) { if (rwpt.Created == null) { rwpt.Created = DateTime.Now; } if (rwpt.Updated == null) { rwpt.Updated = rwpt.Created; } if (rwpt.ReleasedCagesDate == null) { rwpt.ReleasedCagesDate = DateTime.Parse("1/1/2013"); } if (rwpt.ReleasedFieldsDate == null) { rwpt.ReleasedFieldsDate = DateTime.Parse("1/1/2013"); } // at this point, we would insert... string sInsert = rwpt.SGenerateUpdateQuery(sql, fAdd); Sql.ExecuteNonQuery(sql, sInsert, null); } // now, add to the auth table (again checking to see if this already exists) if (!fAuthExists) { InsertAuthUser(rwpt.Identity, rwpt.Name, Guid.Parse(rwpt.Tenant), sql); } } } catch (Exception e) { sql.Rollback(); sql.Close(); return(RSR.Failed(e)); } sql.Commit(); sql.Close(); return(RSR.Success()); }