feat: accreditation crud
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
import prisma from '../prisma/client.js';
|
||||
|
||||
export const createAccreditation = async (req, res) => {
|
||||
try {
|
||||
const { title, type, logo, image, description, sortOrder, isActive } = req.body;
|
||||
|
||||
if (!title || !type) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Title and type are required',
|
||||
});
|
||||
}
|
||||
|
||||
const accreditation = await prisma.accreditation.create({
|
||||
data: {
|
||||
title,
|
||||
type,
|
||||
logo,
|
||||
image,
|
||||
description,
|
||||
sortOrder: sortOrder ? Number(sortOrder) : 1000,
|
||||
isActive: isActive ?? true,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: accreditation,
|
||||
message: 'Accreditation created successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('PRISMA TRANSACTION ERROR:', error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to create accreditation',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getAccreditations = async (req, res) => {
|
||||
try {
|
||||
const { type } = req.query;
|
||||
|
||||
const accreditations = await prisma.accreditation.findMany({
|
||||
where: type
|
||||
? {
|
||||
type,
|
||||
}
|
||||
: undefined,
|
||||
orderBy: {
|
||||
sortOrder: 'asc',
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: accreditations,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch accreditations',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getActiveAccreditations = async (req, res) => {
|
||||
try {
|
||||
const { type } = req.query;
|
||||
|
||||
const accreditations = await prisma.accreditation.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
...(type && { type }),
|
||||
},
|
||||
orderBy: {
|
||||
sortOrder: 'asc',
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: accreditations,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch accreditations',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getAccreditation = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const accreditation = await prisma.accreditation.findUnique({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
});
|
||||
|
||||
if (!accreditation) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Accreditation not found',
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: accreditation,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch accreditation',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const updateAccreditation = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const { title, type, logo, image, description, sortOrder, isActive } = req.body;
|
||||
|
||||
const updateData = {};
|
||||
|
||||
if (title !== undefined) updateData.title = title;
|
||||
if (type !== undefined) updateData.type = type;
|
||||
if (logo !== undefined) updateData.logo = logo;
|
||||
if (image !== undefined) updateData.image = image;
|
||||
if (description !== undefined) updateData.description = description;
|
||||
if (sortOrder !== undefined) updateData.sortOrder = Number(sortOrder);
|
||||
if (isActive !== undefined) updateData.isActive = isActive;
|
||||
|
||||
const accreditation = await prisma.accreditation.update({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: accreditation,
|
||||
message: 'Accreditation updated successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('PRISMA TRANSACTION ERROR:', error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to update accreditation',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteAccreditation = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
await prisma.accreditation.delete({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Accreditation deleted successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to delete accreditation',
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user