Update
This commit is contained in:
136
Server/lib/DatabaseSupport.jl
Normal file
136
Server/lib/DatabaseSupport.jl
Normal file
@@ -0,0 +1,136 @@
|
||||
module DatabaseSupport
|
||||
|
||||
using SearchLight, SearchLightPostgreSQL, LibPQ
|
||||
using DataFrames
|
||||
|
||||
export exist_in_table, insert_into_table, update_table, select_from_table, add_foreign_key
|
||||
|
||||
|
||||
options = SearchLight.Configuration.read_db_connection_data("db/connection.yml")
|
||||
conn = SearchLight.connect(options)
|
||||
|
||||
function insert_into_table(table_name,dict_values)
|
||||
names_string = join(keys(dict_values),", ")
|
||||
vals_raw = values(dict_values)
|
||||
vals = map(x -> (x isa String) || (x isa Symbol) ? string("'",x,"'") : x,vals_raw)
|
||||
vals_string = join(values(vals),", ")
|
||||
query = "INSERT INTO $table_name ($names_string) VALUES ($vals_string)"
|
||||
SearchLight.query(query)
|
||||
return nothing
|
||||
end
|
||||
|
||||
function update_table(table_name,dict_values; where_data=nothing)
|
||||
ns = collect(keys(dict_values))
|
||||
vals_raw = values(dict_values)
|
||||
vals = map(x -> x isa String ? string("'",x,"'") : x,vals_raw)
|
||||
ns_vals = join(map((x,y) -> string(x, " = ",y),ns,vals),", ")
|
||||
|
||||
query = "UPDATE $table_name SET $ns_vals"
|
||||
if !isnothing(where_data)
|
||||
query *= where_query(where_data)
|
||||
end
|
||||
|
||||
cur = SearchLight.query(query)
|
||||
return DataFrame(cur)
|
||||
end
|
||||
|
||||
function where_query(pair::Pair)
|
||||
return " WHERE $(pair[1]) = $(pair[2])"
|
||||
end
|
||||
|
||||
function where_query(data::Vector{<:Pair})
|
||||
conds = String[]
|
||||
for pair in data
|
||||
push!(conds,"$(pair[1]) = $(pair[2])")
|
||||
end
|
||||
query = " WHERE "*join(conds," AND ")
|
||||
return query
|
||||
end
|
||||
|
||||
function join_query(data::Vector{<:Vector},table_name)
|
||||
query = ""
|
||||
for v in data
|
||||
table_join = v[1]
|
||||
column_join = v[2]
|
||||
column2_join = v[3]
|
||||
query *= " JOIN $table_join ON $(table_name).$(column_join) = $(table_join).$(column2_join)"
|
||||
end
|
||||
return query
|
||||
end
|
||||
|
||||
function join_query(data::Vector{<:Union{String,Symbol}},table_name)
|
||||
table_join = data[1]
|
||||
column_join = data[2]
|
||||
column2_join = data[3]
|
||||
query = " JOIN $table_join ON $(table_name).$(column_join) = $(table_join).$(column2_join)"
|
||||
return query
|
||||
end
|
||||
|
||||
function select_query(table::Pair)
|
||||
table_name = table[1]
|
||||
cols = table[2]
|
||||
if isnothing(cols)
|
||||
cols_query = "*"
|
||||
else
|
||||
cols_query = join(cols,", ")
|
||||
end
|
||||
query = "SELECT $cols_query FROM $table_name"
|
||||
return query
|
||||
end
|
||||
|
||||
function as_query(table_name,col_name::Symbol)
|
||||
return table_name*"."*String(col_name)
|
||||
end
|
||||
|
||||
function as_query(table_name,col_name::String)
|
||||
return table_name*"."*col_name
|
||||
end
|
||||
|
||||
function as_query(table_name,col_name::Pair)
|
||||
return table_name*"."*col_name[1]*" AS "*col_name[2]
|
||||
end
|
||||
|
||||
function select_query(tables::Vector{<:Pair})
|
||||
tables_query_ar = String[]
|
||||
cols_query_ar = String[]
|
||||
for table in tables
|
||||
table_name = table[1]
|
||||
cols = table[2]
|
||||
push!(tables_query_ar,table_name)
|
||||
if isnothing(cols)
|
||||
push!(cols_query_ar, table_name*".*")
|
||||
else
|
||||
push!(cols_query_ar, join(map(col_name -> as_query(table_name,col_name),cols),", "))
|
||||
end
|
||||
end
|
||||
tables_query = join(tables_query_ar,", ")
|
||||
cols_query = join(cols_query_ar,", ")
|
||||
query = "SELECT $cols_query FROM $(tables_query)"
|
||||
return query
|
||||
end
|
||||
|
||||
function select_from_table(tables; where_data = nothing)
|
||||
query = select_query(tables)
|
||||
|
||||
if !isnothing(where_data)
|
||||
query *= where_query(where_data)
|
||||
end
|
||||
|
||||
cur = SearchLight.query(query)
|
||||
return DataFrame(cur)
|
||||
end
|
||||
|
||||
function exist_in_table(table_name,where_data)
|
||||
cur = SearchLight.query("SELECT * FROM $table_name "*where_query(where_data)*" LIMIT 1")
|
||||
t = DataFrame(cur)
|
||||
return size(t,1)>0
|
||||
end
|
||||
|
||||
function add_foreign_key(table,name,table2,name2)
|
||||
query = """
|
||||
ALTER TABLE $table
|
||||
ADD CONSTRAINT fk_$(table)_$(table2)_$(name) FOREIGN KEY ($name) REFERENCES $table2 ($name2);"""
|
||||
SearchLight.query(query)
|
||||
end
|
||||
|
||||
end
|
265
Server/lib/TemplateEditor.jl
Normal file
265
Server/lib/TemplateEditor.jl
Normal file
@@ -0,0 +1,265 @@
|
||||
module TemplateEditor
|
||||
|
||||
using JSON3, Genie, Unicode
|
||||
import Unicode.normalize
|
||||
|
||||
export generate_layout_html
|
||||
|
||||
dict_libraries = Dict(
|
||||
"ECharts" => "<script src='https://cdn.jsdelivr.net/npm/echarts@5.3.2/dist/echarts.js'></script>",
|
||||
"GSAP" => "<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.0/gsap.min.js'></script>",
|
||||
)
|
||||
|
||||
function register_components()
|
||||
p_components = "public/js/components"
|
||||
components_filenames = readdir(p_components)
|
||||
components_names = String[]
|
||||
for filename in components_filenames
|
||||
if filename[1:5]!="index"
|
||||
n = filename[1:end-3]
|
||||
push!(components_names,n)
|
||||
Genie.Renderer.Html.register_normal_element(replace(n,"-" => "__"))
|
||||
@info n
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
"""
|
||||
template_filename - name of a template to use
|
||||
controller - name of a controller where to look for a view file
|
||||
view_filename - a view file name
|
||||
css - should be a vector of names of css files
|
||||
libraries - should be a vector of names of library names from `dict_libraries`
|
||||
"""
|
||||
function generate_layout_html(template_filename,controller,view_filename; css=nothing, libraries=nothing)
|
||||
|
||||
template_filename = template_filename*".jl.html"
|
||||
|
||||
function add_spacing(snippet_name,snippet,html)
|
||||
ind = findfirst(snippet_name,html)[1] - 1
|
||||
cnt = 0
|
||||
while true
|
||||
if html[ind]=='\n'
|
||||
break
|
||||
else
|
||||
ind = ind - 1
|
||||
cnt += 1
|
||||
end
|
||||
end
|
||||
snippet = replace(snippet,"\n" => string("\n",repeat(" ",cnt)))
|
||||
return snippet
|
||||
end
|
||||
|
||||
function push_component_tags(tags,html,components_names)
|
||||
for n in components_names
|
||||
if occursin(n,html)
|
||||
push!(tags,n)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function list_files(x)
|
||||
v = String[]
|
||||
for (root, dirs, files) in walkdir(x)
|
||||
for file in files
|
||||
file = joinpath(root, file)
|
||||
push!(v, file)
|
||||
end
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
warning_comment = """
|
||||
<!--
|
||||
|
||||
DO NOT EDIT. CREATED AUTOMATICALLY
|
||||
|
||||
-->
|
||||
|
||||
"""
|
||||
|
||||
p_components = "public/js/components"
|
||||
components_filenames = readdir(p_components)
|
||||
components_names = String[]
|
||||
for filename in components_filenames
|
||||
if filename[1:5]!="index"
|
||||
n = filename[1:end-3]
|
||||
push!(components_names,n)
|
||||
Genie.Renderer.Html.register_normal_element(replace(n,"-" => "__"))
|
||||
end
|
||||
end
|
||||
#=
|
||||
|
||||
p_snippets = "app/layouts/snippets"
|
||||
snippet_filenames = readdir(p_snippets)
|
||||
snippet_names = map(x-> string("<%@",x[1:end-8],"%>"],snippet_filenames)
|
||||
snippets = String[]
|
||||
for snippet_filename in snippet_filenames
|
||||
snippet = open(joinpath(p_snippets,snippet_filename)) do file
|
||||
read(file, String)
|
||||
end
|
||||
push!(snippets,snippet)
|
||||
end
|
||||
=#
|
||||
|
||||
savepath = "app/layouts"
|
||||
p_controllers = "app/resources"
|
||||
p_templates = "app/layouts"
|
||||
p_components = "public/js/components"
|
||||
p_css = "public/css"
|
||||
p_svelte = "./app/svelte/src"
|
||||
|
||||
|
||||
template_filenames = readdir(p_templates)
|
||||
|
||||
controllers_dirs = readdir(p_controllers)
|
||||
dict_views = Dict{String,String}()
|
||||
dict_template_views = Dict{String,Vector{String}}()
|
||||
#for controller in controllers_dirs
|
||||
# Get views
|
||||
p_controller = joinpath(p_controllers,controller)
|
||||
p_views = joinpath(p_controller,"views")
|
||||
#if isdir(p_views)
|
||||
# HTML code for view
|
||||
#for view_filename in readdir(p_views)
|
||||
html_view = open(joinpath(p_views,view_filename*".jl.html")) do file
|
||||
read(file, String)
|
||||
end
|
||||
dict_views[view_filename] = html_view
|
||||
#end
|
||||
#end
|
||||
# Get views per template
|
||||
controller_vec = filter(x -> occursin("Controller",x),readdir(p_controller))
|
||||
if !isempty(controller_vec)
|
||||
controller_filename = controller_vec[1]
|
||||
controller_code = open(joinpath(p_controller,controller_filename)) do file
|
||||
read(file, String)
|
||||
end
|
||||
controller_split = split(controller_code,"\n")
|
||||
controller_filtered = filter(x -> occursin("html(",x) && !occursin("generate_layout_html",x),controller_split)
|
||||
controller_split = map(x -> split(x,",")[2:3],controller_filtered)
|
||||
controller_pairs = map(x -> map(y -> split(y,":")[end],x),controller_split)
|
||||
for pair in controller_pairs
|
||||
view = pair[1]
|
||||
template = pair[end]
|
||||
if template in keys(dict_template_views)
|
||||
push!(dict_template_views[template],view)
|
||||
else
|
||||
dict_template_views[template] = [view]
|
||||
end
|
||||
end
|
||||
end
|
||||
#end
|
||||
|
||||
# html - HTML code for template
|
||||
html = open(joinpath(p_templates,template_filename)) do file
|
||||
read(file, String)
|
||||
end
|
||||
|
||||
html_view = dict_views[view_filename]
|
||||
|
||||
files_svelte = list_files(p_svelte)
|
||||
files_svelte_names = map(x -> split(x, ['/','\\','.'])[end-1],files_svelte)
|
||||
dict_svelte_files = Dict(zip(files_svelte_names,files_svelte))
|
||||
|
||||
css_tags = []
|
||||
tags = []
|
||||
push_component_tags(tags,html,components_names)
|
||||
push_component_tags(tags,html_view,components_names)
|
||||
for tag in tags
|
||||
js = open(joinpath(p_components,string(tag,".js"))) do file
|
||||
read(file, String)
|
||||
end
|
||||
temp_tags = []
|
||||
js_split = split(js,"\n")
|
||||
js_temp = string(js_split[occursin.("import",js_split)])
|
||||
push_component_tags(temp_tags,js_temp,components_names)
|
||||
map(t -> t in tags ? nothing : push!(tags,t),temp_tags)
|
||||
|
||||
if tag in keys(dict_svelte_files)
|
||||
svelte = open(dict_svelte_files[tag]) do file
|
||||
read(file, String)
|
||||
end
|
||||
svelte_split = split(svelte,"\r\n")
|
||||
svelte_css_imports = filter(x -> occursin("@import",x),svelte_split)
|
||||
css_tags_temp = map(x -> split(x,['\'','"'])[end-1], svelte_css_imports)
|
||||
push!(css_tags,css_tags_temp...)
|
||||
end
|
||||
end
|
||||
css_tags = unique(css_tags)
|
||||
|
||||
if !isnothing(css)
|
||||
map(x -> push!(css_tags,"/css/"*x*".css"),css)
|
||||
end
|
||||
|
||||
#html = string(warning_comment,html)
|
||||
#=
|
||||
for (snippet_name,snippet) in zip(snippet_names,snippets)
|
||||
if (occursin(snippet_name, html))
|
||||
snippet = add_spacing(snippet_name,snippet,html)
|
||||
html = replace(html,snippet_name => snippet)
|
||||
end
|
||||
end
|
||||
|
||||
htmls = [html,html_view]
|
||||
|
||||
template_name = template_filename[1:end-8]
|
||||
p_view_snippets = joinpath(p_view,"snippets",template_name)
|
||||
if isdir(p_view_snippets)
|
||||
view_snippets_filenames = readdir(p_view_snippets)
|
||||
view_snippet_html = open(joinpath(p_view,template_filename)) do file
|
||||
read(file, String)
|
||||
end
|
||||
push!(htmls,view_snippet_html)
|
||||
end
|
||||
=#
|
||||
ind_opening = findfirst("<!--Load components-->",html)[1] - 1
|
||||
ind_closing = findfirst("</head>",html)[1] - 1
|
||||
components_to_add = "\n <!--Load components-->"
|
||||
for component_name in tags
|
||||
components_to_add = string(components_to_add,"\n <script type = 'module' src='/js/components/$component_name.js'></script>")
|
||||
end
|
||||
html = string(html[1:ind_opening],components_to_add,"\n",html[ind_closing+1:end])
|
||||
|
||||
ind_opening = findfirst("<!--Load CSS-->",html)[1] - 1
|
||||
ind_closing = findfirst("<!--Load components-->",html)[1] - 1
|
||||
css_to_add = "\n <!--Load CSS-->"
|
||||
for css_name in css_tags
|
||||
css_to_add = string(css_to_add,"\n <link rel='stylesheet' href='$css_name'>")
|
||||
end
|
||||
html = string(html[1:ind_opening],css_to_add,"\n",html[ind_closing+1:end])
|
||||
|
||||
tags_libraries = String["GSAP"]
|
||||
|
||||
if !isnothing(libraries)
|
||||
ks = collect(keys(dict_libraries))
|
||||
ks_normalized = lowercase.(ks)
|
||||
libraries_normalized = lowercase.(libraries)
|
||||
lib = libraries_normalized[1]
|
||||
for lib in libraries_normalized
|
||||
inds = lib .== ks_normalized
|
||||
if count(inds)==1
|
||||
push!(tags_libraries,ks[inds][1])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ind_opening = findfirst("<!--Load libraries-->",html)[1] - 1
|
||||
ind_closing = findfirst("<!--Load fonts-->",html)[1] - 1
|
||||
libraries_to_add = "\n <!--Load libraries-->"
|
||||
for lib in tags_libraries
|
||||
libraries_to_add = string(libraries_to_add,"\n "*dict_libraries[lib])
|
||||
end
|
||||
html = string(html[1:ind_opening],libraries_to_add,"\n",html[ind_closing+1:end])
|
||||
|
||||
#=
|
||||
path = joinpath(savepath,template_filename)
|
||||
open(path, "w") do io
|
||||
write(io, html)
|
||||
end
|
||||
=#
|
||||
return html
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user