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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
import { groceries, categories, references } from '$lib/server/db/schema';
import { eq, inArray, asc, desc } from 'drizzle-orm';
import { referenceOrder } from './references';
import type { AppDatabase } from '$lib/server/db/types';
import type { Category, Grocery, Reference } from './types';
export interface EnrichedGrocery extends Grocery {
categoryName: string | null;
bestRef: Reference | null;
}
function enrichItems(
rawItems: Grocery[],
rawCategories: Category[],
rawReferences: Reference[],
): EnrichedGrocery[] {
const catMap = new Map(rawCategories.map((c) => [c.id, c.name]));
const bestRef = new Map<number, Reference>();
for (const ref of rawReferences) {
if (!bestRef.has(ref.groceryId)) {
bestRef.set(ref.groceryId, ref);
}
}
return rawItems.map((i) => ({
...i,
categoryName: i.categoryId ? (catMap.get(i.categoryId) ?? null) : null,
bestRef: bestRef.get(i.id) ?? null,
}));
}
export class GroceriesService {
constructor(private readonly db: AppDatabase) {}
async loadUnchecked(): Promise<{
items: EnrichedGrocery[];
categories: Category[];
}> {
const [items, cats, refs] = await Promise.all([
this.db
.select()
.from(groceries)
.where(eq(groceries.checked, false))
.orderBy(desc(groceries.createdAt), desc(groceries.id)),
this.db
.select()
.from(categories)
.orderBy(asc(categories.order), asc(categories.name)),
this.db
.select()
.from(references)
.orderBy(...referenceOrder),
]);
return {
items: enrichItems(items, cats, refs),
categories: cats,
};
}
async loadAll(): Promise<{
items: EnrichedGrocery[];
categories: Category[];
}> {
const [items, cats, refs] = await Promise.all([
this.db
.select()
.from(groceries)
.orderBy(desc(groceries.createdAt), desc(groceries.id)),
this.db
.select()
.from(categories)
.orderBy(asc(categories.order), asc(categories.name)),
this.db
.select()
.from(references)
.orderBy(...referenceOrder),
]);
return {
items: enrichItems(items, cats, refs),
categories: cats,
};
}
async toggle(id: number): Promise<void> {
const [item] = await this.db
.select()
.from(groceries)
.where(eq(groceries.id, id));
if (!item) throw new Error('Grocery not found');
await this.db
.update(groceries)
.set({ checked: !item.checked })
.where(eq(groceries.id, id));
}
async addOrUncheck(
name: string,
categoryId: number | null,
): Promise<{ item: Grocery; created: boolean }> {
const [existing] = await this.db
.select()
.from(groceries)
.where(eq(groceries.name, name))
.limit(1);
if (existing) {
await this.db
.update(groceries)
.set({
checked: false,
categoryId: categoryId ?? existing.categoryId,
})
.where(eq(groceries.id, existing.id));
return {
item: {
...existing,
checked: false,
categoryId: categoryId ?? existing.categoryId,
},
created: false,
};
}
const [inserted] = await this.db
.insert(groceries)
.values({
name,
categoryId,
createdAt: Temporal.Now.instant().toString({
smallestUnit: 'millisecond',
}),
})
.returning();
return { item: inserted, created: true };
}
async updateGrocery(
id: number,
name: string,
categoryId: number | null,
): Promise<void> {
await this.db
.update(groceries)
.set({ name, categoryId })
.where(eq(groceries.id, id));
}
async updateGroceryCategory(
id: number,
categoryId: number | null,
): Promise<void> {
await this.db
.update(groceries)
.set({ categoryId })
.where(eq(groceries.id, id));
}
async bulkUpdateGroceryCategory(
ids: number[],
categoryId: number | null,
): Promise<void> {
await this.db
.update(groceries)
.set({ categoryId })
.where(inArray(groceries.id, ids));
}
async deleteGrocery(id: number): Promise<void> {
await this.db.delete(groceries).where(eq(groceries.id, id));
}
}
|