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); }
/* S R F R O M P L S */ /*---------------------------------------------------------------------------- * %%Function: SRFromPls * %%Qualified: RwpSvc.Practice:Teams:RwpTeam.SRFromPls * %%Contact: rlittle * * ----------------------------------------------------------------------------*/ public static RSR SRFromPls(string sReason, List <string> plsDiff) { string s = sReason; foreach (string sItem in plsDiff) { s += sItem + ", "; } return(RSR.Failed(s)); }
/* L O A D R W P T F R O M C S V */ /*---------------------------------------------------------------------------- * %%Function: LoadRwptFromCsv * %%Qualified: RwpSvc.Practice:Teams:CsvTeams.LoadRwptFromCsv * %%Contact: rlittle * * ----------------------------------------------------------------------------*/ public RSR LoadRwptFromCsv(string sLine, TCore.Sql sql, out RwpTeam rwpt, out bool fAdd, out List <string> plsDiff) { string[] rgs = LineToArray(sLine); SqlWhere sw = new SqlWhere(); fAdd = true; rwpt = new RwpTeam(); plsDiff = new List <string>(); sw.AddAliases(RwpTeam.s_mpAliases); try { rwpt.Name = GetStringVal(rgs, "TEAMNAME"); if (rwpt.Name == "") { return(RSR.Success()); } rwpt.Division = GetStringValNullable(rgs, "DIVISION"); rwpt.Password = GetStringValNullable(rgs, "PW"); rwpt.Created = GetDateValNullable(rgs, "DATECREATED"); rwpt.Updated = GetDateValNullable(rgs, "DATEUPDATED"); rwpt.FieldsReleased = GetIntVal(rgs, "FIELDSRELEASECOUNT"); rwpt.CagesReleased = GetIntVal(rgs, "CAGESRELEASECOUNT"); rwpt.FieldsReleasedToday = GetIntVal(rgs, "RELEASEDFIELDSTODAY"); rwpt.CagesReleasedToday = GetIntVal(rgs, "RELEASEDCAGESTODAY"); rwpt.ReleasedFieldsDate = GetDateValNullable(rgs, "RELEASEDFIELDSDATE"); rwpt.ReleasedCagesDate = GetDateValNullable(rgs, "RELEASEDCAGESDATE"); rwpt.Email1 = GetStringValNullable(rgs, "EMAIL1"); rwpt.Email2 = GetStringValNullable(rgs, "EMAIL2"); rwpt.Identity = GetStringValNullable(rgs, "IDENTITY"); rwpt.Tenant = GetStringValNullable(rgs, "TENANT"); } catch (Exception e) { return(RSR.Failed(e)); } return(RSR.Success()); }
/* 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()); }