diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-03-09 13:38:24 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-03-09 13:38:24 -0500 |
commit | 7417966101a0b0bbd0fefc8723482ae5a2010f19 (patch) | |
tree | 9865c76ee5f40a55a7364c316b7cf1c1bb82485c /src/guff/models/post.cr | |
parent | 019667042398a78cf58e6475c81e5c75c4fff895 (diff) | |
download | old-guff-7417966101a0b0bbd0fefc8723482ae5a2010f19.tar.bz2 old-guff-7417966101a0b0bbd0fefc8723482ae5a2010f19.zip |
add state support
Diffstat (limited to 'src/guff/models/post.cr')
-rw-r--r-- | src/guff/models/post.cr | 128 |
1 files changed, 87 insertions, 41 deletions
diff --git a/src/guff/models/post.cr b/src/guff/models/post.cr index 7fd6ec1..7814d9f 100644 --- a/src/guff/models/post.cr +++ b/src/guff/models/post.cr @@ -57,7 +57,11 @@ module Guff remove_posts: " UPDATE posts - SET is_active = 0 + + SET state = (SELECT state + FROM post_states + WHERE name = 'deleted') + WHERE site_id = :site_id AND post_id IN (%{post_ids}) ", @@ -199,45 +203,65 @@ module Guff ) end - DATE_FILTERS = { - :year => { - :re => /^\d{4}$/, - :fmt => "%Y", + FILTERS = { + year: { + type: :int, + re: /^\d{4}$/, + sql: "(strftime('%%Y', a.posted_at) + 0 = %d)", }, - :month => { - :re => /^\d{1,2}$/, - :fmt => "%m", + month: { + type: :int, + re: /^\d{1,2}$/, + sql: "(strftime('%%m', a.posted_at) + 0 = %d)", }, - :day => { - :re => /^\d{1,2}$/, - :fmt => "%d", + day: { + type: :int, + re: /^\d{1,2}$/, + sql: "(strftime('%%d', a.posted_at) + 0 = %d)", }, - } - DATE_FILTER_CLAUSE = "( - strftime('%s', a.posted_at) + 0 = %d - )" + slug: { + type: :string, + sql: "a.slug = '%s'" + }, - SLUG_FILTER_CLAUSE = "( - a.slug = '%s' - )" + state: { + type: :string, + sql: "( + a.state = ( + SELECT state + FROM post_states + WHERE name = '%s' + ) + )", + }, + } private def get_filter_clause( filters : Hash(Symbol, String) ) : String - r = [ - "1 = 1" # true - ] + # define results + r = ["1 = 1"] # true - DATE_FILTERS.each do |key, f| + FILTERS.each do |key, f| if val = filters[key]? # check value format - raise "invalid #{key} filter" unless val =~ (f[:re] as Regex) + if f[:re]? && val !~ (f[:re] as Regex) + raise "invalid #{key} filter" + end - # add to filters - r << DATE_FILTER_CLAUSE % [f[:fmt] as String, val.to_i] + # add to results + r << (f[:sql] as String) % [case f[:type] + when :int + val.to_i + when :string + @db.quote(val) + else + # never reached + raise "unknown format type: #{f[:type]}" + end] end end @@ -245,17 +269,12 @@ module Guff # TODO end - # add slug filter - if filters.has_key?(:slug) - r << SLUG_FILTER_CLAUSE % [@db.quote(filters[:slug])] - end - - # return result + # return filter clause r.join(" AND ") end private def get_sort_clause( - sort : String + sort = "posted_at" : String ) : String # verify sort column raise "unknown sort column" unless COLUMNS.has_key?(sort) @@ -307,6 +326,7 @@ module Guff name = "" : String, body = "" : String, tags = [] of String : Array(String), + state = "draft" : String?, ) : Int64 post_id = -1_i64 @@ -327,11 +347,12 @@ module Guff # get post id post_id = last_insert_row_id - # set post tags - set_tags( - site_id: site_id, - post_id: post_id, - tags: tags, + # update state and tags + update_post( + site_id: site_id, + post_id: post_id, + tags: tags, + state: state, ) end @@ -339,6 +360,10 @@ module Guff post_id end + ####################### + # update_post methods # + ####################### + def update_post( site_id = nil : Int?, post_id = nil : Int?, @@ -346,10 +371,11 @@ module Guff name = nil : String?, body = nil : String?, tags = nil : Array(String)?, - posted = nil : Bool? + state = nil : String? ) raise "null post_id" if post_id.nil? + # build initial query sets = [] of String args = { "site_id": (site_id || @models.site.get_default).to_s, @@ -371,9 +397,20 @@ module Guff args["body"] = body end - unless posted.nil? - val = posted ? "CURRENT_TIMESTAMP" : "NULL" - sets << "posted_at = %s" % [val] + if state + # update state + sets << "state = ( + SELECT state + FROM post_states + WHERE name = :state + )" + args["state"] = state + + # update posted_at + # FIXME: there should be a better way to do this + sets << "posted_at = %s" % [ + (state == "posted") ? "CURRENT_TIMESTAMP" : "NULL" + ] end if sets.size > 0 || tags @@ -399,6 +436,10 @@ module Guff end end + ################ + # remove_posts # + ################ + def remove_posts( site_id = nil : Int?, post_ids = [] of Int : Array(Int) @@ -415,6 +456,10 @@ module Guff nil end + #################### + # set_tags methods # + #################### + def set_tags( site_id = nil : Int?, post_id = nil : Int?, @@ -444,6 +489,7 @@ module Guff end end + # no return value nil end end |