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

User payment #15

Open
wants to merge 1 commit into
base: master
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
23 changes: 23 additions & 0 deletions src/controller/course.controller.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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<void> => {
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;
Comment on lines +88 to +89
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
res.status(404).json({ message: 'User not found' });
return;
return res.status(404).json({ message: 'User not found' });

}

const course = await getCourseById(parseInt(course_id))
if (!course) {
res.status(404).json({ message: 'Course not found' });
return;
Comment on lines +94 to +95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
res.status(404).json({ message: 'Course not found' });
return;
return res.status(404).json({ message: 'Course not found' });

}

const payment = await createPayment(user, course);

res.status(201).json(payment);
});
19 changes: 19 additions & 0 deletions src/controller/payment.controller.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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;
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sửa tương tự e nha
I18n các message nữa nhé

}

const course = payment.course;

res.json({ payment, course, user: payment.user });
});
6 changes: 5 additions & 1 deletion src/routes/course.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 3 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -16,4 +17,6 @@ router.use("/", homeRoute);
router.use("/courses", courseRoute);
router.use("/", userRouter);

router.use('/payment', paymentRoute)

export default router;
9 changes: 9 additions & 0 deletions src/routes/payment.routes.ts
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions src/service/payment.service.ts
Original file line number Diff line number Diff line change
@@ -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 },
})
}
6 changes: 6 additions & 0 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
})
}
9 changes: 9 additions & 0 deletions src/types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}