This repository was archived by the owner on Apr 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusesRepository.ts
More file actions
72 lines (64 loc) · 2.08 KB
/
Copy pathStatusesRepository.ts
File metadata and controls
72 lines (64 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { and, eq } from "drizzle-orm";
import { database } from "../connection.ts";
import { AlreadyInDatabaseError } from "../error/AlreadyInDatabaseError.ts";
import { StatusData, statusData } from "../schema/lodestone-news.ts";
import { Status } from "../../naagostone/type/Status.ts";
export class StatusesRepository {
public static async find(
title: string,
date: Date,
): Promise<StatusData | null> {
const result = await database
.select()
.from(statusData)
.where(
and(
eq(statusData.title, title),
eq(statusData.date, date),
),
)
.limit(1);
return result[0] ?? null;
}
public static async add(status: Status): Promise<number> {
const dateSQL = new Date(status.date);
const currentStatus = await this.find(status.title, dateSQL);
if (currentStatus) {
throw new AlreadyInDatabaseError(
"This status is already in the database",
);
}
const result = await database
.insert(statusData)
.values({
tag: status.tag,
title: status.title,
link: status.link,
date: dateSQL,
description: status.description.markdown,
descriptionV2: status.description.discord_components_v2 ?? null,
})
.$returningId();
return result[0].id;
}
public static async updateDescriptions(
id: number,
description: string,
descriptionV2: StatusData["descriptionV2"],
): Promise<void> {
await database
.update(statusData)
.set({ description, descriptionV2 })
.where(eq(statusData.id, id));
}
public static hasDescriptionChanged(
existing: StatusData,
status: Status,
): { descriptionChanged: boolean; descriptionV2Changed: boolean } {
const descriptionChanged = existing.description !== status.description.markdown;
const existingV2 = JSON.stringify(existing.descriptionV2 ?? null);
const newV2 = JSON.stringify(status.description.discord_components_v2 ?? null);
const descriptionV2Changed = existingV2 !== newV2;
return { descriptionChanged, descriptionV2Changed };
}
}