Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

TFG Playground

auth_users_comments_admin.livemd

TFG Playground

Mix.install([
  {:gen_editor, "~> 0.3.3"},
  {:kino_vega_lite, "~> 0.1.10"}
])

Section

Template with the following features:

  • User Authentication
  • Email Notifications
  • User Comment entities, migration and CRUD views
  • System admins
blueprint = %{
  metadata: [],
  dependencies: [],
  pretasks: [],
  generable_elements: [
    %{
      "app" => "SampleApp",
      "binary_id" => false,
      "database" => "postgres",
      "help_box_type" => "iframe",
      "install" => true,
      "isHelpBoxHidden" => true,
      "no_assets" => false,
      "no_dashboard" => false,
      "no_ecto" => false,
      "no_esbuild" => false,
      "no_gettext" => false,
      "no_html" => false,
      "no_install" => false,
      "no_live" => false,
      "no_mailer" => false,
      "no_tailwind" => false,
      "path" => "sample_app",
      "type" => "App",
      "verbose" => false,
      "version" => false
    }
  ],
  posttasks: []
}

{:ok, "Added: App"}
blueprint =
  blueprint
  |> Map.update!(
    :metadata,
    &(&1 ++
        [
          %{
            "context" => "Authentication",
            "help_box_type" => "iframe",
            "isHelpBoxHidden" => true,
            "merge_with_existing_context" => true,
            "no_merge_with_existing_context" => false,
            "no_schema" => false,
            "standalone" => false,
            "type" => "Context"
          }
        ])
  )

{:ok, "Added: " <> "Context"}
blueprint =
  blueprint
  |> Map.update!(
    :metadata,
    &amp;(&amp;1 ++
        [
          %{
            "help_box_type" => "iframe",
            "isHelpBoxHidden" => true,
            "module" => "Administration",
            "standalone" => false,
            "type" => "Module"
          }
        ])
  )

{:ok, "Added: " <> "Module"}
blueprint =
  blueprint
  |> Map.update!(
    :metadata,
    &amp;(&amp;1 ++
        [
          %{
            "binary_id" => true,
            "help_box_type" => "iframe",
            "isHelpBoxHidden" => true,
            "module" => "Administration",
            "no_migration" => false,
            "plural" => "administrators",
            "standalone" => false,
            "type" => "Schema"
          }
        ])
  )

{:ok, "Added: " <> "Schema"}
blueprint =
  blueprint
  |> Map.update!(
    :generable_elements,
    &amp;(&amp;1 ++
        [
          %{
            "context" => "Authentication",
            "help_box_type" => "iframe",
            "isHelpBoxHidden" => true,
            "live" => true,
            "no_live" => false,
            "schema" => "administrators",
            "type" => "Auth"
          }
        ])
  )

{:ok, "Added: " <> "Auth"}
blueprint =
  blueprint
  |> Map.update!(
    :metadata,
    &amp;(&amp;1 ++
        [
          %{
            "context" => "Content",
            "help_box_type" => "iframe",
            "isHelpBoxHidden" => true,
            "merge_with_existing_context" => true,
            "no_merge_with_existing_context" => false,
            "no_schema" => false,
            "standalone" => false,
            "type" => "Context"
          }
        ])
  )

{:ok, "Added: " <> "Context"}
blueprint =
  blueprint
  |> Map.update!(
    :metadata,
    &amp;(&amp;1 ++
        [
          %{
            "help_box_type" => "iframe",
            "isHelpBoxHidden" => true,
            "module" => "Comment",
            "standalone" => false,
            "type" => "Module"
          }
        ])
  )

{:ok, "Added: " <> "Module"}
blueprint =
  blueprint
  |> Map.update!(
    :metadata,
    &amp;(&amp;1 ++
        [
          %{
            "binary_id" => false,
            "fields" => [
              %{"datatype" => "binary_id", "field_name" => "user"},
              %{"datatype" => "string", "field_name" => "title"},
              %{"datatype" => "string", "field_name" => "content"},
              %{"datatype" => "boolean", "field_name" => "is_public"}
            ],
            "help_box_type" => "iframe",
            "isHelpBoxHidden" => true,
            "module" => "Comment",
            "no_migration" => false,
            "plural" => "comments",
            "standalone" => false,
            "type" => "Schema"
          }
        ])
  )

{:ok, "Added: " <> "Schema"}
blueprint =
  blueprint
  |> Map.update!(
    :generable_elements,
    &amp;(&amp;1 ++
        [
          %{
            "context" => "Content",
            "help_box_type" => "iframe",
            "isHelpBoxHidden" => true,
            "no_context" => false,
            "no_schema" => false,
            "schema" => "comments",
            "type" => "Html"
          }
        ])
  )

{:ok, "Added: " <> "Html"}
app =
  blueprint
  |> Map.fetch!(:generable_elements)
  |> Enum.filter(fn element -> element["type"] == "App" end)
  |> Enum.at(0)

blueprint =
  blueprint
  |> Map.put(
    :metadata,
    blueprint
    |> Map.fetch!(:metadata)
    |> Enum.map(fn element -> element |> Map.put("path", app["path"]) end)
  )

generable_elements =
  blueprint
  |> Map.fetch!(:generable_elements)
  |> Enum.map(fn element -> element |> Map.put("path", app["path"]) end)

schemas =
  blueprint
  |> Map.fetch!(:metadata)
  |> Enum.filter(fn element -> element["type"] == "Schema" end)

generable_elements =
  generable_elements |> Enum.filter(fn element -> element["type"] != "App" end)

generable_elements =
  generable_elements
  |> Enum.map(fn element ->
    case Enum.member?(
           ["Auth", "Context", "Html", "Json", "JSON", "Live"],
           element["type"]
         ) do
      true ->
        schema =
          schemas
          |> Enum.filter(fn schema ->
            IO.puts("SEARCHING FOR SCHEMA: #{inspect(schema)}")
            IO.puts("WITH ELEMENT: #{inspect(element)}")
            schema["plural"] == element["schema"]
          end)
          |> Enum.at(0)

        element |> Map.put("schema", schema)

      false ->
        element
    end
  end)

blueprint =
  blueprint |> Map.put(:generable_elements, generable_elements) |> Map.put(:app, app)

GenDSL.generate_from_blueprint(blueprint, false, File.cwd!())

Kino.Download.new(fn -> Jason.encode!(blueprint) end,
  filename: "blueprint.json",
  label: "Json file"
)

IO.puts("Compressing project...")
files = File.ls!("./" <> app["path"]) |> Enum.map(&amp;String.to_charlist/1)

{:ok, {filename, bytes}} =
  :zip.create("project.zip", files, [:memory, cwd: "./" <> app["path"]])

IO.puts("Files are ready")

Kino.Layout.grid(
  [
    Kino.Download.new(fn -> Jason.encode!(blueprint) end,
      filename: "blueprint.json",
      label: "Blueprint.json"
    ),
    Kino.Download.new(fn -> bytes end, filename: "project.zip", label: "Project.zip")
  ],
  columns: 2,
  boxed: true
)