source: database/drizzle/queries/builds.ts@ 1ebac59

main
Last change on this file since 1ebac59 was 1ebac59, checked in by Tome <gjorgievtome@…>, 7 months ago

add schema and queries

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[1ebac59]1import type { Database } from "../db";
2import {buildComponentsTable, buildsTable, favoriteBuildsTable, ratingBuildsTable, reviewsTable} from "../schema";
3import {eq, desc, and, sql, ilike} from "drizzle-orm";
4
5export async function getPendingBuilds(db: Database) {
6 const pendingBuildsList = await db
7 .select({
8 id: buildsTable.id,
9 user_id: buildsTable.userId,
10 name: buildsTable.name,
11 created_at: buildsTable.createdAt,
12 total_price: buildsTable.totalPrice
13 })
14 .from(buildsTable)
15 .where(
16 eq(buildsTable.isApproved, false)
17 );
18
19 return pendingBuildsList;
20}
21
22export async function getUserBuilds(db: Database, userId: number) {
23 const userBuildsList = await db
24 .select({
25 id: buildsTable.id,
26 user_id: buildsTable.userId,
27 name: buildsTable.name,
28 created_at: buildsTable.createdAt,
29 total_price: buildsTable.totalPrice
30 })
31 .from(buildsTable)
32 .where(
33 eq(buildsTable.userId, userId)
34 );
35
36 return userBuildsList;
37}
38
39export async function setBuildApprovalStatus(db: Database, buildId: number, isApproved: boolean){
40 const result = await db
41 .update(buildsTable)
42 .set({
43 isApproved: isApproved
44 })
45 .where(
46 eq(buildsTable.id, buildId)
47 );
48
49 return result.rowCount;
50}
51
52export async function getFavoriteBuilds(db: Database, userId: number) {
53 const favoriteBuildsList = await db
54 .select({
55 id: buildsTable.id,
56 user_id: buildsTable.userId,
57 name: buildsTable.name,
58 created_at: buildsTable.createdAt,
59 total_price: buildsTable.totalPrice
60 })
61 .from(buildsTable)
62 .innerJoin(
63 favoriteBuildsTable,
64 eq(buildsTable.id, favoriteBuildsTable.buildId)
65 )
66 .where(
67 eq(favoriteBuildsTable.userId, userId)
68 );
69
70 return favoriteBuildsList;
71}
72
73export async function getApprovedBuilds(db: Database, limit?: number, q?: string) {
74 let queryConditions = [];
75
76 if (q) {
77 queryConditions.push(
78 ilike(buildsTable.name, `%${q}%`)
79 );
80 }
81
82 const approvedBuildsList = await db
83 .select({
84 id: buildsTable.id,
85 user_id: buildsTable.userId,
86 name: buildsTable.name,
87 created_at: buildsTable.createdAt,
88 total_price: buildsTable.totalPrice
89 })
90 .from(buildsTable)
91 .where(
92 and (
93 eq(buildsTable.isApproved, true),
94 ...queryConditions
95 )
96 )
97 .orderBy(
98 desc(buildsTable.totalPrice)
99 )
100 .limit(limit || 100); // 100 placeholder
101
102 return approvedBuildsList;
103}
104
105export async function getHighestRankedBuilds(db: Database, limit? : number) {
106 const highestRankedBuildsList = await db
107 .select({
108 id: buildsTable.id,
109 user_id: buildsTable.userId,
110 name: buildsTable.name,
111 created_at: buildsTable.createdAt,
112 total_price: buildsTable.totalPrice
113 })
114 .from(buildsTable)
115 .innerJoin(
116 ratingBuildsTable,
117 eq(buildsTable.id, ratingBuildsTable.buildId)
118 )
119 .where(
120 eq(buildsTable.isApproved, true)
121 )
122 .groupBy(
123 buildsTable.id
124 )
125 .orderBy(
126 desc(sql<number>`AVG(${ratingBuildsTable.value}::float)`)
127 )
128 .limit(limit || 100); // 100 placeholder
129
130 return highestRankedBuildsList;
131}
132
133export async function getBuildDetails(db: Database, buildId: number) {
134 const buildDetails = await db
135 .select()
136 .from(buildsTable)
137 .where(
138 eq(buildsTable.id, buildId)
139 )
140 .limit(1);
141
142 return buildDetails[0] ?? null;
143}
144
145export async function toggleFavoriteBuild(db: Database, userId: number, buildId: number) {
146 const existing = await db
147 .select()
148 .from(favoriteBuildsTable)
149 .where(
150 and(
151 eq(favoriteBuildsTable.userId, userId),
152 eq(favoriteBuildsTable.buildId, buildId)
153 )
154 )
155 .limit(1);
156
157 if (existing.length) {
158 await db
159 .delete(favoriteBuildsTable)
160 .where(
161 and(
162 eq(favoriteBuildsTable.userId, userId),
163 eq(favoriteBuildsTable.buildId, buildId)
164 )
165 );
166
167 return { favorite: false };
168 }
169
170 await db
171 .insert(favoriteBuildsTable)
172 .values({
173 userId,
174 buildId,
175 });
176
177 return { favorite: true };
178}
179
180export async function setBuildRating(db: Database, userId: number, buildId: number, value: number) {
181 const result = await db
182 .insert(ratingBuildsTable)
183 .values({
184 userId,
185 buildId,
186 value: value.toString()
187 })
188 .onConflictDoUpdate({
189 target: [ratingBuildsTable.userId, ratingBuildsTable.buildId],
190 set: {
191 value: value.toString(),
192 },
193 });
194}
195
196export async function setBuildReview(db: Database, userId: number, buildId: number, content: string) {
197 const existing = await db
198 .select()
199 .from(reviewsTable)
200 .where(
201 and(
202 eq(reviewsTable.userId, userId),
203 eq(reviewsTable.buildId, buildId)
204 )
205 )
206 .limit(1);
207
208 if (existing.length) {
209 const result = await db
210 .update(reviewsTable)
211 .set({
212 content: content,
213 })
214 .where(
215 and(
216 eq(reviewsTable.userId, userId),
217 eq(reviewsTable.buildId, buildId)
218 )
219 );
220
221 return;
222 }
223
224 const result = await db
225 .insert(reviewsTable)
226 .values({
227 userId,
228 buildId,
229 content,
230 createdAt: new Date().toISOString().split('T')[0]
231 });
232}
233
234export async function cloneBuild(db: Database, userId: number, buildId: number) {
235 const [buildToClone] = await db
236 .select()
237 .from(buildsTable)
238 .where(
239 eq(buildsTable.id, buildId)
240 )
241 .limit(1);
242
243 if (!buildToClone) return null;
244
245 const [newBuild] = await db
246 .insert(buildsTable)
247 .values({
248 userId: userId,
249 name: `${buildToClone.name} (copy)`,
250 createdAt: new Date().toISOString().split('T')[0],
251 description: buildToClone.description,
252 totalPrice: buildToClone.totalPrice,
253 isApproved: false
254 })
255 .returning({ id: buildsTable.id });
256
257 const components = await db
258 .select()
259 .from(buildComponentsTable)
260 .where(
261 eq(buildComponentsTable.buildId, buildId)
262 );
263
264 if(components.length) {
265 await db
266 .insert(buildComponentsTable)
267 .values(
268 components.map(component => ({
269 buildId: newBuild.id,
270 componentId: component.componentId
271 }))
272 );
273 }
274
275 return newBuild.id;
276}
277
278
279
Note: See TracBrowser for help on using the repository browser.