Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch 1 #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 33 additions & 68 deletions controllers/businessController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ exports.registerBusiness = async (req, res) => {
try {
const {
businessName,
businessEmail,
categoryId,
businessEmail,
categoryId,
businessAddress,
businessPhone,
websiteUrl,
latitude,
longitude
longitude,
openingTime,
closingTime,
businessLicenseNumber
} = req.body;


// Convert latitude and longitude to Float
const latitudeFloat = parseFloat(latitude);
const longitudeFloat = parseFloat(longitude);


// Convert businessLicenseNumber to Integer if it's provided
const businessLicenseNumberInt = businessLicenseNumber ? parseInt(businessLicenseNumber, 10) : undefined;

// Validate that the categoryId exists in the database
const category = await prisma.category.findUnique({
where: { id: categoryId }
});
Expand All @@ -27,7 +33,7 @@ exports.registerBusiness = async (req, res) => {
return res.status(400).json({ message: "Invalid category ID" });
}


// Validate that the user exists in the database
const user = await prisma.user.findUnique({
where: { email: businessEmail }
});
Expand All @@ -39,102 +45,87 @@ exports.registerBusiness = async (req, res) => {
const newBusiness = await prisma.business.create({
data: {
businessName,
ownerId: user.id,
categoryId,
ownerId: user.id,
categoryId,
businessEmail,
businessAddress,
businessPhone,
websiteUrl,
latitude: latitudeFloat,
longitude: longitudeFloat,
},
latitude: latitudeFloat,
longitude: longitudeFloat,
openingTime, // Save opening time
closingTime, // Save closing time
businessLicenseNumber: businessLicenseNumberInt // Save business license number
}
});


res.status(201).json(newBusiness);
} catch (error) {

res.status(500).json({ error: error.message });
}
};

exports.getAllBusinesses = async (req, res) => {
try {

const businesses = await prisma.business.findMany({
include: {
category: true,
category: true,
},
});


res.status(200).json(businesses);
} catch (error) {

res.status(500).json({ error: error.message });
}
};


exports.getBusinessById = async (req, res) => {
const { id } = req.params;

try {

const business = await prisma.business.findUnique({
where: { id },
include: {
category: true,
category: true,
},
});

if (!business) {
return res.status(404).json({ message: 'Business not found' });
}


res.status(200).json(business);
} catch (error) {

res.status(500).json({ error: error.message });
}
};



exports.getCategoryById = async (req, res) => {
const { categoryId } = req.params;

try {

if (!isValidUUID(categoryId)) {
return res.status(400).json({ message: "Invalid category ID format" });
}


const category = await prisma.category.findUnique({
where: { id: categoryId },
});


if (!category) {
return res.status(404).json({ message: "Category not found" });
}


res.status(200).json(category);
} catch (error) {

res.status(500).json({ error: error.message });
}
};

exports.getBusinessesByCategoryId = async (req, res) => {
const { categoryId } = req.params;

console.log('Received categoryId:', categoryId);

try {

if (!isValidUUID(categoryId)) {
return res.status(400).json({ message: "Invalid category ID format" });
}
Expand All @@ -147,33 +138,26 @@ exports.getBusinessesByCategoryId = async (req, res) => {
return res.status(404).json({ message: "Category not found" });
}


const businesses = await prisma.business.findMany({
where: { categoryId },
include: {
category: true,
category: true,
},
});


if (businesses.length === 0) {
return res.status(404).json({ message: "No businesses found for this category" });
}

res.status(200).json(businesses);
} catch (error) {

res.status(500).json({ error: error.message });
}
};




exports.getBusinessesBySearchCriteria = async (req, res) => {
const { name, address, service } = req.query;
const { name, address, service } = req.query;
try {

const searchConditions = {};

if (name) {
Expand All @@ -183,7 +167,6 @@ exports.getBusinessesBySearchCriteria = async (req, res) => {
};
}


if (address) {
searchConditions.businessAddress = {
contains: address,
Expand All @@ -205,41 +188,30 @@ exports.getBusinessesBySearchCriteria = async (req, res) => {
const businesses = await prisma.business.findMany({
where: searchConditions,
include: {
category: true,
services: true,
category: true,
services: true,
},
});


if (businesses.length === 0) {
return res.status(404).json({ message: "No businesses found matching the search criteria" });
}


res.status(200).json(businesses);
} catch (error) {

res.status(500).json({ error: error.message });
}
};



const isValidUUID = (id) => {
const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
return uuidRegex.test(id);
};
/// controllers/businessController.js

/**
* Delete a business by ID
* This function deletes a business identified by its ID.
*/
exports.deleteBusinessById = async (req, res) => {
const { id } = req.params;

try {
// Check if the business exists
const business = await prisma.business.findUnique({
where: { id },
});
Expand All @@ -248,23 +220,16 @@ exports.deleteBusinessById = async (req, res) => {
return res.status(404).json({ message: 'Business not found' });
}

// Delete the business
await prisma.business.delete({
where: { id },
});

// Respond with a success message
res.status(200).json({ message: 'Business deleted successfully' });
} catch (error) {
// Handle errors and respond with an appropriate message
res.status(500).json({ error: error.message });
}
};

/**
* Update a business by ID
* This function updates the details of a business identified by its ID.
*/
exports.updateBusinessById = async (req, res) => {
const { id } = req.params;
const {
Expand All @@ -276,14 +241,15 @@ exports.updateBusinessById = async (req, res) => {
websiteUrl,
latitude,
longitude,
openingTime,
closingTime,
businessLicenseNumber
} = req.body;

try {
// Convert latitude and longitude to Float if provided
const latitudeFloat = latitude ? parseFloat(latitude) : undefined;
const longitudeFloat = longitude ? parseFloat(longitude) : undefined;

// Validate the categoryId if provided
if (categoryId) {
const category = await prisma.category.findUnique({
where: { id: categoryId }
Expand All @@ -294,7 +260,6 @@ exports.updateBusinessById = async (req, res) => {
}
}

// Check if the business exists
const business = await prisma.business.findUnique({
where: { id },
});
Expand All @@ -303,7 +268,6 @@ exports.updateBusinessById = async (req, res) => {
return res.status(404).json({ message: 'Business not found' });
}

// Update the business with the provided data
const updatedBusiness = await prisma.business.update({
where: { id },
data: {
Expand All @@ -315,13 +279,14 @@ exports.updateBusinessById = async (req, res) => {
websiteUrl,
latitude: latitudeFloat,
longitude: longitudeFloat,
openingTime,
closingTime,
businessLicenseNumber: businessLicenseNumber ? parseInt(businessLicenseNumber, 10) : undefined
},
});

// Respond with the updated business details
res.status(200).json(updatedBusiness);
} catch (error) {
// Handle errors and respond with an appropriate message
res.status(500).json({ error: error.message });
}
};
Loading