Skip to content

Commit 20cc3c1

Browse files
committed
add delete method
1 parent f27802f commit 20cc3c1

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

app/api/recipes/route.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
21
import { NextResponse } from 'next/server';
3-
import { join } from "path";
4-
import { stat, writeFile } from "fs/promises";
52
import { connectToDatabase } from "../../../lib/mongodb";
63
import { Recipe } from "../../../models";
7-
import mime from 'mime';
84
import { existsSync } from "fs";
95
import fs from "fs/promises";
106
import path from "path";
@@ -65,4 +61,16 @@ export async function POST(req: Request) {
6561
});
6662
}
6763

64+
}
65+
66+
export async function DELETE(req: any) {
67+
const { id } = req.query;
68+
try {
69+
console.log("id", id)
70+
const recipe = await Recipe.findByIdAndRemove(id)
71+
console.log("re", recipe)
72+
return NextResponse.json({status: 200, data: recipe})
73+
} catch (error) {
74+
return NextResponse.json({ status: 500, success: false, message: error });
75+
}
6876
}

app/recipes/[id]/page.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, { useEffect, useState } from "react";
44
import SingleRecipeMain from "../../../components/SingleRecipeMain";
55
import SingleRecipeTopSection from "../../../components/SingleRecipeTopSection";
66
import { IRecipCreate } from "../../../models/recipe";
7+
import Button from "@/components/ui/Button";
78

89
// کامپوننت SingleRecipe که اطلاعات یک رسپی را نمایش می‌دهد
910
const SingleRecipe = () => {
@@ -12,6 +13,18 @@ const SingleRecipe = () => {
1213
undefined
1314
);
1415

16+
const onDeleteRecipe = async (id: string) => {
17+
try {
18+
const response = await fetch(`/api/recipes/${id}`, {
19+
method: "DELETE"
20+
})
21+
const data = response.json()
22+
console.log("deleted", data);
23+
} catch (error) {
24+
console.log("er",error);
25+
}
26+
}
27+
1528
useEffect(() => {
1629
// دریافت رسپی با استفاده از شناسه
1730
const get_recipe_by_id = async (id: string) => {
@@ -42,6 +55,11 @@ const SingleRecipe = () => {
4255
recipeIngredients={singleRecipe?.ingredients}
4356
recipeSteps={singleRecipe?.steps}
4457
/>
58+
<div className="grid grid-cols-2 gap-6">
59+
{/* <Button status="danger" className="roundedrounded px-4 py-2 bg-orange-600" onClick={() => onDeleteRecipe(id)}>
60+
حذف رسپی
61+
</Button> */}
62+
</div>
4563
</>
4664
);
4765
};

0 commit comments

Comments
 (0)