Skip to content

Commit

Permalink
chore: add webapp tuts:
Browse files Browse the repository at this point in the history
  • Loading branch information
Jcharis committed Sep 8, 2024
1 parent 2a96a83 commit d9aad17
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
Binary file added JormWithOxygenDocs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blog.db
Binary file not shown.
13 changes: 10 additions & 3 deletions docs/src/working_with_webapps.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Define a struct to represent your data model:

```julia
struct BlogPost
id::Int
title::String
content::String
end
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Jorm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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


"""
Expand Down
16 changes: 16 additions & 0 deletions src/JormUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 49 additions & 0 deletions webapp.jl
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit d9aad17

Please sign in to comment.