diff --git a/src/controller/course.controller.ts b/src/controller/course.controller.ts index 9f2019c3..491e08c7 100644 --- a/src/controller/course.controller.ts +++ b/src/controller/course.controller.ts @@ -1,5 +1,7 @@ import { Request, Response } from 'express'; import asyncHandler from 'express-async-handler'; +import { createPayment } from '@src/service/payment.service'; +import { getUserById } from '@src/service/user.service'; import { getCoursesWithSectionsAndHours, filterAndSortCourses, getSectionsWithLessons, getCourseById, countEnrolledUsersInCourse, getProfessorByCourse} from '../service/course.service'; export const filterAndSort = asyncHandler(async (req: Request, res: Response) => { @@ -76,3 +78,24 @@ export const getCourseDetail = asyncHandler(async (req: Request, res: Response) t: req.t }); }); + +export const createPaymentRecord = asyncHandler(async (req: Request, res: Response): Promise => { + const { course_id } = req.params + const userId = req.user.id + + const user = await getUserById(userId); + if (!user) { + res.status(404).json({ message: 'User not found' }); + return; + } + + const course = await getCourseById(parseInt(course_id)) + if (!course) { + res.status(404).json({ message: 'Course not found' }); + return; + } + + const payment = await createPayment(user, course); + + res.status(201).json(payment); +}); diff --git a/src/controller/payment.controller.ts b/src/controller/payment.controller.ts new file mode 100644 index 00000000..7c449d00 --- /dev/null +++ b/src/controller/payment.controller.ts @@ -0,0 +1,19 @@ +import { Request, Response } from "express"; +import asyncHandler from 'express-async-handler'; +import { getPaymentById } from "../service/payment.service"; + +export const showPaymentPage = asyncHandler(async (req: Request, res: Response): Promise => { + const { payment_id } = req.params + const userId = req.user.id + + const payment = await getPaymentById(parseInt(payment_id)); + + if (!payment || payment.user.id !== userId) { + res.status(404).json({ message: "Payment not found or not authorized" }); + return; + } + + const course = payment.course; + + res.json({ payment, course, user: payment.user }); +}); diff --git a/src/routes/course.routes.ts b/src/routes/course.routes.ts index 9e394aba..db7ad82c 100644 --- a/src/routes/course.routes.ts +++ b/src/routes/course.routes.ts @@ -5,7 +5,11 @@ const router: Router = Router(); router.get('/', courseController.courseShowGet); -router.post('/', courseController.filterAndSort); + +router.post('/', courseController.filterAndSort) + +router.post('/:course_id/payment', courseController.createPaymentRecord) + router.get('/:id',courseController.getCourseDetail); export default router; diff --git a/src/routes/index.ts b/src/routes/index.ts index 55dc02cc..bfe0fb52 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -5,6 +5,7 @@ import setLocaleMiddleware from "../middleware/setLocaleMiddleware"; import courseRoute from "./course.routes"; import homeRoute from "./home.routes"; import userRouter from "../routes/user.routes"; +import paymentRoute from "./payment.routes" const router = Router(); @@ -16,4 +17,6 @@ router.use("/", homeRoute); router.use("/courses", courseRoute); router.use("/", userRouter); +router.use('/payment', paymentRoute) + export default router; diff --git a/src/routes/payment.routes.ts b/src/routes/payment.routes.ts new file mode 100644 index 00000000..52bfde6a --- /dev/null +++ b/src/routes/payment.routes.ts @@ -0,0 +1,9 @@ +import { Router } from 'express'; +import { showPaymentPage } from '../controller/payment.controller'; + +const router = Router(); + +// Define the payment route for a specific course +router.get('/:payment_id', showPaymentPage); + +export default router; diff --git a/src/service/payment.service.ts b/src/service/payment.service.ts new file mode 100644 index 00000000..cdefc121 --- /dev/null +++ b/src/service/payment.service.ts @@ -0,0 +1,24 @@ +import { AppDataSource } from "../repos/db"; +import { Payment } from "../entity/Payment"; +import { User } from "../entity/User"; +import { Course } from "../entity/Course"; + +const paymentRepository = AppDataSource.getRepository(Payment); + +export const createPayment = async (user: User, course: Course) => { + const payment = paymentRepository.create({ + user: user, // In a real scenario, you'd fetch the actual user from your auth system + course: course, + amount: course.price, // Assuming the payment amount is the course price + payment_date: new Date(), + status: 'done', // Or set to 'pending' based on your logic + }); + + return await paymentRepository.save(payment); +} + +export const getPaymentById = async (id: number) => { + return await paymentRepository.findOne({ + where: { id: id }, + }) +} diff --git a/src/service/user.service.ts b/src/service/user.service.ts index f6138543..57e381b8 100644 --- a/src/service/user.service.ts +++ b/src/service/user.service.ts @@ -64,3 +64,9 @@ export const userLogin = async (email: string, password: string) => { export const decodeJwtToken = (token: string) => { return Jwt.verify(token, process.env.JWT_SECRET!); }; + +export const getUserById = async (id: number) => { + return await userRepository.findOne({ + where: { id: id } + }) +} diff --git a/src/types/express/index.d.ts b/src/types/express/index.d.ts index 34e16bf7..976934d7 100644 --- a/src/types/express/index.d.ts +++ b/src/types/express/index.d.ts @@ -1,7 +1,16 @@ import 'express-serve-static-core'; +import { User } from '.../entity/User'; declare module 'express-serve-static-core' { interface Response { setLocale?: (locale: string) => void; } } + +declare global { + namespace Express { + interface Request { + user?: User; + } + } +}