Changeset 8fe7f1a for database/drizzle/queries/components.ts
- Timestamp:
- 12/28/25 17:27:19 (6 months ago)
- Branches:
- main
- Children:
- 1ac9ee4
- Parents:
- 67186d2
- File:
-
- 1 edited
-
database/drizzle/queries/components.ts (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/components.ts
r67186d2 r8fe7f1a 896 896 } 897 897 898 export async function addComponentToBuild(db: Database, buildId: number, componentId: number) { 899 const [build] = await db 900 .select() 901 .from(buildsTable) 902 .where( 903 eq(buildsTable.id, buildId) 904 ) 905 .limit(1); 906 907 if(!build) return null; 908 909 const [component] = await db 910 .select() 911 .from(componentsTable) 912 .where( 913 eq(componentsTable.id, componentId) 914 ) 915 .limit(1); 916 917 if(!component) return null; 918 919 const existing = await db 920 .select() 921 .from(buildComponentsTable) 922 .where( 923 and( 924 eq(buildComponentsTable.buildId, buildId), 925 eq(buildComponentsTable.componentId, componentId) 926 ) 927 ) 928 .limit(1); 929 930 if(existing.length > 0) return null; 931 932 const [result] = await db 933 .insert(buildComponentsTable) 934 .values({ 935 buildId, 936 componentId 937 }) 938 .returning({ 939 id: buildComponentsTable.buildId 940 }); 941 942 const buildComponents = await db 943 .select({ 944 price: componentsTable.price, 945 }) 946 .from(buildComponentsTable) 947 .innerJoin( 948 componentsTable, 949 eq(buildComponentsTable.componentId, componentsTable.id) 950 ) 951 .where( 952 eq(buildComponentsTable.buildId, buildId) 953 ); 954 955 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 956 957 await db 958 .update(buildsTable) 959 .set({ 960 totalPrice: totalPrice.toFixed(2) 961 }) 962 .where( 963 eq(buildsTable.id, buildId) 964 ); 965 966 return result?.id ?? null; 967 } 968 969 export async function removeComponentFromBuild(db: Database, buildId: number, componentId: number) { 970 const [build] = await db 971 .select() 972 .from(buildsTable) 973 .where( 974 eq(buildsTable.id, buildId) 975 ) 976 .limit(1); 977 978 if(!build) return null; 979 980 const [component] = await db 981 .select() 982 .from(componentsTable) 983 .where( 984 eq(componentsTable.id, componentId) 985 ) 986 .limit(1); 987 988 if(!component) return null; 989 990 const result = await db 991 .delete(buildComponentsTable) 992 .where( 993 and( 994 eq(buildComponentsTable.buildId, buildId), 995 eq(buildComponentsTable.componentId, componentId) 996 ) 997 ); 998 999 if(result.rowCount === 0) return null; 1000 1001 const buildComponents = await db 1002 .select({ 1003 price: componentsTable.price, 1004 }) 1005 .from(buildComponentsTable) 1006 .innerJoin( 1007 componentsTable, 1008 eq(buildComponentsTable.componentId, componentsTable.id) 1009 ) 1010 .where( 1011 eq(buildComponentsTable.buildId, buildId) 1012 ); 1013 1014 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 1015 1016 await db 1017 .update(buildsTable) 1018 .set({ 1019 totalPrice: totalPrice.toFixed(2) 1020 }) 1021 .where( 1022 eq(buildsTable.id, buildId) 1023 ); 1024 1025 return result; 1026 } 1027 898 export async function addComponentToBuild(db: Database, userId: number, buildId: number, componentId: number) { 899 return db.transaction(async (tx) => { 900 const [build] = await tx 901 .select() 902 .from(buildsTable) 903 .where( 904 and( 905 eq(buildsTable.id, buildId), 906 eq(buildsTable.userId, userId) 907 ) 908 ) 909 .limit(1); 910 911 if(!build || build.isApproved) return null; 912 913 const existing = await tx 914 .select() 915 .from(buildComponentsTable) 916 .where( 917 and( 918 eq(buildComponentsTable.buildId, buildId), 919 eq(buildComponentsTable.componentId, componentId) 920 ) 921 ) 922 .limit(1); 923 924 if (existing.length) return null; 925 926 await tx 927 .insert(buildComponentsTable) 928 .values({ 929 buildId, 930 componentId 931 }); 932 933 const buildComponents = await tx 934 .select({ 935 price: componentsTable.price, 936 }) 937 .from(buildComponentsTable) 938 .innerJoin( 939 componentsTable, 940 eq(buildComponentsTable.componentId, componentsTable.id) 941 ) 942 .where( 943 eq(buildComponentsTable.buildId, buildId) 944 ); 945 946 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 947 948 await tx 949 .update(buildsTable) 950 .set({ 951 totalPrice: totalPrice.toFixed(2) 952 }) 953 .where( 954 eq(buildsTable.id, buildId) 955 ); 956 957 return buildId; 958 }) 959 } 960 961 export async function removeComponentFromBuild(db: Database, userId: number, buildId: number, componentId: number) { 962 return db.transaction(async (tx) => { 963 const [build] = await tx 964 .select() 965 .from(buildsTable) 966 .where( 967 and( 968 eq(buildsTable.id, buildId), 969 eq(buildsTable.userId, userId) 970 ) 971 ) 972 .limit(1); 973 974 if(!build || build.isApproved) return null; 975 976 const result = await tx 977 .delete(buildComponentsTable) 978 .where( 979 and( 980 eq(buildComponentsTable.buildId, buildId), 981 eq(buildComponentsTable.componentId, componentId) 982 ) 983 ); 984 985 if(result.rowCount === 0) return null; 986 987 const buildComponents = await tx 988 .select({ 989 price: componentsTable.price, 990 }) 991 .from(buildComponentsTable) 992 .innerJoin( 993 componentsTable, 994 eq(buildComponentsTable.componentId, componentsTable.id) 995 ) 996 .where( 997 eq(buildComponentsTable.buildId, buildId) 998 ); 999 1000 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 1001 1002 await tx 1003 .update(buildsTable) 1004 .set({ 1005 totalPrice: totalPrice.toFixed(2) 1006 }) 1007 .where( 1008 eq(buildsTable.id, buildId) 1009 ); 1010 1011 return result; 1012 }) 1013 }
Note:
See TracChangeset
for help on using the changeset viewer.
