Initial commit

This commit is contained in:
2023-01-27 22:55:09 +11:00
commit 833f3ea8b2
130 changed files with 5637 additions and 0 deletions

84
app/repos/post_repo.rb Normal file
View File

@@ -0,0 +1,84 @@
module Adamantium
module Repos
class PostRepo < Adamantium::Repo[:posts]
commands :update
def create(post_attrs)
posts.transaction do
new_post = posts.changeset(:create, post_attrs).commit
post_attrs[:category].each do |tag_name|
next if tag_name == ""
tag = posts.tags.where(label: tag_name).one ||
posts
.tags
.changeset(:create, {label: tag_name, slug: tag_name.downcase.strip.tr(" ", "-").gsub(/[^\w-]/, "")})
.commit
posts.post_tags.changeset(:create, {
post_id: new_post.id,
tag_id: tag[:id]
})
.commit
end
new_post
end
end
def post_listing(limit: nil)
posts
.where(post_type: "post")
.published
.combine(:tags)
.order(Sequel.desc(:published_at))
.limit(limit)
.to_a
end
def bookmark_listing(query: nil)
base = posts
.where(post_type: "bookmark")
.published
.combine(:tags)
.order(Sequel.desc(:published_at))
query ? base.where(Sequel.ilike(:name, "%#{query}%")).to_a : base.to_a
end
def for_rss
posts
.where(post_type: "post")
.published
.combine(:tags)
.order(Sequel.desc(:published_at))
.to_a
end
def fetch!(slug)
posts
.published
.combine(:tags)
.where(slug: slug)
.one!
end
def slug_exists?(slug)
!!posts
.where(slug: slug)
.one
end
def delete!(slug)
delete_post = posts.where(slug: slug).command(:update)
delete_post.call(published_at: nil)
end
def restore!(slug)
delete_post = posts.where(slug: slug).command(:update)
delete_post.call(published_at: Time.now)
end
end
end
end

View File

@@ -0,0 +1,26 @@
module Adamantium
module Repos
class PostTagRepo < Adamantium::Repo[:post_tags]
def posts_tagged(tag:)
tag_id = post_tags
.tags
.where(slug: tag)
.one!
.id
post_ids = post_tags
.where(tag_id: tag_id)
.to_a
.map(&:post_id)
post_tags
.posts
.where(id: post_ids)
.published
.combine(:tags)
.order(Sequel.desc(:published_at))
.to_a
end
end
end
end

9
app/repos/tag_repo.rb Normal file
View File

@@ -0,0 +1,9 @@
module Adamantium
module Repos
class TagRepo < Adamantium::Repo[:tags]
def fetch!(slug)
tags.where(slug: slug).one!
end
end
end
end