mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-03 15:52:03 +00:00
Added db models and endpoints for courses/resources/users
This commit is contained in:
parent
e820e47148
commit
2de8b23d07
41
src/db/models/courseModels.js
Normal file
41
src/db/models/courseModels.js
Normal file
@ -0,0 +1,41 @@
|
||||
import prisma from "../prisma";
|
||||
|
||||
const client = new prisma.PrismaClient();
|
||||
|
||||
export const getAllCourses = async () => {
|
||||
return await client.course.findMany({
|
||||
include: {
|
||||
resources: true, // Include related resources
|
||||
purchases: true, // Include related purchases
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const getCourseById = async (id) => {
|
||||
return await client.course.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
resources: true, // Include related resources
|
||||
purchases: true, // Include related purchases
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const createCourse = async (data) => {
|
||||
return await client.course.create({
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
export const updateCourse = async (id, data) => {
|
||||
return await client.course.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteCourse = async (id) => {
|
||||
return await client.course.delete({
|
||||
where: { id },
|
||||
});
|
||||
};
|
39
src/db/models/resourceModels.js
Normal file
39
src/db/models/resourceModels.js
Normal file
@ -0,0 +1,39 @@
|
||||
import prisma from "../prisma";
|
||||
|
||||
export const getAllResources = async () => {
|
||||
return await prisma.resource.findMany({
|
||||
include: {
|
||||
course: true, // Include related course
|
||||
purchases: true, // Include related purchases
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const getResourceById = async (id) => {
|
||||
return await prisma.resource.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
course: true, // Include related course
|
||||
purchases: true, // Include related purchases
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const createResource = async (data) => {
|
||||
return await prisma.resource.create({
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
export const updateResource = async (id, data) => {
|
||||
return await prisma.resource.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteResource = async (id) => {
|
||||
return await prisma.resource.delete({
|
||||
where: { id },
|
||||
});
|
||||
};
|
49
src/db/models/userModels.js
Normal file
49
src/db/models/userModels.js
Normal file
@ -0,0 +1,49 @@
|
||||
import prisma from "../prisma";
|
||||
|
||||
export const getAllUsers = async () => {
|
||||
return await prisma.user.findMany({
|
||||
include: {
|
||||
role: true, // Include related role
|
||||
purchased: {
|
||||
include: {
|
||||
course: true, // Include course details in purchases
|
||||
resource: true, // Include resource details in purchases
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const getUserById = async (id) => {
|
||||
return await prisma.user.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
role: true, // Include related role
|
||||
purchased: {
|
||||
include: {
|
||||
course: true, // Include course details in purchases
|
||||
resource: true, // Include resource details in purchases
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const createUser = async (data) => {
|
||||
return await prisma.user.create({
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
export const updateUser = async (id, data) => {
|
||||
return await prisma.user.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteUser = async (id) => {
|
||||
return await prisma.user.delete({
|
||||
where: { id },
|
||||
});
|
||||
};
|
5
src/db/prisma.js
Normal file
5
src/db/prisma.js
Normal file
@ -0,0 +1,5 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export default prisma;
|
36
src/pages/api/courses/[slug].js
Normal file
36
src/pages/api/courses/[slug].js
Normal file
@ -0,0 +1,36 @@
|
||||
import { getCourseById, updateCourse, deleteCourse } from "@/db/models/courseModels";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const { id } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
const course = await getCourseById(parseInt(id));
|
||||
if (course) {
|
||||
res.status(200).json(course);
|
||||
} else {
|
||||
res.status(404).json({ error: 'Course not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'PUT') {
|
||||
try {
|
||||
const course = await updateCourse(parseInt(id), req.body);
|
||||
res.status(200).json(course);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'DELETE') {
|
||||
try {
|
||||
await deleteCourse(parseInt(id));
|
||||
res.status(204).end();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else {
|
||||
// Handle any other HTTP method
|
||||
res.setHeader('Allow', ['GET', 'PUT', 'DELETE']);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
23
src/pages/api/courses/index.js
Normal file
23
src/pages/api/courses/index.js
Normal file
@ -0,0 +1,23 @@
|
||||
import { getAllCourses, createCourse } from "@/db/models/courseModels";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
const courses = await getAllCourses();
|
||||
res.status(200).json(courses);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'POST') {
|
||||
try {
|
||||
const course = await createCourse(req.body);
|
||||
res.status(201).json(course);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
} else {
|
||||
// Handle any other HTTP method
|
||||
res.setHeader('Allow', ['GET', 'POST']);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
36
src/pages/api/resources/[slug].js
Normal file
36
src/pages/api/resources/[slug].js
Normal file
@ -0,0 +1,36 @@
|
||||
import { getResourceById, updateResource, deleteResource } from "@/db/models/resourceModels";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const { id } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
const resource = await getResourceById(parseInt(id));
|
||||
if (resource) {
|
||||
res.status(200).json(resource);
|
||||
} else {
|
||||
res.status(404).json({ error: 'Resource not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'PUT') {
|
||||
try {
|
||||
const resource = await updateResource(parseInt(id), req.body);
|
||||
res.status(200).json(resource);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'DELETE') {
|
||||
try {
|
||||
await deleteResource(parseInt(id));
|
||||
res.status(204).end();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else {
|
||||
// Handle any other HTTP method
|
||||
res.setHeader('Allow', ['GET', 'PUT', 'DELETE']);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
23
src/pages/api/resources/index.js
Normal file
23
src/pages/api/resources/index.js
Normal file
@ -0,0 +1,23 @@
|
||||
import { getAllResources, createResource } from "@/db/models/resourceModels";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
const resources = await getAllResources();
|
||||
res.status(200).json(resources);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'POST') {
|
||||
try {
|
||||
const resource = await createResource(req.body);
|
||||
res.status(201).json(resource);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
} else {
|
||||
// Handle any other HTTP method
|
||||
res.setHeader('Allow', ['GET', 'POST']);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
36
src/pages/api/users/[slug].js
Normal file
36
src/pages/api/users/[slug].js
Normal file
@ -0,0 +1,36 @@
|
||||
import { getUserById, updateUser, deleteUser } from "@/db/models/userModels";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const { slug } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
const user = await getUserById(parseInt(slug));
|
||||
if (user) {
|
||||
res.status(200).json(user);
|
||||
} else {
|
||||
res.status(404).json({ error: 'User not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'PUT') {
|
||||
try {
|
||||
const user = await updateUser(parseInt(slug), req.body);
|
||||
res.status(200).json(user);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'DELETE') {
|
||||
try {
|
||||
await deleteUser(parseInt(slug));
|
||||
res.status(204).end();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else {
|
||||
// Handle any other HTTP method
|
||||
res.setHeader('Allow', ['GET', 'PUT', 'DELETE']);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
23
src/pages/api/users/index.js
Normal file
23
src/pages/api/users/index.js
Normal file
@ -0,0 +1,23 @@
|
||||
import { getAllUsers, createUser } from '@/db/models/userModels';
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
const users = await getAllUsers();
|
||||
res.status(200).json(users);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else if (req.method === 'POST') {
|
||||
try {
|
||||
const user = await createUser(req.body);
|
||||
res.status(201).json(user);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
} else {
|
||||
// Handle any other HTTP method
|
||||
res.setHeader('Allow', ['GET', 'POST']);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user