Examples completed
6b649753843b6a1993740561c9148a4442e1a5f9
12 files changed
WebFormBuilder/Controllers/FormConfigController.csWebFormBuilder/Examples/checkbox.jsonWebFormBuilder/Examples/combobox.jsonWebFormBuilder/Examples/email.jsonWebFormBuilder/Examples/file.jsonWebFormBuilder/Examples/navigation.jsonWebFormBuilder/Examples/number.jsonWebFormBuilder/Examples/radio.jsonWebFormBuilder/Examples/select.jsonWebFormBuilder/Examples/textarea.jsonWebFormBuilder/Models/FormField.csWebFormBuilder/Models/HtmlInputType.cs
diff --git a/WebFormBuilder/Controllers/FormConfigController.cs b/WebFormBuilder/Controllers/FormConfigController.cs
index a469d25..e6ee5f3 100644
--- a/WebFormBuilder/Controllers/FormConfigController.cs
+++ b/WebFormBuilder/Controllers/FormConfigController.cs
@@ -1,6 +1,6 @@
-using WebFormBuilder.Models;
-using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
+using WebFormBuilder.Models;
namespace WebFormBuilder.Controllers;
@@ -9,17 +9,74 @@ namespace WebFormBuilder.Controllers;
public class FormConfigController : ControllerBase
{
[HttpGet]
- public IActionResult GetFormConfig([FromQuery] string name)
+ public async Task<IActionResult> GetFormConfig([FromQuery] string name)
{
- List<FormField>? list = [];
+ string jsonFilePath;
- var jsonFilePath = Path.Combine(AppContext.BaseDirectory, "Examples", name + ".json");
+ FileStream stream;
+
+ if (name == "navigation")
+ {
+ var listNav = new List<FormField>();
+
+ foreach (HtmlInputType type in Enum.GetValues(typeof(HtmlInputType)))
+ {
+ listNav.Add(new FormField
+ {
+ Type = HtmlInputType.Button,
+ Name= type.ToString().ToLower(),
+ Label = string.Empty,
+ Value = $"Example {type}"
+ });
+ }
+
+ listNav.Add(new FormField
+ {
+ Type = HtmlInputType.Button,
+ Name = "formConfigData",
+ Label = string.Empty,
+ Value = "Complete form"
+ });
+
+ listNav.Add(new FormField
+ {
+ Type = HtmlInputType.Button,
+ Name = "navigation",
+ Label = string.Empty,
+ Value = "This Navigation"
+ });
+
+ return Ok(listNav);
+ }
+
+
+ jsonFilePath = Path.Combine(AppContext.BaseDirectory, "Examples", name + ".json");
+
+ if (System.IO.File.Exists(jsonFilePath))
+ {
+ // do not use 'using' here, it is read async!
+ stream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true);
+
+ return File(stream, "application/json");
+ }
+
+ jsonFilePath = Path.Combine(AppContext.BaseDirectory, "Examples", "formConfigData.json");
if (!System.IO.File.Exists(jsonFilePath))
return NotFound();
- var stream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true);
+ stream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true);
+
+ var list = await JsonSerializer.DeserializeAsync<List<FormField>>(stream) ?? [];
+
+ stream.Dispose();
+
+ if(!Enum.TryParse(name, ignoreCase: true, out HtmlInputType typeEnum))
+ return NotFound();
+
+ list.RemoveAll(x => x.Type != typeEnum);
+
+ return Ok(list);
- return File(stream, "application/json");
}
}
diff --git a/WebFormBuilder/Examples/checkbox.json b/WebFormBuilder/Examples/checkbox.json
deleted file mode 100644
index 82fa3cd..0000000
--- a/WebFormBuilder/Examples/checkbox.json
+++ /dev/null
@@ -1,11 +0,0 @@
-[
- {
- "Type": "Checkbox",
- "Name": "hobbies",
- "Label": "Hobbies",
- "Tooltip": "Select hobbies.",
- "Options": [ "Sports", "Music", "Reading" ],
- "Value": "Music,Sports"
- }
-]
-
diff --git a/WebFormBuilder/Examples/combobox.json b/WebFormBuilder/Examples/combobox.json
deleted file mode 100644
index fd9618e..0000000
--- a/WebFormBuilder/Examples/combobox.json
+++ /dev/null
@@ -1,10 +0,0 @@
-[
- {
- "Type": "Combobox",
- "Name": "Fruit",
- "Label": "Fruit",
- "Tooltip": "Select fruit or make new",
- "Options": [ "Appletje", "Banana", "Cherry", "Date", "Fig", "Grape", "Honeydew" ]
- }
-]
-
diff --git a/WebFormBuilder/Examples/email.json b/WebFormBuilder/Examples/email.json
deleted file mode 100644
index 563132f..0000000
--- a/WebFormBuilder/Examples/email.json
+++ /dev/null
@@ -1,12 +0,0 @@
-[
- {
- "Type": "Email",
- "Name": "email",
- "Label": "Email Address",
- "Placeholder": "Enter your email address",
- "Tooltip": "sending email.",
- "Properties": [ "autocomplete=email" ],
- "Value": "example@example.com"
- }
-]
-
diff --git a/WebFormBuilder/Examples/file.json b/WebFormBuilder/Examples/file.json
deleted file mode 100644
index eda4559..0000000
--- a/WebFormBuilder/Examples/file.json
+++ /dev/null
@@ -1,10 +0,0 @@
-[
- {
- "Type": "File",
- "Name": "Files",
- "Label": "Choose a file",
- "Placeholder": "Select a file",
- "Tooltip": "Select a file please."
- }
-]
-
diff --git a/WebFormBuilder/Examples/navigation.json b/WebFormBuilder/Examples/navigation.json
deleted file mode 100644
index 3fff356..0000000
--- a/WebFormBuilder/Examples/navigation.json
+++ /dev/null
@@ -1,75 +0,0 @@
-[
- {
- "Type": "Button",
- "Name": "text",
- "Label": "",
- "Value": "Text"
- },
- {
- "Type": "Button",
- "Name": "password",
- "Label": "",
- "Value": "Password"
- },
- {
- "Type": "Button",
- "Name": "email",
- "Label": "",
- "Value": "Email"
- },
- {
- "Type": "Button",
- "Name": "combobox",
- "Label": "",
- "Value": "Combobox"
- },
- {
- "Type": "Button",
- "Name": "radio",
- "Label": "",
- "Value": "Radio"
- },
- {
- "Type": "Button",
- "Name": "checkbox",
- "Label": "",
- "Value": "Checkbox"
- },
- {
- "Type": "Button",
- "Name": "select",
- "Label": "",
- "Value": "Select"
- },
- {
- "Type": "Button",
- "Name": "file",
- "Label": "",
- "Value": "File"
- },
- {
- "Type": "Button",
- "Name": "textarea",
- "Label": "",
- "Value": "Textarea"
- },
- {
- "Type": "Button",
- "Name": "number",
- "Label": "",
- "Value": "Number Example"
- },
- {
- "Type": "Button",
- "Name": "formConfigData",
- "Label": "",
- "Value": "Complete form"
- },
- {
- "Type": "Button",
- "Name": "navigation",
- "Label": "",
- "Value": "This Navigation"
- }
-]
-
diff --git a/WebFormBuilder/Examples/number.json b/WebFormBuilder/Examples/number.json
deleted file mode 100644
index bc820a5..0000000
--- a/WebFormBuilder/Examples/number.json
+++ /dev/null
@@ -1,12 +0,0 @@
-[
- {
- "Type": "Number",
- "Name": "age",
- "Label": "Age",
- "Placeholder": "Enter your age",
- "Tooltip": "Age in years.",
- "Properties": [ "min=18", "max=88", "step=1" ],
- "Value": "30"
- }
-]
-
diff --git a/WebFormBuilder/Examples/radio.json b/WebFormBuilder/Examples/radio.json
deleted file mode 100644
index 5922db0..0000000
--- a/WebFormBuilder/Examples/radio.json
+++ /dev/null
@@ -1,11 +0,0 @@
-[
- {
- "Type": "Radio",
- "Name": "gender",
- "Label": null,
- "Tooltip": "Select your gender.",
- "Options": [ "Male", "Female", "Other" ],
- "Value": "Male"
- }
-]
-
diff --git a/WebFormBuilder/Examples/select.json b/WebFormBuilder/Examples/select.json
deleted file mode 100644
index 60cc5a0..0000000
--- a/WebFormBuilder/Examples/select.json
+++ /dev/null
@@ -1,12 +0,0 @@
-[
- {
- "Type": "Select",
- "Name": "country",
- "Label": "Country",
- "Tooltip": "Choose a country.",
- "Options": [ "Netherlands", "Belgium", "Germany" ],
- "Properties": [ "autocomplete=off" ],
- "Value": "Belgium"
- }
-]
-
diff --git a/WebFormBuilder/Examples/textarea.json b/WebFormBuilder/Examples/textarea.json
deleted file mode 100644
index e6560c6..0000000
--- a/WebFormBuilder/Examples/textarea.json
+++ /dev/null
@@ -1,11 +0,0 @@
-[
- {
- "Type": "Textarea",
- "Name": "remark",
- "Label": "Remark",
- "Tooltip": "Any remarks...",
- "Properties": [ "rows=4" ],
- "Value": "1\n2\n"
- }
-]
-
diff --git a/WebFormBuilder/Models/FormField.cs b/WebFormBuilder/Models/FormField.cs
index f2b30ee..60952d9 100644
--- a/WebFormBuilder/Models/FormField.cs
+++ b/WebFormBuilder/Models/FormField.cs
@@ -6,7 +6,6 @@ namespace WebFormBuilder.Models;
public class FormField
{
- [JsonConverter(typeof(JsonStringEnumConverter))]
public HtmlInputType? Type { get; set; }
public string Name { get; set; } = "Name";
public string? Label { get; set; } // can be "", when null it PascalCases Name
diff --git a/WebFormBuilder/Models/HtmlInputType.cs b/WebFormBuilder/Models/HtmlInputType.cs
index a699a59..ce7762f 100644
--- a/WebFormBuilder/Models/HtmlInputType.cs
+++ b/WebFormBuilder/Models/HtmlInputType.cs
@@ -1,10 +1,14 @@
-namespace WebFormBuilder.Models;
+using System.Text.Json.Serialization;
+namespace WebFormBuilder.Models;
+
+[JsonConverter(typeof(JsonStringEnumConverter))]
public enum HtmlInputType
{
Text,
Email,
Password,
+ Combobox,
Tel,
Url,
Number,