From f2159db19a5aeeb4b4b7a53d065480adf1df47fc Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 16 Jul 2016 03:44:30 -0400 Subject: first crack at project rendering --- src/guff/handlers.cr | 13 ++++--- src/guff/models/project.cr | 84 +++++++++++++++++++++++++++++++++++++++++----- src/guff/views/project.cr | 7 ++++ src/views/project.ecr | 31 +++++++++++++++++ 4 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 src/guff/views/project.cr create mode 100644 src/views/project.ecr diff --git a/src/guff/handlers.cr b/src/guff/handlers.cr index d9b39c8..d34a9a7 100644 --- a/src/guff/handlers.cr +++ b/src/guff/handlers.cr @@ -401,7 +401,7 @@ module Guff::Handlers PATH_RE = %r{^/(?[^/]+)/?$} def call(context : HTTP::Server::Context) - if post_id = get_post_id(context.request) + if r = get(context.request) path = context.request.path.not_nil! if /\/$/.match(path) @@ -409,7 +409,8 @@ module Guff::Handlers context.response.headers["x-frame-options"] = "SAMEORIGIN" context.response.content_type = "text/html; charset=utf-8" context.response.status_code = 200 - context.response << "project: #{post_id}" + + Views::ProjectView.new(@context, r).to_s(context.response) else # redirect to project context.response.headers["location"] = path + "/" @@ -421,16 +422,20 @@ module Guff::Handlers end end - private def get_post_id(request : HTTP::Request) : Int64? + private def get(request : HTTP::Request) : Hash(String, String)? r = nil if request.method == "GET" if md = PATH_RE.match(request.path.not_nil!) if site_id = get_site_id(request.headers["host"]?) - r = @context.models.project.get_id( + results = @context.models.project.find( site_id: site_id, slug: md["slug"], ) + + if results.num_rows > 0 + r = results.rows.first + end end end end diff --git a/src/guff/models/project.cr b/src/guff/models/project.cr index 2ead9db..3c493c1 100644 --- a/src/guff/models/project.cr +++ b/src/guff/models/project.cr @@ -1,7 +1,7 @@ class Guff::Models::ProjectModel < Guff::Models::Model SQL = { - get_id: " - SELECT b.post_id + count: " + SELECT COUNT(*) FROM sites a JOIN posts b @@ -10,13 +10,48 @@ class Guff::Models::ProjectModel < Guff::Models::Model ON (c.post_id = b.post_id) JOIN states d ON (d.state_id = b.state_id) + JOIN users e + ON (e.user_id = b.created_by) WHERE a.site_id = ? AND b.slug = ? AND d.state = 'public' + AND (0 + strftime('%s')) + BETWEEN (0 + strftime('%s', COALESCE(b.posted_at, 'now'))) + AND (0 + strftime('%s', COALESCE(b.expires_at,'now'))) + ", + + find: " + SELECT b.post_id, + b.posted_at, + datetime(b.posted_at, 'localtime') AS posted_at_text, + b.slug, + b.name, + b.body, + c.repo_url, + e.user_id, + e.name AS user_name + + FROM sites a + JOIN posts b + ON (b.site_id = a.site_id) + JOIN projects c + ON (c.post_id = b.post_id) + JOIN states d + ON (d.state_id = b.state_id) + JOIN users e + ON (e.user_id = b.created_by) + + WHERE a.site_id = ? + AND b.slug = ? + AND d.state = 'public' + AND (0 + strftime('%s')) + BETWEEN (0 + strftime('%s', COALESCE(b.posted_at, 'now'))) + AND (0 + strftime('%s', COALESCE(b.expires_at,'now'))) ORDER BY b.created_at DESC - LIMIT 1 + + LIMIT ? OFFSET ? ", add: " @@ -51,12 +86,44 @@ class Guff::Models::ProjectModel < Guff::Models::Model ", } - def get_id( + LIMIT = 50 + + def find( site_id : Int64, - slug : String - ) : Int64? - r = @context.dbs.ro.one(SQL[:get_id], [site_id.to_s, slug]) - r ? r.to_i64 : nil + slug : String, + page : Int32? = nil + ) : PagedResultSet + # build query args + args = [site_id.to_s, slug] + + # get page + page ||= 1 + raise "invalid page" unless page > 0 + + # count rows + num_rows = @context.dbs.ro.one(SQL[:count], args).not_nil!.to_i64 + + rows = [] of Hash(String, String) + if num_rows > 0 + # add limit and offset to args + args.push(LIMIT.to_s, ((page - 1) * LIMIT).to_s) + + # get rows + @context.dbs.ro.all(SQL[:find], args) do |row| + rows << row.reduce({} of String => String) do |r, kv| + r[kv[0]] = kv[1].to_s + r + end + end + end + + # return results + PagedResultSet.new( + page: page, + num_rows: num_rows, + limit: LIMIT, + rows: rows, + ) end def add( @@ -116,6 +183,7 @@ class Guff::Models::ProjectModel < Guff::Models::Model body: body, ) + puts "DEBUG: repo_url = #{repo_url}, post_id = #{post_id}" if repo_url db.query(SQL[:set], [repo_url, post_id.to_s]) end diff --git a/src/guff/views/project.cr b/src/guff/views/project.cr new file mode 100644 index 0000000..e0d9bc0 --- /dev/null +++ b/src/guff/views/project.cr @@ -0,0 +1,7 @@ +class Guff::Views::ProjectView < Guff::Views::HTMLView + def initialize(context : Context, @item : Hash(String, String)) + super(context) + end + + ECR.def_to_s("src/views/project.ecr") +end diff --git a/src/views/project.ecr b/src/views/project.ecr new file mode 100644 index 0000000..9b1d21b --- /dev/null +++ b/src/views/project.ecr @@ -0,0 +1,31 @@ + + + + + Project: <%= + h(@item["name"]) + %> + + <% + # TODO: add theme styles + %> + + + +
+ Project: <%= h(@item["name"]) %>
+ <%= @item["body"] %> + +

+ Repo URL: <%= h(@item["repo_url"]) %> +

+
+ + + <% + # TODO: add theme scripts + %> + -- cgit v1.2.3