@k34a/blog 0.0.16 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/services/articles.js +46 -40
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { articleSortByVsQuery as
|
|
1
|
+
import { articleSortByVsQuery as h } from "./search-params.js";
|
|
2
2
|
import "zod";
|
|
3
|
-
import { PAGE_SIZE as
|
|
4
|
-
class
|
|
3
|
+
import { PAGE_SIZE as p } from "../cfg.js";
|
|
4
|
+
class q {
|
|
5
5
|
db;
|
|
6
6
|
constructor(e) {
|
|
7
7
|
this.db = e;
|
|
@@ -11,25 +11,25 @@ class y {
|
|
|
11
11
|
* Includes its tags.
|
|
12
12
|
*/
|
|
13
13
|
async getBySlug(e) {
|
|
14
|
-
const { data: t, error:
|
|
15
|
-
if (
|
|
16
|
-
return console.error("Error fetching article details:",
|
|
17
|
-
const { data:
|
|
18
|
-
if (
|
|
14
|
+
const { data: t, error: r } = await this.db.from("articles").select("*").eq("slug", e).eq("status", "Published").single();
|
|
15
|
+
if (r)
|
|
16
|
+
return console.error("Error fetching article details:", r.message), null;
|
|
17
|
+
const { data: g, error: s } = await this.db.from("tag_articles").select("tag_id").eq("article_id", t.id);
|
|
18
|
+
if (s)
|
|
19
19
|
return console.error(
|
|
20
20
|
"Error fetching article tag links:",
|
|
21
|
-
|
|
21
|
+
s.message
|
|
22
22
|
), { ...t, tags: [] };
|
|
23
|
-
const n =
|
|
24
|
-
let
|
|
23
|
+
const n = g?.map((c) => c.tag_id) || [];
|
|
24
|
+
let o = [];
|
|
25
25
|
if (n.length > 0) {
|
|
26
|
-
const { data:
|
|
27
|
-
|
|
26
|
+
const { data: c, error: l } = await this.db.from("tags").select("name").in("id", n);
|
|
27
|
+
l ? console.error(
|
|
28
28
|
"Error fetching article tag names:",
|
|
29
|
-
|
|
30
|
-
) :
|
|
29
|
+
l.message
|
|
30
|
+
) : o = c.map((d) => d.name);
|
|
31
31
|
}
|
|
32
|
-
return { ...t, tags:
|
|
32
|
+
return { ...t, tags: o };
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Get total number of articles.
|
|
@@ -46,45 +46,51 @@ class y {
|
|
|
46
46
|
*/
|
|
47
47
|
async getTagNames() {
|
|
48
48
|
const { data: e, error: t } = await this.db.from("tags").select("name");
|
|
49
|
-
return t ? (console.error("Error fetching tags:", t.message), []) : e?.map((
|
|
49
|
+
return t ? (console.error("Error fetching tags:", t.message), []) : e?.map((r) => r.name) ?? [];
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Paginated list of articles with filters and sorting.
|
|
53
53
|
*/
|
|
54
54
|
async list(e) {
|
|
55
|
-
const { page: t, search:
|
|
56
|
-
let
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
55
|
+
const { page: t, search: r, sortBy: g, tags: s } = e, n = h[g] ?? h.latest;
|
|
56
|
+
let o = this.db.from("article_with_tags").select("id").eq("status", "Published");
|
|
57
|
+
r && (o = o.ilike("title", `%${r}%`)), s && s.length > 0 && (o = o.in("tag", s));
|
|
58
|
+
const { data: c, error: l } = await o;
|
|
59
|
+
if (l)
|
|
60
|
+
return console.error("Count failed:", l), { items: [], total: 0 };
|
|
61
|
+
const d = new Set(c?.map((a) => a.id)).size;
|
|
62
|
+
let i = this.db.from("article_with_tags").select("*").eq("status", "Published");
|
|
63
|
+
r && (i = i.ilike("title", `%${r}%`)), s && s.length > 0 && (i = i.in("tag", s)), i = i.order(n.column, { ascending: n.ascending });
|
|
64
|
+
const u = t * p, _ = u + p - 1;
|
|
65
|
+
i = i.range(u, _);
|
|
66
|
+
const { data: b, error: f } = await i;
|
|
67
|
+
if (f)
|
|
68
|
+
return console.error("Error fetching paginated articles:", f), { items: [], total: 0 };
|
|
69
|
+
const m = /* @__PURE__ */ new Map();
|
|
70
|
+
for (const a of b ?? [])
|
|
71
|
+
m.has(a.id) || m.set(a.id, {
|
|
72
|
+
id: a.id,
|
|
73
|
+
title: a.title,
|
|
74
|
+
description: a.description,
|
|
75
|
+
slug: a.slug,
|
|
76
|
+
created_at: a.created_at,
|
|
77
|
+
banner_image: a.banner_image
|
|
72
78
|
});
|
|
73
79
|
return {
|
|
74
|
-
items: [...
|
|
75
|
-
total:
|
|
80
|
+
items: [...m.values()],
|
|
81
|
+
total: d
|
|
76
82
|
};
|
|
77
83
|
}
|
|
78
84
|
async getDescription(e) {
|
|
79
|
-
const { data: t, error:
|
|
80
|
-
return
|
|
85
|
+
const { data: t, error: r } = await this.db.storage.from("content").download(`articles/${e}/description.html`);
|
|
86
|
+
return r ? (console.error(
|
|
81
87
|
`Error fetching article description for article ID "${e}":`,
|
|
82
|
-
|
|
88
|
+
r.message
|
|
83
89
|
), null) : t ? await t.text() : (console.warn(
|
|
84
90
|
`No article description file found for article ID: ${e}`
|
|
85
91
|
), null);
|
|
86
92
|
}
|
|
87
93
|
}
|
|
88
94
|
export {
|
|
89
|
-
|
|
95
|
+
q as ArticleService
|
|
90
96
|
};
|