@asgerami/zemenay-blog
Version:
Plug-and-play blog system for Next.js - Get a fully functional blog running in minutes with zero configuration
235 lines (234 loc) • 7.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchCategories = fetchCategories;
exports.fetchTags = fetchTags;
exports.fetchPostsWithCategoriesAndTags = fetchPostsWithCategoriesAndTags;
exports.createCategory = createCategory;
exports.createTag = createTag;
exports.addCategoriesToPost = addCategoriesToPost;
exports.addTagsToPost = addTagsToPost;
exports.filterPostsByCategory = filterPostsByCategory;
exports.filterPostsByTag = filterPostsByTag;
const supabase_1 = require("./supabase");
/**
* Fetch all categories
*/
async function fetchCategories() {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
const { data, error } = await supabase
.from("categories")
.select("*")
.order("name");
if (error)
throw error;
return data || [];
}
catch (error) {
console.error("Error fetching categories:", error);
return [];
}
}
/**
* Fetch all tags
*/
async function fetchTags() {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
const { data, error } = await supabase
.from("tags")
.select("*")
.order("name");
if (error)
throw error;
return data || [];
}
catch (error) {
console.error("Error fetching tags:", error);
return [];
}
}
/**
* Fetch posts with their categories and tags
*/
async function fetchPostsWithCategoriesAndTags() {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
// First, get all posts
const { data: posts, error: postsError } = await supabase
.from("posts")
.select("*")
.order("created_at", { ascending: false });
if (postsError)
throw postsError;
if (!posts || posts.length === 0) {
return [];
}
// Get categories for all posts
const { data: postCategories, error: categoriesError } = await supabase
.from("post_categories")
.select(`
post_id,
categories (
id,
name,
slug,
description,
color,
created_at
)
`)
.in("post_id", posts.map((p) => p.id));
if (categoriesError)
throw categoriesError;
// Get tags for all posts
const { data: postTags, error: tagsError } = await supabase
.from("post_tags")
.select(`
post_id,
tags (
id,
name,
slug,
color,
created_at
)
`)
.in("post_id", posts.map((p) => p.id));
if (tagsError)
throw tagsError;
// Combine posts with their categories and tags
const postsWithCategoriesAndTags = posts.map((post) => {
var _a, _b, _c, _d;
const categories = ((_b = (_a = postCategories === null || postCategories === void 0 ? void 0 : postCategories.filter((pc) => pc.post_id === post.id)) === null || _a === void 0 ? void 0 : _a.map((pc) => pc.categories)) === null || _b === void 0 ? void 0 : _b.filter(Boolean)) || [];
const tags = ((_d = (_c = postTags === null || postTags === void 0 ? void 0 : postTags.filter((pt) => pt.post_id === post.id)) === null || _c === void 0 ? void 0 : _c.map((pt) => pt.tags)) === null || _d === void 0 ? void 0 : _d.filter(Boolean)) || [];
return Object.assign(Object.assign({}, post), { categories: categories, tags: tags });
});
return postsWithCategoriesAndTags;
}
catch (error) {
console.error("Error fetching posts with categories and tags:", error);
return [];
}
}
/**
* Create a new category
*/
async function createCategory(name, description, color) {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
const slug = name
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "");
const { data, error } = await supabase
.from("categories")
.insert([
{
name,
slug,
description,
color: color || "#0066cc",
},
])
.select()
.single();
if (error)
throw error;
return data;
}
catch (error) {
console.error("Error creating category:", error);
return null;
}
}
/**
* Create a new tag
*/
async function createTag(name, color) {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
const slug = name
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "");
const { data, error } = await supabase
.from("tags")
.insert([
{
name,
slug,
color: color || "#666666",
},
])
.select()
.single();
if (error)
throw error;
return data;
}
catch (error) {
console.error("Error creating tag:", error);
return null;
}
}
/**
* Add categories to a post
*/
async function addCategoriesToPost(postId, categoryIds) {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
// Remove existing categories
await supabase.from("post_categories").delete().eq("post_id", postId);
// Add new categories
if (categoryIds.length > 0) {
const { error } = await supabase.from("post_categories").insert(categoryIds.map((categoryId) => ({
post_id: postId,
category_id: categoryId,
})));
if (error)
throw error;
}
return true;
}
catch (error) {
console.error("Error adding categories to post:", error);
return false;
}
}
/**
* Add tags to a post
*/
async function addTagsToPost(postId, tagIds) {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
// Remove existing tags
await supabase.from("post_tags").delete().eq("post_id", postId);
// Add new tags
if (tagIds.length > 0) {
const { error } = await supabase.from("post_tags").insert(tagIds.map((tagId) => ({
post_id: postId,
tag_id: tagId,
})));
if (error)
throw error;
}
return true;
}
catch (error) {
console.error("Error adding tags to post:", error);
return false;
}
}
/**
* Filter posts by category
*/
function filterPostsByCategory(posts, categorySlug) {
return posts.filter((post) => { var _a; return (_a = post.categories) === null || _a === void 0 ? void 0 : _a.some((category) => category.slug === categorySlug); });
}
/**
* Filter posts by tag
*/
function filterPostsByTag(posts, tagSlug) {
return posts.filter((post) => { var _a; return (_a = post.tags) === null || _a === void 0 ? void 0 : _a.some((tag) => tag.slug === tagSlug); });
}