Skip to content

Fix bugs #10

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

Open
wants to merge 2 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
78 changes: 44 additions & 34 deletions src/app/modules/Course/course.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,55 @@ import {
TPreRequisiteCourses,
} from './course.interface';

const preRequisiteCoursesSchema = new Schema<TPreRequisiteCourses>({
course: {
type: Schema.Types.ObjectId,
ref: 'Course',
const preRequisiteCoursesSchema = new Schema<TPreRequisiteCourses>(
{
course: {
type: Schema.Types.ObjectId,
ref: 'Course',
},
isDeleted: {
type: Boolean,
default: false,
},
},
isDeleted: {
type: Boolean,
default: false,
{
_id: false,
},
});
);

const courseSchema = new Schema<TCourse>({
title: {
type: String,
unique: true,
trim: true,
required: true,
},
prefix: {
type: String,
trim: true,
required: true,
},
code: {
type: Number,
trim: true,
required: true,
},
credits: {
type: Number,
trim: true,
required: true,
const courseSchema = new Schema<TCourse>(
{
title: {
type: String,
unique: true,
trim: true,
required: true,
},
prefix: {
type: String,
trim: true,
required: true,
},
code: {
type: Number,
trim: true,
required: true,
},
credits: {
type: Number,
trim: true,
required: true,
},
preRequisiteCourses: [preRequisiteCoursesSchema],
isDeleted: {
type: Boolean,
default: false,
},
},
preRequisiteCourses: [preRequisiteCoursesSchema],
isDeleted: {
type: Boolean,
default: false,
{
timestamps: true,
},
});
);

export const Course = model<TCourse>('Course', courseSchema);

Expand Down
22 changes: 11 additions & 11 deletions src/app/modules/Course/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const createCourseIntoDB = async (payload: TCourse) => {

const getAllCoursesFromDB = async (query: Record<string, unknown>) => {
const courseQuery = new QueryBuilder(
Course.find().populate('preRequisiteCourses.course'),
Course.find(),
// .populate('preRequisiteCourses.course'),
query,
)
.search(CourseSearchableFields)
Expand All @@ -40,7 +41,6 @@ const updateCourseIntoDB = async (id: string, payload: Partial<TCourse>) => {

try {
session.startTransaction();

//step1: basic course info update
const updatedBasicCourseInfo = await Course.findByIdAndUpdate(
id,
Expand All @@ -53,7 +53,7 @@ const updateCourseIntoDB = async (id: string, payload: Partial<TCourse>) => {
);

if (!updatedBasicCourseInfo) {
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course');
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!');
}

// check if there is any pre requisite courses to update
Expand All @@ -78,7 +78,7 @@ const updateCourseIntoDB = async (id: string, payload: Partial<TCourse>) => {
);

if (!deletedPreRequisiteCourses) {
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course');
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!');
}

// filter out the new course fields
Expand All @@ -99,18 +99,18 @@ const updateCourseIntoDB = async (id: string, payload: Partial<TCourse>) => {
);

if (!newPreRequisiteCourses) {
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course');
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!');
}

const result = await Course.findById(id).populate(
'preRequisiteCourses.course',
);

return result;
}

await session.commitTransaction();
await session.endSession();

const result = await Course.findById(id).populate(
'preRequisiteCourses.course',
);

return result;
} catch (err) {
await session.abortTransaction();
await session.endSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const academicDepartmentSchema = new Schema<TAcademicDepartment>(
},
);


academicDepartmentSchema.pre('save', async function (next) {
const isDepartmentExist = await AcademicDepartment.findOne({
name: this.name,
Expand Down