-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Course schema, controller, module and service (#35)
* added Course schema, controller, module and service * fixed lint error * minor modifications * corrections in service and controller
- Loading branch information
Showing
9 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CourseController } from './course.controller'; | ||
|
||
describe('CourseController', () => { | ||
let controller: CourseController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [CourseController], | ||
}).compile(); | ||
|
||
controller = module.get<CourseController>(CourseController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpStatus, | ||
NotFoundException, | ||
Param, | ||
Post, | ||
Put, | ||
Query, | ||
Res, | ||
} from '@nestjs/common'; | ||
import { Response } from 'express'; | ||
import { CourseService } from './course.service'; | ||
import { CourseDTO } from './create-course.dto'; | ||
|
||
@Controller('course') | ||
export class CourseController { | ||
constructor(private courseService: CourseService) {} | ||
|
||
// get all courses | ||
@Get('/all') | ||
async getAllCourses(@Res() res: Response) { | ||
const courses = await this.courseService.getAllCourses(); | ||
return res.status(HttpStatus.OK).json(courses); | ||
} | ||
|
||
// get selected course | ||
@Get('/:CourseID') | ||
async getSelectedCourse(@Res() res: Response, @Param('CourseID') CourseID) { | ||
const course = await this.courseService.getSelectedCourse(CourseID); | ||
if (!course) { | ||
throw new NotFoundException('Selected course not found'); | ||
} | ||
return res.status(HttpStatus.OK).json(course); | ||
} | ||
|
||
// add a Course | ||
@Post('/create') | ||
async addCourse(@Res() res: Response, @Body() courseDTO: CourseDTO) { | ||
const course = await this.courseService.addCourse(courseDTO); | ||
return res.status(HttpStatus.OK).json({ | ||
message: 'Course has been added successfully', | ||
course, | ||
}); | ||
} | ||
|
||
// update a course | ||
@Put('/edit') | ||
async editCourse( | ||
@Res() res: Response, | ||
@Query('CourseID') CourseID, | ||
@Body() courseDTO: CourseDTO, | ||
) { | ||
const Course = await this.courseService.editCourse(CourseID, courseDTO); | ||
if (!Course) { | ||
throw new NotFoundException('Course does not exist'); | ||
} | ||
return res.status(HttpStatus.OK).json({ | ||
message: 'Course has been successfully edited', | ||
Course, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Document } from 'mongoose'; | ||
|
||
export interface Course extends Document { | ||
readonly id: string; | ||
readonly name: string; | ||
readonly price: number; | ||
readonly start_date: Date; | ||
readonly end_date: Date; | ||
readonly duration: string; | ||
readonly active: boolean; | ||
readonly coupons: number; | ||
readonly student_num: number; | ||
readonly video_num: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { CourseSchema } from '../../schemas/course.schema'; | ||
import { CourseController } from './course.controller'; | ||
import { CourseService } from './course.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: 'Course', schema: CourseSchema }]), | ||
], | ||
controllers: [CourseController], | ||
providers: [CourseService], | ||
}) | ||
export class CourseModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CourseService } from './course.service'; | ||
|
||
describe('CourseService', () => { | ||
let service: CourseService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CourseService], | ||
}).compile(); | ||
|
||
service = module.get<CourseService>(CourseService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
import { Course } from './course.interface'; | ||
import { CourseDTO } from './create-course.dto'; | ||
|
||
@Injectable() | ||
export class CourseService { | ||
constructor( | ||
@InjectModel('Course') private readonly CourseModel: Model<Course>, | ||
) {} | ||
|
||
// fetch all courses | ||
async getAllCourses(): Promise<Course[]> { | ||
const Courses = await this.CourseModel.find().exec(); | ||
return Courses; | ||
} | ||
|
||
// fetch selected course | ||
async getSelectedCourse(CourseID: string): Promise<Course> { | ||
const Course = await this.CourseModel.findById(CourseID).exec(); | ||
return Course; | ||
} | ||
|
||
// add course | ||
async addCourse(courseDTO: CourseDTO): Promise<Course> { | ||
const newCourse = new this.CourseModel(courseDTO); | ||
return newCourse.save(); | ||
} | ||
|
||
// edit course | ||
async editCourse(CourseId: string, courseDTO: CourseDTO): Promise<Course> { | ||
const updatedCourse = await this.CourseModel.findByIdAndUpdate( | ||
CourseId, | ||
courseDTO, | ||
{ new: true }, | ||
); | ||
return updatedCourse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export class CourseDTO { | ||
readonly id: string; | ||
readonly name: string; | ||
readonly price: number; | ||
readonly start_date: Date; | ||
readonly end_date: Date; | ||
readonly duration: string; | ||
readonly active: boolean; | ||
readonly coupons: number; | ||
readonly student_num: number; | ||
readonly video_num: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import { Document } from 'mongoose'; | ||
|
||
export type CourseDocument = Course & Document; | ||
|
||
@Schema() | ||
export class Course { | ||
@Prop({ required: true }) | ||
id: string; | ||
|
||
@Prop({ required: true }) | ||
name: string; | ||
|
||
@Prop({ required: true }) | ||
price: number; | ||
|
||
@Prop({ required: true, default: Date.now }) | ||
start_date: Date; | ||
|
||
@Prop() | ||
end_date: Date; | ||
|
||
@Prop() | ||
duration: string; | ||
|
||
@Prop() | ||
active: boolean; | ||
|
||
@Prop() | ||
coupons: number; | ||
|
||
@Prop({ default: 0 }) | ||
student_num: number; | ||
|
||
@Prop() | ||
video_num: number; | ||
} | ||
|
||
export const CourseSchema = SchemaFactory.createForClass(Course); |