private void HandlePostRequest(HttpListenerContext ctx) { try { #region Input Validation if (!ctx.Request.HasEntityBody) { WriteBodyResponse(ctx, 400, "Invalid Format", "Request did not contain a body"); return; } UsableCompanyListRetrieveRequest entry = JsonDataObjectUtil <UsableCompanyListRetrieveRequest> .ParseObject(ctx); if (entry == null) { WriteBodylessResponse(ctx, 400, "Invalid Format"); return; } if (!ValidateUsableRetrieveRequest(entry)) { WriteBodyResponse(ctx, 400, "Invalid Format", "One or more fields contained an invalid value or were missing"); return; } #endregion MySqlDataManipulator connection = new MySqlDataManipulator(); using (connection) { bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Connection to database failed"); return; } #region User Validation OverallUser mappedUser = connection.GetUserById(entry.UserId); if (mappedUser == null) { WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server"); return; } if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken)) { WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect."); return; } #endregion #region Post CompanyList List <CompanyId> companies = connection.GetPublicCompanies(); CompanyId userCompany = connection.GetCompanyById(mappedUser.Company); if (companies == null) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while retrieving companies: " + connection.LastException.Message); return; } if (!companies.Contains(userCompany)) { companies.Add(userCompany); } JsonListStringConstructor retConstructor = new JsonListStringConstructor(); companies.ForEach(req => retConstructor.AddElement(WriteCompanyIdToOutput(req))); WriteBodyResponse(ctx, 200, "OK", retConstructor.ToString()); #endregion } } catch (HttpListenerException) { //HttpListeners dispose themselves when an exception occurs, so we can do no more. } catch (Exception e) { WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message); } }
public static bool InitializeDatabaseSchema() { if (DatabaseInitialized) { return(true); } MySqlDataManipulator manipulator = new MySqlDataManipulator(); using (manipulator) { if (!manipulator.Connect(TestingConstants.DatabaselessConnectionString)) { Console.WriteLine("Encountered an error opening the global configuration connection"); Console.WriteLine(manipulator.LastException.Message); return(false); } if (!manipulator.ValidateDatabaseIntegrity(TestingConstants.DatabaselessConnectionString, "db_test")) { Console.WriteLine("Encountered an error opening the global configuration connection"); Console.WriteLine(manipulator.LastException.Message); return(false); } if (!manipulator.Connect(TestingConstants.ConnectionString)) { Console.WriteLine("Encountered an error opening the global configuration connection"); Console.WriteLine(manipulator.LastException.Message); return(false); } if (manipulator.GetCompanyById(1) == null) { if (!manipulator.AddCompany(TestingCompanyStorage.ValidCompany1)) { Console.WriteLine("Encountered an error adding the first valid company"); Console.WriteLine(manipulator.LastException.Message); return(false); } } if (manipulator.GetCompanyById(2) == null) { if (!manipulator.AddCompany(TestingCompanyStorage.ValidCompany2)) { Console.WriteLine("Encountered an error adding the second valid company"); Console.WriteLine(manipulator.LastException.Message); return(false); } } DatabaseInitialized = true; if (!InitializeUsers()) { return(false); } if (!InitializeJoinRequests()) { return(false); } if (!InitializePartCatelogueEntries()) { return(false); } if (!InitializePartsRequests()) { return(false); } if (!GlobalModelHelper.LoadOrTrainGlobalModels(ReflectionHelper.GetAllKeywordPredictors())) { return(false); } } MySqlDataManipulator.GlobalConfiguration.Connect(TestingConstants.ConnectionString); MySqlDataManipulator.GlobalConfiguration.Close(); return(true); }