diff --git a/JormWithOxygenDocs.png b/JormWithOxygenDocs.png new file mode 100644 index 0000000..ba0f1c3 Binary files /dev/null and b/JormWithOxygenDocs.png differ diff --git a/blog.db b/blog.db new file mode 100644 index 0000000..79b1742 Binary files /dev/null and b/blog.db differ diff --git a/docs/src/working_with_webapps.md b/docs/src/working_with_webapps.md index 2712960..8830f4e 100644 --- a/docs/src/working_with_webapps.md +++ b/docs/src/working_with_webapps.md @@ -25,7 +25,6 @@ Define a struct to represent your data model: ```julia struct BlogPost - id::Int title::String content::String end @@ -54,6 +53,14 @@ Define the CRUD endpoints using `Oxygen.jl`: ```julia using Oxygen + +# Output Model +struct BlogPostOutput + id::Int # to handle autoincrement ID field + title::String + content::String +end + # Create a new blog post @post "/api/v1/blogs/" function(req::HTTP.Request) data = Oxygen.json(req, BlogPost) @@ -64,13 +71,13 @@ end # Read all blog posts @get "/api/v1/blogs/" function(req::HTTP.Request) results = Jorm.read_all(db, BlogPost) - return results + return Jorm.serialize_to_list(BlogPostOutput,results) end # Read one blog post by ID @get "/api/v1/blogs/{blog_id}" function(req::HTTP.Request, blog_id::Int) result = Jorm.read_one(db, BlogPost, blog_id) - return result + return Jorm.serialize_to_list(BlogPostOutput,result) end # Update an existing blog post diff --git a/src/Jorm.jl b/src/Jorm.jl index 3307259..b7ae5ea 100644 --- a/src/Jorm.jl +++ b/src/Jorm.jl @@ -9,7 +9,7 @@ export connect,disconnect,SQLiteConnectionString export create_table,delete_table export read_one_sql,insert_sql,update_sql,delete_sql,filter_by_sql,groupby_sql,read_all_sql export read_one,insert!,update!,delete!,read_all -export delete_db,drop_all_tables,backup_sqlite_db,backup_postgresql_db +export delete_db,drop_all_tables,backup_sqlite_db,backup_postgresql_db,serialize_to_list """ diff --git a/src/JormUtils.jl b/src/JormUtils.jl index 82b7ab2..b0c164a 100644 --- a/src/JormUtils.jl +++ b/src/JormUtils.jl @@ -138,4 +138,20 @@ function backup_postgresql_db(db_name::String, host::String="localhost", user::S end +""" + serialize_to_list(outputmodel::Type,query) + Convert an SQL Query result into serialized list + outputmodel:: Same Model but with id::Int defined to handle auto generated ID + from autoincrement + +""" +function serialize_to_list(outputmodel::Type,query) + if !isempty(query) + # Create a list of structs + struct_list = [outputmodel(row...) for row in query] + return struct_list + else + return nothing + end +end \ No newline at end of file diff --git a/webapp.jl b/webapp.jl new file mode 100644 index 0000000..dbaa616 --- /dev/null +++ b/webapp.jl @@ -0,0 +1,49 @@ +include("Jorm.jl") +using Oxygen +using StructTypes + +# Define the model +struct BlogPost + title::String + content::String +end + +# Support JSON serialization and deserialization +StructTypes.StructType(::Type{BlogPost}) = StructTypes.Struct() + +# Connect to the database and create the table +connection_string = Jorm.SQLiteConnectionString(database_name="blog.db") +db = Jorm.connect(connection_string) +tb = Jorm.tablename(BlogPost) +Jorm.create_table(db, BlogPost, tb) + +# Define CRUD endpoints +@post "/api/v1/blogs/" function(req::HTTP.Request) + data = Oxygen.json(req, BlogPost) + Jorm.insert!(db, BlogPost, data) + return data +end + +@get "/api/v1/blogs/" function(req::HTTP.Request) + results = Jorm.read_all(db, BlogPost) + return results +end + +@get "/api/v1/blogs/{blog_id}" function(req::HTTP.Request, blog_id::Int) + result = Jorm.read_one(db, BlogPost, blog_id) + return result +end + +@patch "/api/v1/blogs/{blog_id}" function(req::HTTP.Request, blog_id::Int) + data = Oxygen.json(req, BlogPost) + Jorm.update!(db, BlogPost, blog_id, data) + return data +end + +@delete "/api/v1/blogs/{blog_id}" function(req::HTTP.Request, blog_id::Int) + result = Jorm.delete!(db, BlogPost, blog_id) + return result +end + +# Start the server +serve(port=8001) \ No newline at end of file