From 8470bc93da236fa3607b9469618722a79abe494e Mon Sep 17 00:00:00 2001 From: FylypekUNO Date: Mon, 25 Nov 2024 09:49:23 +0100 Subject: [PATCH] cleanup --- app/api/recipes/[id]/nutrition/route.ts | 15 ++++-- app/api/recipes/[id]/route.ts | 13 +++-- app/api/recipes/route.ts | 4 +- app/api/test-auth/route.ts | 4 +- app/api/test-db/route.ts | 56 --------------------- app/recipes/[id]/page.tsx | 1 - app/recipes/favorite/route.ts | 12 +++-- app/recipes/page.tsx | 6 +-- app/user/status/page.tsx | 1 - components/CustomSessionProvider.tsx | 1 - components/RecipesList.tsx | 1 - lib/validation.ts | 66 ------------------------- 12 files changed, 32 insertions(+), 148 deletions(-) delete mode 100644 app/api/test-db/route.ts delete mode 100644 lib/validation.ts diff --git a/app/api/recipes/[id]/nutrition/route.ts b/app/api/recipes/[id]/nutrition/route.ts index 50a102c..c71dd8a 100644 --- a/app/api/recipes/[id]/nutrition/route.ts +++ b/app/api/recipes/[id]/nutrition/route.ts @@ -2,17 +2,24 @@ import { NextResponse } from "next/server"; import dbConnect from "@/lib/connectToDatabase"; import Recipe from "@/models/Recipe"; -export async function GET( - request: Request, - { params }: { params: { id: string } } -) { +export type GETParams = { + params: { + id: string; + }; +}; + +export async function GET({ params }: GETParams) { try { await dbConnect(); + const recipe = await Recipe.findById(params.id); + if (!recipe) { return NextResponse.json({ error: "Recipe not found" }, { status: 404 }); } + const nutrition = await recipe.calculateNutrition(); + return NextResponse.json(nutrition); } catch (error) { return NextResponse.json({ error: "Server error" }, { status: 500 }); diff --git a/app/api/recipes/[id]/route.ts b/app/api/recipes/[id]/route.ts index 98ee5a4..1cd009e 100644 --- a/app/api/recipes/[id]/route.ts +++ b/app/api/recipes/[id]/route.ts @@ -3,10 +3,13 @@ import { isValidObjectId } from "mongoose"; import connectDB from "@/lib/connectToDatabase"; import Recipe from "@/models/Recipe"; -export async function GET( - request: Request, - { params }: { params: { id: string } } -) { +export type GETParams = { + params: { + id: string; + }; +}; + +export async function GET({ params }: GETParams) { try { const { id } = params; @@ -29,7 +32,7 @@ export async function GET( } catch (error) { console.error("Error fetching recipe:", error); return NextResponse.json( - { error: "Internal server error" }, + { error: "Server error occurred" }, { status: 500 } ); } diff --git a/app/api/recipes/route.ts b/app/api/recipes/route.ts index 7a5ca9a..0c8cc9c 100644 --- a/app/api/recipes/route.ts +++ b/app/api/recipes/route.ts @@ -5,12 +5,14 @@ import Recipe from "@/models/Recipe"; export async function GET(req: NextRequest) { try { await connectMongo(); + const recipes = await Recipe.find({}).populate("ingredients.ingredient"); + return NextResponse.json({ recipes }, { status: 200 }); } catch (error) { console.error("Error:", error); return NextResponse.json( - { error: "Wystąpił błąd serwera" }, + { error: "Server error occurred" }, { status: 500 } ); } diff --git a/app/api/test-auth/route.ts b/app/api/test-auth/route.ts index 375dd63..97a6b20 100644 --- a/app/api/test-auth/route.ts +++ b/app/api/test-auth/route.ts @@ -1,8 +1,8 @@ -import { NextRequest, NextResponse } from "next/server"; +import { NextResponse } from "next/server"; import { getServerSession, Session } from "next-auth"; import authOptions from "@/lib/nextauth"; -export async function GET(req: NextRequest) { +export async function GET() { const session: Session | null = await getServerSession(authOptions); if (!session) { diff --git a/app/api/test-db/route.ts b/app/api/test-db/route.ts deleted file mode 100644 index 62d750d..0000000 --- a/app/api/test-db/route.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import connectDB from "@/lib/connectToDatabase"; -import User from "@/models/User"; -import Recipe from "@/models/Recipe"; -import Ingredient from "@/models/Ingredient"; - -export async function GET(req: NextRequest) { - await connectDB(); - - const testUser = new User({ - username: "testuser3", - email: "testuser@example.com3", - password: "securepassword", - permissions: ["read:recipes"], - experience: 0, - }); - - await testUser.save(); - - const testIngredient = new Ingredient({ - name: "Test Ingredient3", - unit: "g", - nutrition: { - calories: 100, - protein: 5, - fats: 2, - carbs: 20, - fiber: 3, - sugar: 10, - sodium: 1, - }, - }); - - await testIngredient.save(); - - const testRecipe = new Recipe({ - name: "Test Recipe3", - description: "This is a test recipe.", - ingredients: [ - { - ingredient: testIngredient._id, - amount: 2, - }, - ], - steps: ["Step 1: Do something", "Step 2: Do something else"], - prepTime: 10, - cookTime: 20, - difficulty: "Easy", - experience: 10, - createdBy: testUser._id, - }); - - await testRecipe.save(); - - return NextResponse.json({ message: "Successfully created test data." }); -} diff --git a/app/recipes/[id]/page.tsx b/app/recipes/[id]/page.tsx index 10752bb..337393c 100644 --- a/app/recipes/[id]/page.tsx +++ b/app/recipes/[id]/page.tsx @@ -1,5 +1,4 @@ "use client"; - import { useState, useEffect } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Navbar } from "@/components/Navbar"; diff --git a/app/recipes/favorite/route.ts b/app/recipes/favorite/route.ts index 0bf6e63..b82d1d6 100644 --- a/app/recipes/favorite/route.ts +++ b/app/recipes/favorite/route.ts @@ -1,4 +1,3 @@ -// route.ts import { NextResponse } from "next/server"; import { getServerSession } from "next-auth"; import connectDB from "@/lib/connectToDatabase"; @@ -27,11 +26,14 @@ export async function GET() { } } +export type POSTParams = { + params: { + recipeId: string; + }; +}; + // Add to favorites -export async function POST( - request: Request, - { params }: { params: { recipeId: string } } -) { +export async function POST({ params }: POSTParams) { try { const session = await getServerSession(authOptions); diff --git a/app/recipes/page.tsx b/app/recipes/page.tsx index 9a42545..834d4ae 100644 --- a/app/recipes/page.tsx +++ b/app/recipes/page.tsx @@ -1,12 +1,8 @@ import { RecipesList } from "@/components/RecipesList"; import connectDB from "@/lib/connectToDatabase"; -import Recipe, { RecipeType } from "@/models/Recipe"; +import Recipe from "@/models/Recipe"; import { Navbar } from "@/components/Navbar"; -type RecipesPageProps = { - recipes: RecipeType[]; -}; - const RecipesPage = async () => { await connectDB(); const recipes = await Recipe.find({}) diff --git a/app/user/status/page.tsx b/app/user/status/page.tsx index a24d9bb..dda7bae 100644 --- a/app/user/status/page.tsx +++ b/app/user/status/page.tsx @@ -1,5 +1,4 @@ "use client"; - import { Logo } from "@/components/Logo"; import { useSession, signIn, signOut } from "next-auth/react"; import { useState } from "react"; diff --git a/components/CustomSessionProvider.tsx b/components/CustomSessionProvider.tsx index 6655824..6137a3d 100644 --- a/components/CustomSessionProvider.tsx +++ b/components/CustomSessionProvider.tsx @@ -1,5 +1,4 @@ "use client"; - import { SessionProvider } from "next-auth/react"; import { ReactNode } from "react"; diff --git a/components/RecipesList.tsx b/components/RecipesList.tsx index 4d15866..8ef8b7c 100644 --- a/components/RecipesList.tsx +++ b/components/RecipesList.tsx @@ -1,5 +1,4 @@ "use client"; - import { FC, useState } from "react"; import Link from "next/link"; import { diff --git a/lib/validation.ts b/lib/validation.ts deleted file mode 100644 index a72812f..0000000 --- a/lib/validation.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { ChatOpenAI } from "@langchain/openai"; -import { ChatPromptTemplate } from "@langchain/core/prompts"; -import Ingredient from "@/models/Ingredient"; -import connectDB from "@/lib/connectToDatabase"; - -// Initialize the language model -const model = new ChatOpenAI({ - modelName: "gpt-4o-mini", - temperature: 0, - openAIApiKey: process.env.OPENAI_API_KEY, -}); - -// Define the prompt template -const validationPrompt = ChatPromptTemplate.fromTemplate(` - Evaluate if the given ingredient is an edible food product. Answer only "true" or "false". - - Ingredient to evaluate: {ingredient} - - Evaluation rules: - - Answer "true" if the ingredient is: - * A fruit or vegetable - * A food product available in stores - * A spice or herb - * Meat or fish - * Dairy - * Grain or its derivative - * An ingredient used in cooking - - - Answer "false" if the ingredient is: - * Inedible or toxic - * A random string of characters - * A non-food item - * Does not exist as a food product - - Examples: - "banana": true - "apple": true - "salt": true - "chicken": true - "XKCD123": false - "stone": false - "dirt": false - "poison": false - - Answer only "true" or "false". - `); - -// Create the validation chain -const validationChain = validationPrompt.pipe(model); - -// Function to validate if an ingredient is food -export async function isValidFood(ingredient: string): Promise { - try { - const result = await validationChain.invoke({ ingredient }); - return result.content.toString().toLowerCase().includes("true"); - } catch (error) { - console.error(`Validation failed for ingredient "${ingredient}":`, error); - return false; // Fail safe - if validation fails, assume ingredient is invalid - } -} - -// Function to check if an ingredient exists in the database -export async function findExistingIngredient(name: string) { - await connectDB(); - return await Ingredient.findOne({ name: { $regex: new RegExp(name, "i") } }); -}