Files
OnDoc/API_DocTest/Form1.cs
2026-02-13 06:42:52 +01:00

243 lines
7.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using Syncfusion.Drawing;
using Syncfusion.Windows.Forms.Tools;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Windows.Forms.PdfViewer;
using System.IO;
using Syncfusion.Windows.Forms.Edit.Interfaces;
using Syncfusion.Windows.Forms.Edit.Enums;
using Syncfusion.Windows.Forms.Edit.Implementation.Config;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace API_DocTest
{
public partial class Form1 : Form
{
static string connectionstring = Properties.Settings.Default.ConnectionString;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==";
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey);
LoadJSON();
Form1_Resize(sender, e);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
var key = GetKey();
if (string.IsNullOrEmpty(key)) return;
string json = LoadJsonFromDb(key);
editControl1.Text = json ?? "{}";
editControl1.MoveToBeginning();
pdfViewerControl1.Unload(); // PDF zurücksetzen
}
private string LoadJsonFromDb(string key)
{
string json = null;
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JavaScriptObject FROM ProvDokuments WHERE ProvDokumentid = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
if (json == null)
{
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JsonObjekt FROM _OnDoc_API_TestScripts WHERE id = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
}
return json;
//using (var con = new SqlConnection(connectionstring))
//using (var cmd = new SqlCommand(
// "SELECT JavaScriptObject FROM ProvDokuments WHERE ProvDokumentid = @key", con))
//{
// cmd.Parameters.AddWithValue("@key", key);
// con.Open();
// return cmd.ExecuteScalar() as string;
//}
}
private void SaveJsonToDb(string key, string json)
{
using (var con = new SqlConnection(connectionstring))
using (var cmd = new SqlCommand(@"
IF EXISTS (SELECT 1 FROM _OnDoc_API_TestScripts WHERE id = @key)
UPDATE _OnDoc_API_TestScripts SET JsonObjekt =@json
WHERE id = @key
ELSE
INSERT INTO _OnDoc_API_TestScripts (id,JsonObjekt)
VALUES (@key, @json)
", con))
{
cmd.Parameters.AddWithValue("@key", key);
cmd.Parameters.AddWithValue("@json", json);
con.Open();
cmd.ExecuteNonQuery();
}
}
private string GetKey()
{
foreach (Control c in Controls)
if (c is ToolStrip ts)
foreach (ToolStripItem i in ts.Items)
if (i.Name == "txtKey")
return ((ToolStripTextBox)i).Text;
return null;
}
static readonly string ApiUrl = Properties.Settings.Default.DogGenURI;
static readonly string token = Properties.Settings.Default.Token;
public static byte[] Generate(string json)
{
try
{
using (var client = new HttpClient())
{
// 🔐 Bearer Token
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);
var content = new StringContent(
json,
Encoding.UTF8,
"application/json");
var response = client.PostAsync(ApiUrl, content).Result;
var responseText = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode)
{
// optional: Logging
return null;
}
var jsonResult = JObject.Parse(responseText);
var base64Pdf = jsonResult["file"]?.ToString();
if (string.IsNullOrEmpty(base64Pdf))
return null;
return Convert.FromBase64String(base64Pdf);
}
}
catch (Exception ex)
{
// Logging
return null;
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
var key = GetKey();
if (string.IsNullOrEmpty(key)) return;
SaveJsonToDb(key,editControl1.Text);
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
try
{
byte[] byteArray = Generate(editControl1.Text);
string temp_inBase64 = Convert.ToBase64String(byteArray);
var stream = new MemoryStream(Convert.FromBase64String(temp_inBase64));
if (stream.Length > 0)
{
this.pdfViewerControl1.Load(stream);
this.pdfViewerControl1.Visible = true;
pdfViewerControl1.ZoomMode = ZoomMode.FitWidth;
}
else
{
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void Form1_Resize(object sender, EventArgs e)
{
try
{
splitContainer1.SplitterDistance = this.Width / 2;
}
catch { }
}
private void LoadJSON()
{
using (SqlConnection conn = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT id FROM _OnDoc_API_TestScripts ORDER BY id", conn))
{
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
toolStripComboBox1.Items.Clear();
while (reader.Read())
{
toolStripComboBox1.Items.Add(reader.GetString(0));
}
}
}
}
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.txtKey.Text= toolStripComboBox1.Text.ToString();
toolStripButton1_Click(sender, e);
}
}
}