/// <summary> /// PATCH request format located in the Web Api Enumeration v2 /// under the tab Company/Settings, starting row 27 /// </summary> /// <param name="ctx">HttpListenerContext to respond to</param> private void HandlePatchRequest(HttpListenerContext ctx) { try { #region Input Validation if (!ctx.Request.HasEntityBody) { WriteBodyResponse(ctx, 400, "No Body", "Request lacked a body"); return; } CompanySettingsApiPatchRequest entry = JsonDataObjectUtil <CompanySettingsApiPatchRequest> .ParseObject(ctx); if (entry == null) { WriteBodyResponse(ctx, 400, "Incorrect Format", "Request was in the wrong format"); return; } if (!ValidatePatchRequest(entry)) { WriteBodyResponse(ctx, 400, "Incorrect Format", "Not all fields in the request were filled"); return; } #endregion MySqlDataManipulator connection = new MySqlDataManipulator(); using (connection) { bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected ServerError", "Connection to database failed"); return; } #region User Validation var user = connection.GetUserById(entry.UserId); if (user == null) { WriteBodyResponse(ctx, 404, "Not Found", "User was not found on the server"); return; } List <CompanySettingsEntry> entries = connection.GetCompanySettings(user.Company); if (entries == null) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while retrieving settings: " + connection.LastException.Message); return; } #endregion #region Action Handling var toModify = entries.Where(obj => obj.SettingKey.Equals(entry.SettingsKey)).FirstOrDefault(); if (toModify == null) { WriteBodyResponse(ctx, 404, "Not Found", "Setting with the specified key was not found on the server"); return; } toModify.SettingValue = entry.SettingsValue; if (!connection.UpdateCompanySettings(user.Company, toModify)) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occurred while updating company's settings: " + connection.LastException.Message); return; } WriteBodylessResponse(ctx, 200, "OK"); #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", "Error occurred while processing request: " + e.Message); } }