Skip to content

Commit fba061f

Browse files
committed
feat(libs): データ作成処理を分離
- 取得する情報を追加(imas:Idol_1st, schema:MusicRelease) - データを件数降順でソート
1 parent 41b97d9 commit fba061f

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

libs/create.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { CardDetail } from "@interfaces/card.ts";
2+
import { ImasparqlResponse } from "@interfaces/imasparql.ts";
3+
4+
/**
5+
* クラス名をラベルに変換
6+
* @param c クラス名
7+
* @returns ラベル
8+
*/
9+
function toLabel(c: string): string {
10+
const altLabels = new Map([
11+
["MusicAlbum", "アルバム"],
12+
["MusicRelease", "CD"],
13+
["MusicComposition", "作詞・作曲家"],
14+
["MusicRecording", "楽曲"],
15+
]);
16+
17+
return altLabels.get(c) || "Unknown";
18+
}
19+
20+
/**
21+
* アイコンを取得
22+
* @param c クラス名
23+
* @returns アイコン名
24+
*/
25+
function getIcon(c: string): string {
26+
const icons = new Map([
27+
["CallName", "messages"],
28+
]);
29+
30+
return icons.get(c) || "error-404";
31+
}
32+
33+
/**
34+
* APIのレスポンスからカードに表示するデータを作成
35+
* @param json レスポンス
36+
* @returns カードに表示するデータの配列
37+
*/
38+
export function createCardDetails(json: ImasparqlResponse): CardDetail[] {
39+
console.log(json);
40+
41+
const cards = json.results.bindings.map((e): CardDetail => {
42+
return {
43+
title: e.label?.value || toLabel(e.class.value),
44+
count: e.count.value,
45+
icon: getIcon(e.class.value),
46+
};
47+
});
48+
49+
return cards;
50+
}

libs/fetch.ts

+6-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { createCardDetails } from "./create.ts";
2+
13
import { CardDetail } from "@interfaces/card.ts";
24
import { ImasparqlResponse } from "@interfaces/imasparql.ts";
35

4-
const baseUrl = "https://sparql.crssnky.xyz/spql/imas/query";
6+
const endpointUrl = "https://sparql.crssnky.xyz/spql/imas/query";
57

68
const query = `PREFIX owl: <http://www.w3.org/2002/07/owl#>
79
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@@ -12,52 +14,18 @@ PREFIX imas: <https://sparql.crssnky.xyz/imasrdf/URIs/imas-schema.ttl#>
1214
SELECT (IF(CONTAINS(STR(?o), "#"), STRAFTER(str(?o), "#"), STRAFTER(STR(?o), STR(schema:))) AS ?class) ?label (COUNT(DISTINCT ?s) AS ?count)
1315
WHERE {
1416
?s rdf:type ?o.
15-
FILTER(?o NOT IN (rdf:Property, rdfs:Class, owl:Ontology, imas:Idol_1st, schema:MusicRelease))
17+
FILTER(?o NOT IN (rdf:Property, rdfs:Class, owl:Ontology))
1618
OPTIONAL { ?o rdfs:label ?label }
1719
}
1820
GROUP BY ?o ?label
19-
ORDER BY ?o`;
20-
21-
/**
22-
* APIのレスポンスからカードに表示するデータを作成
23-
* @param json レスポンス
24-
* @returns カードに表示するデータの配列
25-
*/
26-
function createCardDetails(json: ImasparqlResponse): CardDetail[] {
27-
console.log(json);
28-
29-
const cards: CardDetail[] = [
30-
{
31-
title: "アイドル",
32-
count: "335",
33-
icon: "user",
34-
},
35-
{
36-
title: "ユニット",
37-
count: "1520",
38-
icon: "users",
39-
},
40-
{
41-
title: "ボイスアイドルオーディション",
42-
count: "6",
43-
icon: "microphone-2",
44-
},
45-
{
46-
title: "呼び名",
47-
count: "5923",
48-
icon: "messages",
49-
},
50-
];
51-
52-
return cards;
53-
}
21+
ORDER BY DESC (?count)`;
5422

5523
/**
5624
* カードに表示するデータを取得
5725
* @returns カードに表示するデータの配列
5826
*/
5927
export async function fetchCardDetails(): Promise<CardDetail[]> {
60-
const url = new URL(baseUrl);
28+
const url = new URL(endpointUrl);
6129
url.searchParams.append("query", query);
6230

6331
// TODO: アクセスエラー時の処理・タイムアウトの実装

0 commit comments

Comments
 (0)