Make community service rely on Supabase for reads

cgen-5e5b0294d5b843cdb86140eca686cef6
This commit is contained in:
Builder.io 2025-10-01 00:57:36 +00:00
parent 30704020b0
commit 13bd717336

View file

@ -234,47 +234,27 @@ export const achievementService = {
// Community Services // Community Services
export const communityService = { export const communityService = {
async getPosts(limit = 10): Promise<CommunityPost[]> { async getPosts(limit = 10): Promise<CommunityPost[]> {
try { const { data, error } = await supabase
const resp = await fetch(`/api/posts?limit=${limit}`); .from("community_posts")
if (resp.ok) { .select(
const data = await resp.json(); `
if (Array.isArray(data)) { *,
return data as CommunityPost[]; user_profiles (
} username,
} else if (resp.status >= 400) { full_name,
console.warn("API responded with", resp.status); avatar_url
}
} catch (error) {
console.warn("Failed to fetch posts via API:", error);
}
try {
const { data, error } = await supabase
.from("community_posts")
.select(
`
*,
user_profiles (
username,
full_name,
avatar_url
)
`,
) )
.eq("is_published", true) `,
.order("created_at", { ascending: false }) )
.limit(limit); .eq("is_published", true)
.order("created_at", { ascending: false })
.limit(limit);
if (error) { if (error) {
console.error("Failed to load posts from Supabase:", error); throw error;
return [];
}
return (Array.isArray(data) ? data : []) as CommunityPost[];
} catch (error) {
console.error("Unexpected error loading posts:", error);
return [];
} }
return (Array.isArray(data) ? data : []) as CommunityPost[];
}, },
async createPost( async createPost(
@ -314,37 +294,17 @@ export const communityService = {
}, },
async getUserPosts(userId: string): Promise<CommunityPost[]> { async getUserPosts(userId: string): Promise<CommunityPost[]> {
try { const { data, error } = await supabase
const resp = await fetch(`/api/user/${userId}/posts`); .from("community_posts")
if (resp.ok) { .select("*")
const data = await resp.json(); .eq("author_id", userId)
if (Array.isArray(data)) { .order("created_at", { ascending: false });
return data as CommunityPost[];
} if (error) {
} else if (resp.status >= 400) { throw error;
console.warn("API responded with", resp.status);
}
} catch (error) {
console.warn("Failed to fetch user posts via API:", error);
} }
try { return (Array.isArray(data) ? data : []) as CommunityPost[];
const { data, error } = await supabase
.from("community_posts")
.select("*")
.eq("author_id", userId)
.order("created_at", { ascending: false });
if (error) {
console.error("Failed to load user posts from Supabase:", error);
return [];
}
return (Array.isArray(data) ? data : []) as CommunityPost[];
} catch (error) {
console.error("Unexpected error loading user posts:", error);
return [];
}
}, },
}; };