Skip to content

Commit

Permalink
used new RunQuery method and updated create recipe function
Browse files Browse the repository at this point in the history
  • Loading branch information
GouravNG committed Sep 11, 2024
1 parent a546474 commit bb82f82
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 80 deletions.
21 changes: 2 additions & 19 deletions db/createRecipe.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,8 @@ export const subCategoryQueryBuilder = (subCateg: string, parentId: string, desc
return query
}

export const recipeQueryBuilder = (
name: string,
description: string,
servings: number,
prepTime: number,
cookTime: number,
mainImg: string,
mainImgAlt: string,
userId: string,
categoryId: string
) => {
// INSERT INTO Recipe (name, description, servings, prep_time, cook_time, main_img, main_img_alt, created_at, updated_at, user_id, category_id)
// VALUES ('Cheesecake','A delicious and creamy cheesecake.',8,30,60,'/cheese.jpg','A slice of cheesecake',NOW(),NOW(),(SELECT id FROM User WHERE username = 'john_doe'),(SELECT id FROM Category WHERE name = 'Desserts'));
mainImg = '/cheese.jpg'
const query = `INSERT INTO rs.Recipe
(name, description, servings, prep_time, cook_time, main_img, main_img_alt, user_id, category_id) VALUES (
'${name}','${description}',${servings},${prepTime},${cookTime},'${mainImg}','${mainImgAlt}','${userId}','${categoryId}')`
console.log(query)
return query
export const createNewRecipeQueryBuilder = () => {
return `INSERT INTO rs.Recipe (name, description,preview, servings, prep_time, cook_time, main_img, main_img_alt, user_id, category_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING id`
}

export const ingridientsQueryBuilder = (valueString: string) => {
Expand Down
66 changes: 25 additions & 41 deletions db/dbFn/recipeFunction.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { RecipeSet, UserSet } from '@/types/database.types'
import pool from '../createPool.db'
import { recipeQueryBuilder } from '../createRecipe.db'
import { createNewRecipeQueryBuilder } from '../createRecipe.db'
import { runQueryV2 } from '@/utils/runQuery'

export const extractRecifeInformation = (
formData: FormData,
imgUrl: string = '/cheese.jpg',
categoryId: string = 'addOtherCategoryIdHere'
) => {
const recipeSet: RecipeSet = {
recipeName: formData.get('recipeName')?.toString()!,
recipeDescription: formData.get('imgDesc')?.toString()?.toString()!,
recipeServings: +formData.get('servings')!,
recipePrepTime: +formData.get('prep_time')!,
recipeCookTime: +formData.get('cook_time')!,
recipeMainImage: imgUrl,
recipeMainAlt: formData.get('mainImageAlt')?.toString()!,
recipeCategory_Id: categoryId,
name: formData.get('recipeName')?.toString()!,
description: formData.get('imgDesc')?.toString()?.toString(),
servings: +formData.get('servings')!,
prep_time: +formData.get('prep_time')!,
cook_time: +formData.get('cook_time')!,
main_img: imgUrl,
main_img_alt: formData.get('mainImageAlt')?.toString(),
category_id: categoryId,
}
return recipeSet
}
Expand Down Expand Up @@ -88,39 +89,22 @@ export const valueSetForInstruction = (
}

export const createNewRecipe = async ({ userInfo, recipeInfo }: { userInfo: UserSet; recipeInfo: RecipeSet }) => {
try {
const {
recipeName,
recipeDescription,
recipeServings,
recipePrepTime,
recipeCookTime,
recipeMainImage,
recipeMainAlt,
recipeCategory_Id,
} = recipeInfo
const { name, description, preview, servings, prep_time, cook_time, main_img, main_img_alt, category_id } = recipeInfo
const { userId } = userInfo

const { userId } = userInfo
const res = await pool.query(
recipeQueryBuilder(
recipeName,
recipeDescription,
recipeServings,
recipePrepTime,
recipeCookTime,
recipeMainImage,
recipeMainAlt,
userId,
recipeCategory_Id
)
)
return '3f2d10f7-d1a9-4fac-99f3-73b0888d3811'
} catch (error) {
if (error instanceof Error) {
console.log('Probelm in creating the recipe' + error.message)
}
} finally {
}
const res = await runQueryV2<{ id: string }>(createNewRecipeQueryBuilder(), [
name,
description ?? 'No description Provided',
preview ?? '',
servings,
prep_time,
cook_time,
main_img,
main_img_alt ?? 'Main image of the recipe',
userId,
category_id,
])
return res
}

export const createIngredient = async (query: string) => {
Expand Down
39 changes: 20 additions & 19 deletions lib/actions/admin.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,44 @@ import {
} from '@/db/dbFn/recipeFunction'
import { getUserInfo } from '@/db/dbFn/userFunctions'
import { UserSet } from '@/types/database.types'
import { errorlogger } from '@/utils/logger'
import { runQueryV2 } from '@/utils/runQuery'

export const adminCustomQuery = async (formData: FormData) => {
try {
const response = await runQueryV2<any>(formData.get('myQuery')?.toString()!)
if (response.status === 'fail') throw new Error(response.errorResponse)
console.log(response.queryResponse)
console.log(JSON.stringify(response.queryResponse))
} catch (e) {
console.error(e)
}
}

export const addNewRecipeToDatabase = async (formData: FormData) => {
// Getting the username
const userSet: UserSet = getUserInfo(`userIdGoesHere`)
try {
const userSet: UserSet = getUserInfo(`userIdGoesHere`)

const categoryId = getCategoryIdByName(formData.get('subCategoryName')?.toString()!)

// Getting the category Informations
const categoryId = getCategoryIdByName(formData.get('subCategoryName')?.toString()!)
const imgUrl = getImageURL(`imgFromFormDataGoesHere`)

//Getting the image Informations
const imgUrl = getImageURL(`imgFromFormDataGoesHere`)
const recipeSet = extractRecifeInformation(formData, imgUrl, categoryId)

//getting the recipeinformations
const recipeSet = extractRecifeInformation(formData, imgUrl, categoryId)
const qRes = await createNewRecipe({ userInfo: userSet, recipeInfo: recipeSet })
if (qRes.status === 'fail') throw new Error(qRes.errorResponse)

//create the newRecipe and return the id of recipe created
const recipeId = await createNewRecipe({ userInfo: userSet, recipeInfo: recipeSet })
const recipeId = qRes.queryResponse[0].id
console.log(recipeId)

//getting the ingredients informations
const ingredientsSet = extractIngredientsInformation(formData)
const ingredientsSet = extractIngredientsInformation(formData)

//add te ingredients to the database
createIngredient(ingridientsQueryBuilder(valueSetForIngredient(ingredientsSet, recipeId!)))
createIngredient(ingridientsQueryBuilder(valueSetForIngredient(ingredientsSet, recipeId!)))

//getting the instruction Information
const instructionSet = extractInstructionInformation(formData)
const instructionSet = extractInstructionInformation(formData)

//add the instructions to the database
createInstruction(instructionQueryBuilder(valueSetForInstruction(instructionSet, recipeId!)))
createInstruction(instructionQueryBuilder(valueSetForInstruction(instructionSet, recipeId!)))
} catch (e) {
if (e instanceof Error) errorlogger(e)
else console.error(e)
}
}
22 changes: 21 additions & 1 deletion types/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,25 @@ export interface UserInfo {
password: string
}

export type RecipeSet = RecipePrimaryInfo & Miscinfo
// export type RecipeSet = RecipePrimaryInfo & Miscinfo
export type UserSet = Pick<UserInfo, 'userId'>

export type RecipeTableResponse = {
id: string
name: string
description?: string
preview?: string
servings: number
prep_time: number
cook_time: number
main_img: string
main_img_alt?: string
created_at: string
updated_at: string
user_id: string
category_id: string
}

export type RecipeTableInsert = Omit<RecipeTableResponse, 'created_at' | 'updated_at' | 'id'>

export type RecipeSet = Omit<RecipeTableInsert, 'user_id'>

0 comments on commit bb82f82

Please sign in to comment.