Skip to content

Commit

Permalink
Product API
Browse files Browse the repository at this point in the history
  • Loading branch information
aleepe-1 committed Dec 6, 2023
1 parent 1364973 commit a55db46
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 20 deletions.
121 changes: 121 additions & 0 deletions src/pages/api/v1/product/[export-product].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { defaultHandler } from '@/utils/server/api-helpers'
import { connectToDatabase } from '@/utils/server/db'
import { ObjectId } from 'mongodb'
import { Product, productSchema } from '@/models/Product'
import { data } from 'autoprefixer'

// Define handleSignup before using it in the handler
/*const getProduct = async (req, res) => {
try {
Check if the user already exists
const existingUser = await User.findOne({ email })
if (existingUser) {
return res.status(409).json({ message: 'User already exists' })
}
// Hash the password
const hashedPassword = await hashPassword(password)
// new instance of product
const product = new Product({
_id,
id,
name,
dpp_class,
creation_time,
privacy,
admin: false, // Set admin to false by default
})
// Save the user
// await user.save()
// Respond with success
const { _id } = req.query
const productData =[id,data]
return res.status(200).json({ message: 'product displayed successfully' })
} catch (error) {
console.error('Error fetching product data:', error)
return res.status(500).json({ message: 'Internal server error' })
}
}
const handler = async (req, res) =>
defaultHandler(
req,
res,
{
GET: getProduct,
},
{
requiresAuth: false,
requiresAdmin: false,
}
)
export default handler
*/

/*export default async function handler(req, res) {
const {
query: { id },
method,
} = req
const db = await connectToDatabase()
switch (method) {
case 'GET':
try {
const data = await productSchema.findOne({ _id: ObjectId(id) })
if (!data) {
return res.status(404).json({ message: 'Data not found' })
}
res.status(200).json(data)
} catch (error) {
console.error(error)
res.status(500).json({ message: 'Server error' })
}
break
default:
res.setHeader('Allow', ['GET'])
res.status(405).end(`Method ${method} Not Allowed`)
}
}*/

const getProduct = async (req, res) => {
try {
const {
query: { id },
} = req

const productData = await Product.findOne({ _id: ObjectId(id) })
} catch (error) {
console.error('Error creating user:', error)
return res.status(500).json({ message: 'Internal server error' })
}
}

const handler = async (req, res) =>
defaultHandler(
req,
res,
{
GET: getProduct,
},
{
requiresAuth: false,
requiresAdmin: false,
}
)

export default handler
18 changes: 0 additions & 18 deletions src/pages/api/v1/product/export-product.js

This file was deleted.

39 changes: 37 additions & 2 deletions src/pages/product/[productId].js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,44 @@ import { Container } from '@/components/utils/Container'
import { useEffect, useState } from 'react'

function ProductDetails() {
const router = useRouter()
/* const router = useRouter()
const ProductId = router.query.productId
const [productData, setProductData] = useState(null)
const [productData, setProductData] = useState({
_id,
id,
name,
dpp_class,
creation_time,
privacy,
})
const fetchproduct = async () => {
try {
// Make a GET request to the API route
const response = await fetch('/api/v1/product/export-product')
// Check if the response is successful (status code 200)
if (response.ok) {
// Parse the response body as JSON
const result = await response.json()
// Update the component state with the fetched data
setData(result)
} else {
// Handle non-successful responses (e.g., log an error)
console.error(
'Error fetching data:',
response.status,
response.statusText
)
}
} catch (error) {
// Handle network errors or other exceptions
console.error('Error fetching data:', error.message)
}
}
fetchproduct()*/

return (
<LayoutGlobal>
Expand Down

0 comments on commit a55db46

Please sign in to comment.